<?php 

$diary = mysql_fetch_assoc(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.id = " . (int) $maps['diary_id']));

if (!isset($diary['id'])) {
    header('Location: ?');
    exit;
}

// Права доступа
$token = $classDiary->is_access($diary, $user_id, 'private');

if (!$token) {
    header('Location: ?f=access&s='. $maps['s'] .'&q='. $maps['search'] .'&r='. $maps['r'] .'&u='. $maps['u'] .'&id='. $maps['diary_id'] .'&act=alert');
    exit;
}

if (isset($user)) {
    mysql_query("UPDATE `notification` SET `read` = '1' WHERE `type` = 'notes_komm' AND `id_user` = '$user[id]' AND `id_object` = '$diary[id]'");
    mysql_query("UPDATE `discussions` SET `count` = '0' WHERE `id_user` = '$user[id]' AND `type` = 'notes' AND `id_sim` = '$diary[id]' LIMIT 1");  
}

$ank = user::get_user($diary['id_user']);

if ($maps['s'] == 'u') {
    $nameSort['u'] = $ank['nick'];
}

$diaryLeft = mysql_fetch_assoc(mysql_query("SELECT n.*
                                              FROM notes AS n
                                              WHERE n.id_user = '$ank[id]' AND n.id > '$diary[id]' 
                                              ORDER BY n.id ASC
                                              LIMIT 1"));
                                              
$diaryRight = mysql_fetch_assoc(mysql_query("SELECT n.*
                                              FROM notes AS n
                                              WHERE n.id_user = '$ank[id]' AND n.id < '$diary[id]'
                                              ORDER BY n.id DESC
                                              LIMIT 1"));

if ($diary['attachments']) {  
    $attach = unserialize($diary['attachments']);
} else {
    $attach = array();
}

if ($user_id && $diary['id_user'] != $user_id) {
    if (mysql_result(mysql_query("SELECT COUNT(*) FROM `notes_count` WHERE `id_user` = '$user_id' AND `id_notes` = '$diary[id]'"),0) == 0) {
        mysql_query("INSERT INTO `notes_count` (`id_notes`, `id_user`) VALUES ('$diary[id]', '$user_id')");
        mysql_query("UPDATE `notes` SET `count` = `count` + '1' WHERE `id` = '$diary[id]' LIMIT 1");      
    }
}

$_SESSION['sid'] = mt_rand(111, 999);
$set['title'] = $classDiary->get_name($diary['name'], $diary['msg']);
require H.'sys/inc/thead.php';
title();
aut();
err();
?>
<ol class="breadcrumb">
  <li><a href="/index.php"><i class="ic ic-home"></i></a></li>
  <li><a href="?"><?= __('Дневники')?></a></li>
  <li><a href="?f=index&amp;s=<?= $maps['s']?>&amp;q=<?= $maps['search']?>&amp;r=<?= $maps['r']?>&amp;u=<?= $ank['id']?>&amp;zsort=<?= $maps['zsort']?>"><?= $nameSort[$maps['s']]?></a></li>
</ol>

<div class="list-post list-post-full">
  <div class="list-post-item">
    <div class="list-post-header">
      <?= $ank['avatar']?> <?= $ank['icon']?> <?= $ank['link']?> <?= $ank['online']?> 
      <b><?= text($diary['name'])?></b>
      <span class="list-post-time">
        <?= vremja($diary['time'])?> 
        
        <? if ($user_id && user_access('notes_edit') || $user_id == $diary['id_user']) { ?>
          <a href="?f=access&amp;s=<?= $maps['s']?>&amp;q=<?= $maps['search']?>&amp;r=<?= $maps['f']?>&amp;u=<?= $ank['id']?>&amp;zsort=<?= $maps['zsort']?>&amp;id=<?= $maps['diary_id']?>&amp;act=edit&amp;edit=view">
            <img src="/style/icons/<?= $accessList[$diary['private']]['icon']?>.png" title="<?= $accessList[$diary['private']]['name']?>" />
          </a>
        <? } else { ?>
          <img src="/style/icons/<?= $accessList[$diary['private']]['icon']?>.png" title="<?= $accessList[$diary['private']]['name']?>" />
        <? } ?>
      </span>
    </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-<?= $diary['id']?>">
      <?= output_text($diary['msg'])?>
      <span class="list-post-item-end" onclick="this.style.display='none';document.getElementById('post-<?= $diary['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" /> <?= $diary['view']?> 
          <img src="/style/icons/comm_num_gray.png" /> <?= $diary['comments']?> 
        </span>
        <span class="pull-right">
          <img src="/style/icons/vote_up.png" /> <?= $diary['like']?> 
          <img src="/style/icons/vote_down.png" /> <?= $diary['dlike']?> 
        </span>
      </div>
      
      <? if (isset($user) && $user_id == $diary['id_user'] || user_access('notes_edit')) { ?>
      <a href="?f=edit&amp;s=<?= $maps['s']?>&amp;q=<?= $maps['search']?>&amp;r=<?= $maps['r']?>&amp;u=<?= $diary['id_user']?>&amp;id=<?= $diary['id']?>&amp;zsort=<?= $maps['zsort']?>&amp;sid=<?= $_SESSION['sid']?>"><i class="icpb icpb-edit"></i></a> 
      <? } ?>
      
      <a href="?f=share&amp;s=<?= $maps['s']?>&amp;q=<?= $maps['search']?>&amp;r=<?= $maps['r']?>&amp;u=<?= $maps['u']?>&amp;id=<?= $diary['id']?>&amp;zsort=<?= $maps['zsort']?>"><i class="icpb icpb-share"></i> <?= __('Поделиться')?></a> 
      
      <ul class="list-like pull-right">
        <li>
          <a href="?f=<?= $maps['f']?>&amp;s=<?= $maps['s']?>&amp;q=<?= $maps['search']?>&amp;r=<?= $maps['f']?>&amp;u=<?= $maps['u']?>&amp;id=<?= $diary['id']?>&amp;zsort=<?= $maps['zsort']?>&amp;like=1">
            <i class="icpb icpb-like"></i>
          </a>
        </li>
        <li><?= ($diary['like'] + $diary['dlike'])?></li>
        <li>
          <a href="?f=<?= $maps['f']?>&amp;s=<?= $maps['s']?>&amp;q=<?= $maps['search']?>&amp;r=<?= $maps['f']?>&amp;u=<?= $maps['u']?>&amp;id=<?= $diary['id']?>&amp;zsort=<?= $maps['zsort']?>&amp;like=0">
            <i class="icpb icpb-dlike"></i>
          </a>
        </li>
      </ul>
    </div>
  </div>
</div>

<? if ($diaryLeft || $diaryRight) { ?>
  <div class="list-post" id="page-up">
    <div class="list-post-item">
      <ul class="pager">
        <? if ($diaryLeft) { ?>
          <li><a href="?f=<?= $maps['f']?>&amp;s=u&amp;q=<?= $maps['search']?>&amp;r=<?= $maps['r']?>&amp;u=<?= $maps['u']?>&amp;id=<?= $diaryLeft['id']?>&amp;zsort=<?= $maps['zsort']?>">&larr; <?= __('Пред')?></a></li>
        <? } ?>
        
        <? if ($diaryRight) { ?>
        <li><a href="?f=<?= $maps['f']?>&amp;s=u&amp;q=<?= $maps['search']?>&amp;r=<?= $maps['r']?>&amp;u=<?= $maps['u']?>&amp;id=<?= $diaryRight['id']?>&amp;zsort=<?= $maps['zsort']?>"><?= __('Cлед')?> &rarr;</a></li>
        <? } ?>
      </ul>
    </div>
  </div>
<? } ?>

<div class="list-post" id="page-up">
  <div class="list-post-item">
    <div class="list-post-header">
      <b><?= __('Комментарии')?></b> (<?= $diary['comments']?>)<br />
      <? if ($diary['comments'] > 0) { ?>
      <a class="btn btn-link pull-right" href="#page-down">&darr;</a>
        <?= __('Новые')?> <a<?= ($maps['zsort'] == '1' ? ' class="active"' : '')?> href="?f=<?= $maps['f']?>&amp;s=<?= $maps['s']?>&amp;q=<?= $maps['search']?>&amp;r=<?= $maps['r']?>&amp;u=<?= $maps['u']?>&amp;id=<?= $diary['id']?>&amp;zsort=1"><?= __('Вверху')?></a>
        | 
        <a<?= ($maps['zsort'] == '0' ? ' class="active"' : '')?> href="?f=<?= $maps['f']?>&amp;s=<?= $maps['s']?>&amp;q=<?= $maps['search']?>&amp;r=<?= $maps['r']?>&amp;u=<?= $maps['u']?>&amp;id=<?= $diary['id']?>&amp;zsort=0"><?= __('Внизу')?></a>        
      <? } ?>
    </div>
  </div>
</div>
<?
if ($diary['comments'] > 0) {
  $k_page = k_page($diary['comments'], $set['p_str']);
  $maps['page'] = page($k_page);
  $start = $set['p_str'] * $maps['page'] - $set['p_str'];

  $q = mysql_query("SELECT * FROM notes_komm WHERE id_notes = '$diary[id]' ORDER BY id $zsort LIMIT $start, $set[p_str]");
  ?>
  <ul class="media-list media-post">
    <? 
    while ($post = mysql_fetch_assoc($q)) { 
    $anketa = user::get_user($post['id_user']);
    ?>
      <li class="media">
        <div class="media-avatar">
          <?= $anketa['avatar']?>
        </div>
        <div class="media-body">
          <h5 class="media-heading">
            <?= $anketa['icon']?> <?= $anketa['link']?> <?= $anketa['online']?>
            <span class="list-post-time">
              <?= $classDiary->vremja($post['time'])?> 
              <a href="?f=delete&amp;type=comment&amp;s=<?= $maps['s']?>&amp;q=<?= $maps['search']?>&amp;r=diary&amp;u=<?= $maps['u']?>&amp;id=<?= $diary['id']?>&amp;delete_id=<?= $post['id']?>">
                <img src="/style/icons/delete.gif" />
              </a>
            </span>
          </h5>
          <?= output_text($post['msg'])?>
          <? if ($user_id && $user_id != $post['id_user']) { ?>
            <div>
              <a href="?f=<?= $maps['f']?>&amp;s=<?= $maps['s']?>&amp;q=<?= $maps['search']?>&amp;r=<?= $maps['r']?>&amp;u=<?= $maps['u']?>&amp;id=<?= $diary['id']?>&amp;response=<?= $post['id_user']?>&amp;zsort=<?= $maps['zsort']?>#page-down"><?= __('Ответ')?></a>
            </div>
          <? } ?>
        </div>
      </li>
    <? } ?>
    <li class="media"><a id="page-down" class="btn btn-link pull-right" href="#page-up">&uarr;</a></li>
  </ul>
  <?
  if ($k_page > 1) { 
      str('?f=' . $maps['f'] . '&amp;s=' . $maps['s'] . '&amp;q=' . $maps['search'] . '&amp;r=' . $maps['r'] . '&amp;u=' . $maps['u'] . '&amp;id=' . $diary['id'] . '&amp;zsort=' . $maps['zsort'] . '&amp;', $k_page, $maps['page']);
  }  
}

if (isset($user)) {
  $token = $classDiary->is_access($diary, $user_id, 'private_komm');
  
  if ($token) {
    ?>
    <div class="list-form">   
      <form method="post" name="message" action="?f=<?= $maps['f']?>&amp;s=<?= $maps['s']?>&amp;q=<?= $maps['search']?>&amp;r=diary&amp;zsort=<?= $maps['zsort']?>&amp;u=<?= $maps['u']?>&amp;id=<?= $diary['id']?>">
        <? 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"><?= $otvet?></textarea><br /><?
        }
        ?>
        <input name="reply" value="<?= $ank_reply['id']?>" type="hidden" /> 
        <input name="comment" value="<?= isset($ank_reply['id']) ? __('Ответить') . ' ' . text($ank_reply['nick']) : __('Отправить')?>" type="submit" />
      </form>
    </div>
    <?      
  } else {
    ?>
    <div class="alert alert-warning">
      <?= __('Автор ограничил комментирование этой записи.')?>
    </div>
    <?
  }
}
