<?php
class  AdministratorModel extends DBModel {
	public function Init(){
	}
	public function CreateGroup($group=array()){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_SUPERMAN)->order('id ASC')->toString();
		$data = $this->_DB->fetAll($sql);
		$op = '';
		//print_r($data);
		foreach ($data as $row){
			if (in_array($row['name'], $group)){
				$op.= '<option value="'.$row['name'].'" selected >'.$row['name'].'</option>';
			}else{
				$op.= '<option value="'.$row['name'].'">'.$row['name'].'</option>';
			}
		}
		return $op;
	}
	/*
	添加管理员
	*/
	public function Add($POST){
		if (!$POST['group'][0]){
			$this->error = _('请选择用户组!');
			return false;
		}
		if ($this->vali($POST)){
			$POST['group'] = implode(',', $POST['group']);
			$POST['password'] = md5($POST['password']);
			$POST['lastime'] = time();
			$POST['logintime'] = time();
			unset($POST['passwd']);
			$select = $this->_DB->select();
			$sql = $select->from(VODCMS_ADMIN)->where("username='".$POST['username']."'")->limit(1);
			if ($this->_DB->fetrow($sql)){
				$this->error = _('登陆用户已经存在!请更换');
				return false;
			}
			systemlog::set(_('添加了一个管理员帐户'));
			return $this->_DB->insert(VODCMS_ADMIN, $POST);
		}else{
			return false;
		}
	}
	public function  vali($data, $rule=null){
		if (is_null($rule)){
			$rule = array();
			$rule['username'] = array('/(.+?)/is', '请输入5-30个字符的用户名!', true, 5,30);
			$rule['password'] = array('/(.+?)/is', '请输入5-30个字符的密码!', true, 6,30);
			$rule['realname'] = array('/(.+?)/is', '请输入2-50个字符的真实姓名!', true, 2,30);
			if ($data['password'] != $data['passwd']){
				$this->error = _('登陆密码与确认密码不一致!');
				return false;
			}
		}
		if (Easy_Validate_Default::validate($data, $rule)===false){
			$this->error = Easy_Validate_Default::$error;
			return true;
		}else{
			return true;
		}
	}
	public function modify($POST){
		if ($POST['password']){
			if (strlen($POST['password'])<=5){
				$this->error = _('密码长度必须大于5位数!');
				return false;
			}else{
				$POST['password'] = md5($POST['password']);
			}
		}else{
			unset($POST['password']);
		}
		if (!$POST['group'][0]){
			$this->error = _('请选择用户组!');
			return false;
		}else{
			$POST['group'] = implode(',', $POST['group']);
		}
		
		if (empty($POST['realname'])){
			$this->error = _('请填写真实姓名!');
			return false;
		}
		$row = $this->_DB->update(VODCMS_ADMIN, $POST, 'id='.$POST['id']);
		if ($row){
			return $row;
		}else{
			$this->error = _('未作任何更改!');
			return 0;
		}

	}
	public function getAll($where=null){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_ADMIN)->where($where)->order('id DESC')->toString();
		return $this->_DB->fetAll($sql);
	}
	public function getRow($id){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_ADMIN)->where(array('id'=> $id))->order('id DESC')->toString();
		return $this->_DB->fetRow($sql);
	}
	public function locked($id){
		$sql = 'UPDATE '.VODCMS_ADMIN.' SET locked = NOT(locked) WHERE id='.$id;
		$this->_DB->Exec($sql);
	}
	public function delete($id){
		return $this->_DB->DELETE(VODCMS_ADMIN, array('id'=>$id));
	}
	public function modifyPass($id, $newpass){
		if ($id && $newpass){
			return $this->_DB->update(VODCMS_ADMIN, array('password'=> md5($newpass)), array('id'=> $id));
		}
	}
}