<?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');

if ($id) {
    //Проверка существования пользователя
    $reqs = mysql_query("SELECT * FROM `users` WHERE `id` = '$id' LIMIT 1;");
    if (mysql_num_rows($reqs) == 0) {
        $textl = 'Личная почта';
        require_once('../incfiles/head.php');
        echo display_error('Такого пользователя не существует');
        require_once("../incfiles/end.php");
        exit;
    }
    $res = mysql_fetch_assoc($reqs);
    $textl = 'Личная почта';
    require_once('../incfiles/head.php');
    echo '<div class="phdr"><h3>Отправленные сообщения для ' . $res['name'] . '</h3></div>';
    //Отображаем список исходящих сообщений
    $total = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_mail` WHERE `user_id`='$user_id' AND `from_id`='$id' AND `delete`!='$user_id';"), 0);
    if ($total) {
        $req = mysql_query("SELECT `cms_mail`.*, `cms_mail`.`id` as `mid`, `cms_mail`.`time` as `mtime`, `users`.* FROM `cms_mail` LEFT JOIN `users` ON `cms_mail`.`user_id`=`users`.`id` WHERE `cms_mail`.`user_id`='$user_id' AND `cms_mail`.`from_id`='$id' AND `cms_mail`.`delete`!='$user_id' ORDER BY `cms_mail`.`time` DESC LIMIT " . $start . "," . $kmess);
        for ($i = 0; ($row = mysql_fetch_assoc($req)) !== false; ++$i) {
            echo $i % 2 ? '<div class="list1">' : '<div class="list2">';
            $vrp1 = date("d.m.Y / H:i", $row['mtime'] + $set_user['sdvig'] * 3600);
            $post = $row['text'];
            $post = checkout($post, 1, 1);
            if ($set_user['smileys'])
                $post = smileys($post, $row['rights'] >= 1 ? 1 : 0);
            if ($row['file_name'])
                $post .= '<div class="func">Файл: <a href="index.php?act=load&amp;id=' . $row['mid'] . '">' . $row['file_name'] . '</a> (' . formatsize($row['size']) . ')(' . $row['count'] . ')</div>';
            $subtext = '<a href="index.php?act=delete&amp;id=' . $row['mid'] . '">Удалить</a>';
            echo show_user($row, 1, ($rights >= 6 && $rights >= $row['rights'] ? 1 : 0), $vrp1, $post, $subtext);
            echo '</div>';
        }
        echo '<div class="phdr">Всего: ' . $total . '</div>';
        //Навигация
        if ($total > $kmess) {
            echo '<p>' . pagenav('index.php?act=output&amp;id=' . $id . '&amp;', $start, $total, $kmess) . '</p>';
            echo '<p><form action="index.php" method="get">
			<input type="hidden" name="act" value="output"/>
			<input type="hidden" name="id" value="' . $id . '"/>
			<input type="text" name="page" size="2"/>
			<input type="submit" value="К странице &gt;&gt;"/></form></p>';
        }
    } else {
        echo '<div class="rmenu">Сообщений нет</div>';
    }
    echo '<div class="menu"><a href="index.php?act=write&amp;id=' . $id . '">Написать</a></div>';
    echo '<div class="menu"><a href="index.php?act=output">Отправленные</a></div>';
} else {
    $textl = 'Личная почта';
    require_once('../incfiles/head.php');
    echo '<div class="phdr"><h3>Отправленные сообщения</h3></div>';
    //Групируем по контактам
    $count = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_mail` WHERE `user_id`='$user_id' AND `delete`!='$user_id';"), 0);
    $total = mysql_result(mysql_query("SELECT COUNT(*) FROM (SELECT DISTINCT `from_id` FROM `cms_mail` WHERE `user_id`='$user_id') a;"), 0);
    if ($total) {
        $req = mysql_query("SELECT `users`.* FROM `cms_mail`
		    LEFT JOIN `users` ON `cms_mail`.`from_id`=`users`.`id`
		    WHERE `cms_mail`.`user_id`='" . $user_id . "' AND `cms_mail`.`delete`!='$user_id' GROUP BY `cms_mail`.`from_id` ORDER BY `cms_mail`.`time` DESC LIMIT " . $start . "," . $kmess
        );
        for ($i = 0; ($row = mysql_fetch_assoc($req)) !== false; ++$i) {
            echo $i % 2 ? '<div class="list1">' : '<div class="list2">';
            $subtext = '<a href="index.php?act=output&amp;id=' . $row['id'] . '">Отправленные</a> | <a href="index.php?act=write&amp;id=' . $row['id'] . '">Переписка</a> | <a href="index.php?act=deluser&amp;id=' . $row['id'] . '">Удалить</a> | <a href="index.php?act=ignor&amp;id=' . $row['id'] . '&amp;add">В игнор</a>';
            $count_message = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_mail` WHERE `user_id`='$user_id' AND `from_id`='{$row['id']}' AND `delete`!='$user_id';"), 0);
            $new_count_message = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_mail` WHERE `cms_mail`.`user_id`='$user_id' AND `cms_mail`.`from_id`='{$row['id']}' AND `read`='0' AND `delete`!='$user_id';"), 0);
            echo show_user($row, 1, ($rights >= 6 && $rights >= $row['rights'] ? 1 : 0), '(' . $count_message . ($new_count_message ? '/<span class="red">+' . $new_count_message . '</span>' : '') . ')', NULL, $subtext);
            echo '</div>';
        }
        echo '<div class="phdr">Всего: ' . $count . '</div>';
        //Навигация
        if ($total > $kmess) {
            echo '<p>' . pagenav('index.php?act=output&amp;', $start, $total, $kmess) . '</p>';
            echo '<p><form action="index.php" method="get">
			<input type="hidden" name="act" value="output"/>
			<input type="text" name="page" size="2"/>
			<input type="submit" value="К странице &gt;&gt;"/></form></p>';
        }
    } else {
        echo '<div class="rmenu">Сообщений нет</div>';
    }
}

echo '<div class="bmenu"><a href="../index.php?act=cab">Кабинет</a></div>';