<?php
/*
+--------------------------------------------------------------------------
|   TanyaCMS - Question and Answer Sofware
|   -----------------------------------------------------------------------
|   by fbschool dev team
|   (c) 2011 - 2012 FBSCHOOL
|   http://www.fbschool.com/tanyacms
|   -----------------------------------------------------------------------
|   This is NOT a freeware, use is subject to license terms
|   Support: 13455n@gmail.com
+--------------------------------------------------------------------------
*/


if (!defined('IN_TANYACMS'))
{
	die;
}

class message_class extends ASK_MODEL
{

	/**
	 * Users to send short message class
	 * @param $sender_uid	Send User ID
	 * @param $recipient_uid	Receiving a user ID
	 * @param $notice_title	SMS title
	 * @param $notice_content	Short Message Content
	 * @param $notice_type		Short message type 0-General News 10-System made the news, can not reply to 11-System Notifications
	 * @param $sender_del		Sender to delete, default 0
	 * @param $recipient_del	Recipient to delete, default 0
	 */
	public function send_message($sender_uid, $recipient_uid, $notice_title, $notice_content, $notice_type = 0, $sender_del = 0, $recipient_del = 0)
	{
		if (empty($sender_uid) || empty($recipient_uid) || empty($notice_content))
		{
			return false;
		}
		
		$diag_info = $this->get_dialog_id($sender_uid, $recipient_uid);
		
		//Session does not exist, create
		if (! $diag_info)
		{
			$diag_id = $this->create_dialog($sender_uid, $recipient_uid);
		}
		else
		{
			$diag_id = $diag_info['dialog_id'];
			
			if ($diag_info['sender_uid'] == $sender_uid)
			{
				$this->add_dialog_unread($diag_id, "recipient_unread");
			}
			else if ($diag_info['recipient_uid'] == $sender_uid)
			{
				$this->add_dialog_unread($diag_id, "sender_unread");
			}
		}
		
		$mesg_id = $this->add_notice($diag_id, $notice_title, $notice_content, $sender_uid, $notice_type);
		
		if (! $mesg_id)
		{
			return false;
		}
		
		//Adding additional table data
		$this->add_recipient_data($mesg_id, $diag_id, $sender_uid, $recipient_uid);
		
		//Update User Meeting Total number of unread notifications, according to
		$this->model('account')->increase_user_statistics(account_class::NOTICE_UNREAD, 1, $recipient_uid);
		
		$userinfo = $this->model('account')->get_user_info_by_uid($sender_uid);
		
		$this->model('email')->action_email(email_class::TYPE_NEW_MESSAGE, $recipient_uid, get_js_url('/inbox/'), array(
			'user_name' => $userinfo['user_name'],
		));
		
		return $mesg_id;
	}

	/**
	 * Insert data into the message table
	 * @param $diag_id
	 * @param $notice_title
	 * @param $notice_content
	 * @param $sender_uid
	 * @param $notice_type
	 */
	public function add_notice($diag_id, $notice_title, $notice_content, $sender_uid, $notice_type)
	{
		$data = array(
			"dialog_id" => $diag_id,  //Session ID
			"notice_title" => htmlspecialchars($notice_title), 
			"notice_content" => htmlspecialchars($notice_content), 
			"add_time" => time(),  //Add Time
			"sender_uid" => $sender_uid,  //Finally a short message ID
			"notice_type" => $notice_type
		);
		
		return $this->insert('notice', $data);
	}

	/**
	 * Insert data into additional tables
	 * @param $mesg_id
	 * @param $diag_id
	 * @param $sender_uid
	 * @param $recipient_uid
	 */
	public function add_recipient_data($mesg_id, $diag_id, $sender_uid, $recipient_uid)
	{
		$data = array(
			"dialog_id" => $diag_id, 
			"notice_id" => $mesg_id, 
			"sender_uid" => $sender_uid, 
			"sender_time" => time(), 
			"sender_del" => 0, 
			"recipient_uid" => $recipient_uid, 
			"recipient_time" => 0, 
			"recipient_del" => 0
		);
		
		return $this->insert('notice_recipient', $data);
	}

	/**
	 * Creating dialogue
	 */
	public function create_dialog($sender_uid, $recipient_uid)
	{
		$data = array(
			"sender_uid" => $sender_uid,  //Sender UID
			"sender_unread" => 0,  //Sender Unread
			"recipient_uid" => $recipient_uid,  //UID recipient
			"recipient_unread" => 1,  //Recipient Unread
			"add_time" => time(),  //Add Time
			"last_time" => time(),  //Last updated
			"last_notice_id" => 0,  //Finally a short message ID
			"sender_count" => 1,  //Display the number of the sender dialogue
			"recipient_count" => 1,  //Show the number of recipients dialogue
			"all_count" => 1 //Total number of dialogue
		);
		
		return $this->insert('notice_dialog', $data);
	}

