<?php

/**
 * Implementation of hook_permission()
 */
function custom_menu_perms_permission()
{
	$permissions = array
	(
		'administer_custom_menu_perms' => array
		(
			'title' => t('Administer custom menu permissions'),
			'description' => t('Allows administrators to apply custom permissions to be used for menu items'),
		),
		'create_custom_perms' => array
		(
			'title' => t('Create custom permissions'),
			'description' => t('Allows administrators to create custom permissions that can be used anywhere on the site'),
		),
	);

	$custom_perms = db_query
	(
		'SELECT perm_name, perm_key, description ' .
		'FROM {cmp_permissions} '
	);

	foreach($custom_perms as $permission)
	{
		$permissions[$permission->perm_key] = array
		(
			'title' => $permission->perm_name,
			'description' => $permission->description,
		);
	}

	return $permissions;
}

/**
 * Implementation of hook_menu()
 */
function custom_menu_perms_menu()
{
	$menu['admin/config/system/custom_menu_perms/add_permission'] = array
	(
		'title' => 'Add new permission',
		'page callback' => 'custom_menu_perms_add_perm',
		'access arguments' => array('create_custom_perms'),
		'file' => 'includes/custom_menu_perms.pages.inc',
		'type' => MENU_CALLBACK,
	);
	$menu['admin/config/system/custom_menu_perms/edit_permission/%custom_menu_perms_perm'] = array
	(
		'title' => 'Edit permission',
		'page callback' => 'custom_menu_perms_perm_edit',
		'page arguments' => array(5),
		'access arguments' => array('create_custom_perms'),
		'file' => 'includes/custom_menu_perms.pages.inc',
		'type' => MENU_CALLBACK,
	);
	$menu['admin/config/system/custom_menu_perms'] = array
	(
		'title' => 'Custom menu permissions',
		'description' => 'Allows users to apply custom permissions to menu items that work with user_access(), overriding the existing permissions',
		'page callback' => 'drupal_get_form',
		'page arguments' => array('custom_menu_perms_alter_menu_perms_form'),
		'access arguments' => array('administer_custom_menu_perms'),
		'file' => 'includes/custom_menu_perms.pages.inc',
	);
	$menu['admin/config/system/custom_menu_perms/menu_permissions'] = array
	(
		'title' => 'Alter menu permissions',
		'type' => MENU_DEFAULT_LOCAL_TASK,
	);
	$menu['admin/config/system/custom_menu_perms/permissions'] = array
	(
		'title' => 'Custom permissions list',
		'page callback' => 'custom_menu_perms_permission_list',
		'access arguments' => array('create_custom_perms'),
		'file' => 'includes/custom_menu_perms.pages.inc',
		'type' => MENU_LOCAL_TASK,
	);

	return $menu;
}

function custom_menu_perms_menu_alter(&$menu)
{
	$custom_menu_perms = db_query
	(
		'SELECT menu_path, cmp_permission_key FROM {cmp_menu_perms}'
	);
	foreach($custom_menu_perms as $perm)
	{
		if(isset($menu[$perm->menu_path]))
		{
			$menu[$perm->menu_path]['access callback'] = 'user_access';
			$menu[$perm->menu_path]['access arguments'] = array($perm->cmp_permission_key);
		}
	}
}

function custom_menu_perm_perm_exists($permission_key)
{
	$permissions = module_invoke_all('permission');

	return isset($permissions[$permission_key]);
}

function custom_menu_perms_perm_load($permission_key)
{
	$permission = db_query_range
	(
		'SELECT perm_name, perm_key, description ' .
		'FROM {cmp_permissions} ' .
		'WHERE perm_key = :perm_key',
		0,1,
		array
		(
			':perm_key' => $permission_key,
		)
	)->fetchObject();

	return $permission;
}

function custom_menu_perms_theme($existing, $type, $theme, $path)
{
	return array
	(
		'custom_menu_perms_perm_table' => array
		(
			'render element' => 'form',
			'path' => $path . '/includes/custom_menu_perms.pages.inc',
		),
	);
}
