<?php

if ($maps['diary_id'] != 0) {
    unset($_SESSION['diary'], 
          $_SESSION['diary_name'], 
          $_SESSION['diary_message'], 
          $_SESSION['diary_access'], 
          $_SESSION['diary_comment']);
}

$_SESSION['sid'] = mt_rand(111, 999);
$set['title'] = $maps['s'] != 'u' ? __('Дневники') : $nameSort['u'];
require H.'sys/inc/thead.php';
title();
aut();
err();

if ($maps['s'] == 'u') { 
  $sql = mysql_query("SELECT n.*, 
                          (SELECT COUNT(*) FROM frends AS f WHERE f.frend = n.id_user AND f.user = '$user_id') AS is_frends
                        FROM notes AS n 
                        WHERE id_user = '" . (int) $maps['u'] . "' AND (n.private = '0' OR n.private = '1' OR (n.private = '2' AND n.id_user = '$user_id'))
                        HAVING is_frends = '1' OR (is_frends = '0' AND n.private != '1') OR n.id_user = '$user_id'");

   $k_post = mysql_num_rows($sql);
   
} elseif ($maps['s'] == 's') { 
  $k_post = mysql_result(mysql_query("SELECT COUNT(*) 
                                      FROM notes AS n
                                      WHERE (n.name like '%$querySearch%' OR n.tags like '%$querySearch%' OR n.msg like '%$querySearch%') AND n.private = '0'
                                      "), 0);
} elseif ($maps['s'] == 'f') { 
  $k_post = mysql_result(mysql_query("SELECT COUNT(*) 
                                      FROM notes AS n
                                      LEFT JOIN `frends` AS f ON f.frend = n.id_user AND f.i = '1'
                                      WHERE f.user = '$user[id]' AND (n.private = '0' OR n.private = '1')
                                      "), 0);
} elseif ($maps['s'] == 'r') { 
  $k_post = mysql_result(mysql_query("SELECT COUNT(*) 
                                      FROM notes AS n
                                      LEFT JOIN `user` AS u ON u.id = n.id_user
                                      WHERE u.date_last > '" . ($time - 604800) . "' AND (n.private = '0' OR ((n.private = '1' OR n.private = '2') AND id_user = '$user_id'))
                                      "), 0);
} else {
  $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM notes AS n WHERE n.private = '0'"), 0);
}
?>
<ol class="breadcrumb">
  <li><a href="/index.php"><i class="ic ic-home"></i></a></li>
  <? if ($maps['s'] != 'n') { ?>
    <li><a href="?"><?= __('Дневники')?></a></li>
  <? } else { ?>
    <li class="active"><?= __('Дневники')?></li>
  <? } ?>
  <li class="active"><?= $nameSort[$maps['s']]?></li>
  <? if (isset($user)) { ?>
    <a href="?f=edit&amp;s=u&amp;u=<?= $user_id?>&amp;sid=<?= $_SESSION['sid']?>" class="pull-right"><img src="/style/icons/note.png" title="<?= __('Написать в дневник')?>"/></a>
  <? } ?>
</ol>

<? if ($maps['s'] != 'u') { ?>
  <div class="email-search">
    <form action="?" method="GET">
      <input type="hidden" name="s" value="s"/>
      <table cellpadding="0" cellspacing="0">
        <tr>
          <td class="search-input"><input type="text" name="q" value="<?= text($maps['search'])?>" placeholder="<?= __('Поиск по дневникам')?>.."/></td> 
          <td class="search-submit"><button type="submit"><?= $nameSort['s']?></button></td>
        </tr>
      </table>
    </form>
  </div>
<? } elseif ($maps['s'] == 'u' && $user_id == $maps['u']) { 
  $diary_access = (int) $tools->keyGlobals('diary_access', 'sess', 0);
  $insert = text($tools->keyGlobals('diary_message', 'sess', null));
  $placeholder = __('Напишите в свой дневник...');
?>
  <div class="form-create">
    <form action="?f=<?= $maps['f']?>&s=<?= $maps['s']?>&r=index&u=<?= $maps['u']?>" method="POST" enctype="multipart/form-data">
      <? if (is_file(H.'style/themes/' . $set['set_them'] . '/altername_post_form.php')) { 
         include_once H.'style/themes/' . $set['set_them'] . '/altername_post_form.php';
      } else {
        echo $tPanel;?><textarea name="msg"><?= $insert?></textarea><br /><?
      }
      ?>
      <input type="hidden" name="view" value="<?= $diary_access?>" />
      <div class="form-create-panel">
        <button class="button-create pull-left" type="submit" name="diary_access">
            <img src="/style/icons/<?= $accessList[$diary_access]['icon']?>.png" title="<?= $accessList[$diary_access]['name']?>" />
        </button>
        <button class="pull-right" type="submit" name="diary_create">
            <?= __('Написать')?>
        </button>
        <? if (!isset($_SESSION['diary']['attachments']) || count($_SESSION['diary']['attachments']['photo']) < 10) { ?>
          <button class="button-create pull-right" type="submit" name="diary_attach">
              <img src="/style/icons/attach.png" title="<?= __('Добавить вложение')?>" />
          </button>
        <? } ?>
      </div>
      <?
      $attachments = new Attachments('diary');
      echo $attachments->get_list();
      ?>
    </form>
  </div>
<? } ?>

<? if ($maps['s'] != 's' && $maps['s'] != 'u') { ?>
  <ul class="sort sort-justified">
    <li<?= ($maps['s'] == 'n' ? ' class="active"' : '')?>><a href="?s=n"><?= $nameSort['n']?></a></li>
    <li<?= ($maps['s'] == 'r' ? ' class="active"' : '')?>><a href="?s=r"><?= $nameSort['r']?></a></li>
    <? if (isset($user)) { ?>
      <li<?= ($maps['s'] == 'f' ? ' class="active"' : '')?>><a href="?s=f"><?= $nameSort['f']?></a></li>
    <? } ?>
  </ul>
<? 
}

if ($k_post > 0) {

    $k_page = k_page($k_post, $set['p_str']);
    $maps['page'] = page($k_page);
    $start = $set['p_str'] * $maps['page'] - $set['p_str'];

    if ($maps['s'] == 'u') {  
        $q = mysql_query("SELECT n.*, 
                        (SELECT COUNT(*) FROM frends AS f WHERE f.frend = n.id_user AND f.user = '$user_id') AS is_frends,
                        (SELECT COUNT(*) FROM notes_komm AS k WHERE k.id_notes = n.id) AS comments,
                        (SELECT COUNT(*) FROM notes_count AS c WHERE c.id_notes = n.id) AS view,
                        (SELECT COUNT(*) FROM notes_like AS l WHERE l.id_notes = n.id AND l.like = '1') AS 'like',
                        (SELECT COUNT(*) FROM notes_like AS l WHERE l.id_notes = n.id AND l.like = '0') AS 'dlike'
                      FROM notes AS n 
                      WHERE id_user = '" . (int) $maps['u'] . "' AND (n.private = '0' OR n.private = '1' OR (n.private = '2' AND id_user = '$user_id'))
                      HAVING is_frends = '1' OR (is_frends = '0' AND n.private != '1') OR n.id_user = '$user_id'
                      ORDER BY n.id DESC 
                      LIMIT $start, $set[p_str]");    
    } elseif ($maps['s'] == 's') {  
        $q = mysql_query("SELECT n.*, 
                        (SELECT COUNT(*) FROM notes_komm AS k WHERE k.id_notes = n.id) AS comments,
                        (SELECT COUNT(*) FROM notes_count AS c WHERE c.id_notes = n.id) AS view,
                        (SELECT COUNT(*) FROM notes_like AS l WHERE l.id_notes = n.id AND l.like = '1') AS 'like',
                        (SELECT COUNT(*) FROM notes_like AS l WHERE l.id_notes = n.id AND l.like = '0') AS 'dlike'
                      FROM notes AS n 
                      WHERE (n.name like '%$querySearch%' OR n.tags like '%$querySearch%' OR n.msg like '%$querySearch%') AND n.private = '0'
                      ORDER BY n.id DESC 
                      LIMIT $start, $set[p_str]");    
    } elseif ($maps['s'] == 'f') {  
        $q = mysql_query("SELECT n.*, 
                        (SELECT COUNT(*) FROM notes_komm AS k WHERE k.id_notes = n.id) AS comments,
                        (SELECT COUNT(*) FROM notes_count AS c WHERE c.id_notes = n.id) AS view,
                        (SELECT COUNT(*) FROM notes_like AS l WHERE l.id_notes = n.id AND l.like = '1') AS 'like',
                        (SELECT COUNT(*) FROM notes_like AS l WHERE l.id_notes = n.id AND l.like = '0') AS 'dlike'
                      FROM notes AS n 
                      LEFT JOIN `frends` AS f ON f.frend = n.id_user AND f.i = '1'
                      WHERE f.user = '$user[id]' AND (n.private = '0' OR n.private = '1')
                      ORDER BY n.id DESC 
                      LIMIT $start, $set[p_str]");    
    } elseif ($maps['s'] == 'r') {
        $q = mysql_query("SELECT n.*, 
                        (SELECT COUNT(*) FROM notes_komm AS k WHERE k.id_notes = n.id) AS comments,
                        (SELECT COUNT(*) FROM notes_count AS c WHERE c.id_notes = n.id) AS view,
                        (SELECT COUNT(*) FROM notes_like AS l WHERE l.id_notes = n.id AND l.like = '1') AS 'like',
                        (SELECT COUNT(*) FROM notes_like AS l WHERE l.id_notes = n.id AND l.like = '0') AS 'dlike'
                      FROM notes AS n 
                      LEFT JOIN `user` AS u ON u.id = n.id_user
                      WHERE u.date_last > '" . ($time - 604800) . "' AND (n.private = '0' OR ((n.private = '1' OR n.private = '2') AND id_user = '$user_id'))
                      ORDER BY `like` DESC 
                      LIMIT $start, $set[p_str]");     
    } else {
        $q = mysql_query("SELECT n.*, 
                        (SELECT COUNT(*) FROM notes_komm AS k WHERE k.id_notes = n.id) AS comments,
                        (SELECT COUNT(*) FROM notes_count AS c WHERE c.id_notes = n.id) AS view,
                        (SELECT COUNT(*) FROM notes_like AS l WHERE l.id_notes = n.id AND l.like = '1') AS 'like',
                        (SELECT COUNT(*) FROM notes_like AS l WHERE l.id_notes = n.id AND l.like = '0') AS 'dlike'
                      FROM notes AS n 
                      WHERE n.private = '0'
                      ORDER BY n.id DESC 
                      LIMIT $start, $set[p_str]");     
    }

    ?>
    <div class="list-post">
    <?
    while($post = mysql_fetch_assoc($q)) {
      $ank = user::get_user($post['id_user']);
      
      if ($post['attachments']) {  
          $attach = unserialize($post['attachments']);
      } else {
          $attach = array();
      }

      ?>
      <div class="list-post-item">
        <div class="list-post-header">
          <span class="list-post-time">
          <?= $classDiary->vremja($post['time'])?>
            
          <? if ($user_id && user_access('notes_edit') || $user_id == $post['id_user']) { ?>
            <a href="?f=access&amp;s=<?= $maps['s']?>&amp;q=<?= $maps['search']?>&amp;r=index&amp;u=<?= $ank['id']?>&amp;page=<?= $maps['page']?>&amp;zsort=<?= $maps['zsort']?>&amp;id=<?= $post['id']?>&amp;act=edit&amp;edit=view">
              <img src="/style/icons/<?= $accessList[$post['private']]['icon']?>.png" title="<?= $accessList[$post['private']]['name']?>" />
            </a>
          <? } else { ?>
            <img src="/style/icons/<?= $accessList[$post['private']]['icon']?>.png" title="<?= $accessList[$post['private']]['name']?>" />
          <? } ?>
          </span>
          <?= $ank['avatar']?> <?= $ank['icon']?> <?= $ank['nick']?> <?= $ank['online']?>
          <a href="?f=diary&amp;s=<?= $maps['s']?>&amp;q=<?= text($maps['search'])?>&amp;r=index&amp;u=<?= $post['id_user']?>&amp;id=<?= $post['id']?>&amp;zsort=<?= $maps['zsort']?>"><?= $classDiary->get_name($post['name'], $post['msg'])?></a>
          
        </div>
            
        <? if (isset($attach['photo'])) { ?>
          <div class="attach">
          <? 
          $countPhoto = count($attach['photo']);

          foreach($attach['photo'] AS $file) { 
            if (is_file(H.'sys/gallery/foto/' . $file['fileId'] . '.jpg')) {
              ?>
              <div class="attach-photo attach-photo-<?= $countPhoto?>">
                <a href="/user/attachments/download/photo/<?= $file['fileSize']?>_0/<?= $file['fileId']?>/<?= $file['fileNameSend']?>"><img src="/user/attachments/download/photo/<?= $file['fileSize']?>_<?= ($countPhoto == 1 ? 640 : 128)?>/<?= $file['fileId']?>/<?= $file['fileNameSend']?>" /></a>
              </div>
              <?    
            }
          } ?>
          </div>      
        <? } ?>
        
        <div class="list-post-item-text" id="post-<?= $post['id']?>">
          <?= output_text($post['msg'])?>
          <span class="list-post-item-end" onclick="this.style.display='none';document.getElementById('post-<?= $post['id']?>').style.maxHeight='none'; return false;"><b>&equiv;</b></span>
        </div>
        
        <div class="list-post-item-panel">
          <div class="list-post-info">
            <span class="pull-left">
              <img src="/style/icons/views_num_gray.png" /> <?= $post['view']?> 
              <img src="/style/icons/comm_num_gray.png" /> <?= $post['comments']?> 
            </span>
            <span class="pull-right">
              <img src="/style/icons/vote_up.png" /> <?= $post['like']?> 
              <img src="/style/icons/vote_down.png" /> <?= $post['dlike']?> 
            </span>
          </div>
          
          <a href="?f=diary&amp;s=<?= $maps['s']?>&amp;q=<?= text($maps['search'])?>&amp;r=index&amp;u=<?= $post['id_user']?>&amp;id=<?= $post['id']?>&amp;zsort=<?= $maps['zsort']?>&amp;page=<?= $maps['page']?>#page-up"><i class="icpb icpb-comments"></i> <?= __('Обсудить')?></a> 
          <a href="?f=share&amp;s=<?= $maps['s']?>&amp;q=<?= $maps['search']?>&amp;r=<?= $maps['r']?>&amp;u=<?= $post['id_user']?>&amp;id=<?= $post['id']?>&amp;zsort=<?= $maps['zsort']?>&amp;page=<?= $maps['page']?>"><i class="icpb icpb-share"></i> <span class="hidden-xs"><?= __('Поделиться')?></span></a> 
        </div>
      </div>
      <?
    }
    ?>
    </div>
    <?
    if ($k_page > 1) { 
        str('?f=' . $maps['f'] . '&amp;s=' . $maps['s'] . '&amp;q=' . text($maps['search']) . '&amp;r=' . $maps['r'] . '&amp;u=' . $maps['u'] . '&amp;zsort' . $maps['zsort'] . '&amp;', $k_page, $maps['page']);
    }  
} else {
    ?>
    <div class="alert">
      <?= __('Ничего не найдено!')?>
    </div>
    <?
}
