<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

class lc_pm_list
{
	public $pager=array();
	protected $user_id;
	public $box_id=0; //0-входящие, 1 - исходящие
	public $items_at_page=50;
	
	function __construct($page=1, $box_id=0)
	{
		$this->user_id=User::i()->id;
		$this->page=$page;
		$this->box_id=$box_id;
		$this->items_at_page=config::get('msg_at_page');
	}
	
	public function getInboxList($user)
	{
		$this->total=Db::i()->selectCell(
			"SELECT COUNT(mess_id)
			FROM ?_lc_pm m
			WHERE receiver_id=? AND m.status IN (?a)", $user->user_id, lc_pm_main::$in_statuses
		);
		
		$this->_setPager();
		
		$messages=array();

		if ($this->total) {
			$result=Db::i()->select(
				"SELECT mess_id, unread, putdate, subj, sender_id, receiver_id
				FROM ?_lc_pm m
				WHERE receiver_id=? AND m.status IN (?a) ORDER BY mess_id DESC LIMIT ?d, ?d",
				$this->user_id, lc_pm_main::$in_statuses,
				$this->pager['sql_start'], $this->pager['sql_limit']
			);
			
			$users=array();
			foreach ($result as $msg) {
				$users[]=$msg['sender_id'];
				$oMsg=$this->_getMessageObject();
				$oMsg->set($msg);
				$messages[$oMsg->id]=$oMsg;
			}
			
			unset($result);
			
			if ($users) {
				$users=User::i()->getUsersByIds($users);
			} else {
				$users=array();
			}
			
			$users[$user->id]=$user;
			
			foreach ($messages as $key=>$msg) {
				$messages[$key]->sender=$this->_getUserObj($msg->sender_id, $users);
				$messages[$key]->receiver=$this->_getUserObj($msg->receiver_id, $users);
			}
			
		}
		return $messages;
	}
	
	protected function _getUserObj($user_id, &$users)
	{
		if ($user_id) return $users[$user_id];
		return User::getUser();
	}
	
	public function getOutboxList($user)
	{
		$this->total=Db::i()->selectCell(
			"SELECT COUNT(mess_id)
			FROM ?_lc_pm m
			WHERE sender_id=? AND m.status IN (?a)", $user->id, lc_pm_main::$out_statuses
		);
	
		$this->_setPager();
		
		$messages=array();

		if ($this->total) {
			$result=Db::i()->select(
				"SELECT mess_id, subj, putdate, m.receiver_id, sender_id
				FROM ?_lc_pm m
				WHERE sender_id=? AND m.status IN (?a) ORDER BY mess_id DESC LIMIT ?d, ?d",
				$user->id, lc_pm_main::$out_statuses, $this->pager['sql_start'],
				$this->pager['sql_limit']
			);
			
			$users=array();
			foreach ($result as $msg) {
				$users[]=$msg['sender_id'];
				$oMsg=$this->_getMessageObject();
				$oMsg->set($msg);
				$messages[$oMsg->id]=$oMsg;
			}
			
			unset($result);
			
			if ($users) {
				$users=User::i()->getUsersByIds($users);
			} else {
				$users=array();
			}
			
			$users[$user->id]=$user;
			
			foreach ($messages as $key=>$msg) {
				$messages[$key]->sender=$this->_getUserObj($msg->sender_id, $users);
				$messages[$key]->receiver=$this->_getUserObj($msg->receiver_id, $users);
			}
		}
		return $messages;
	}
	
	private function _setPager()
	{
		$pg=Factory::create('Paginator', $this->total, $this->items_at_page, $this->page);
		$this->pager=$pg->getPages();
	}
	
	protected function _getMessageObject()
	{
		return Factory::create('lc_pm.message');
	}
}