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

$total = mysql_result(mysql_query("SELECT COUNT(*) FROM (SELECT DISTINCT `user_id` FROM `cms_mail` WHERE `from_id`='$user_id' AND `read`='0') a;"), 0);
if($total == 1) {
	//Если все сообщения от одного итого же чела показываем сразу переписку
	$max = mysql_result(mysql_query("SELECT `user_id`, count(*) FROM `cms_mail` WHERE `from_id`='$user_id' AND `read`='0' GROUP BY `user_id`;"), 0);
	Header('Location: index.php?act=write&id='.$max);
	exit();
}
$textl = 'Личная почта';
require_once('../incfiles/head.php');
echo '<div class="phdr"><h3>Новые сообщения</h3></div>';

if($total) {
	//Групируем по контактам
	$query = mysql_query("SELECT `users`.* FROM `cms_mail`
		LEFT JOIN `users` ON `cms_mail`.`user_id`=`users`.`id`
		WHERE `cms_mail`.`from_id`='" . $user_id . "' AND `cms_mail`.`read`='0' GROUP BY `cms_mail`.`user_id` ORDER BY `cms_mail`.`time` DESC LIMIT " . $start . "," . $kmess
    );
	for ($i = 0; ($row = mysql_fetch_assoc($query)) !== false; ++$i) {
		echo $i % 2 ? '<div class="list1">' : '<div class="list2">';
		$subtext = '<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`='{$row['id']}' AND `from_id`='$user_id') OR (`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`='{$row['id']}' AND `cms_mail`.`from_id`='$user_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>';
	}
	//Навигация
	if ($total > $kmess) {
		echo '<p>' . pagenav('index.php?act=new&amp;', $start, $total, $kmess) . '</p>';
		echo '<p><form action="index.php" method="get">
		<input type="hidden" name="act" value="new"/>
		<input type="text" name="page" size="2"/>
		<input type="submit" value="К странице &gt;&gt;"/></form></p>';
	}
	echo '<div class="bmenu"><a href="../">В кабинет</a></div>';
} else {
	echo '<div class="rmenu">Новых сообщений нет</div>';
	echo '<div class="bmenu"><a href="../">В кабинет</a></div>';
}