<?php
class AdministratorController extends Easy_Controller_Action{
	
	public function init(){
		$this->_Administrator = new AdministratorModel();
	}
	/*
	管理员控制模块浏览页面
	*/
	public function IndexAction(){
		$this->_View->option = $this->_Administrator->CreateGroup();
		$this->_View->listAdmin = $this->_Administrator->getAll();
	}
	/*
	添加管理员
	*/
	public function AddAction(){
		if ($this->_Request->isPost()){
			if ($this->_Administrator->add($this->_Request->getPost())){
				$this->_View->subject = _('添加管理员');
				$this->_View->message['content'] = _('添加成功!');
			}else{
				$this->_View->subject = _('添加管理员失败');
				$this->_View->message = $this->_Administrator->error;
			}
			exit($this->_View->render('message'));
		}
	}
	/*
	修改管理员资料
	*/
	public function modifyAction(){
		$id = $this->_Request->getParam('id');
		$this->_View->modify = $this->_Administrator->getRow($id);
		$this->_View->option = $this->_Administrator->CreateGroup(explode(',', $this->_View->modify['group']));
		if ($this->_Request->isPost()){
			if ($this->_Administrator->modify($this->_Request->getPost())){
				$this->_View->subject = _('修改管理员');
				$this->_View->message = _('修改成功!');
			}else{
				$this->_View->subject = _('修改管理员');
				$this->_View->message = $this->_Administrator->error;
			}
			exit($this->_View->render('message'));
		}
	}
	public function lockedAction(){
		$id = (int)$this->_Request->getParam('id');
		$this->_Administrator->locked($id);
		$this->_View->redirect('administrator/index');
		exit;
	}
	public function deleteAction(){
		$id = (int)$this->_Request->getParam('id');
		$this->_Administrator->delete($id);
		$this->_View->redirect('administrator/index');
		exit;
	}
	public function modifypassAction(){
		$this->_View->username = $this->_Request->getCookie('admin');
		//$this->_View->adminid = $this->_Request->getSession('id');
		if ($this->_Request->isPost()){
			$oldpass = $this->_Request->getPost('pass');
			$newpwd = $this->_Request->getPost('newpwd');
			$row = $this->_Administrator->getRow( $this->_Request->getSession('id'));
			if ($row['id'] == 0){
				$this->_View->message = _('用户不存在或者登录超时!');
			}elseif ($row['password'] != md5($oldpass)){
				$this->_View->message = _('当前登录密码不正确!');
				exit($this->_View->render('message'));
			}else{
				$this->_Administrator->modifyPass($row['id'], $newpwd);
				$this->_View->message = _('密码修改成功!');
			}

			exit($this->_View->render('message'));
		}
	}
}