<?php

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

class AdminPermissionModl extends Modl
{
	/* $gorupSort:是否分组 */
	public function get_permissionList($groupSort = false)
	{
		$_APL = $this->select();
		if(!empty($_APL) && $groupSort)
		{
			$_t = array();
			foreach($_APL as $ap)
			{
				$_t[$ap['ap_group']][] = $ap;
			}
			$_APL = $_t;
		}
		return $_APL;
	}

	/* 返回全部权限集 */
	public function get_allPermission()
	{
		$allPermission = '';
		$_t = $this->select();
		if(!empty($_t))
		{
			foreach($_t as $ap)
			{
				$allPermission .= $ap['ap_content'].',';
			}
		}
		$allPermission = rtrim($allPermission, ',');
		return $allPermission;
	}

	public function get_permissionInfo($adminPermissionId)
	{
		$_API = $this->where('admin_permission_id = \''.$adminPermissionId.'\'')->find();
		return $_API;
	}

	public function add_permission($data)
	{
		$result = array('data' => '', 'error' => '');

		$data['permission'] = array_unique($data['permission']);
		sort($data['permission']);
		$data['ap_content'] = implode(',', $data['permission']);

		unset($data['permission']);
		$_t_id = $this->insert($data);
		if(false === $_t_id)
		{
			$result['error'] = L('ADD_FAILED');
			return $result;
		}
		$result['data'] = $_t_id;

		return $result;
	}

	public function edit_permission($data)
	{
		$result = array('data' => '', 'error' => '');

		$data['permission'] = array_unique($data['permission']);
		sort($data['permission']);
		$data['ap_content'] = implode(',', $data['permission']);
		unset($data['permission']);
		if(false === $this->update($data))
		{
			$result['error'] = L('EDIT_FAILED');
			return $result;
		}

		return $result;
	}

	public function delete_permission($adminPermissionId)
	{
		$result = array('data' => '', 'error' => '');

		if(false === $this->delete($adminPermissionId))
		{
			$result['error'] = L('DELETE_FAILED');
			return $result;
		}

		return $result;
	}
}

?>