<?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() {
                
	       TPL::display($this->cptpl.'schedule/index.tpl');
        }
        
	public function action_day_view() {
		TPL::display($this->cptpl.'schedule/day_view.tpl');
	}
        
        public function action_add(){
            TPL::display($this->cptpl.'schedule/add.tpl');
        }
        public function action_edit(){
            $id=$_REQUEST['id'];
            $user_id = $_SESSION['USER_AUTH_KEY'];
            $model = parent::model('schedul', 'oa'); 
              $vo=$model->get_data($id,$user_id);
               $vo['content']=htmlspecialchars_decode($vo['content']);
              $file = explode(";", $vo['add_file']);
              $actor = explode(";", $vo['actor']);
              $files=array();
                foreach($file as $k=>$v){
                   if($v!=''){
                        $files[$k]['name']= trim(strrchr($v, '/'),'/');
                       $files[$k]['path']=$v;
                    }  
                }
               $actors= array();
                foreach($actor as $k=>$v){
                    if($v!=''){
                        $actors[$k]['name']= substr($v,0,strpos($v, '|'));
                        $actors[$k]['id']= trim(strrchr($v, '|'),'|');
                    }  
                }
              //  print_r($actors);
                $var_array = array(
                    'vo'=>$vo,
                    'file'=>$files,
                    'actors'=>$actors
                  );       
               
                unset($model);
                TPL::assign($var_array);
		TPL::display($this->cptpl.'schedule/edit.tpl');
        }

                public function action_read() {
                $id = $_REQUEST['id'];
                $user_id = $_SESSION['USER_AUTH_KEY'];
                $model = parent::model('schedul', 'oa'); 
		$list1 = $_REQUEST['list'];  
		$list = array_filter(explode("|", $list1));
		$current = array_search($id, $list);
                
		if ($current !== false) {
			$next = $list[$current + 1];
			$prev = $list[$current - 1];
		}
                $vo=$model->get_data($id,$user_id);
                
                $vo['content']=htmlspecialchars_decode($vo['content']);
                $file = explode(";", $vo['add_file']);
                $files=array();
                foreach($file as $k=>$v){
                    $files[$k]['name']= trim(strrchr($v, '/'),'/');
                    $files[$k]['path']=$v;
                }
                $var_array = array(
                    'list'=>$list1,
                    'next'=>$next,
                    'prev'=>$prev,
                    'vo'=>$vo,
                    'file'=>$files
                  );       
               
                unset($model);
                TPL::assign($var_array);
		TPL::display($this->cptpl.'schedule/read.tpl');
	}
        public 	function action_json() {
		header("Cache-Control: no-cache, must-revalidate");
		header("Content-Type:text/html; charset=utf-8");
		$user_id = $_SESSION['USER_AUTH_KEY'];
		$start_date = $_REQUEST["start_date"];
		$end_date = $_REQUEST["end_date"];
                $model = parent::model('schedul', 'oa'); 
                $where= " `user_id` = '$user_id' AND `is_del` = 0 AND ( `start_time` >= '$start_date' AND `start_time` <= '$end_date'  )";         
		$list=$model->get_list($where);
		exit(json_encode($list));
	}
        public function action_save(){
            $arr=$_POST;
            $id=$arr['id'];
            $model= parent::model('schedul', 'oa'); 
            $arr['content']=str_replace('\\','',$arr['content']);
            $arr['content']=htmlspecialchars($arr['content']);
            if($arr['priority']==''){
                $arr['priority']=0;
            }
            if(!empty($arr['upload_file'])){
                        $str='';
                        foreach($arr['upload_file'] as $v){
                            $str .=$v.";";
                        }
                        $arr['add_file']=substr($str,0,strlen($str)-1); 
            }
            $arr=$this->SafeFilter($arr);
            if($arr['opmode']=='add'){
                unset($arr['upload_file']);
                $arr['user_id']=$_SESSION['USER_AUTH_KEY'];
                $result = $model->doadd($arr);
            }else{
                 unset($arr['upload_file']);
      
                $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=$_REQUEST['id'];
             
                $dept = parent::model('schedul', 'oa');
		
		$res=$dept->del($id);
                if ($res !== false) {//保存成功
			 XHandle::halt('操作成功', $this->cpfile.'?c=Schedule', 0);
		} else {
                         XHandle::halt('操作失败！', '', 1);
		}
          
	}
}
?>
