<?php
class GroupModel extends DBModel {
	public function Init(){
	}
	public function add($POST){
		if ($this->validate($POST)){
			$row = $this->_DB->Insert(VODCMS_USERGROUP, $POST);
			systemlog::set('添加用户组名称为'.$row['title'].'的用户组信息!');
			$this->Cache();
			return $row;
		}else{
			return false;
		}
	}
	public function modify($POST){
		if ($this->validate($POST)){
			$row = $this->_DB->update(VODCMS_USERGROUP, $POST, array('id'=>$POST['id']));
			$this->Cache();
			return $row;
		}else{
			return false;
		}
	}
	public function validate($data, $rule=null){
		if (is_null($rule)){
			$rule['title'] = array('/(.+?)/is', '请填写3至20个字符的组名称!', true, 3 , 20);
			$rule['description'] = array('/(.+?)/is', '请填写3至100个字符的组描述!', true, 3 , 100);
			$rule['flag'] = array('/(\d+)/is', '请填写数字的权重值!，值越高表示权限越大', true);
			$rule['maxlook'] = array('/(\d+)/is', '请填写点播上限!必须为数字类型', true);
			$rule['maxdown'] = array('/(\d+)/is', '请填写下载上限!必须为数字类型', true);
			$rule['repeat'] = array('/(\d+)/is', '请填写重复扣点!必须为数字类型', true);
		}
		if (Easy_Validate_Default::validate($data, $rule)==true){
			return true;
		}else{
			$this->error = Easy_Validate_Default::$error;
			return false;
		}
	}
	/*
	根据传入条件返回多维记录数
	*/
	public function getAll($where=null){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_USERGROUP)->where($where)->order('id DESC')->toString();
		return $this->_DB->fetAll($sql);
	}
	/*
	获取一行记录数据
	*/
	public function getRow($id=0){
		if ($id){
			$select = $this->_DB->select();
			$sql = $select->from(VODCMS_USERGROUP)->where('id='.$id)->order('id DESC')->toString();
			return $this->_DB->fetRow($sql);
		}
	}
	/*
	根据ID删除记录
	*/
	public function Delete($id=0){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_USERGROUP)->where('id='.$id)->order('id DESC')->toString();
		$row = $this->_DB->fetRow($sql);
		systemlog::set('删除了名称为'.$row['title'].'的用户组信息!');
		unset($row);
		return $this->_DB->delete(VODCMS_USERGROUP, 'id='.$id);
	}
	public function CreateOption($id, $prefix=''){
		$array = $this->getAll();
		$body = '';
		foreach ($array as $row){
			if ($id == $row['id']){
				$body.= '<option value="'.$row['id'].'" selected="selected">'.$row['title'].'</option>';
			}else{
				$body.= '<option value="'.$row['id'].'">'.$row['title'].'</option>';
			}
		}
		return $body;
	}
	/*
	将用户组列表写入缓存
	*/
	public function cache(){
		$cache = new Easy_Cache();
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_USERGROUP)->where($where)->order('id DESC')->toString();
		foreach($this->_DB->fetAll($sql) as $row){
			$data[$row['id']] = $row;
		}
		$data = serialize($data);
		$cache->set('usergroup', $data);
		unset($data);
	}
	public function getGroupName($groupid){
		if (!$this->usergroup){
			if (Easy_Cache::isCache('usergroup')===false){
				$this->cache();
			}
			$this->usergroup = unserialize(Easy_Cache::get('usergroup'));
		}
		$flag = $this->usergroup[$groupid]['flag'];
		$str = null;
		if ($groupid){
			foreach($this->usergroup as $row){
				if ($row['flag'] >= $flag){
					$str.= $str ?  '&nbsp;' .$row['title'] : $row['title'];
				}
			}
		}else{
			$str = _('免费');
		}
		unset($usergroup);
		return $str;
	}
	public function groupName($id){
		if (!$this->usergroup){
			if (Easy_Cache::isCache('usergroup')===false){
				$this->cache();
			}
			$this->usergroup = unserialize(Easy_Cache::get('usergroup'));
		}
		if ($this->usergroup){
			return $this->usergroup[$id]['title'];
		}else{
			return '获取缓存失败!';
		}
	}
 }