<?php
/*
*/
class UserController extends Easy_Controller_Action {
	public function Init(){
		$this->_User = new UsersModel();
		$this->_Playlog = new PlaylogModel();
	}
	public function IndexAction(){
		//echo $this->_Request->getParams('usergroup');
		$this->_View->groupOption = $this->_User->groupOption((int)$this->_Request->getParams('usergroup'));
		$this->_View->Users = $this->_User->getAll();
		$this->_View->printpage = $this->_User->printpage;
	}
	public function AddAction(){
		if ($this->_Request->isPost()){
			if ($this->_User->add($this->_Request->getPost())===false){
				$this->_View->message = $this->_User->error;
			}else{
				$this->_View->message = _('添加成功!');
			}
			exit($this->_View->render('message'));
		}
	}
	/*修改用户资料*/
	public function ModifyAction(){
		if ($this->_Request->isPost()){
			if ($this->_User->modify($this->_Request->getPost())===false){
				$this->_View->message = $this->_User->error;
			}else{
				$this->_View->message = _('修改成功!');
			}
			exit($this->_View->render('message'));
		}else{
			$uid = $this->_Request->getParam('uid');
			$username = $this->_Request->getParam('username');
			if ($username){
				$this->_View->modify = $this->_User->getRow($username);
			}else{
				$this->_View->modify = $this->_User->getRow($uid);
			}
			$this->_View->groupOption = $this->_User->groupOption($this->_View->modify['usergroup']);
		}
	}
	public function lockedAction(){
		$uid = $this->_Request->getParam('uid');
		$this->_User->locked($uid);
		echo '<script>history.back();</script>';
		exit;
	}
	/*
	搜索用户
	*/
	public function SearchAction(){
		$this->_View->groupOption = $this->_User->groupOption($this->_Request->getParams('usergroup'));
		if ($this->_Request->getParams('search')){
			$this->_View->Users = $this->_User->Search($this->_Request->getParams());
			$this->_View->printpage = $this->_User->printpage;
			exit($this->_View->render('user/user_index'));
		}
	}
	/*
	删除用户
	*/
	public function deleteAction(){
		$uid = $this->_Request->getParam('uid');
		$this->_User->delete($uid);
		$this->_View->message = _('删除成功!');
		exit($this->_View->render('message'));
	}
	/*
	批量编辑用户
	*/
	public function postAction(){
		if ($this->_Request->isPost()){
			$this->_View->groupOption = $this->_User->groupOption($this->_Request->getPost('usergroup'));
			if (count($this->_Request->getPost('username'))==0){
				$this->_View->message = _('请选择一个用户!');
				exit($this->_View->render('message'));
			}
			$this->_View->username = $this->_Request->getPost('username');
			$this->_View->post = $this->_Request->getPost('post');
			if ($this->_Request->getParam('do') == 'finish'){
				if ($this->_User->post($this->_Request->getPost())===false){	//批量设置用户资料
					$this->_View->message = $this->_User->error;
				}else{
					$this->_View->message = _('批量更新资料成功!');
					$this->_View->jumpurl = $this->_Request->getPost('jumpurl');
				}
				exit($this->_View->render('message'));
			}
		}
	}
	/*
	活动用户
	*/
	public function onlineAction(){
		if ($this->_Request->isPost() || $this->_Request->getParam('do') == 'delete' ){
			$this->_User->delOnline($this->_Request->getParams('username'));
			$this->_View->message = _('删除成功!');
			exit($this->_View->render('message'));
		}else{
			$this->_Playlog->clearPlayer($this->config['refreshonline']);
			$this->_View->userOnline = $this->_User->userOnline();
			$this->_View->printpage = $this->_User->printpage;
			
		}
		
	}
	/*
	*/
	public function activesAction(){
		if ($this->_Request->getParam('do') == 'del'){
			$this->_User->delActive($this->_Request->getParam('id'));
			$this->_View->message = _('删除成功!');
			exit($this->_View->render('message'));
		}else{
			$this->_Playlog->clearPlayer($this->config['refreshplayer']);
			$this->_View->active = $this->_User->getActive();
			$this->_View->printpage = $this->_User->printpage;
		}
	}
	/**/
	public function LoginAction(){
		exit;
	}
	/**/
	public function playlogAction(){
		if ($this->_Request->getPost('id')){
			$this->_User->delPlaylog($this->_Request->getPost('id'));
			$this->_View->message = _('删除成功!');
			exit($this->_View->render('message'));
		}else{
			$this->_Playlog->clearlog($this->config['clearlog']);
			$this->_View->active = $this->_User->getPlaylog($this->_Request->getPost());
			$this->_View->printpage = $this->_User->printpage;
		}
	}
	
}