<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class roleOModel extends X {


  
        public function get_top_menu_data($id){
	        $sql = "SELECT * FROM ".DB_PREFIXOA."Role WHERE id='".$id."'";
                return parent::$obj->fetch_first($sql);
	}


        public function get_node_list($role_id){
            $sql= 'select * from ' . DB_PREFIXOA . 'role_node as a  where a.role_id=' . $role_id . ' ';
            return parent::$obj->getall($sql);
        }
        

        public function get_list(){
             $sql=' SELECT * FROM `oa_Role` WHERE ( is_del=0 ) ORDER BY sort asc';
             $list = parent::$obj->getall($sql);
             return $list;
        }

                public function doEdit($id,$arr){
                unset($arr['id']);
                unset($arr['opmode']);
                unset($arr['ajax']); 
                  $arr['update_time']= time();
                $result = parent::$obj->update(DB_PREFIXOA.'Role', $arr, 'id='.$id.'');
                return $result;
        }  
                public function doAdd($array) {
                   // print_r($array);die;
                     unset($array['ajax']);
                     unset($array['opmode']);
                    $array['create_time']= time();
                   return parent::$obj->insert(DB_PREFIXOA.'Role', $array); 
              }
      public function del($id){
        $sql = "DELETE FROM ".DB_PREFIXOA."Role WHERE id='".intval($id)."'";
        return parent::$obj->query($sql);
      }
      
      public function del_node($role_id, $node_list) {
		if (empty($node_list)) {
			return true;
		}
		if (is_array($node_list)) {
			$node_list = array_filter($node_list);
		} else {
			$node_list = explode(",", $node_list);
			$node_list = array_filter($node_list);
		}
                foreach ($node_list as $v){
                       $sql = "DELETE FROM ".DB_PREFIXOA."role_node WHERE role_id='".intval($role_id)."' AND node_id='".intval($v)."'";
                       $result= parent::$obj->query($sql);
                }
		
		if ($result === false) {
			return false;
		} else {
			return true;
		}
	}
        function set_node($role_id, $node_list) {
		if (empty($node_list)) {
			return true;
		}
		if (is_array($node_list)) {
			$node_list = array_filter($node_list);
		} else {
			$node_list = explode(",", $node_list);
			$node_list = array_filter($node_list);
		}
                $array['role_id']=$role_id;
		foreach ($node_list as $node) {
                        $array['node_id']=$node;
                        $result = parent::$obj->insert(DB_PREFIXOA.'role_node', $array); 
			if ($result === false) {
				return false;
			}
                        unset($array['node_id']);
		}
		return true;
	}
        
        public function saveop($name,$where){
            $name="`".$name."`";
              $arr[$name]=1;
              $result = parent::$obj->update(DB_PREFIXOA.'role_node', $arr, $where);
              return $result;
        }
        
        
        public function get_userlist($searchsql){
            $sql="SELECT User.id AS id,User.emp_no AS emp_no,User.name AS name,User.letter AS letter,User.dept_id AS dept_id,User.position_id AS position_id,User.email AS email,User.duty AS duty,User.office_tel AS office_tel,User.mobile_tel AS mobile_tel,User.pic AS pic,User.birthday AS birthday,User.sex AS sex,User.password AS password,User.is_del AS is_del,Position.name AS position_name,Dept.name AS dept_name FROM oa_user User JOIN oa_position Position ON Position.id=User.position_id JOIN oa_dept Dept ON Dept.id=User.dept_id WHERE ".$searchsql;
             return parent::$obj->getall($sql);
        }
        public function get_role_list($id){
            	$sql='select a.role_id from '.DB_PREFIXOA.'role_user  as a where a.user_id=' . $id . ' ';	
		$rs =   parent::$obj->getall($sql);
		return $rs;
	
        }
        
//              public function del_node($role_id, $node_list) {
//		if (empty($node_list)) {
//			return true;
//		}
//		if (is_array($node_list)) {
//			$node_list = array_filter($node_list);
//		} else {
//			$node_list = explode(",", $node_list);
//			$node_list = array_filter($node_list);
//		}
//                foreach ($node_list as $v){
//                       $sql = "DELETE FROM ".DB_PREFIXOA."role_node WHERE role_id='".intval($role_id)."' AND node_id='".intval($v)."'";
//                       $result= parent::$obj->query($sql);
//                }
//		
//		if ($result === false) {
//			return false;
//		} else {
//			return true;
//		}
//	}
         public function del_role($user_list) {
		if (empty($user_list)) {
			return true;
		}
		if (is_array($user_list)) {
			$user_list = array_filter($user_list);
		} else {
			$user_list = explode(",", $user_list);
			$user_list = array_filter($user_list);
		}
                foreach ($user_list as $v){
                       $sql = "DELETE FROM ".DB_PREFIXOA."role_user WHERE user_id='".intval($v)."'";
                       $result= parent::$obj->query($sql);
                }
		
		if ($result === false) {
			return false;
		} else {
			return true;
		}
	}
                public function set_role($user_list, $role_list) {

		if (empty($user_list)) {
			return true;
		}
		if (empty($role_list)) {
			return true;
		}
		if (is_array($user_list)) {
			$user_list = array_filter($user_list);
		} else {
			$user_list = explode(",", $user_list);
			$user_list = array_filter($user_list);
		}
		$user_list = implode(",", $user_list);

		if (is_array($role_list)) {
			$role_list = array_filter($role_list);
		} else {
			$role_list = explode(",", $role_list);
			$role_list = array_filter($role_list);
		}
		$role_list = implode(",", $role_list);

		$where = 'a.id in (' . $user_list . ') AND b.id in(' . $role_list . ')';
		$sql = 'INSERT INTO ' . DB_PREFIXOA. 'role_user (user_id,role_id) ';
		$sql .= ' SELECT a.id, b.id FROM ' . DB_PREFIXOA . 'user a, ' . DB_PREFIXOA . 'role b WHERE ' . $where;
		$result = parent::$obj->query($sql);
		if ($result === false) {
			return false;
		} else {
			return true;
		}
	}
}
?>
