<?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() {
           	$model = parent::model('role', 'oa');
                $list=$model->get_list();   
              
                $var_array = array(
                    'list'=>$list,
                    'controller'=>$this->gb_c
                  );            
               unset($model);
               TPL::assign($var_array);
	       TPL::display($this->cptpl.'role/role.tpl');
        }
        
        	/**编辑页面 **/
	public function action_read() {
                $id=$_REQUEST['id'];
		$node = parent::model('role', 'oa');
		$vo = $node -> get_top_menu_data($id);
		
			if (!empty($vo)) {// 读取成功
				$return['data'] = $vo;
				$return['status'] = 1;
				$return['info'] = "读取成功";
				echo json_encode($return);
			} else {
				$return['status'] = 0;
				$return['info'] = "读取错误";
				echo json_encode($return);
			}
		
		//$this -> assign('vo', $vo);
	//	$this -> display();
		//return $vo;
	}
        public function action_add(){
             	$controller=$_REQUEST['controller'];
                $var_array = array(
                    'controller'=>$controller,
                  );            
               unset($dept);
               TPL::assign($var_array);
             TPL::display($this->cptpl.'role/add.tpl');
        }

        public function action_save(){
            $arr=$_POST;
            $id=$arr['id'];
            $dept = parent::model('role', 'oa'); 
              $arr=$this->SafeFilter($arr);
            if($arr['opmode']=='add'){
                $result = $dept->doadd($arr);
            }else{
                $result = $dept->doEdit($id, $arr);
            }
            if (true === $result) {
                $data['status']=1;
                $data['info']="编辑成功";
            }
            else {
                $data['info']="编辑失败";
            }
            echo json_encode($data);
        }
        

        public function action_del(){
                $id=$_POST['id'];
                $dept = parent::model('Role', 'oa');
		$res=$dept->del($id);
                if($res){
                     $data['status']=1;
                     $data['info']='删除成功';
                } else {
                     $data['info']='操作失败';
                }
                 echo json_encode($data);
                die;
	}
        
        
        //权限设置
        
        public function action_node(){
                //获取权限组
            
             	$model = parent::model('role', 'oa');
                $list=$model->get_list();   
                //获取菜单组
                $node = parent::model('node', 'oa');
		if ($_REQUEST['eq_pid']!='') {
                        $eq_pid = $_REQUEST['eq_pid'];
		} else {
			$eq_pid = "#";
		}      
                $grouplist=$node->get_grouplist(); 
                //获取菜单
                $node_list = $node->get_menu();
                $publicmodel=parent::model('public', 'oa');
                if ($eq_pid != "#") {
			$node_list = $publicmodel->tree_to_list($publicmodel->list_to_tree($node_list, $eq_pid));
		} else {
			$node_list = $publicmodel->tree_to_list($publicmodel->list_to_tree($node_list));
		}
          
               	$node_list = $publicmodel->rotate($node_list);

                    foreach($node_list['id'] as $k=>$v){
                        $node_list['id'][$k]="'".$v."'";
                    }
               if ($eq_pid != "#") {
			$node_list = implode(",", $node_list['id']) . ",$eq_pid";
		} else {
			$node_list = implode(",", $node_list['id']);
		}
		
                 
                $menu=$node->get_nodelist($node_list);
                 
            	$tree = $publicmodel->list_to_tree1($menu);
               //     print_r($tree);
		$menu_list = $publicmodel->tree_to_list1($tree);
                $var_array = array(
                    'list'=>$list,
                    'groupList'=>$grouplist,
                    'eq_pid'=>$eq_pid,
                    'node_list'=>$menu_list
                  );            
               unset($model);
               TPL::assign($var_array);          
            TPL::display($this->cptpl.'role/node.tpl');
        }
        public function action_get_node_list() {
		$role_id = $_POST["role_id"];
                $model = parent::model('role', 'oa');
                $data=$model->get_node_list($role_id);
              
		if ($data !== false) {// 读取成功
			$return['data'] = $data;
			$return['status'] = 1;
			echo json_encode($return);
		}
	}
        
        
	public function action_set_node() {
		$role_id = $_POST["role_id"];            
		$org_list = $_POST["org_node_list"];             
		$node_list = $_POST["node_list"];              
		$admin_list = $_POST["admin"]; 
		$write_list = $_POST["write"];
		$read_list = $_POST["read"];

		 $model = parent::model('role', 'oa');
		 $model -> del_node($role_id, $org_list);
               
		$result = $model -> set_node($role_id, $node_list);

		
		

		if (!empty($admin_list)) {
                        foreach($admin_list as $v){
                            $where="";
                            $where ="role_id='$role_id'";
                            $where .=" AND node_id='$v'";
                            $model->saveop('admin',$where);
                        }
			
		}

		if (!empty($write_list)) {
                        foreach($write_list as $v){
                            $where="";
                            $where ="role_id='$role_id'";
                            $where .=" AND node_id='$v'";
                            $model->saveop('write',$where);
                        }
			
		}

		if (!empty($read_list)) {
                         foreach($read_list as $v){
                            $where="";
                            $where ="role_id='$role_id'";
                            $where .=" AND node_id='$v'";
                            $model->saveop('read',$where);
                        }
			
		}

		if ($result === false) {
			 $data['info']='操作失败!';
		} else {
			$data['status']=1;
                        $data['info']='操作成功';
		}
                 echo json_encode($data);
                die;
	}
        
        
        public function action_user(){
                $keyword=$_POST['keyword'];
	        $searchsql = "User.is_del='0'";
                if ($keyword!='') {
                    $searchsql .= " AND User.name LIKE '%$keyword%' OR `emp_no` LIKE '%$keyword%' OR Position.name LIKE '%$keyword%' OR Dept.name LIKE '%$keyword%' ";
                }

                $model = parent::model('role', 'oa');
                $user_list=$model->get_userlist($searchsql);
                $role_list = $model ->get_list();
                $var_array = array(
                    'user_list'=>$user_list,
                    'role_list'=>$role_list,
                  );            
               unset($model);
               TPL::assign($var_array);  

		
//		$this -> assign("role_list", $role_list);
		TPL::display($this->cptpl.'role/user.tpl');
        }
        
	public function action_get_role_list() {
		  $model = parent::model('role', 'oa');
		$id = $_REQUEST['id'];
		$data = $model -> get_role_list($id);
		if ($data !== false) {// 读取成功
			$return['data'] = $data;
			$return['status'] = 1;
			echo json_encode($return);
		}
	}
        	public function action_set_role() {
		$emp_list = $_POST["emp_id"];
            
		$role_list = $_POST["role_list"];
	        $model = parent::model('role', 'oa');
		$model -> del_role($emp_list);

		$result = $model -> set_role($emp_list, $role_list);
		
		if ($result === false) {
			 $data['info']='操作失败!';
		} else {
			$data['status']=1;
                        $data['info']='操作成功';
		}
                 echo json_encode($data);
                die;
	}

    
}
?>
