<?php
// +----------------------------------------------------------------------
// | WBlog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.w3note.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 网菠萝果
// +----------------------------------------------------------------------
// $Id$
class RoleAction extends CommonAction {
    
	 public function index(){
        $this->lists();
        $this->display();
		
	   }
	   public function setrole(){
		if(!$_GET['user_id']) $this->error("该用户不存在或已被删除!");
	    $user_id =$_GET['user_id'];
		$Role_user = M('Role_user');
		$condition['user_id']=$user_id;
		$Role_userlist = $Role_user->where($condition)->select();
		if(!false==$Role_userlist){
		$role_ids=array();
		foreach ($Role_userlist as $k => $v) {
			$role_ids[]=$v['role_id'];
			}		
		}
	    $Role = M('Role');
		$Rolelist = $Role->order('id desc')->select(); 	
		foreach ($Rolelist as $key => $value) {
			$Rolelist[$key]['checked']=in_array($value['id'],$role_ids) ? 1 :0;
			}	
			//print_r($Nodelist);
        $this->assign('rolelist', $Rolelist);
		$this->assign('user_id', $user_id);
		
       $this->display();
		
	   }
	   public function saverole(){ 
	      if(!$_POST['user_id']) $this->error("该用户不存在或已被删除!");
	      $user_id = $_POST['user_id']; 
		  $role_ids = $_POST['id']; 
		   //echo  $role_id;
		   //print_r($node_id);
		   $Role_user=M('Role_user');
		   $condition['user_id']=$user_id;
		   $delets=$Role_user->where($condition)->delete(); //$delets是返回被删除的记录数
		  
		   
	      foreach($role_ids as $v){
				$data['user_id'] = $user_id;
				$data['role_id'] = $v;
				$result=$Role_user->add($data);
			}
			if(false !== $result){
			   $this->success("操作成功！");
			}else{
				$this->error("操作失败！");
				}
    }
	  public function add(){
	     // 创建数据对象
		$Role	 =	 D('Role');
		if(!$Role->create()) {
			$this->error($Role->getError());
		}else{
			// 写入帐号数据
			if($result	 =	 $Role->add()) {
				//$this->addRole($result);
				$this->success('用户添加成功！');
			}else{
				$this->error('用户添加失败！');
			}
		}
		
	}
	public function insert() {
		$Role = M("Role");
		$Rolelist=$Role->field('id,name')->select();
		$this->assign("rolelist", $Rolelist);
		$this->display();
		
	}
	function edit() {
                    
         $Role = M("Role");              
           $id = $_GET['id'];            
		if(!$id) $this->error("编辑项不存在或已删除!");			   
		 $Rolelist=$Role->field('id,name')->select();	  			               
         $vo = $Role->getById($id);              
		 $this->assign("rolelist", $Rolelist);			   
			$this->assign('vo', $vo);		 
                       
         $this->display();              
        }
	public  function update() {
           $Role = D("Role");
            if (false === $Role->create()) {
                    $this->error($Role->getError());
              }                  
                    $list = $Role->save();
              if (false !== $list) {                    
                    $this->success('修改成功!');
              } else {                      
                       $this->error('修改失败!');
              }
       }
                
	public function del(){   
		$condition['id'] = $_REQUEST['id'];
		$Role = M('Role');
		if($Role->where($condition)->delete()){
			$this->success("删除成功！");
		}else{
		$this->error("删除失败!");
		}
    }                    
}