<?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';
	require_once('../incfiles/head.php');
	if($do == 'delall') {
		echo '<div class="phdr">Удаление исходящих сообщений</div>';
		if(!isset($_POST['submit'])) {
			echo '<form action="'.$home.'/pochta/index.php?act=out&amp;do=delall" method="post"><div class="rmenu">';
			echo 'Подтвердите удаление сообщений.';
			echo '</div>';
			echo '<div class="menu"><input type="submit" name="submit" value="Удалить"/></div>';
			echo '</form>';
		}
		else {
			
			$query = mysql_query('SELECT * FROM `mail_msg` WHERE `id_user`=' . $user_id . ' AND `delete`!='.$user_id);
			while($row = mysql_fetch_assoc($query)) {
				if(!empty($row['delete'])) {
					mysql_query("DELETE FROM `mail_msg` WHERE `id_user`='" . $user_id . "' AND `delete`!='".$user_id."' AND `delete`!='' AND `id`='".$row['id']."' LIMIT 1;");
					mysql_query("OPTIMIZE TABLE `mail_msg`;");
					if($row['file'])
						@unlink('../files/pochta/' . $row['file']);
				}
				else
					mysql_query("UPDATE `mail_msg` SET `delete` = '" . $user_id . "' WHERE `id_user`='" . $user_id . "' AND `delete`='' AND `id`='".$row['id']."' LIMIT 1;");
			}
			Header('Location: index.php?act=out');
			exit;
		}
	}
	else {
		echo '<div class="phdr">Исходящие сообщения</div>';
		$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `mail_msg` WHERE `id_user`='" . $user_id . "' AND `sys`=1 AND `delete`!='" . $user_id . "'"), 0);
		if($total > 0) {
			$req = mysql_query("SELECT `mail_msg`.`id` as `mid`,`mail_msg`.`time` as `tm`, `mail_msg`.`from_out` as `name`, `mail_msg`.`id_kont` as `user_id`, `mail_msg`.*, `users`.* FROM `mail_msg` LEFT JOIN `users` ON `mail_msg`.`id_kont`=`users`.`id` WHERE `mail_msg`.`id_user`='" . $user_id . "' AND `mail_msg`.`sys`='1' AND `mail_msg`.`delete`!='" . $user_id  . "' ORDER BY `mail_msg`.`time` DESC LIMIT $start, $kmess");
			if($do == 'note') {
				$_SESSION['note'] = 1;
			} else if($do == 'dnote') {
				$_SESSION['note'] = '';
			}
			if(isset($_SESSION['note'])) {
				$note = $_SESSION['note'];
			} else {
				$note = '';
			}
			echo '<div class="gmenu">'.($note == 1 ? '<a href="index.php?act=out&amp;do=dnote">Снять все отметки</a>':'<a href="index.php?act=out&amp;do=note">Отметить все сообщения</a>').'</div>';
			echo '<form class="class_form" action="'.$home.'/pochta/index.php?act=massout" method="post">';
			while($row = mysql_fetch_assoc($req)) {
				$text = '';
				echo ($i % 2) ? '<div class="list2">':'<div class="list1">';
				if($row['read']==1) $text .='Не прочитано!<br />';
				if($row['file']) $text .='+ Вложение<br />';
				$subtext = '<input type="checkbox" name="delmes[]" value="' . $row['mid'] . '"'.($note == 1 ? ' checked="checked"':'').'/>&nbsp;<a href="'.$home.'/pochta/index.php?act=read&amp;id='.$row['mid'].'">Читать</a> | <a href="'.$home.'/pochta/index.php?act=add&amp;id='.$row['id'].'">Написать</a> | <a href="'.$home.'/pochta/index.php?act=del&amp;id='.$row['mid'].'">Удалить</a> | <a href="'.$home.'/pochta/index.php?act=history&amp;id='.$row['id'].'">История</a> | <a href="index.php?act=send&amp;id='.$row['mid'].'">Переслать сообщение</a>';
				echo show_user($row, 1, ($rights >= 6 && $rights >= $row['rights'] ? 1 : 0), '[' . date('d.m.o в H:i',$row['tm']) . ']', $text, $subtext);
				echo '</div>';
				++$i;
			}
			echo '<div class="line"><input type="submit" value="Удалить отмеченные"/></div>';
			echo '</form>';
			echo '<div class="phdr">Всего: ' . $total . '</div>';
			if ($total > $kmess) {
				echo '<div class="menu">' . pagenav($home.'/pochta/index.php?act=out&amp;', $start, $total, $kmess);
				echo '<form action="" method="post"><p><input type="text" name="page" size="2"/> <input type="submit" value="Перейти"/></p></form></div>';
			}
			echo'<div class="menu"><a href="index.php?act=out&amp;do=delall">Удалить все исходящие</a></div>';
		}
		else {
			echo '<div class="rmenu">Сообщений нет!</div>';
		}
		echo '<div class="bmenu"><a href="'.$home.'/index.php?act=cab">Кабинет</a></div>';
	}
?>