<?php
// +----------------------------------------------------------------------
// | Sunnyi 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.sunnyi.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 商易网络(admin@sunnyi.cn)
// +----------------------------------------------------------------------

// 管理员角色
class RoleAction extends CommonAction{
	public function edit()
	{
		$role_id = intval($_REQUEST['id']);		
		$node_ids_res = D("RoleAccess")->where("role_id=".$role_id)->field("node_id")->findAll();	
			
		$node_ids = array();
		foreach($node_ids_res as $row)
		{
			array_push($node_ids,$row['node_id']);	
		}
		
		$nodes = D("RoleNode")->where("status=1")->findAll();
		foreach($nodes as $k=>$row)
		{
			if(in_array($row['id'],$node_ids))
			{
				$row['checked'] = true;
			}
			else 
			{
				$row['checked'] = false;
			}
			if($row['auth_type']!=0)
				$access_list[$row['auth_type']][] = $row;
			else 
			{
				$access_list[$row['auth_type']][$row['module']][] = $row;
			}
		}
		$this->assign('access_list',$access_list);
		parent::edit();
	}
	
	public function update()
	{
		 $role_id = intval($_REQUEST['id']);
		//B('FilterString');
			$name=$this->getActionName();
			$model = D ( $name );
			if (false === $model->create ()) {
				$this->error ( $model->getError () );
			}
			// 更新数据
			$list=$model->save ();
			if (false !== $list) {
				//成功提示
				D("RoleAccess")->where("role_id=".$role_id)->delete();
				$node_ids = $_REQUEST['access_node'];
				foreach($node_ids as $node_id)
				{
					$data['role_id'] = $role_id;
					$data['node_id'] = $node_id;
					D("RoleAccess")->add($data);
				}
				$this->saveLog(1);
	//			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
				$this->success (L('EDIT_SUCCESS'));
			} else {
				//错误提示
				$this->saveLog(0);
				$this->error (L('EDIT_FAILED'));
			}
	}
	
	public function add()
	{
		$nodes = D("RoleNode")->where("status=1")->findAll();
		foreach($nodes as $k=>$row)
		{
			if($row['auth_type']!=0)
				$access_list[$row['auth_type']][] = $row;
			else 
			{
				$access_list[$row['auth_type']][$row['module']][] = $row;
			}
		}
		$this->assign('access_list',$access_list);
		parent::add();
	}
	public function insert()
	{
	//B('FilterString');
		$name=$this->getActionName();
		$model = D ($name);
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		//保存当前数据对象
		$list=$model->add ();
		if ($list!==false) { //保存成功
				$node_ids = $_REQUEST['access_node'];
				foreach($node_ids as $node_id)
				{
					$data['role_id'] = $list;
					$data['node_id'] = $node_id;
					D("RoleAccess")->add($data);
				}
			$this->saveLog(1,$list);
//			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success (L('ADD_SUCCESS'));
		} else {
			//失败提示
			$this->saveLog(0,$list);
			$this->error (L('ADD_FAILED'));
		}
	}
	public function foreverdelete() {
		//删除指定记录
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$pk = $model->getPk ();
			$id = $_REQUEST [$pk];
			if (isset ( $id )) {
				if(D("Admin")->where(array ("role_id" => array ('in', explode ( ',', $id ) ) ))->count()>0)
				{
					$this->saveLog(0);
					$this->error (L('ADM_EXIST_IN_ROLE'));
				}
				$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
				if (false !== $model->where ( $condition )->delete ()) {
					//echo $model->getlastsql();
					//删除相关角色的权限
					D("RoleAccess")->where(array ("role_id" => array ('in', explode ( ',', $id ) ) ))->delete();
					$this->saveLog(1);
					$this->success (L('DEL_SUCCESS'));
				} else {
					$this->saveLog(0);
					$this->error (L('DEL_FAILED'));
				}
			} else {
				$this->saveLog(0);
				$this->error ( L('INVALID_OP'));
			}
		}
		$this->forward ();
	}
}
?>