<?php
 /**
 *      [QKSHOP] (C) QIKEWEB Inc.
 *      This is NOT a freeware, use is subject to license terms
 */

class RoleModel extends BaseModel{
	function action(){
		$sql = "select * from qk_role_action order by sortId asc,id asc";
		$info = $this->db->getAll($sql);
		return $info;
	}
	function page(){
		$sql = "select * from qk_role_page order by sortId asc,id asc";
		$info = $this->db->getAll($sql);
		return $info;
	}

	function role(){
		global $cfg_lang;
		$sql = "select id as userId from qk_user where groupId=1 order by id asc";
		$user = $this->db->getAll($sql);
		$i=0;
		foreach($user as $item){
			$user[$i]['loginName'] = $this->db->getOne("select loginName,id as userId from qk_user where id='".$item['userId']."'");
			$sql = "select actionId from qk_role where userId='".$item['userId']."'";
			$actions = $this->db->getAll($sql);
			$user[$i]['action']  = array();
			foreach($actions as $action){
				$user[$i]['action'][] = $action['actionId'];
			}
			$i++;
		}

		$role = $user;
		return $role;
	}

	function edit($params){
		//print_r($params);
		$userId = $params['userId'];
		$action = $params['action'];
		if($userId && settype($userId,"integer")){
			$sql = "delete from qk_role where userId='$userId'";
			$this->db->query($sql);
			foreach($action as $actionId){
				$sql = "insert into qk_role set userId='$userId',actionId='$actionId'";
				$this->db->query($sql);
			}
			return true;
		}
		return false;
	}
}
?>