<?php
	/*
	* Модуль почты для JohnCMS 3.2.2 v2.6
	* Copyright © 2010-2011 UPcoder.RU Group All Rights Reserved.
	* Coding by Krite 2010-2011
	*/
	defined('_IN_JOHNCMS') or die('Error: restricted access');
	$textl = 'Непрочитанные сообщения';
	$headmod = 'pochta';
		$out = '';
		$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `mail_msg` WHERE `id_kont`='" . $user_id . "' AND `read`='1' AND `spam`='1'"), 0);
		if($total > 0) {
			$total_one = mysql_result(mysql_query("SELECT COUNT(*) FROM (SELECT DISTINCT `id_user` FROM `mail_msg` WHERE `id_kont`='$user_id' AND `read`='1' AND `sys`!='2') a;"), 0);
			if($total_one == 1) {
				$max = mysql_result(mysql_query("SELECT `id_user`, count(*) FROM `mail_msg` WHERE `id_kont`='$user_id' AND `read`='1' AND `sys`!='2' GROUP BY `id_user`;"), 0);
				Header('Location: index.php?act=history&id='.$max);
				exit();
			}
			$req = mysql_query("SELECT * FROM `mail_msg` WHERE `id_kont`='" . $user_id . "' AND `read`='1' AND `spam`='1' ORDER BY `time` DESC LIMIT $start, $kmess");
			while($row = mysql_fetch_assoc($req)) {
				$out .= ($i % 2) ? '<div class="list2">':'<div class="list1">';
				if($row['file']) $text = '+ Вложение<br />';
				$subtext = '<a href="'.$home.'/pochta/index.php?act=read&amp;id='.$row['id'].'">Читать</a>'.($row['sys'] == 2?'':' | <a href="'.$home.'/pochta/index.php?act=add&amp;id='.$row['id_user'].'">Ответить</a>').' | <a href="'.$home.'/pochta/index.php?act=del&amp;id='.$row['id'].'">Удалить</a>';
				
				if($row['sys'] == 1) {
					$result = mysql_query('SELECT * FROM `users` WHERE `id` = ' . $row['id_user']);
					if(mysql_num_rows($result))
						$res = mysql_fetch_assoc($result);
					else {
						$res['name'] = $row['from'];
						$res['id'] = $row['id_user'];
						$res['ip'] = $row['ip'];
						$res['browser'] = $row['ua'];
					}
					$out .= show_user($res, 1, ($rights >= 6 && $rights >= $res['rights'] ? 1 : 0), '[' . date('d.m.o в H:i',$row['time']) . ']', $text, $subtext);
				}
				
				if($row['sys'] == 2) {
					$out .= $row['tema'].' [' . date('d.m.o в H:i',$row['time']) . ']<br />';
					$out .= 'Системное сообщение<br />';
					$out .= '<div class="sub">'.$subtext.'</div>';
				}
				
				$out .= '</div>';
				++$i;
			}
			$out .= '<div class="phdr">Всего: ' . $total . '</div>';
			if ($total > $kmess) {
				$out .= '<div class="menu">' . pagenav($home.'/pochta/index.php?act=new&amp;', $start, $total, $kmess);
				$out .= '<form action="" method="post"><p><input type="text" name="page" size="2"/><input type="submit" value="Перейти"/></p></form></div>';
			}
		}
		else {
			$out .= '<div class="rmenu">Новых сообщений нет!</div>';
		}
		require_once('../incfiles/head.php');
		echo '<div class="phdr"><h3>Непрочитанные сообщения</h3></div>';
		echo $out;
		echo '<div class="menu"><a href="'.$home.'/pochta/index.php?act=add">Отправить письмо</a></div>';
		echo '<div class="menu"><a href="'.$home.'/pochta/">Почта</a></div>';
?>