<?php
// 后台用户模块
//require_once("CommonAction.class.php");
class UserAction extends Action {
	function _filter(&$map){
		  $map['id'] = array('egt',2);
		$map['account'] = array('like',"%".$_POST['account']."%");
	}
	// 检查帐号
	public function checkAccount() {
        if(!preg_match('/^[a-z]\w{4,}$/i',$_POST['account'])) {
            $this->error( '用户名必须是字母，且5位以上！');
        }
		$User = M("User");
        // 检测用户名是否冲突
        $name  =  $_REQUEST['account'];
        $result  =  $User->getByAccount($name);
        if($result) {
        	$this->error('该用户名已经存在！');
        }else {
           	$this->success('该用户名可以使用！');
        }
    }
    public function index()
	{
	  $user = M("user");
	  $map["del"] = array("eq",0);
	  $data = $user->where($map)->findall();
	  $this->assign("data",$data);
	  $this->display();
	}
	public function add()
	{
	    $category = M("node");
	    $map["level"] = array("eq","2");
		//$map["parentid"] = array("eq",0);
		$data = $category->where($map)->field("id,title")->order("id asc")->findall();
		//获取栏目的个数
	    $this->assign("data",$data);
	  $this->display();
	}
	public function updateUser()
	{ 
	   $user = M("user");
	   $map["account"] = array("eq",$_POST["account"]);
	   $password= $_POST["password"];
	    $repassword =$_POST["repassword"];
        if(''== trim($password)) {
        	$this->error('密码不能为空！');
        }else if('' == trim($repassword))
		{
		   $this->error('密码确认不能为空');
		}
		else if($password != $repassword)
        {
		   $this->error("两次输入的密码不一致");
		}
		
	    //判断数据是否未更新
	  $old = $user->where($map)->getField("password");
	  if($password == $old)
	  {
	        $this->assign("jumpUrl","./index");
			$this->changeRole($_POST["id"]);
			$this->success('用户信息更新成功！');
	  }
	  else
	  {
	   $data["password"] = sha1($password);
	    $result = $user->where($map)->save($data); 
	   if($resule){
			    $this->assign("jumpUrl","./index");
				$this->changeRole($_POST["id"]);
				$this->success('用户信息更新成功！');
			}else{
				$this->error('用户信息更新失败！');
			}
	  }
	}
	//修改资料
	public function changePwd()
	{
	   $user = M("user");
	   $map["del"] = array("eq",0);
	   $map["id"] = array("eq",$_GET["id"]);
	   $userData = $user->where($map)->field("id,account,status,password")->findall();
	   $this->assign("userData",$userData);
	   $category = M("node");
	   $maps["level"] = array("eq","2");
	   //$maps["parentid"] = array("eq",0);
	   $data = $category->where($maps)->field("id,title")->order("id asc")->findall(); 
	   //获取权限
	   //申请空节点
	   $array = array();
	   $RoleUser = M("RoleUser");
	   $mapd["user_id"] = array("eq",$_GET["id"]);
	   $role = $this->StringChangeArray($RoleUser->where($mapd)->getfield("role_id"));
       foreach($data as $value)
	   {
	     if(in_array($value["id"],$role))
		 {
		     array_push($value,"1");
			 array_push($array,$value);
		 }
		 else
		 {
		  array_push($array,$value);
		 }
	   }
	   $this->assign("data",$array);
	   $this->display();
	}
	
	// 插入数据
	public function insert() {
		// 创建数据对象
		$User =	D("admin.user");
		if(!$User->create()) {
			$this->error($User->getError());
		}else{
			// 写入帐号数据
			if($result	 =	 $User->add()) {
			    $this->assign("jumpUrl","./index");
				$this->addRole($result);
				$this->success('用户添加成功！');
			}else{
		         $this->assign("jumpUrl","./add");
				$this->error('用户添加失败！');
			}
		}
	}
   
  
	protected function addRole($userId) {
		//新增用户自动加入相应权限组
		$RoleUser = M("RoleUser");
		$RoleUser->user_id	=	$userId;
        // 默认加入网站编辑组
        $RoleUser->role_id	= $this->arrayChangeString($_POST["ManagePower"]);
		$RoleUser->add();
	}
	
	protected function changeRole($userId) {
		//新增用户自动加入相应权限组
		$RoleUser = M("RoleUser");
		$map["user_id"]	=array("eq",$userId);
		// 默认加入网站编辑组
        $data["role_id"] = $this->arrayChangeString($_POST["ManagePower"]);
		if(!$RoleUser->where($map)->save($data))
		{
		   $data["user_id"] = $userId;
		   $RoleUser->add($data);
		}
		
	}
	//将数组装换成成字符串
	protected function arrayChangeString($array)
	{
	
	   if(is_array($array))
	   {
	     foreach($array as $value)
		 {
		    $string .= $value."|";
		 }
	   }else
	   {
	       $string = "";
	   }
	   return $string;
	}
    //将字符串转换成数组
	protected function StringChangeArray($string)
	{
	   if(is_string($string))
	   {
	       $array = explode("|",$string);
	   }
	     return $array;
	}
}
?>