<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Handles loading of site resources (CSS, JS, images) using Views.
 * By default it is assumed that your media files will be stored in
 * `application/views/media`.
 *
 * Usage:
 *  `http://example.com/index.php/media/css/styles.css` 
 *
 * $Id: media.php 2545 2008-04-20 23:39:42Z JAAulde $
 *
 * @package	   Media Module
 * @author	   Greg MacLellan
 * @copyright  (c) 2007-2008 Kohana Team
 * @license	   http://kohanaphp.com/license.html
 */
class User_Controller extends Base_Controller {
function __construct(){ 
			parent::__construct(); 
			$this->user = new Auth_User_Model(); 
			$this->group = new Auth_Group_Model(); 
			$this->tree = new Tree_Model(); 
	}

	function index(){  
		//echo Site_Controller::$_container;
		$user=$this->user->userlist()->result_array(false);
		$this->view = new View('admin/user/list'); 
		$this->view ->menu=$user;
		$this->view ->mainurl=BASEURI."menu/index";
		$this->main ->formAction="user/delete";
		$this->display();
	}
	
	function add(){ 
		if($_POST){
			$add=$this->input->post('form', TRUE);
			//$roles=$this->input->post('menu_roles', TRUE);
			$add["user_pwd"]=md5($add["user_pwd"]);
			if(!$this->user->getcheck($add['user_name'])->result_array(false)){
				if($this->user->add($add)){
					$this->main->message='添加成功！';
					$this->main->message_type='success';
				}else{
					$this->main->message='添加失败！';
					$this->main->message_type='error';
				}
			}else{
				$this->main->message='用户已存在！';
				$this->main->message_type='warning';
			}
			$this->index();
		}else{
		$group=$this->group->grouplist()->result_array(false);
		
		$this->main ->formAction="user/add";
		$this->view = new View('admin/user/add'); 
		$this->view ->groups=$group;
		$this->main ->is_validator = "TRUE";
		$this->display();
		}
	}
	
	function edit($id=0){
		if($_POST){
			$edit=$this->input->post('form', TRUE);
			if($edit["user_pwd"]!=""){
				$edit["user_pwd"]=md5($edit["user_pwd"]);
			}else{
				unset($edit["user_pwd"]);
			}
			
			if($this->user->edit($edit)){
				$this->index();
			}
		}else{
		$group=$this->group->grouplist()->result_array(false);
		$countent=$this->user->getone($id)->current();
		$this->main ->formAction="user/edit";
		$this->view = new View('admin/user/edit'); 
		$this->view ->group=$group;
		$this->view ->countent = $countent;
		$this->display();
		}
	
	}
	
		function show($id=0){
		
		$group=$this->group->grouplist()->result_array(false);
		$countent=$this->user->getone($id)->current();
		$this->main ->formAction="user/edit";
		$this->view = new View('admin/user/show'); 
		$this->view ->group=$group;
		$this->view ->countent = $countent;
		$this->display();
		
	
	}
	
	function review(){
	if(@$_POST){
		
			$id	=	$this->input->post('select_id', TRUE);
			foreach ($id as $id){
				$this->user->review($id);
			}
		}
		$this->index();
	}
	
	function delete($id=0){
		if(@$_POST){
			$menu_id	=	$this->input->post('select_id', TRUE);
		}
		else{
			$menu_id[]	=	$id;
		}
		if(is_array($menu_id)){
			foreach ($menu_id as $id){
				$id_list['user_id']	=	$id;
				if($this->user->delete($id_list)){
					
					$this->main->message='删除用户成功！';
					$this->main->message_type='success';
				}else{
					$this->main->message='删除用户失败！';
					$this->main->message_type='error';
					
				}
			}
		}
		$this->index();
	
	}
	function editmypwd(){
		if($this->input->post()){
			$user_id	=	$_SESSION['adminmanager']->user_id;
			$pwd		=	$this->input->post('pwd');
			$oldpwd		=	md5($pwd['old_pwd']);
			$pwd		=	md5($pwd['new_pwd']);
			if($this->user->validOldPwd($oldpwd,$user_id)){
				$this->user->updateUserPwd($pwd,$user_id);
				$this->main->message='！';
				$this->main->message_type='success';
			}
			url::redirect(BASEURI."front/report/successpwd/".base64_encode("user/editmypwd")."/true");
		}
		else{
			$this->view	=	new View("admin/user/mypwd");
			$this->main->is_validator	=	true;
			$this->main->formAction		=	"user/editmypwd";
			$this->display();
		}
	}
}

	