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

站内消息_php教程

PHP中文网
PHP中文网원래의
2016-05-23 16:40:381461검색

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으로 문의하세요.
이전 기사:php投票小程序다음 기사:使用极光推送信息到APP