<?php

/**
 * @package     JohnCMS
 * @link        http://johncms.com
 * @copyright   Copyright (C) 2008-2011 JohnCMS Community
 * @license     LICENSE.txt (see attached file)
 * @version     VERSION.txt (see attached file)
 * @author      http://johncms.com/about
 */

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

/*
-----------------------------------------------------------------
История активности
-----------------------------------------------------------------
*/
$textl = htmlspecialchars($user['name']) . ': ' . $lng_profile['activity'];
require('../incfiles/head.php');
echo '<div class="phdr"><a href="profile.php?user=' . $user['id'] . '"><b>' . $lng['profile'] . '</b></a> | ' . $lng_profile['activity'] . '</div>';
$menu = array(
    (!$mod ? '<b>' . $lng['messages'] . '</b>' : '<a href="profile.php?act=activity&amp;user=' . $user['id'] . '">' . $lng['messages'] . '</a>'),
    ($mod == 'topic' ? '<b>' . $lng['themes'] . '</b>' : '<a href="profile.php?act=activity&amp;mod=topic&amp;user=' . $user['id'] . '">' . $lng['themes'] . '</a>'),
    ($mod == 'comments' ? '<b>' . $lng['comments'] . '</b>' : '<a href="profile.php?act=activity&amp;mod=comments&amp;user=' . $user['id'] . '">' . $lng['comments'] . '</a>'),
);
echo '<div class="topmenu">' . functions::display_menu($menu) . '</div>' .
     '<div class="user"><p>' . functions::display_user($user, array('iphide' => 1,)) . '</p></div>';