	/**
	 * Dialogue table is not updated readings
	 * Enter description here ...
	 * @param $diag_id
	 * @param $filed
	 */
	public function add_dialog_unread($diag_id, $filed)
	{
		if (! in_array($filed, array('sender_unread','recipient_unread')))
		{
			return false;
		}
		
		$data = array(
			'last_time' => time(), 
			'sender_count' => "sender_count + 1", 
			'recipient_count' => "recipient_count + 1", 
			'all_count' => "all_count + 1", 
			$filed => $filed . " + 1"
		);
		
		if ($data)
		{
			foreach ($data as $key => $val)
			{
				$update_arr[] = "`{$key}` = {$val}";
			}
		}
		
		return $this->query("UPDATE " . $this->get_table('notice_dialog') . " SET " . implode(',', $update_arr) . " WHERE dialog_id = " . intval($diag_id));
	}

	/**
	 * Mark Read dialogue
	 * Enter description here ...
	 * @param $dialog_id
	 */
	public function read_message($dialog_id)
	{
		$uid = USER::get_client_uid();
		
		$message_unread = $this->get_message_unread_num($uid, $dialog_id);
		
		if ($message_unread)
		{
			//Update Short List
			$this->update('notice_dialog', array('recipient_unread' => 0), "recipient_uid = " . $uid . " AND dialog_id = " . $dialog_id);
			
			$this->update('notice_dialog', array('sender_unread' => 0), 'sender_uid = ' . $uid . ' AND dialog_id = ' . $dialog_id);
			
			//Updated reception table time
			$this->update('notice_recipient', array('recipient_time' => time()), 'recipient_uid = ' . $uid . ' AND dialog_id = ' . $dialog_id);
			
			//Update User Meeting Total number of unread notifications, according to
			$this->model('account')->increase_user_statistics(account_class::NOTICE_UNREAD, 0, $uid);
		}
		
		return true;
	}

	/**
	 * 
	 * Reading a text message
	 * @param int $dialog_id Information id
	 * @param int $cur_page Page number
	 * @param int $page_size Number of pages
	 * 
	 * @return Information Content array array
	 */
	public function get_message_by_dialog_id($dialog_id, $limit = "0,10")
	{
		$uid = USER::get_client_uid();
		
		$sql = "SELECT a.*, b.recipient_id, b.sender_uid, b.recipient_uid, b.sender_del, b.recipient_del FROM " . $this->get_table('notice') . " as a LEFT JOIN " . $this->get_table('notice_recipient') . " as b ON a.notice_id = b.notice_id";
		
		$sql .= " WHERE a.dialog_id = " . intval($dialog_id) . " AND ((b.sender_uid=" . $uid . " AND b.sender_del = 0) OR (b.recipient_uid = " . $uid . " AND b.recipient_del = 0)) ORDER BY notice_id DESC LIMIT " . $limit;
		
		$rs['list'] = $this->query_all($sql);
		
		$rs['list_one'] = $this->query_all("SELECT * FROM " . $this->get_table('notice_dialog') . " WHERE dialog_id = " . intval($dialog_id));
		
		return $rs;
	}

	public function count_message($dialog_id)
	{
		$sql = "SELECT COUNT(*) as count FROM " . $this->get_table('notice') . " as a LEFT JOIN " . $this->get_table('notice_recipient') . " as b ON a.notice_id = b.notice_id WHERE a.dialog_id = " . intval($dialog_id);
		
		$rs = $this->query_row($sql);
		
		return $rs['count'];
	}

	/**
	 * Get the number of unread SMS
	 * Enter description here ...
	 */
	public function get_message_unread_num($uid, $dialog = 0)
	{
		if ($uid == 0)
		{
			$uid = USER::get_client_uid();
		}
		
		$where = "recipient_uid = " . intval($uid) . " AND recipient_time = 0 AND recipient_del = 0";
		
		if ($dialog > 0)
		{
			$where .= " AND dialog_id = " . intval($dialog);
		}
		
		return $this->count('notice_recipient', $where);
	}

	/**
	 * 
	 * Delete session information
	 * @param int $dialog_id
	 * 
	 * @return boolean true|false
	 */
	public function delete_dialog($dialog_id)
	{
		$uid = USER::get_client_uid();
		
		$dialog_id = intval($dialog_id);
		
		$this->read_message($dialog_id);
		
		//Short update information table
		$this->update('notice_dialog', array('sender_count' => 0), 'sender_uid = ' . $uid . ' AND dialog_id = ' . $dialog_id);
		
		$this->update('notice_dialog', array('recipient_count' => 0), 'recipient_uid = ' . $uid . ' AND dialog_id = ' . $dialog_id);
		
		//Updated reception table
		$this->update('notice_recipient', array('recipient_del' => 1), 'recipient_uid = ' . $uid . ' AND dialog_id = ' . $dialog_id);
		
		$this->update('notice_recipient', array('sender_del' => 1), 'sender_uid = ' . $uid . ' AND dialog_id = ' . $dialog_id);
		
		return true;
	}

