<?php

/**
 * msg_wiipu.php
 *
 * @version       v0.01
 * @create time   2011-5-30
 * @update time   
 * @author        wiipu
 * @copyright     Copyright (c) 寰櫘绉戞妧 WiiPu Tech Inc. ( http://www.wiipu.com)
 * @informaition  

 * Update Record:
 *
 */
	class Msg{
		function __construct($registry)
		{
			$this->db=$registry->get('db');
			$this->session = $registry->get('session');
			$this->ucenter = $registry->get('ucenter');
			$this->sysFun=$registry->get('funcSysten');
		}
		
		/**
		* function addInbox()娣诲姞涓€灏佹敹浠朵俊鎭?		* $arr涓哄垪鍚?>鍒楀€?		*/
		function addInbox($arr)
		{
			$sql="insert into ".DB_TABLE_PREFIX."msg (msg_send,msg_received,msg_title,msg_content,msg_addtime,msg_isreader,msg_isreply,msg_side) values ('".$this->session->data[WiiBBS_ID."wiibbsUser"]."','".$arr['msg_received']."','".$arr['msg_title']."','".$arr['msg_content']."','".date('Y-m-d H:i:s')."','0','0','1')";
			$this->db->query($sql);
		}

		/**
		* function addOubbox()娣诲姞涓€灏佸彂浠朵俊鎭?		* $arr涓哄垪鍚?>鍒楀€?		*/
		function addOutbox($arr)
		{
			$sql="insert into ".DB_TABLE_PREFIX."msg (msg_send,msg_received,msg_title,msg_content,msg_addtime,msg_isreader,msg_isreply,msg_side) values ('".$this->session->data[WiiBBS_ID."wiibbsUser"]."','".$arr['msg_received']."','".$arr['msg_title']."','".$arr['msg_content']."','".date('Y-m-d H:i:s')."','0','0','0')";
			$this->db->query($sql);
		}

		/**
		* function deleteBox();
		* $type=0 鍒犻櫎鍙戜欢绠?$type=1 鍒犻櫎鏀朵欢绠?		*/
		function deleteBox($id,$type)
		{
			$sql="delete from ".DB_TABLE_PREFIX."msg where msg_id=".$id." and msg_side='".$type."'"	;
			if($this->db->query($sql))
			{
				return true;
			}else{
				return false;
			}
		}

		/**
		*	function getBoxCount()鑾峰緱閭鏁扮洰
		*/
		function getBoxCount($type)
		{
			if($type=='send')
			{
				$where="msg_send='".$this->session->data[WiiBBS_ID."wiibbsUser"]."' and msg_side=0";
			}else if($type=='receive')
			{
				$where="msg_received='".$this->session->data[WiiBBS_ID."wiibbsUser"]."' and msg_side=1";
			}else if($type=='new')
			{
				$where="msg_received='".$this->session->data[WiiBBS_ID."wiibbsUser"]."' and msg_side=1 and msg_isreader=0";
			}
			$sql="select * from ".DB_TABLE_PREFIX."msg where ".$where;
			$result=$this->db->query($sql);
			return $result->num_rows;
		}

		/**
		*	function getBoxList()鑾峰緱閭鍒楄〃
		*/
		function getBoxList($startRow,$pageSize,$type)
		{
			if($type=='send')
			{
				$where="msg_send='".$this->session->data[WiiBBS_ID."wiibbsUser"]."' and msg_side=0";
			}else if($type=='receive')
			{
				$where="msg_received='".$this->session->data[WiiBBS_ID."wiibbsUser"]."' and msg_side=1";
			}else if($type=='new')
			{
				$where="msg_received='".$this->session->data[WiiBBS_ID."wiibbsUser"]."' and msg_side=1 and msg_isreader=0";
			}
			$sql="select * from ".DB_TABLE_PREFIX."msg where ".$where." order by msg_id desc limit $startRow,$pageSize";
			$query=$this->db->query($sql);
			return $query->rows;
		}

		/**
		* function getMsgById()鏍规嵁id鑾峰緱鐩稿簲鐨刴sg
		* return false涓嶅瓨鍦?		*/
		function getMsgById($id)
		{
			$sql="select * from ".DB_TABLE_PREFIX."msg where msg_id=".$id;
			$query=$this->db->query($sql);
			if($query->row)
			{
				return $query->row;
			}else{
				return false;
			}
		}

		/**
		* function updateMsgById() 鏍规嵁id鏇存柊鐩稿簲鐨刴sg
		*/
		function updateMsgById($id)
		{
			$sql="update ".DB_TABLE_PREFIX."msg set msg_isreader=1,msg_isreadertime='".date('Y-m-d H:i:s')."' where msg_id=".$id;
			$this->db->query($sql);
		}

		/**
		*	deletMsgById()鍒犻櫎
		*/
		function deletMsgById($id)
		{
			$sql="delete from ".DB_TABLE_PREFIX."msg where msg_id=".$id;
			$this->db->query($sql);
		}
	}
?>