<?php

/**
 * Implementation of hook_permission
 */
function subpermissions_permission(){
  return array(
    'access subpermission form' => array(
      'title' => t('Access subpermission form'),
      'description' => t('Specify which roles can access the subpermission form page, and therefore update permissions for other users and themselves')
    ),
    'administer subpermission' => array(
      'title' => t('Administer subpermissions'),
      'description' => t('Select which permissions should be displayed on the subpermission form page'),
      'restrict access' => TRUE,
      'warning' => t('Users with this permission effectively have access to ALL unrestricted permissions')
    )
  );
}

/**
 * Implementation of hook_menu
 */
function subpermissions_menu(){
  return array(
    'admin/people/subpermissions' => array(
      'title' => 'Permissions',
      'description' => 'Determine access to features by selecting permissions for roles.',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'subpermissions_admin_permissions'
      ),
      'access callback' => 'subpermissions_access_subpermissions_form',
      'type' => MENU_LOCAL_TASK,
      'file' => 'subpermissions.admin.inc'
    ),
    'admin/config/people/subpermissions' => array(
      'title' => 'Subpermissions form',
      'description' => 'Select which permissions users with the "access subpermissions form" permission should be able to assign',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'subpermissions_config_form'
      ),
      'access arguments' => array(
        'administer subpermission'
      ),
      'type' => MENU_NORMAL_ITEM,
      'file' => 'subpermissions.admin.inc'
    )
  );
}

/**
 * Access callback to check whether or not we have access to the sub form (or
 * need it if we have access to the full form).
 */
function subpermissions_access_subpermissions_form(){
  if(!user_access('administer permissions') && user_access('access subpermission form') && count(variable_get('subpermissions_config', array()))){return TRUE;}
  return FALSE;
}

/**
 * Implementation of hook_theme()
 */
function subpermissions_theme(){
  return array(
    'subpermissions_config_form' => array(
      'render element' => 'form',
      'file' => 'subpermissions.admin.inc'
    )
  );
}