<?php
class group{
	var $key = "sys_usergroup";
	var $field = array('reply','post','upload');
	function group(){}
	function manage(){
		return qdb::fetchAll("select * from ".TABLEPRE."user_group order by gid asc");
	}
	function add(){
		$param = $this->param();
		$num = qdb::queryAlone("select count(*) as num from ".TABLEPRE."user_group where gname = '{$param['gname']}' ");
		if($num > 0) qbase::showmsg("该用户组名称已被占用，还是换一个吧");
		$sql = qbase::createSql(TABLEPRE."user_group", $param, 'insert') ;
		qdb::query($sql);
		$this->cache();
	}
	function edit($gid){
		$param = $this->param();
		$num = qdb::queryAlone("select count(*) as num from ".TABLEPRE."user_group where gname = '{$param['gname']}' and gid != '{$gid}' ");
		if($num > 0) qbase::showmsg("该用户组名称已被占用，还是换一个吧");
		$sql = qbase::createSql(TABLEPRE."user_group", $param, 'update') . " where gid = '{$gid}' ";
		qdb::query($sql);
		$this->cache();
	}
	function del($gid){
		qdb::query("delete from ".TABLEPRE."user_group where gid = '{$gid}' ");
		$this->cache();
	}
	function param(){
		$param = array();
		$param['gname'] = trim($_POST['gname']); 
		$param['point'] = intval($_POST['point']);
		$param['purview'] = $_POST['purview'];
		foreach($this->field as $val){
			if(!isset($param['purview'][$val])) $param['purview'][$val] = 0;
			else $param['purview'][$val] = intval($param['purview'][$val]);
		}
		$param['purview'] = serialize($param['purview']);
		if(empty($param['gname'])) qbase::showmsg("请填写用户组名称");
		return $param;
	}
	function cache(){
		$temp = $this->manage();
		$data = array();
		foreach($temp as $val){
			$val['purview'] = @unserialize($val['purview']);
			if(!is_array($val['purview'])){
				$val['purview'] = array(
					'post'=>0,
					'reply'=>0,
					'upload'=>0,
					'upload_ext'=>'',
					'upload_size'=>0
				);
			}
			$data[$val['gid']] = $val;
		}
		cache::set($this->key, $data, 86400 * 30);
		return $data;
	}
}
?>