<?

class lc_pm extends ModuleTools
{
	static $in_statuses=array(0, 1);
	static $out_statuses=array(0, 2);
	static $mess=0;
	static $umess=0;
	
	public function __construct()
	{
		self::_assign('pm_stat', self::getStat());
	}
	
	public static function getStat()
	{
		$stat=Db::i()->selectRow("SELECT * FROM ?_lc_pm_stat WHERE user_id=?", User::i()->id);
		return array('mess'=>self::$mess=intval($stat['mess']),	'umess'=>self::$umess=intval($stat['umess']));
	}
	
	public function pm_users()
	{
		if ($_SERVER['REQUEST_METHOD']=='POST')
		{
			User::checkToken();
			if ($_POST['login'])
			{
				self::_assign('users', Db::i()->select("SELECT user_id, login FROM ?_users WHERE login LIKE ? LIMIT 100", '%'.$_POST['login'].'%'));
			}
		}
		self::_setTpl('users');
		addBreadcrumb('Найти пользователя');
	}
	
	public function pm_list()
	{
		$this->_getList();
	}
	
	public function pm_sended()
	{
		$this->_getList(1);
		addBreadcrumb('Отправленные');
	}

	protected function _getList($box_id=0)
	{
		if ($_SERVER['REQUEST_METHOD']=='POST')
		{
			User::checkToken();
			if (is_array($_POST['delete']))
			{
				$lm=$this->_getMessageObject();
				$lm->inbox=$box_id?0:1;
				$lm->deleteMessage(array_keys($_POST['delete']));
			}
			refresh('', _url(false, $lm->inbox?null:'sended'));
		}
		
		self::_setTpl("msg_list");
		$pl=$this->_getMessageListObject($_GET['var1'], $box_id);
		$messages=$pl->getList();
		self::_assign('messages', $messages);
		self::_assign('pager', $pl->pager);
		self::_assign('box_id', $pl->box_id);
	}
		
	public function pm_msg()
	{
		$lm=$this->_getMessageObject();
		$lm->inbox=$_GET['var2']?0:1;

		if (!$msg=$lm->getMessage($_GET['var1'])) return false;
		self::_setTpl("show_msg");
		self::_assign(array('next'=>$lm->next, 'prev'=>$lm->prev));
		self::_assign('msg', $msg);
		self::_assign('history', $lm->history);
		self::_assign('box_id', 1-$lm->inbox);
		if (!$lm->inbox) addBreadcrumb('Отправленные', _url(false, 'sended'));
		addBreadcrumb($msg['subj'], _url($msg['mess_id'], 'msg'));
	}
	
	public function pm_delete()
	{
		User::checkToken();
		$lm=$this->_getMessageObject();
		$lm->inbox=$_GET['var2']?false:true;
		if ($lm->deleteMessage($_GET['var1']))
		{
			$next=$lm->getNext();
			if ($next) refresh('', _url(array($next, $lm->inbox?0:1), 'msg'));
			else refresh('', $lm->inbox?null:_url('', 'sended'));
		}
		self::_error("Сообщение не найдено!");
	}
	
	public function pm_ignore_remove()
	{
		User::checkToken();
		$il=$this->_getIgnoreObject();
		$il->removeFromIgnore($_GET['var1']);
		refresh('', _url(false, 'ignore'));
	}
	
	public function pm_ignore_add()
	{
		User::checkToken();
		$il=$this->_getIgnoreObject();
		$il->addToIgnore($_GET['var1']);
		refresh('', _url(false, 'ignore'));
	}
	
	public function pm_ignore()
	{
		$il=$this->_getIgnoreObject();
		self::_setTpl("ignore");
		self::_assign('ignores', $il->getIgnoreList());
		addBreadcrumb('игнор');
	}
	
	public function pm_send()
	{
		$lm=$this->_getMessageObject();
		if (!$lm->isSendPossible($_GET['var1'])) return;

		if ($_SERVER['REQUEST_METHOD']=='POST')
		{
			User::checkToken();
			if ($lm->send($_GET['var1'], $_POST['subj'], $_POST['msg']))
				refresh();
		}
		
		self::_setTpl("msg_edit");
		self::_assign('login', $lm->receiver);
		addBreadcrumb('Написать сообщение', _url($_GET['var1'], 'send'));
	}
	
	public function pm_reply()
	{
		$lm=$this->_getMessageObject();
		if(!$msg=$lm->getReply($_GET['var1'])) return;

		if ($_SERVER['REQUEST_METHOD']=='POST')
		{
			User::checkToken();
			if ($lm->send($msg['sender_id'], $_POST['subj'], $_POST['msg']))
				refresh();
		}
		
		self::_setTpl("msg_edit");
		self::_assign($msg);
		self::_assign('history', $this->history);
		addBreadcrumb($msg['subj'], _url($msg['mess_id'], 'msg'));
		addBreadcrumb('ответить');
	}
	
	public function pm_status()
	{
		return array('mess'=>self::$mess, 'umess'=>self::$umess);
	}
	
	protected function _getMessageObject()
	{
		include_once dirname(__FILE__).'/class.pmMessage.php';
		return new lc_pm_message();
	}
	
	protected function _getMessageListObject($page, $box_id=0)
	{
		include_once dirname(__FILE__).'/class.pmList.php';
		return new lc_pm_list($page, $box_id);
	}
	
	protected function _getIgnoreObject()
	{
		include_once dirname(__FILE__).'/class.pmIgnoreList.php';
		return new lc_pm_ignore_list();
	}

}