switch ($mod) {
    case 'comments':
        /*
        -----------------------------------------------------------------
        Список сообщений в Гостевой
        -----------------------------------------------------------------
        */
        $total = mysql_result(mysql_query("SELECT COUNT(*) FROM `guest` WHERE `user_id` = '" . $user['id'] . "'" . ($rights >= 1 ? '' : " AND `adm` = '0'")), 0);
        echo '<div class="phdr"><b>' . $lng['comments'] . '</b></div>';
        if ($total > $kmess) echo '<div class="topmenu">' . functions::display_pagination('profile.php?act=activity&amp;mod=comments&amp;user=' . $user['id'] . '&amp;', $start, $total, $kmess) . '</div>';
        $req = mysql_query("SELECT * FROM `guest` WHERE `user_id` = '" . $user['id'] . "'" . ($rights >= 1 ? '' : " AND `adm` = '0'") . " ORDER BY `id` DESC LIMIT $start, $kmess");
        if (mysql_num_rows($req)) {
            $i = 0;
            while ($res = mysql_fetch_assoc($req)) {
                echo ($i % 2 ? '<div class="list2">' : '<div class="list1">') . functions::checkout($res['text'], 2, 1) . '<div class="sub">' .
                     '<span class="gray">(' . functions::display_date($res['time']) . ')</span>' .
                     '</div></div>';
                ++$i;
            }
        } else {
            echo '<div class="menu"><p>' . $lng_profile['guest_empty'] . '</p></div>';
        }
        break;

    case 'topic':
        /*
        -----------------------------------------------------------------
        Список тем Форума
        -----------------------------------------------------------------
        */
        require_once('../forum/includes/functoins.php');
        $total = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_topics` WHERE `user_id` = '".$user['id']."' "), 0);
        if ($start >= $total){
          // Исправляем запрос на несуществующую страницу
          $start = max(0, $total - (($total % $kmess) == 0 ? $kmess : ($total % $kmess)));
        }
        
        echo '<div class="phdr"><b>' . $lng['forum'] . '</b>: ' . $lng['themes'] . '</div>';
        if ($total > $kmess) echo '<div class="topmenu">' . functions::display_pagination('profile.php?act=activity&amp;mod=topic&amp;user=' . $user['id'] . '&amp;', $start, $total, $kmess) . '</div>';
        $req = mysql_query("SELECT * FROM `forum_topics` WHERE `user_id` = '".$user['id']."' ORDER BY `time` DESC LIMIT $start, $kmess ");
        
        if ($total){
            $i = 0;
            while ($res = mysql_fetch_assoc($req)) {
              echo $i % 2 ? '<div class="list2">' : '<div class="list1">';

              $sub = '';
              $lastPost = explode(':|:', $res['lastpost']);
              $sub .= ' <a href="../forum/index.php?post='.$lastPost[1].'&amp;find">Последн.</a>: '.$lastPost[0];
              $sub .= ' '.functions::display_date($res['time']).'<br />';

              $sub .= 'Подфорум: <a href="../forum/index.php?forum='.$res['forum'].'">'.$res['forumname'].'</a>';
              echo show_topic($res, $sub);
              echo '</div>';
              ++$i;  
            }
        } else {
            echo '<div class="menu"><p>' . $lng['list_empty'] . '</p></div>';
        }
        break;

    default:
        /*
        -----------------------------------------------------------------
        Список постов Форума
        -----------------------------------------------------------------
        */
        require_once('../forum/includes/functoins.php');
        $total = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_posts` WHERE `user_id` = '".$user['id']."' "), 0);
        if ($start >= $total){
          // Исправляем запрос на несуществующую страницу
          $start = max(0, $total - (($total % $kmess) == 0 ? $kmess : ($total % $kmess)));
        }
        
        echo '<div class="phdr"><b>' . $lng['forum'] . '</b>: ' . $lng['messages'] . '</div>';
        if ($total > $kmess) echo '<div class="topmenu">' . functions::display_pagination('profile.php?act=activity&amp;user=' . $user['id'] . '&amp;', $start, $total, $kmess) . '</div>';
        $req = mysql_query("SELECT * FROM `forum_posts` WHERE `user_id` = '".$user['id']."' ORDER BY `time` DESC LIMIT $start, $kmess ");
        if ($total) {
            $i = 0;
            while ($res = mysql_fetch_assoc($req)) {
              echo $i % 2 ? '<div class="list2">' : '<div class="list1">';

              echo '<span class="gray">'.functions::display_date($res['time']).'</span> <a href="../forum/index.php?post='.$res['post'].'&amp;find" title="Перейти к сообщению">&gt;&gt;</a> '.'<br />';
              echo functions::checkout($res['text'], 1, 1);

              if ($res['files']){
                $file = mysql_query("SELECT * FROM `forum_files` WHERE `post` = '".$res['post']."' LIMIT ".$res['files']." ");
                echo '<div class="func">Файл(ы):<br />';
                while($fileRes = mysql_fetch_array($file)){
                  echo show_file($fileRes).'<br />';
                }
                echo '</div>';
              }
              $topicRes = mysql_fetch_array(mysql_query("SELECT * FROM `forum_topics` WHERE `topic`='".$res['topic']."' LIMIT 1 "));
              echo '<div class="sub">Тема: <a href="../forum/index.php?topic='.$topicRes['topic'].'">'.$topicRes['topicname'].'</a></div>';
              echo '</div>';
              ++$i;
            }
        } else {
            echo '<div class="menu"><p>' . $lng['list_empty'] . '</p></div>';
        }
}
echo '<div class="phdr">' . $lng['total'] . ': ' . $total . '</div>';
if ($total > $kmess) {
    echo '<div class="topmenu">' . functions::display_pagination('profile.php?act=activity' . ($mod ? '&amp;mod=' . $mod : '') . '&amp;user=' . $user['id'] . '&amp;', $start, $total, $kmess) . '</div>' .
         '<div class="topmenu"><form action="profile.php?act=activity&amp;user=' . $user['id'] . ($mod ? '&amp;mod=' . $mod : '') . '" method="post">' .
         '<input type="text" name="page" size="2"/>' .
         '<input type="submit" value="' . $lng['to_page'] . ' &gt;&gt;"/>' .
         '</form></div>';
}
?>