<?
/*
=============================================
Движок: SHCMS Engine
=============================================
Название файла: Почта
=============================================
Official website: http://shcms.ru
=============================================
*/
define('SHCMS', true);
include_once'../system/inc/basic_settings.php';
if($_GET['id']){$id = $_GET['id'];}
$users_mail = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '$id'"));
registrat();
    if($user_id == $id)
	{
	$shcmsengine['title'] = 'Вы не можете писать себе';
    include_once'../template/head.php';
	errors("Вы не можете писать себе");
	header("Refresh: 1; url=?act=mail");
	include_once '../template/foot.php';
	exit;
	}

switch($act):
    case '':
        $shcmsengine['title'] = 'Почта';
        include_once'../template/head.php';
		        $allmail = mysql_result(mysql_query("SELECT COUNT(DISTINCT(`posted`)) FROM `mails` WHERE `user_id` = '$user_id'"),0);
        $newmail = new Navigation($allmail, 10, true); 
	$mail = mysql_query("SELECT DISTINCT(`posted`) FROM `mails` WHERE `user_id` = '$user_id' ORDER BY `id` DESC ". $newmail->limit()."");
		echo '<div class="maintitle mainrazd">Все ваши контакты</div><div class="maintext">';
		if($mail == NULL)
		{
		    echo '<div class="posts">У вас нет контактов</div>'; 
		}
		else
		{
	while($mails = mysql_fetch_array($mail))
	{
            $profile_users = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = ".$mails['posted']." LIMIT 1"));
			echo '<div class="posts"> ';
	            if($profile_users['pol'] == 1){
		            echo ' <img src="/template/icon/users/1.png"/>';
		        }
	            else {
		            echo ' <img src="/template/icon/users/2.png"/>';
		        }
            echo ' <a href="'.DIR_SHCMS.'change_view.php?id='.$profile_users['id'].'">'.$profile_users['login'].'</a>';
			
		echo '<span class="maintime"><a href="mails.php?act=message&id='.$mails['posted'].'">Написать</a></span>';
		
			echo '</div>';
    
	}
	    echo '<div class="pages">';
        echo $newmail->pagination();
        echo '</div>';
		}
	echo '</div>';
	$mail_proch = mysql_result(mysql_query("SELECT COUNT(*) FROM `mails` WHERE `user_id` = '$user_id' AND `read` = 'yes'"),0); 
	$mail_noproch = mysql_result(mysql_query("SELECT COUNT(*) FROM `mails` WHERE `user_id` = '$user_id' AND `read` = 'no'"),0); 
    $mail_read = mysql_result(mysql_query("SELECT COUNT(*) FROM `mails` WHERE `posted` = '$user_id'"),0); 
	echo '<div class="maintitle">Действие</div><div class="maintext">';
	echo '<div class="posts"><a href="?act=written">Прочитанные письма</a>&nbsp('.$mail_proch.')</div>';
	echo '<div class="posts"><a href="?act=mail">Непрочитанные письма</a>&nbsp('.$mail_noproch.')</div>';
	echo '<div class="posts"><a href="?act=read">Отправленные письма</a>&nbsp('.$mail_read.')</div>';
	echo '</div>';
	break;

		
    case 'message':
        $shcmsengine['title'] = 'Написать сообщение для  '.$users_mail['login'].'';
        include_once'../template/head.php';

    if($_POST['submit'])
    {
        $submit = $_POST['submit'];
    }
	if(isset($submit))
    {
	
        if($_POST['text'])
        {
            $text = $_POST['text'];
        }

        if(empty($text))
        {
            errors('Введите пожалуйста текст сообщения');
            include_once'../template/foot.php';
            exit;
        }
        if(strlen($text) < 4 or strlen($text) > 10000)
        {
            errors('Сообщение должно быть не меньше 4 и не больше 10000');
            include_once'../template/foot.php';
            exit;
        }
		
    if($_POST['tran'] == 1)
    {
        $text = $translits->translit_rus($text);
    }
	elseif($_POST['tran'] == 2)
    {
        $text = $translits->translit_eng($text);
    }
        $text = antimat($text);


            $message = mysql_query("INSERT INTO `mails` (`user_id`,`read`,`text`,`posted`,`time`) VALUES ('".$users_mail['id']."','no','$text','$user_id','".time()."')");
        if($message == true)
        {
            messag("Сообщение успешно отправлено");
            header("Refresh: 1; url=?act=mail");
            include_once'../template/foot.php';
            exit;
        }
        else
        {
            messag("Возникла ошибка при отправки письма");
            header("Refresh: 1; url=?act=mail");
            include_once'../template/foot.php';
            exit;
        }
    }
            echo '<div class="border_message">';
	    if($brodilka == "web")
        {
            $text_list->text_lister();
        }

    echo '<form action="?act=message&id='.$id.'" method="post" name="form">';
    echo '<textarea name="text"></textarea>';
    $form->textarea('Написать',true);

	break;

    case 'mail':
        $countmail = mysql_result(mysql_query("SELECT COUNT(*) FROM `mails` WHERE `user_id` = '" .$user_id."' AND `read` = 'no'"), 0);
    if($countmail == NULL){
        $shcmsengine['title'] = 'Новых сообщений нет';
    }
    else{
        $shcmsengine['title'] = 'Новых сообщений ('.$countmail.')';
    }
    include_once'../template/head.php';
        $allfield = mysql_result(mysql_query("SELECT COUNT(*) FROM `mails` WHERE `user_id` = '" .$user_id."' AND `read` = 'no'"), 0);
        $newlist = new Navigation($allfield, 10, true); 
        $mail = mysql_query("SELECT * FROM `mails` WHERE `user_id` = '$user_id' AND `read` = 'no' ORDER BY `id` DESC ". $newlist->limit()."");
        $mails = mysql_fetch_array($mail);
    if($mails == NULL)
    {
        echo '<div class="posts">Новых сообщений для вас нет</div>';
    }
    else
    {
        do
        {
		        $mail_users = mysql_fetch_array(mysql_query("SELECT * FROM `users`	WHERE `id` = '$mails[posted]'"));
            echo '<div class="mysql_no"><a href="?act=prosm&id='.$mails['id'].'">'.$mail_users['login'].'</a>
			<div class="maintime">'.$timedate->times($mails['time']).'</div></div>';
        }
        while($mails = mysql_fetch_array($mail));
            echo '<div class="pages">';
            echo $newlist->pagination('act=mail');
            echo '</div>';
    }
    break;


    case'prosm':
        $mail = mysql_fetch_array(mysql_query("SELECT * FROM `mails` WHERE `id` = '$id'"));
        $user_mail = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '$mail[posted]'"));
        $shcmsengine['title'] = 'Переписка с пользователем '.$user_mail['login'].'';
        include_once'../template/head.php';

        mysql_query("UPDATE `mails` SET `read` = 'yes' WHERE `id` = '$id'");

        echo '<strong>Написал: </strong>';
        echo ''.users_list($mail['posted']).'<br/>';
        echo '<strong>Отправлено: </strong>'.$timedate->times($mail['time']).'<br/>';
		echo '<strong>Ответить: </strong><a href="'.DIR_SHCMS.'mails.php?act=message&id='.$mail['posted'].'">Написать письмо</a>';
        echo '<hr/>';
        echo processing($mail['text']);
    

    break;
	
	case 'written':
	    $shcmsengine['title'] = 'Прочитанные письма';
        include_once'../template/head.php';
		
		$mail = mysql_query("SELECT * FROM `mails` WHERE `user_id` = '$user_id' AND `read` = 'yes'");

		while($mails = mysql_fetch_array($mail))
		{
		       $mail_users = mysql_fetch_array(mysql_query("SELECT * FROM `users`	WHERE `id` = '$mails[posted]'"));
		echo '<div class="mysql_yes"><a href="?act=prosm&id='.$mails['id'].'">'.$mail_users['login'].'</a>
			<div class="maintime">'.$timedate->times($mails['time']).'</div></div>';
		}
	break;
	
	case 'read':
	$shcmsengine['title'] = 'Отправленные письма';
        include_once'../template/head.php';
		$mail = mysql_query("SELECT * FROM `mails` WHERE `posted` = '$user_id'");
		while($mails = mysql_fetch_array($mail))
		{
				       $mail_users = mysql_fetch_array(mysql_query("SELECT * FROM `users`	WHERE `id` = '$mails[posted]'"));
		echo '<div class="mysql_yes"><a href="?act=prosm&id='.$mails['id'].'">'.$mail_users['login'].'</a>
			<div class="maintime">'.$timedate->times($mails['time']).'</div></div>';
		}

	
	break;
endswitch;

include_once'../template/foot.php';

?>