首頁 > php教程 > PHP源码 > 主體

站内消息_php教程

PHP中文网
發布: 2016-05-23 16:40:38
原創
1451 人瀏覽過

php代码

class MessageModel extends Model {

    public $_fields = array(
        //字段
        'id' => 'Id',
        'title' =>'title',
        'content' =>'content',
        'addtime' =>'addtime'
    );
    
    //自动验证
    protected $_validate = array();
    //自动完成
    protected $_auto = array();
    
    //会员id
    private $member_id='';
    //新消息id
    private $message_id='';
    
    //老消息id
    private $old_message='';
    
    private  $old_ids = '';
    
    private $OBJ;
    /**
     *  
     * @example $re = D("Message");$re->setMember('1');$ar = $re->delMessage(4);
     * @param unknown $member_id
     */
    public function setMember($member_id,$OBJ = null)
    {
        $this->member_id = $member_id;
        $memberObj = D('Member');
        if($OBJ instanceof $memberObj){
            $this->OBJ = $OBJ;
        }else {
            $this->OBJ = $memberObj;
        }
        
    }
    /**
     * 将messageid写入会员信息表中
     * @param unknown $message_id
     * @param number $read 0未读,1已读
     */
    public function writeMessage($message_id,$read = 0)
    {
        $this->message_id = $message_id;
        $this->getOldMessIds();
        $message_ids = $this->old_message.$this->message_id.',';
        if ($read ==1){
            $message_ids = '';
            $arr = explode(',',substr($this->old_message,0,-1));
            if(in_array($message_id,$arr))
            {
                $message_ids = preg_replace('/,'.$message_id.',/',',-'.$message_id.',',','.$this->old_message);
                $message_ids = substr($message_ids,1);
            }
        }
        $data = array(
            'message_ids' => $message_ids
        );
        return $this->OBJ->where("id = {$this->member_id}")->save($data);
    }
    
    /**
     * 获得会员信息表中的messageid
     */
    public function getOldMessIds()
    {
        $member_info = $this->OBJ->where("id = {$this->member_id}")->find();
        if(!empty($member_info))
        {
            $this->old_message = $member_info['message_ids'];
        }
    }
    
    /**
     * 获取所有信息ID
     * @return Ambigous <NULL, mixed>
     */
    public function getMessIds()
    {
        $member_info = $this->OBJ->where("id = {$this->member_id}")->find();
        $message = null;
        $this->old_ids = $member_info[&#39;message_ids&#39;];
        if(!empty($member_info))
        {
            $message = str_replace(&#39;-&#39;,&#39;&#39;,$member_info[&#39;message_ids&#39;]);
        }
        return $message;
    }
    
    
    /**
     * 获取未读信息ID
     * @return Ambigous <multitype:, array>
     */
    public function readMessage()
    {
        $arr = array();
        $this->getOldMessIds();
        if(count($this->old_message)>0)
        {
            $this->old_message = substr($this->old_message,0,-1);
            $arr = explode(&#39;,&#39;,$this->old_message);
            foreach ($arr as $key =>$value) {
                if(stristr($value,"-")===false)
                {
                    unset ($arr[$key]);
                }else {
                    $arr[$key] = abs($arr[$key]);
                }
            }
        }
        return $arr;
    }
    
    /**
     * 
     * @param unknown $message_id
     * @return boolean|\Think\boolean
     */
    public function delMessage($message_id) {
        $arr = array();
		$this->getOldMessIds();
		$message_ids = $this->old_message;
		if($this->old_message)
		{
			$this->old_message = substr($this->old_message,0,-1);
			$arr = explode(&#39;,&#39;,$this->old_message);
			foreach ($arr as $key =>$value) {
				if($message_id==$value)
				{
					unset ($arr[$key]);
					break;
				}
				if(&#39;-&#39;.$message_id==$value)
				{
					unset ($arr[$key]);
					break;
				}
			}
			if(count($arr)>0)
			{
				$message_ids = implode(&#39;,&#39;,$arr);
				$message_ids =$message_ids.&#39;,&#39;;
			}
			else
			{
				$message_ids = &#39;&#39;;
			}
		}
		
		$data = array(
		    &#39;message_ids&#39; => $message_ids
		);
		return $this->OBJ->where("id = {$this->member_id}")->save($data);
    }
    
    /**
     * 注意如果 $member_id 值的变化
     * @param unknown $title
     * @param unknown $content
     * @param unknown $member_id
     * @return boolean|\Think\boolean
     */
    public function sendToMember($title,$content,$member_id = &#39;&#39;) {
        
        set_time_limit(0);

		//插入$content
		$arr = array();
		$arr[&#39;title&#39;] =$title;
		$arr[&#39;content&#39;] = $content;
        
		$id = $this->add($arr);
		if($id === false)
		{
			return false;
		}
		else
		{
		    if(is_array($member_id)){
		        $this->member_id = join(&#39;,&#39;,$member_id);
		    }
		    return $this->writeMessage($id);
		}
    }
}
登入後複製
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板