<?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() {
               $user_id = $_SESSION['USER_AUTH_KEY'];
               $model = parent::model('todo', 'oa'); 
               $where=" 1=1 AND user_id='$user_id' AND status in ('1','2') ";
               $list=$model->get_list($where);
               $where1=" 1=1 AND user_id='$user_id' AND status='3' ";
               $list1=$model->get_list($where1);
                 $var_array = array(
                    'list'=>$list,
                    'list1'=>$list1,
                  );       
               
                unset($model);
                TPL::assign($var_array);              
	       TPL::display($this->cptpl.'todo/index.tpl');
        }
        

        public function action_add(){
            TPL::display($this->cptpl.'todo/add.tpl');
        }
        public function action_edit(){
            $id=$_REQUEST['id'];
            $user_id = $_SESSION['USER_AUTH_KEY'];
            $model = parent::model('todo', 'oa'); 
              $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){
                     if($v!=''){
                        $files[$k]['name']= trim(strrchr($v, '/'),'/');
                       $files[$k]['path']=$v;
                    }  
                    
                }
                $var_array = array(
                    'vo'=>$vo,
                    'file'=>$files,

                  );       
               
                unset($model);
                TPL::assign($var_array);
		TPL::display($this->cptpl.'todo/edit.tpl');
        }

      

        public function action_save(){
            $arr=$_POST;
            $id=$arr['id'];
            $model= parent::model('todo', '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('todo', 'oa');
		
		$res=$dept->del($id);
            if (true === $res) {
                $data['status']=1;
                $data['info']="删除成功";
            }
            else {
                $data['info']="删除失败";
            }
            echo json_encode($data);
          
	}
        
        	public function action_set_sort() {
		$node = $_REQUEST['node'];
		$priority = $_REQUEST['priority'];
		$sort = $_REQUEST['sort'];
		$model= parent::model('todo', 'oa'); 
		// 实例化User对象
		//$where['user_id'] = $_SESSION['USER_AUTH_KEY'];
		foreach ($node as $key => $val) {
                       $arr['priority']=$priority[$key];
                       $arr['sort']=$sort[$key];
                       
                       $model->doEdit($val, $arr);
		}
            }
            	public function action_mark_status() {
		$id = $_REQUEST['id'];
		$val = $_REQUEST['val'];
                $model= parent::model('todo', 'oa'); 
		if ($val == 3) {
                        $arr['end_date']=date("Y-m-d");
                        $arr['status']=$val;
			$result=$model->doEdit($id, $arr);
		} else {
                     $arr['status']=$val;
                     $result=$model->doEdit($id, $arr);
                }
            if (true === $result) {
                $data['status']=1;
                $data['info']="编辑成功";
            }
            else {
                $data['info']="编辑失败";
            }
            echo json_encode($data);
	}
}
?>
