欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

站内消息_php教程

程序员文章站 2022-03-17 13:14:08
...
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['message_ids'];
        if(!empty($member_info))
        {
            $message = str_replace('-','',$member_info['message_ids']);
        }
        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(',',$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(',',$this->old_message);
			foreach ($arr as $key =>$value) {
				if($message_id==$value)
				{
					unset ($arr[$key]);
					break;
				}
				if('-'.$message_id==$value)
				{
					unset ($arr[$key]);
					break;
				}
			}
			if(count($arr)>0)
			{
				$message_ids = implode(',',$arr);
				$message_ids =$message_ids.',';
			}
			else
			{
				$message_ids = '';
			}
		}
		
		$data = array(
		    'message_ids' => $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 = '') {
        
        set_time_limit(0);

		//插入$content
		$arr = array();
		$arr['title'] =$title;
		$arr['content'] = $content;
        
		$id = $this->add($arr);
		if($id === false)
		{
			return false;
		}
		else
		{
		    if(is_array($member_id)){
		        $this->member_id = join(',',$member_id);
		    }
		    return $this->writeMessage($id);
		}
    }
}
相关标签: 站内消息