	/**
	 * 
	 * Deleting text messages 
	 * @param int/array $recipient_id Array is batch delete
	 * 
	 * @return boolean true|false
	 */
	public function delete_message($recipient_id)
	{
		$uid = USER::get_client_uid();
		$recipient_id = intval($recipient_id);
		$retval = false;
		
		$mes_info = $this->fetch_all('notice_recipient', "recipient_id = " . $recipient_id);
		
		if (! empty($mes_info))
		{
			$mes_info = $mes_info[0];
			
			if ($mes_info['sender_uid'] == $uid)
			{
				$data = array(
					"sender_del" => 1
				);
				
				$retval = $this->update('notice_recipient', $data, "sender_uid=" . $uid . " AND recipient_id=" . $mes_info['recipient_id']);
			}
			else if ($mes_info['recipient_uid'] == $uid)
			{
				$data = array(
					"recipient_del" => 1
				);
				
				$retval = $this->update('notice_recipient', $data, "recipient_uid = " . $uid . " AND recipient_id = " . $mes_info['recipient_id']);
			}
			
			//Update Short List
			$sql = " UPDATE " . $this->get_table('notice_dialog') . " SET sender_count = sender_count - 1 ";
			$sql .= " WHERE sender_uid = " . $uid . " AND dialog_id = " . $mes_info['dialog_id'];
			$this->query($sql);
			
			//Update Short List
			$sql = " UPDATE " . $this->get_table('notice_dialog') . " SET recipient_count = recipient_count - 1 ";
			$sql .= " WHERE recipient_uid = " . $uid . " AND dialog_id = " . $mes_info['dialog_id'];
			
			$this->query($sql);
		}
		
		return $retval;
	}

	/**
	 * 
	 * Information related to short list the user
	 * @param $cur_page Page number
	 * @param $page_size Number of pages
	 * 
	 * @return array
	 */
	public function list_message($page = 1, $limit = 10, $uid = null)
	{
		if ($list_msg = $this->fetch_page('notice_dialog', '(sender_uid = ' . intval($uid) . ' AND sender_count > 0) OR (recipient_uid = ' . intval($uid) . ' AND recipient_count > 0)', 'last_time DESC', $page, $limit))
		{
			$diag_ids = array();
			$send_ids = array();
			
			foreach ($list_msg as $recordset)
			{
				$diag_ids[] = $recordset['dialog_id'];

				if ($uid == $recordset['recipient_uid'])
				{
					$send_ids[] = $recordset['sender_uid'];
				}
				else
				{
					$send_ids[] = $recordset['recipient_uid'];
				}
			}
		}
		
		$result['diag_ids'] = $diag_ids;
		$result['content_list'] = $list_msg;
		$result['user_list'] = $send_ids;
		
		return $result;
	}
	
	public function get_last_messages($diag_ids)
	{
		if (!is_array($diag_ids))
		{
			return false;
		}
		
		foreach ($diag_ids as $diag_id)
		{
			$dialog_message = $this->fetch_row('notice', 'dialog_id = ' . intval($diag_id), 'notice_id DESC');
				
			$last_message[$diag_id] = utf8_substr($dialog_message['notice_content'], 0, 60, 'UTF-8', '...');
		}
		
		return $last_message;
	}

	/**
	 * Users get Watchlist
	 * @param int $uid
	 * 
	 * @return array
	 */
	public function check_send_message($uid)
	{
		$data = array();
		
		if ($user_focus = $this->model('follow')->get_user_friends($uid))
		{
			foreach ($user_focus as $user_info)
			{
				$data[] = $user_info['uid'];
			}
		}
		
		return $data;
	}

	/**
	 * 
	 * Determine whether there is a user dialogue, including the recipient or sender, to get the conversation ID
	 * 
	 * @return int The user does not exist, return to the New User dialog ID  There is a direct return to the user session ID
	 */
	public function get_dialog_id($u_id, $s_id)
	{
		return $this->fetch_row('notice_dialog', "(`sender_uid` = " . intval($u_id) . " AND `recipient_uid` = " . intval($s_id) . ")  OR (`recipient_uid` = " . intval($u_id) . " AND `sender_uid` = " . intval($s_id) . ")");
	}

	/**
	 * 
	 * Determine whether the user is set up to receive the attention of people
	 */
	public function check_recv($uid, $sender_uid)
	{
		$recipicent_info = $this->model('account')->get_user_info_by_uid($uid);
		
		if ($recipicent_info['inbox_recv'] == 1)
		{
			$user_list = $this->model('message')->check_send_message($uid);
			
			if (! in_array($sender_uid, $user_list))
			{
				return false;
			}
		}
		
		return true;
	}
}