<?php
/*
'#################################################################
'# 程序名称：中环CMSV2.0
'# 授权方式：免费软件
'# 作者：善
'# 联系QQ:9118847
'# 作者网站:http://www.ps173.com
'# 发布时间：2015-6-10
'# 声明：欢迎广大的PHP爱好者一起共同探讨
'# 声明：各种用途均可免费使用、自由传播，但必须保留程序代码中的版权信息。
'#################################################################
 */

//用户控制器入口
class UserAction extends CommonAction{
	private $user;
		//成员初始化
	public function __construct(){
		parent::__construct();
		$this->user = new UserModel();
	}

		//用户列表页面
	public function index(){
		if(isset($_GET['id']) && isset($_GET['disable'])){
			$data['id'] = $_GET['id'];
			$data['disable'] = $_GET['disable']; 
			$this->status($data);
		}

		$uid = isset($_GET['uid']) ? $_GET['uid'] : null;
		$page = new Page($this->user->user_total(@$uid), 15);
		$this->assign("userinfo", $this->user->user_all($page->limit, @$uid));
		$this->assign("page", $page->pageinfo());
		$this->display();
	}

		//删除用户
	public function del(){
		if(isset($_GET['id'])){
			if($_GET['id'] == $_SESSION['authid']){
				$this->msg('自己不能删除自己');
			}else if($_GET['id'] == 1){
				$this->msg('该账号不允许删除');
			}else{
				if($this->user->delete($_GET['id'])){
					$this->msg('删除户用成功');
				}else{
					$this->msg('删除用户失败');
				}
			}

		}
	}
		//修改用户
	public function mod(){
		if(isset($_GET['id'])){
			$user = $this->user->get_one($_GET['id']);
			if(!$user){
				$this->msg('该用户不存在，或者已经被删除');
			}
		}else{
			$this->msg('非法操作');
		}
		$this->assign("user", $user);
		$this->assign("select", $this->user->select("gid", $user['gid']));
		$this->display();
	}
		//修改用户资料
	public function update(){
		if(empty($_POST['passwd'])){
			unset($_POST['passwd']);
			unset($_POST['newpasswd']);
			$this->checkmod($_POST);
		}else{
			$this->checkuser($_POST);
		}

		if(isset($_POST['sub'])){
			if($this->user->update($_POST)){
				$this->msg('修改用户成功');
			}else{
				$this->msg('数据保持原样');
			}
		}else{
			$this->msg('修改用户失败');
		}
	}
		//检测用户是否存在
	public function checkname($name){
		if($this->user->checkuser($name)){
			$this->msg('该用户名已存在，请重新输入');
			return false;
		}
		return $name;
	}
		//禁用用户或者启用户用
	public function status($data){
		if($data['id'] == $_SESSION['authid']){
			$this->msg('自己不能禁用自己');
		}

		if($data['id'] == 1){
			$this->msg('该账号不允许禁用');
		}

		$this->user->setuser($data);

	}
		//添加用户
	public function add(){
		if(isset($_POST['sub'])){
			if($this->checkuser($_POST)){
				 if($this->checkname($_POST['user'])){
					if($this->user->insert($_POST)){
						$this->msg('用户添加成功！');
					}else{
						$this->msg('数据保持原样');
					}
				}else{
					$this->msg('用户添加失败!');
				}
			}
		}
		$this->assign("select", $this->user->select("gid", 2));
		$this->display();
	}
		//修改密码
	public function password(){
		if(isset($_POST['sub'])){
			if($this->checkpwd($_POST)){
				$data['user'] = $_SESSION['user'];
				$data['passwd'] = $_POST['passwd'];
				$result = $this->user->checkpwd($data);
				unset($data);
				if(!$result){
					$this->msg('旧密码不正确');
				}else{
					$post['id'] = $result['id'];
					$post['passwd'] = $_POST['newpasswd'];
					$res = $this->user->changuser($post);
					if(!empty($res)){
						$this->msg('密码修改成功');
					}else{
						$this->msg('密码修改失败');
					}
				}

			}
		}
		$this->display();
	}

}

?>