<?php

/**
 *--------------------------------------
 * 管理权限集
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-9
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class AdminPermissionCtrlr extends ManageCtrlr
{
	public function list_permission()
	{
		/* 筛选集合 */
		$apGroup = ARequest::get('ap_group');
		if(!empty($apGroup))
		{
			$where['__ADMIN_PERMISSION__.ap_group'] = array('EQ', $apGroup);
		}

		/* 筛选标题 */
		$apName = ARequest::get('ap_name');
		if(!empty($apName))
		{
			$where['__ADMIN_PERMISSION__.ap_name'] = array('LIKE', '%'.$apName.'%');
		}

		/* 列表排序 */
		$orderBy = ARequest::get('order_by') ? ARequest::get('order_by') : 'admin_permission_id';
		$orderTurn = ARequest::get('order_turn') ? ARequest::get('order_turn') : 'desc';
		$order = "`{$orderBy}` {$orderTurn}";

		/* 获取分页信息 */
		$pageSize = (ARequest::get('page_size') > 0 ? ARequest::get('page_size') : 10);
		$_GET[C('VAR.PAGE')] = ARequest::get(C('VAR.PAGE')) ? ARequest::get(C('VAR.PAGE')) : 1;
		$rowsNum = M('AdminPermission')->where($where)->count();
		$p = new APage($rowsNum, $pageSize, Url::U('admin_permission/list_permission?ap_group='.$apGroup
			.'&ap_name='.$apName
			.'&order_by='.$orderBy
			.'&order_turn='.$orderTurn
			.'&'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		/* 权限列表 */
		$_APL = M('AdminPermission')->where($where)->order($order)->limit($limit)->select();
		$this->assign('_APL', $_APL);

		$this->display();
	}

	public function add_permission()
	{
		$_CL = get_ctrlrList(dirname(__FILE__));
		$this->assign('_CL', $_CL);

		$this->display();
	}
	public function add_permission_do()
	{
		$data = ARequest::get();
		$result = M('AdminPermission')->add_permission($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ADMIN_PERMISSION'), 0);
			$this->error($result['error'], Url::U('admin_permission/list_permission'));
		}

		M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ADMIN_PERMISSION').': ADMIN_PERMISSION_ID['.$result['data'].']');
		$this->success(L('ADD_SUCCESS'), Url::U('admin_permission/list_permission'));
	}

	public function edit_permission()
	{
		$adminPermissionId = ARequest::get('admin_permission_id');

		$_API = M('AdminPermission')->get_permissionInfo($adminPermissionId);
		if(empty($_API))
		{
			$this->error(L('ITEM_NOT_EXSIT'), Url::U('admin_permission/list_permission'));
		}
		$_API['ap_content'] = explode(',', $_API['ap_content']);
		$this->assign('_API', $_API);

		$_CL = get_ctrlrList(dirname(__FILE__));
		$this->assign('_CL', $_CL);

		$this->display();
	}
	public function edit_permission_do()
	{
		$data = ARequest::get();
		$result = M('AdminPermission')->edit_permission($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ADMIN_PERMISSION').': ADMIN_PERMISSION_ID['.$data['admin_permission_id'].']'.$result['error'], 0);
			$this->error($result['error'], Url::U('admin_permission/list_permission'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ADMIN_PERMISSION').': ADMIN_PERMISSION_ID['.$data['admin_permission_id'].']');
		$this->success(L('EDIT_SUCCESS'), Url::U('admin_permission/list_permission'));
	}

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

		if(is_array($adminPermissionId))
		{
			$adminPermissionId = implode(',', $adminPermissionId);
		}
		$result = M('AdminPermission')->delete_permission($adminPermissionId);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ADMIN_PERMISSION').': ADMIN_PERMISSION_ID['.$adminPermissionId.']'.$result['error'], 0);
			$this->error($result['error'], Url::U('admin_permission/list_permission'));
		}

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

	/* 返回控制器的相关操作 */
	public function get_actnList()
	{
		$ctrlr = ARequest::get('ctrlr');
		$actnList = get_actnList($ctrlr);
		$this->ajax_return($actnList);
	}
}

?>