<?php

class InfoSMS_model extends CI_Model
{
	var $insert_id = -1;
	var $top_num=false;
	var $order_by = false;
	var $m_object_type = false;
	var $m_object_id = false;
	var $m_readed = false;
	var $m_receiver = false;
	
    function __construct()
    {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }
	
	function init()
	{
		$this->top_num=false;
		$this->order_by=" ORDER BY i.m_id DESC ";
		$this->m_object_type = false;
		$this->m_object_id = false;		
		$this->m_readed = false;
		$this->m_receiver = false;
	}
	
	//??ȡ?????Ķ????б?
	function get_task_sms($t_id)
	{
		$this->init();
		$this->m_object_type = " AND m_object_type='task' ";
		$this->m_object_id = " AND m_object_id= ".$this->HT->escape($t_id);
		$this->order_by=" ORDER BY i.m_id DESC ";
		return $this->get_list();
	}
	
	//?????Ѷ?
	function readed($m_id)
	{
		$sql = "UPDATE infosms \n"
			 . "SET    m_readed = 1 \n"
			 . "WHERE  m_id = ?";
		return $this->HT->query($sql,array($m_id));
	}
	
	//??ȡδ??????Ϣ
	function unread_sms($m_object_type,$m_object_id)
	{
		$this->init();
		$this->m_object_type = " AND m_object_type=".$this->HT->escape($m_object_type);
		$this->m_object_id = " AND m_object_id= ".$this->HT->escape($m_object_id);
		$this->m_readed=" AND m_readed=0 ";
		return $this->get_list();
	}
	//获取作者平台所有的未读信息
	public function all_unread_sms($m_receiver,$m_object_type='task'){
		$sql = "SELECT	m_object_id,
						t_title 
				FROM 	infosms
						INNER JOIN infoTasks ON m_object_id=t_id
				WHERE	m_object_type=? 
						AND m_receiver= '$m_receiver' 
						AND m_readed=0
						AND t_delete=0
				ORDER BY m_datetime DESC";
		$query=$this->HT->query($sql,array($m_object_type));
		$result=$query->result();
		$sms=array();
		$num=count($result);
		if (!empty($result)) {
			foreach ($result as $v) {
				$sms[$v->m_object_id][]=$v;
			}
		}
		$unread_sms['sms_count']=$num;
		$unread_sms['sms']=$sms;
		return $unread_sms;
	}
	
    function add($m_object_type,$m_object_id,$m_receiver,$m_sender,$m_content)
    {
		$sql = "INSERT INTO infosms \n"
			 . "  ( \n"
			 . "    m_object_type, \n"
			 . "    m_object_id, \n"
			 . "    m_receiver, \n"
			 . "    m_sender, \n"
			 . "    m_content, \n"
			 . "    m_readed, \n"
			 . "    m_datetime \n"
			 . "  ) \n"
			 . "VALUES \n"
			 . "  ( \n"
			 . "    ?, \n"
			 . "    ?, \n"
			 . "    ?, \n"
			 . "    ?, \n"
			 . "    N?, \n"
			 . "    0, \n"
			 . "    GETDATE() \n"
			 . "  )";
        return $this->HT->query($sql, array($m_object_type,$m_object_id,$m_receiver,$m_sender,$m_content));
    }
	
	function get_list()
	{
		$this->top_num ? $sql = "SELECT TOP " . $this->top_num : $sql = "SELECT ";
		$sql .= "      i.m_id, \n"
			 . "       i.m_object_type, \n"
			 . "       i.m_object_id, \n"
			 . "       i.m_receiver, \n"
			 . "       i.m_sender, \n"
			 . "       i.m_readed, \n"
			 . "       i.m_content, \n"
			 . "       i.m_datetime \n"
			 . "FROM   infosms i \n"
			 . "WHERE  1 = 1";
		$this->m_object_type ? $sql.=$this->m_object_type : false;
		$this->m_object_id ? $sql.=$this->m_object_id : false;
		$this->m_receiver ? $sql.=$this->m_receiver : false;
		$this->m_readed ? $sql.=$this->m_readed : false;
		$this->order_by ? $sql.=$this->order_by : false;
		$query=$this->HT->query($sql);
		if($this->top_num==1)
		{
			if ($query->num_rows() > 0)
			{
				$row = $query->row();
				return $row;
			}
			else
			{
				return FALSE;
			}
		}
		else
		{
			return $query->result();
		}			 
	}
	
	    /*
     * ?????ʼ?
     */

    function send_mail($fromName, $fromEmail, $toName, $toEmail, $subject, $body) {
        $sql = "INSERT INTO Email_AutomaticSend \n"
                . "  ( \n"
                . "	M_ReplyToName, M_ReplyToEmail, M_ToName, M_ToEmail, M_Title, M_Body, M_Web,  \n"
                . "	M_FromName, M_State \n"
                . "  ) \n"
                . "VALUES \n"
                . "  (  \n"
                . "	?, ?, ?, ?, ?, ?, ?, ?, 0 \n"
                . "  ) ";
        $query = $this->HT->query($sql, array($fromName, $fromEmail, $toName, $toEmail, $subject, $body, 'info', 'ChinaHighlights Online Writers Platform'));
        return $query;
    }   
	
	
}