<?php

/**
 *--------------------------------------
 * 管理员角色
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-9
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class AdminRoleCtrlr extends ManageCtrlr
{
	public function list_role()
	{
		$_ARL = M('AdminRole')->get_roleList();
		$this->assign('_ARL', $_ARL);
		$this->display();
	}

	public function add_role()
	{
		/* 获取权限集 */
		$_APL = M('AdminPermission')->get_permissionList(true);
		$this->assign('_APL', $_APL);

		$this->display();

	}
	public function add_role_do()
	{
		$data = ARequest::get();
		if(empty($data['admin_permission_id']) && empty($data['ar_permission']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ADMIN_ROLE').': '.L('PERMISSION_NULL'), 0);
			$this->error(L('PERMISSION_NULL'), Url::U('admin_role/list_role'));
		}

		$result = M('AdminRole')->add_role($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ADMIN_ROLE').': '.$result['error'], 0);
			$this->error($result['error'], Url::U('admin_role/list_role'));
		}

		M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ADMIN_ROLE').': ADMIN_ROLE_ID['.$result['data'].']');
		$this->success(L('ADD_SUCCESS'), Url::U('admin_role/list_role'));
	}

	public function edit_role()
	{
		$adminRoleId = ARequest::get('admin_role_id');
		$_ARI = M('AdminRole')->get_roleInfo($adminRoleId);
		if(empty($_ARI))
		{
			$this->error(L('ITEM_NOT_EXSIT'), Url::U('admin_role/list_role'));
		}
		$this->assign('_ARI', $_ARI);
		if(0 == $_ARI['ar_type'])
		{
			if(-1 == $_ARI['ar_rank'])
			{
				$this->error(L('SUPER_ADMIN_IS_LOCKED'), Url::U('admin_role/list_role'));
			}
			$this->error(L('SYSTEM_ROLE_IS_LOCKED'), Url::U('admin_role/list_role'));
		}

		/* 获取权限集 */
		$_APL = M('AdminPermission')->get_permissionList(true);
		$this->assign('_APL', $_APL);

		$this->display();
	}
	public function edit_role_do()
	{
		$data = ARequest::get();
		if(empty($data['admin_permission_id']) && empty($data['ar_permission']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ADMIN_ROLE').': '.L('PERMISSION_NULL'), 0);
			$this->error(L('PERMISSION_NULL'), Url::U('admin_role/list_role'));
		}

		$result = M('AdminRole')->edit_role($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ADMIN_ROLE').': ADMIN_ROLE_ID['.$data['admin_role_id'].']'.$result['error'], 0);
			$this->error($result['error'], Url::U('admin_role/list_role'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ADMIN_ROLE').': ADMIN_ROLE_ID['.$data['admin_role_id'].']');
		$this->success(L('EDIT_SUCCESS'), Url::U('admin_role/list_role'));
	}

	public function delete_role_do()
	{
		$adminRoleId = ARequest::get('admin_role_id');
		$_L_ID = is_array($adminRoleId) ? implode(',', $adminRoleId) : $adminRoleId; /* 日志记录ID */

		if(is_array($adminRoleId))
		{
			$adminRoleId = implode(',', $adminRoleId);
		}
		$result = M('AdminRole')->delete_role($adminRoleId);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ADMIN_ROLE').': ADMIN_ROLE_ID['.$adminRoleId.']'.$result['error'], 0);
			$this->error($result['error'], Url::U('admin_role/list_role'));
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ADMIN_ROLE').': ADMIN_ROLE_ID['.$_L_ID.']');
		$this->success(L('DELETE_SUCCESS'), Url::U('admin_role/list_role'));
	}
}


?>