<?php
class SupermanModel extends DBModel {
	protected $_name1;
	protected $_name2;
	protected $_name3;
	protected $_name4;
	public function init(){
		$this->_name1 = VODCMS_SUPERMAN;
		$this->_name2 = VODCMS_PRIVILEGES;
		$this->_name3 = VODCMS_METHOD;
		$this->_name4 = VODCMS_MODULE;
	}
	public function Add($POST){
		$array = array();
		$array['name'] = $POST['name'];
		$array['flag'] = (int)$POST['flag'];
		$module = $POST['module'];
		$data = null;
		if ($this->validate($POST)){
			$select = $this->_DB->select();
			$sql = $select->from($this->_name1)->where(array('name'=>$POST['name']))->limit(1)->toString();
			if ($this->_DB->fetRow($sql)){
				$this->error = _('组名称已经存在!');
				return false;
			}
			$this->_DB->insert($this->_name1, $array);
			if(is_array($POST['privileges'])){
				foreach ($POST['privileges'] as $key=>$row){
					foreach($row as $method){
					$this->_DB->insert($this->_name2, 
					array('module'=> strtolower($key), 'group'=>$POST['name'], 'method'=>strtolower($method)));
					}
				}
			}
		}else{
			return false;
		}
	}
	public function validate($POST, $rule=null){
		if (is_null($rule)){
			$rule['name'] = array('/(.+?)/is', '请填写组名称30个字符组名称!', true, 2, 30);
		}
		if (Easy_Validate_Default::validate($POST, $rule)===false){
			$this->error = Easy_Validate_Default::$error;
			return false;
		}else{
			return true;
		}
	}
	/*
	传入条件返回数组
	*/
	public function getAll($where = null){
		$select = $this->_DB->select();
		$select->from($this->_name1)->where($where)->order('id DESC');
		$sql = $select->toString();
		return $this->_DB->fetAll($sql);
	}
	public function scan($path = null){
		$path = ROOT.'Application/Controllers';
		$ignore = array('.', '..', '.svn','.html','.jpg','.bak');
		foreach( scandir($path) as $file){
			if ( in_array(strrchr($file, '.'), $ignore)===false){
				$this->getMethod($path.'/'. $file);
			}
		}
	}
	public function getMethod($filename){
		$ignore = array('init', '_construct', 'loginaction','authaction','topaction', 
		'leftaction', 'mainaction','notieaction', 'copyrightaction', 'top', 'left', 'main', 'notie',
		'copyright', 'auth', 'login');
		preg_match_all('/function+\s+(\w+)action\(/is', file_get_contents($filename), $result);
		//$this->_DB->delete($this->_name3, ');
		//print_r($result[1]);
		
		foreach($result[1] as $method){
			if (in_array(strtolower($method), $ignore) === false ){
				$select = $this->_DB->select();
				$where = array('controller' => strtolower(basename($filename, '.php')),'method' => strtolower($method));
				$data = array('controller' => strtolower(basename($filename, '.php')),'method' => strtolower($method),
				'comment'=> $this->getComment(strtolower(basename($filename, '.php')), strtolower($method))
				);	//end array
				$sql = $select->from($this->_name3)->where($where)->limit(1)->toString();
				$row = $this->_DB->FetRow($sql);
				//echo $sql.'<br>';
				//echo $row['id'].'<br>';
				if (!$row['id']){
					$this->_DB->Insert($this->_name3, $data);
				}
			}
		}
	}
	public function getComment($controller, $method = 'index'){
		$control = str_replace('controller', '', strtolower($controller));
		 $url = $control.'/'. $method;
		 $select = $this->_DB->select();
		 if ($method == 'index'){
			 $select->from($this->_name4)->where(' `url` LIKE \''.$url.'%\' OR `url` LIKE \''.$control.'%\'')->limit(1);
		 }else{
		  	$select->from($this->_name4)->where(' `url` LIKE \''.$url.'%\'')->limit(1);	
		 }
		 $sql = $select->toString();
		 $row = $this->_DB->fetRow($sql);
		 return $row['name'];
	}
	/*
	从method表返回所有方法名记录
	*/
	public function listMethod($where=null){
		$select = $this->_DB->select();
		$select->from($this->_name3)->where($where)->order('id DESC');
		$sql = $select->toString();
		$total = $this->_DB->getCount($sql);
		$this->_Page->set($total);
		$this->printpage = $this->_Page->PrintPage();
		//echo $sql;
		return $this->_DB->fetAll($sql.$this->_Page->limit());
	}
	/*
	从method表返回一行方法名记录
	*/
	public function getMethods($where = null){
		$select = $this->_DB->select();
		$select->from($this->_name3)->where($where)->order('id DESC');
		$sql = $select->toString();
		return $this->_DB->fetRow($sql);
	}
	public function modifyMethod($POST){
		if($POST['name']){
			$this->_DB->update($this->_name3, array('name'=> $POST['name']), array('controller' => $POST['controller']));
		}
		return $this->_DB->update($this->_name3, $POST, 'id='. $POST['id']);
	}
	public function deleteMethod($id){
		return $this->_DB->delete($this->_name3, 'id='.$id);
	}
	/*
	获取一行用户组资料
	*/
	public function getRow($id){
		$Permission = new Permission();
		$data = array();
		$select = $this->_DB->select();
		$sql = $select->from($this->_name1)->where('id='.$id)->toString();
		$data = $this->_DB->fetRow($sql);
		$temp = $Permission->getAll(array('group'=>$data['name']));;
		$data['Permission'] = (array)$temp[$data['name']];
		return $data;
	}
	/*
	修改用户组资料
	*/
	public function modify($POST){
		if (empty($POST['name'])){
			$this->error = _('请填写用户组名称!');
			return false;
		}
		$this->_DB->update($this->_name1, array('name'=>$POST['name'],'flag'=>intval($POST['flag'])), 'id='.(int)$POST['id']);
		$this->_DB->delete($this->_name2, array('group'=> $POST['oldname']));
		if(is_array($POST['privileges'])){
			foreach ($POST['privileges'] as $key=>$row){
				foreach($row as $method){
					$this->_DB->insert($this->_name2, 
					array('module'=> strtolower($key), 'group'=> $POST['name'], 
					'method'=>strtolower($method)));
				}
			}
		}
		return true;
	}
	/*
	删除组信息
	*/
	public function delete($id){
		$select = $this->_DB->select();
		$select->from(VODCMS_SUPERMAN)->where('id='.$id);
		$sql = $select->toString();
		$row = $this->_DB->fetRow($sql);
		if($row['name'] == 'Guest'){
			$this->error = _('系统保留用户组不允许删除!');
			return false;
		}
		$select = $this->_DB->select();
		$select->from(VODCMS_SUPERMAN)->where('flag=1');
		$sql = $select->toString();
		$total = $this->_DB->getCount($sql);
		if ($total<2){
			$this->error = _('系统必须保留一个超级用户组!');
			return false;
		}
		systemlog::set('删除管理员组',$row['name']);
		$this->_DB->delete($this->_name1, array('id'=> $id));
		$this->_DB->delete($this->_name2, array('group'=> $row['group']));
		return true;
	}
}
?>