You are here

fpa.module in Fast Permissions Administration 6.2

Same filename and directory in other branches
  1. 8.2 fpa.module
  2. 7.2 fpa.module
  3. 3.0.x fpa.module

File

fpa.module
View source
<?php

function fpa_menu() {
  $items = array();
  $items['fpa_modal/%fpa_js/permissions'] = array(
    'title' => 'Permissions',
    'page callback' => '_fpa_ctools',
    'page arguments' => array(
      1,
    ),
    'access arguments' => array(
      'administer permissions',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}
function fpa_js_load($arg) {
  if (module_exists('ctools')) {
    return ctools_js_load($arg);
  }
  return 0;
}
function fpa_l($permission = '', $text = NULL, $options = array()) {

  // available modal modules in order of priority
  $supported_modules = array(
    //'dialog', // dialog api is buggy
    'ctools',
    'modalframe',
  );
  if (is_null($text)) {
    $text = t('Manage Permissions');
  }

  // gets the most prefered modal method from the available methods
  $modules = array_intersect($supported_modules, array_keys(module_list()));
  $method = array_shift($modules);
  $path = 'fpa_modal/nojs/permissions';
  $attributes = array();
  $query = array(
    'fpa_perm' => $permission,
    'fpa_method' => $method,
  );
  switch ($method) {
    case 'dialog':
      dialog_add_js();
      $attributes['class'] = 'ctools-use-dialog';
      break;
    case 'ctools':
      ctools_include('ajax');
      ctools_include('modal');
      ctools_modal_add_js();
      $attributes['class'] = 'ctools-use-modal';
      break;
    case 'modalframe':
      modalframe_parent_js();
      drupal_add_js(drupal_get_path('module', 'fpa') . '/js/fpa.js', 'module');
      $attributes['class'] = 'fpa_modalframe';
      break;
    default:
      $path = 'admin/user/permissions';
      $attributes['target'] = '_blank';
      $query['fpa_method'] = '_blank';
      break;
  }
  $options = array_merge_recursive(array(
    'query' => $query,
    'attributes' => $attributes,
  ), $options);
  return l($text, $path, $options);
}
function fpa_fieldset($permission, &$parent_item) {
  $parent_item['fpa_fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Permissions'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#description' => t('Clicking on this link will launch a modal/tab/window displaying the appropriate permissions.'),
  );
  $parent_item['fpa_fieldset']['fpa'] = array(
    '#type' => 'markup',
    '#value' => fpa_l($permission),
  );
}
function fpa_form_user_admin_perm_alter(&$form, &$form_state) {
  if ($_GET['fpa_method'] == 'modalframe') {
    modalframe_child_js();
    $form['#submit'][] = '_fpa_modalframe_submit';
  }
  $fpa_module_path = drupal_get_path('module', 'fpa');
  drupal_add_css($fpa_module_path . '/css/fpa.css', 'module');
  drupal_add_js($fpa_module_path . '/js/fpa.js', 'module');
  drupal_add_js(array(
    'fpa' => array(
      'perm' => $_GET['fpa_perm'] ? $_GET['fpa_perm'] : '',
    ),
  ), 'setting');
}
function fpa_form_content_field_edit_form_alter(&$form, &$form_state) {
  if (user_access('administer permissions') && (module_exists('content_permissions') || module_exists('field_permissions'))) {
    fpa_fieldset($form['#field']['field_name'], $form['field']);
  }
}
function fpa_form_node_type_form_alter(&$form, &$form_state) {
  if ((!empty($form['type']['#default_value']) || !empty($form['old_type']['#value'])) && user_access('administer permissions')) {
    fpa_fieldset($form['#node_type']->type . ' content', $form);
  }
}
function _fpa_modalframe_submit() {
  modalframe_close_dialog();
}
function _fpa_ctools($js = NULL) {

  // Need to include the file that contains the permissions form.
  module_load_include('inc', 'user', 'user.admin');

  // Fall back if $js is not set.
  if (!$js) {
    return drupal_get_form('user_admin_perm');
  }
  ctools_include('modal');
  ctools_include('ajax');
  $form_state = array(
    'title' => t('Permissions'),
    'ajax' => TRUE,
  );
  switch ($_GET['fpa_method']) {
    case 'dialog':
      $output = dialog_get_form('user_admin_perm', $form_state);
      break;
    case 'ctools':
      $output = ctools_modal_form_wrapper('user_admin_perm', $form_state);
      break;
  }
  if (empty($output)) {
    $output = array(
      ctools_ajax_command_reload(),
    );
  }
  ctools_ajax_render($output);
}