<?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() {
                $id= $_SESSION['USER_AUTH_KEY'];
                $keyword=$_REQUEST['keyword'];
                $model = parent::model('task', 'oa');
                $searchsql = " AND is_del='0'";  
                if ($keyword!='') {
                    $searchsql .= " AND name LIKE '%".$keyword."%'";
                }
                $fid = $_GET['fid'];
                if($fid=='') {
                    $folder_name="所有任务";
                    $fid="all";
                }
                switch ($fid) {                 
			case 'all' :
                               $folder_name="所有任务";
				break;
				
			case 'dept' :
                                $folder_name="我们部门的任务";
                                $userid= $_SESSION['USER_AUTH_KEY']; 
                                $sql = "SELECT * FROM ".DB_PREFIXOA."user WHERE id='".$userid."'";
                                $user=parent::$obj->fetch_first($sql);
                                $dept_id=$user['dept_id'];
                                $sql = "SELECT `task_id` FROM ".DB_PREFIXOA."task_log WHERE type='2' AND status='0' AND executor='$dept_id'";
                                $task_list=parent::$obj->getall($sql);
                                if (!empty($task_list)) {
                                   $task_ids="";
                                    foreach ($task_list as $v){
                                        $task_ids .= $v['task_id'].",";
                                    }
                                   $task_ids=substr($task_ids, 0, -1);
                                   $searchsql .= " AND id in ($task_ids)";
                                  }else {
                                     $searchsql .= " AND id =''";
                                }   
				break;

			case 'no_assign' :
                                $folder_name="不知道由谁处理的任务";
				$sql = "select id from ".DB_PREFIXOA."task task where status=0 and not exists (select * from ".DB_PREFIXOA."task_log task_log where task.id=task_log.task_id)";
                                $task_list=parent::$obj->getall($sql);
                                if (!empty($task_list)) {
                                      $task_ids="";
                                                foreach ($task_list as $v){
                                                    $task_ids .= $v['id'].",";
                                                }
                                               $task_ids=substr($task_ids, 0, -1);
                                               $searchsql .= " AND id in ($task_ids)";
                                }else {
                                     $searchsql .= " AND id =''";
                                }
				break;

			case 'no_finish' :
                                $folder_name="我未完成";
                                $user_id=$_SESSION['USER_AUTH_KEY']; 
                                $sql = "SELECT `task_id` FROM ".DB_PREFIXOA."task_log WHERE type='1' AND status<'20' AND executor='$user_id'";
                                $task_list=parent::$obj->getall($sql);
                                if (!empty($task_list)) {
                                        $task_ids="";
                                        foreach ($task_list as $v){
                                            $task_ids .= $v['task_id'].",";
                                        }
                                       $task_ids=substr($task_ids, 0, -1);
                                       $searchsql .= " AND id in ($task_ids)";
                                } else {
                                     $searchsql .= " AND id =''";
                                }
				break;

			case 'finished' :
                                $folder_name="我已完成";
				$user_id=$_SESSION['USER_AUTH_KEY']; 
                                $sql = "SELECT `task_id` FROM ".DB_PREFIXOA."task_log WHERE type='1' AND executor='$user_id'";
                                $task_list=parent::$obj->getall($sql);
                                if (!empty($task_list)) {
                                        $task_ids="";
                                        foreach ($task_list as $v){
                                            $task_ids .= $v['task_id'].",";
                                        }
                                       $task_ids=substr($task_ids, 0, -1);
                                       $searchsql .= " AND status='30' AND id in ($task_ids)";
                                } else {
                                     $searchsql .= "AND status='30' AND id =''";
                                }                           
				break;

			case 'my_task' :
                                $folder_name="我发布的任务";
                                $user_id=$_SESSION['USER_AUTH_KEY'];
                                $searchsql .= "AND user_id='$user_id'";
				break;

			case 'my_assign' :
                                $folder_name="我指派的任务";
                                $user_id=$_SESSION['USER_AUTH_KEY']; 
                                $sql = "SELECT `task_id` FROM ".DB_PREFIXOA."task_log WHERE assigner='$user_id'";
                                $task_list=parent::$obj->getall($sql);
                                 if (!empty($task_list)) {
                                        $task_ids="";
                                        foreach ($task_list as $v){
                                            $task_ids .= $v['task_id'].",";
                                        }
                                       $task_ids=substr($task_ids, 0, -1);
                                       $searchsql .= " AND id in ($task_ids)";
                                } else {
                                     $searchsql .= " AND id =''";
                                }       
				break;

			default :
				break;
		}                

                list($total, $data) = $model->getList(array('page'=>$this->page, 'pagesize'=>$this->pagesize, 'searchsql'=>$searchsql));
                foreach ($data as $k=>$v){
                    $data[$k]['executor']=$this->show_contact($v['executor']);
                }
                $no_finish_task_count = $model->badge_count_no_finish_task();
		$dept_task_count = $model->badge_count_dept_task();
	        $no_assign_task_count = $model->badge_count_no_assign_task();
                $url = XRequest::getPhpSelf();
                $url .= '?c=Task';
                $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,
                    'fid'=>$fid,
                    'folder_name'=>$folder_name,
                    'user_id'=>$id,
                    'no_finish_task_count'=>$no_finish_task_count,
                     'dept_task_count'=>$dept_task_count,
                     'no_assign_task_count'=>$no_assign_task_count,
                );
               unset($model);
               TPL::assign($var_array);
	       TPL::display($this->cptpl.'task/index.tpl');
    }
    
        public function action_add(){
            $id=$_REQUEST['id'];
            $opmode="add";
            if($id){
                $opmode="edit";
                $model = parent::model('task', 'oa');
                $vo=$model->get_data($id);
                $vo['executor']= $this->show_contact($vo['executor'],'edit');
                $vo['content']=htmlspecialchars_decode($vo['content']);
                $file = explode(";", $vo['add_file']);
                 $files=array();
                foreach($file as $k=>$v){
                   if($v!=''){
                        $files[$k]['name']= trim(strrchr($v, '/'),'/');
                       $files[$k]['path']=$v;
                    }  
                }
            }
             $var_array = array(
                    'opmode'=>$opmode,
                    'vo'=>$vo,
                 'file'=>$files
                );            
            TPL::assign($var_array);          
            TPL::display($this->cptpl.'task/add.tpl');
    }
        public function action_save(){
            $arr=$_POST;
            $id=$arr['id'];
            $model = parent::model('task', 'oa');
            $arr['content']=str_replace('\\','',$arr['content']);
            $arr['content']=htmlspecialchars($arr['content']);
            if(!empty($arr['upload_file'])){
                        $str='';
                        foreach($arr['upload_file'] as $v){
                            $str .=$v.";";
                        }
                        $arr['add_file']=substr($str,0,strlen($str)-1); 
            }
     unset($arr['upload_file']);
            $arr=$this->SafeFilter($arr);
            if($arr['opmode']=='add'){
           
                $result = $model->doadd($arr);
                $data=$model->get_addinfo();
                    if($data['executor']!=''){
                        $model->addlog($data);
                    }
            }else{
                $result = $model->doEdit($id, $arr);
            }
            if (true === $result) {
                $data['status']=1;
                $data['info']="编辑成功";
            }
            else {
                $data['info']="编辑失败";
            }
            echo json_encode($data);
        }
        public function action_del(){
                $id=$_GET['id'];
                if(empty($id)){
                       XHandle::halt('操作失败', '', 1);
                die;
                }
                $model = parent::model('task', 'oa');

                $res=$model->del($id);
         
	
                if($res){
                     XHandle::halt('删除成功', $this->cpfile.'?c=Task', 0);
                } else {
                     XHandle::halt('删除失败', '', 1);
                }
	}
        	public function action_read() {
                $id=$_REQUEST['id'];
                $model = parent::model('task', 'oa');
                $vo=$model->get_data($id);
                $vo['executor1']= $this->show_contact($vo['executor'],'edit');
                $vo['content']=htmlspecialchars_decode($vo['content']);
                if (empty($vo['executor'])) {
                        $no_assign=1;
		}
                $file = explode(";", $vo['add_file']);
                $files=array();
                foreach($file as $k=>$v){
                   if($v!=''){
                       $files[$k]['name']= trim(strrchr($v, '/'),'/');
                       $files[$k]['path']=$v;
                    }  
                }
                $task_log=$model->get_task_log($id);
                foreach($task_log as $k=>$v){
                    $task_log[$k]['finish_rate']=$this->finish_rate($v['finish_rate']);    
                    $log_file = explode(";", $v['add_file']);
                    $log_files=array();
                        foreach($log_file as $k1=>$v1){
                           if($v!=''){
                               $log_files[$k1]['name']= trim(strrchr($v1, '/'),'/');
                               $log_files[$k1]['path']=$v1;
                            }  
                        }
                        $task_log[$k]['add_file']=$log_files;
                }
                 $user_id=$_SESSION['USER_AUTH_KEY']; 
                 if(strpos($vo['executor'],'dept') !== false){
                                $userid= $_SESSION['USER_AUTH_KEY']; 
                                $sql = "SELECT * FROM ".DB_PREFIXOA."user WHERE id='".$userid."'";
                                $user=parent::$obj->fetch_first($sql);
                                $dept_id=$user['dept_id'];
                     $where = " type='2' AND executor='$dept_id'";
                 }else{
                     $where =" status in ('0','10') AND task_id='$id' AND type='1' AND executor='$user_id'";
                 }
                 $task_working=$model->get_task_loginfo($where);
                    if ($task_working) {
                      //                         
                            $is_working=1; 
                    }               
                    $var_array = array(
                        'task_id'=>$id,
                        'vo'=>$vo,
                        'file'=>$files,
                        'task_log'=>$task_log,
                        'no_assign'=>$no_assign,
                         'is_working'=>$is_working,
                         'task_working'=>$task_working,
                       );            
                   TPL::assign($var_array);     

         TPL::display($this->cptpl.'task/read.tpl');
	}
        
        public function action_save_log(){
            $model = parent::model('task', 'oa');
            $arr=$_POST;
            $id=$arr['id'];
            $where= " id='$id'";
            $loginfo=$model->get_task_loginfo($where);
 
            if(!empty($arr['upload_file'])){
                        $str='';
                        foreach($arr['upload_file'] as $v){
                            $str .=$v.";";
                        }
                        $arr['add_file']=substr($str,0,strlen($str)-1); 
            }
            unset($arr['upload_file']);
            $arr['transactor']=$_SESSION['USER_AUTH_KEY']; 
            $arr['transactor_name']=$_SESSION['user_name']; 
            $arr['finish_time']= date("Y-m-d H:i:s",time());
            if($arr['finish_rate']==100){
                $arr['status']=20;
            }
            $result = $model->doEditlog($id, $arr);
            
            if (true === $result) {
                $save['status']=$arr['status'];
                $model->doEdit($loginfo['task_id'],$save);
                $data['status']=1;
                $data['info']="编辑成功";
            }
            else {
                $data['info']="编辑失败";
            }
            echo json_encode($data);
           // print_r($arr);
        
}
public 	function action_let_me_do() {
            $task_id=$_REQUEST['task_id'];
            $arr['executor']=$_SESSION['user_name'] . "|" . $_SESSION['USER_AUTH_KEY'];
            $arr['status']=10;
            $model = parent::model('task', 'oa');   
            $result = $model->doEdit($task_id, $arr);
            if($result){
                $data['task_id'] = $task_id;
			$data['executor'] = $_SESSION['USER_AUTH_KEY'];
			$data['executor_name'] = $_SESSION['user_name'];
			$data['transactor'] = $_SESSION['USER_AUTH_KEY'];
			$data['transactor_name'] = $_SESSION['user_name'];
			$data['status'] = 10;
			$data['type'] = 1;

			$list = $model -> add_log($data);
			if ($list != false) {
				//$this -> _add_to_schedule($task_id);
				$return['info'] = '接受成功';
				$return['status'] = 1;
				echo json_encode($return);
			} else {
				$this -> error('提交成功');
			}
            }
			
		
	}
public function finish_rate($rate) {
                                if ($rate == 0) {
                                        return "任务未开始执行";
                                }
                                if ($rate > 0 and $rate < 100) {
                                        return "任务已完成$rate%";
                                }
                                if ($rate == 100) {
                                        return "任务已完成";
                                }
                        }
        
}