<?php
# Script by seg0ro http://mobilarts.ru
# Not for sale!!!

defined('_IN_JOHNCMS') or die('Error: restricted access');

$textl .= ' | Новые, последние темы';
require_once('../incfiles/head.php');

if (!$user_id){
  echo functions::display_error('Только для авторизованных!<br /><a href="index.php">Форум</a>');
  require_once('../incfiles/end.php');
  exit;
}

$timeRes = isset($_REQUEST['time']) ? abs(intval($_REQUEST['time'])) : null;
if ($timeRes)
  $time = time() - $timeRes * 3600;

$unread = isset($_GET['unread']) ? 1 : 0;
$nav = '';

if ($unread || isset($_GET['reset'])){
  $tree = array('<a href="index.php">Форум</a>', 'Непрочитанные');
  $nav = 'unread&amp;';
}else{
  $nav = $time ? 'time='.$timeRes.'&amp;' : '';
  $tree = array('<a href="index.php">Форум</a>', 'Последние темы'.($time ? ' за '.$timeRes.' час.' : ''));
}
echo '<div class="phdr">'.functions::display_menu($tree).'</div>';

if (isset($_GET['reset'])){

  $req = mysql_query("SELECT `forum_topics`.`topic`, `forum_topics`.`lastpost` FROM `forum_topics` LEFT JOIN `forum_readed` ON `forum_topics`.`topic` = `forum_readed`.`topic` AND `forum_readed`.`user_id` = '$user_id' WHERE `forum_readed`.`topic` Is Null ");
  while ($res = mysql_fetch_array($req)){
    $lastPost = explode(':|:', $res['lastpost']);
    mysql_query("INSERT INTO `forum_readed` SET
    `topic` = '".$res['topic']."',
    `user_id` = '$user_id',
    `time` = '".time()."',
    `lastpost` = '".$lastPost[1]."' ");
  }
  $req = mysql_query("SELECT `forum_topics`.`topic`, `forum_topics`.`lastpost` FROM `forum_topics` LEFT JOIN `forum_readed` ON `forum_topics`.`topic` = `forum_readed`.`topic` AND `forum_readed`.`user_id` = '$user_id' WHERE `forum_topics`.`time` > `forum_readed`.`time` ");
  while ($res = mysql_fetch_array($req)) {
    $lastPost = explode(':|:', $res['lastpost']);
    mysql_query("UPDATE `forum_readed` SET
    `time` = '".time()."',
    `lastpost` = '".$lastPost[1]."'
    WHERE `topic` = '".$res['topic']."' AND `user_id` = '$user_id' ");
  }

  echo '<div class="gmenu">Все темы приняты как прочитанные<br /><a href="index.php">Форум</a></div>';
  require_once('../incfiles/end.php');
  exit;
}

if ($unread){
  $total = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_topics` LEFT JOIN `forum_readed` ON `forum_topics`.`topic` = `forum_readed`.`topic` AND `forum_readed`.`user_id` = '$user_id' WHERE (`forum_readed`.`topic` Is Null OR `forum_topics`.`time` > `forum_readed`.`time`) "), 0);
}else{
  $total = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_topics` ".($time ? "WHERE `forum_topics`.`time` > '".$time."' " : "")), 0);
}

if ($start >= $total){
  // Исправляем запрос на несуществующую страницу
  $start = max(0, $total - (($total % $kmess) == 0 ? $kmess : ($total % $kmess)));
}
    
if ($total){
  if ($total > $kmess)
    echo '<div class="topmenu">'.functions::display_pagination('index.php?act=last&amp;'.$nav, $start, $total, $kmess).'</div>';

  if ($unread){
    $req = mysql_query("SELECT `forum_topics`.*, `forum_topics`.`time` AS `topictime`, `forum_topics`.`lastpost` AS `lastpost` FROM `forum_topics` LEFT JOIN `forum_readed` ON `forum_topics`.`topic` = `forum_readed`.`topic` AND `forum_readed`.`user_id` = '$user_id' WHERE (`forum_readed`.`topic` Is Null OR `forum_topics`.`time` > `forum_readed`.`time`) ORDER BY `forum_topics`.`time` DESC LIMIT $start, $kmess ");
  }else{
    $req = mysql_query("SELECT *, `forum_topics`.`time` AS `topictime` FROM `forum_topics` ".($time ? "WHERE `time` > '$time' " : "")."ORDER BY `time` DESC LIMIT $start, $kmess ");
  }

  while ($res = mysql_fetch_array($req)){
    echo $i % 2 ? '<div class="list2">' : '<div class="list1">';
    $sub = 'Автор: '.$res['user'];
    if ($res['count'] > 1){
      $lastPost = explode(':|:', $res['lastpost']);
      $sub .= ' / '.$lastPost[0];
    }
    $sub .= ' '.functions::display_date($res['topictime']).'<br />';
    $sub .= 'Подфорум: <a href="index.php?forum='.$res['forum'].'">'.$res['forumname'].'</a>';
    echo show_topic($res, $sub, ($unread ? 1 : 0));
    echo '</div>';
    ++$i;
  }
  
  echo '<div class="phdr">Тем: '.$total.'</div>';
  if ($total > $kmess){
    echo '<div class="topmenu">'.functions::display_pagination('index.php?act=last&amp;'.$nav, $start, $total, $kmess).'</div>';
  }
    
}else{
  echo '<div class="list1">Пусто</div>';
}

if ($unread && $total)
  echo '<div class="topmenu"><a href="index.php?act=last&amp;reset">Принять как прочитанное</a></div>';
echo '<div class="topmenu"><form action="index.php?act=last" method="get">
<input type="hidden" name="act" value="last" />
Показать за период (час.) <input type="text" size="2" name="time" value="'.($timeRes ? $timeRes : '24').'" /> <input type="submit" value="OK" /></div>';
if ($time || $unread)
  echo '<div class="topmenu"><a href="index.php?act=last">Показать все</a></div>';
