首页 >php教程 >PHP源码 >站内消息_php教程

站内消息_php教程

PHP中文网
PHP中文网原创
2016-05-23 16:40:381481浏览

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);
		}
    }
}
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn