<?php
/**
 * [LJCMS] (C)2012-2099 LJdev,Inc.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2014.04.03
*/

class control extends oabase {
    	public function action_run() {
        //    SELECT * FROM (select t2.*,t3.count,t3.count- t3.is_read unread from oa_message t2,( select max(id) id,count(*) count,SUM(t1.is_read) is_read from (select id,`sender_id`,`receiver_id`,`create_time`,is_read from oa_message where is_del = '0' and owner_id = '1' and receiver_id = '1' union select id,a.receiver_id,a.sender_id,`create_time`,1 is_read from oa_message a where a.is_del = 0 and owner_id = '1' and sender_id = '1') t1 where t1.receiver_id = '1' group by t1.sender_id) t3 where t3.id = t2.id order by t3.is_read,t2.create_time desc) a ORDER BY id desc LIMIT 0,10
                $keyword=$_POST['keyword'];
                $model = parent::model('message', 'oa');
	        $searchsql = "";
                if ($keyword!='') {
                      $searchsql .= " AND  content LIKE '%".$keyword."%'";
                      list($total, $data) = $model->getList(array('page'=>$this->page, 'pagesize'=>$this->pagesize, 'searchsql'=>$searchsql));
                } else {
                     $data = $model->get_List(array('page'=>$this->page, 'pagesize'=>$this->pagesize,));
                     
                     $total= count($data);
                }
                foreach($data as $k=>$v){
                     $data[$k]['content']=htmlspecialchars_decode($v['content']);
                }
                $url = XRequest::getPhpSelf();
                $url .= '?c=Message';
                $var_array = array(
                    'page'=>$this->page,
                    'nextpage'=>$this->page+1,
                    'prepage'=>$this->page-1,
                    'pagecount'=>ceil($total/$this->pagesize),
                    'pagesize'=>$this->pagesize,
                    'total'=>$total,
                    'showpage'=>XPage::index($total, $this->pagesize, $this->page, $url, 10),
                    'list'=>$data,
                    'owner_id'=> $_SESSION['USER_AUTH_KEY']
                );
                unset($model);
		TPL::assign($var_array);
	       TPL::display($this->cptpl.'message/index.tpl');
        }
        
        
        public function action_add() {
		   TPL::display($this->cptpl.'message/add.tpl');
	}
         public function action_save(){
            $arr=$_POST;
            $content=str_replace('\\','',$arr['content']);
            $content=htmlspecialchars($content);
            $model = parent::model('message', 'oa'); 
            if(!empty($arr['upload_file'])){
                        $str='';
                        foreach($arr['upload_file'] as $v){
                            $str .=$v.";";
                        }
            }
                $data['content'] =$content;
		$data['add_file'] = substr($str,0,strlen($str)-1); 
		$data['sender_id'] = $_SESSION['USER_AUTH_KEY'];
		$data['sender_name'] = $_SESSION['user_name'];
		$data['create_time'] = time();
                $data=$this->SafeFilter($data);
                $arr_recever = array_filter(explode(";", $arr['to']));
		$recever_list = array();
		foreach ($arr_recever as $val) {
			$tmp = explode("|", $val);
			$data['receiver_id'] = $tmp[1];
			$data['receiver_name'] = $tmp[0];
			$data['owner_id'] =  $_SESSION['USER_AUTH_KEY'];

			$list = $model -> doAdd($data);

			$data['owner_id'] = $tmp[1];
			$recever_list[] = $tmp[1];

			$list = $model -> doAdd($data);
		}
          
            if (true === $list) {
                $data['status']=1;
                $data['info']="编辑成功";
            }
            else {
                $data['info']="编辑失败";
            }
            echo json_encode($data);
        }
        
        	public function action_read() {
                $reply_id=$_REQUEST['reply_id'];
		$receiver_id = $reply_id;
		$sender_id = $_SESSION['USER_AUTH_KEY'];
		$model = parent::model('message', 'oa'); 
                $sql="SELECT * FROM `oa_message` WHERE `owner_id` = '$sender_id' AND ( (sender_id='$sender_id' and receiver_id='$receiver_id') or (receiver_id='$sender_id' and sender_id='$receiver_id') ) ORDER BY create_time desc";
                $list = $model ->get_read($sql);
                $arr['is_read']=1;
                foreach ($list as $k=>$v){
                    $id=$v['id'];
                    $model->doEdit($id,$arr);
                    $list[$k]['content']=htmlspecialchars_decode($v['content']);
                    $file = explode(";", $v['add_file']);
              
                        $files=array();
                          foreach($file as $k=>$v){
                               if($v!=''){
                                  $files[$k]['name']= trim(strrchr($v, '/'),'/');
                                  $files[$k]['path']=$v;
                              }  

                          }
                   $list[$k]['files']= $files;
                          
                }


		if (is_array($list)) {
			$vo = $list[0];
			if ($vo['sender_id'] == $_SESSION['USER_AUTH_KEY']) {
				$reply_id = $vo['receiver_id'];
				$reply_name = $vo['receiver_name'];
			}
			if ($vo['receiver_id'] == $_SESSION['USER_AUTH_KEY']) {
				$reply_id = $vo['sender_id'];
				$reply_name = $vo['sender_name'];
			}
		}
                 $var_array = array(
                    'list'=>$list,
                    'reply_id'=> $reply_id,
                      'reply_name'=> $reply_name,
                );
                unset($model);
		TPL::assign($var_array);
		TPL::display($this->cptpl.'message/read.tpl');
	}
        
      	public function action_reply() {
            
		if ($_POST) {
                  
                            $content=str_replace('\\','',$_POST['content']);
                            $content=htmlspecialchars($content);
                            if(!empty($_POST['upload_file'])){
                                        $str='';
                                        foreach($_POST['upload_file'] as $v){
                                            $str .=$v.";";
                                        }
                            }
                        $data['content'] =$content;
                        $data['add_file'] = substr($str,0,strlen($str)-1); 
                        $data['sender_id'] = $_SESSION['USER_AUTH_KEY'];
                        $data['sender_name'] = $_SESSION['user_name'];
			$data['create_time'] = time();
			$data['receiver_id'] = $_REQUEST['receiver_id'];
			$data['receiver_name'] = $_REQUEST['receiver_name'];
			$data['owner_id'] = $_SESSION['USER_AUTH_KEY'];

			$model = parent::model('message', 'oa'); 
			$list = $model -> doAdd($data);

			$data['owner_id'] = $_REQUEST['receiver_id'];
			$list = $model -> doAdd($data);

//			$push_data['type'] = '消息';
//			$push_data['action'] = '';
//			$push_data['title'] = "来自：" . get_dept_name() . "-" . $data['sender_name'] . "的消息";
//			$push_data['content'] = strip_tags($data['content']);
//			$push_data['url']=U('Message/index','return_url=Message/index');
//		
//			send_push($push_data, I('receiver_id'));

			//保存当前数据对象
	            if (true === $list) {
                $data['status']=1;
                $data['info']="编辑成功";
            }
            else {
                $data['info']="编辑失败";
            }
            echo json_encode($data);die;
		}



		$receiver_id = $_REQUEST['reply_id'];
		$sender_id = $_SESSION['USER_AUTH_KEY'];
		$model = parent::model('message', 'oa'); 
                $sql="SELECT * FROM `oa_message` WHERE `owner_id` = '$sender_id' AND ( (sender_id='$sender_id' and receiver_id='$receiver_id') or (receiver_id='$sender_id' and sender_id='$receiver_id') ) ORDER BY create_time desc";
                $list = $model ->get_read($sql);
                $arr['is_read']=1;
                foreach ($list as $k=>$v){
                    $id=$v['id'];
                    $model->doEdit($id,$arr);
                    $list[$k]['content']=htmlspecialchars_decode($v['content']);
                    $file = explode(";", $v['add_file']);
              
                        $files=array();
                          foreach($file as $k=>$v){
                               if($v!=''){
                                  $files[$k]['name']= trim(strrchr($v, '/'),'/');
                                  $files[$k]['path']=$v;
                              }  

                          }
                   $list[$k]['files']= $files;
                          
                }


		if (is_array($list)) {
			$vo = $list[0];
			if ($vo['sender_id'] == $_SESSION['USER_AUTH_KEY']) {
				$reply_id = $vo['receiver_id'];
				$reply_name = $vo['receiver_name'];
			}
			if ($vo['receiver_id'] == $_SESSION['USER_AUTH_KEY']) {
				$reply_id = $vo['sender_id'];
				$reply_name = $vo['sender_name'];
			}
			
		}
		                 $var_array = array(
                    'list'=>$list,
                    'reply_id'=> $reply_id,
                      'reply_name'=> $reply_name,
                );
                unset($model);
		TPL::assign($var_array);
		TPL::display($this->cptpl.'message/reply.tpl');
	}
}