Browse Source

updated Fast permission admin module to last dev

Bachir Soussi Chiadmi 7 years ago
parent
commit
fedd8f4216

+ 3 - 0
sites/all/modules/contrib/admin/fpa/.gitignore

@@ -0,0 +1,3 @@
+.project
+.settings
+/.idea

File diff suppressed because it is too large
+ 0 - 3
sites/all/modules/contrib/admin/fpa/css/fpa.css


File diff suppressed because it is too large
+ 361 - 0
sites/all/modules/contrib/admin/fpa/css/fpa.css.less


+ 66 - 0
sites/all/modules/contrib/admin/fpa/fpa.form_alter.inc

@@ -0,0 +1,66 @@
+<?php
+
+/**
+ * @file
+ * Contains form alter hooks to inject FPA functionality.
+ */
+
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function fpa_form_node_type_form_alter(&$form, &$form_state) {
+  if (!empty($form['type']['#default_value']) && user_access('administer permissions')) {
+    fpa_fieldset($form['#node_type']->type . ' content', $form, array('#group' => 'additional_settings'));
+  }
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function fpa_form_user_admin_permissions_alter(&$form, &$form_state) {
+  
+  // Prevent duplication of memory limit warning.
+  if (module_exists('filter_perms') && empty($form_state['input'])) {
+    
+    $memory_limit = ini_get('memory_limit');
+    
+    if (!drupal_check_memory_limit(_fpa_memory_required() . 'b', $memory_limit)) {
+      drupal_set_message(t('If you attempt to display all roles and permissions on this page at the same time, you will most likely exceed your PHP memory limit of %memory_limit.', array('%memory_limit' => $memory_limit)), 'warning');
+    }
+  }
+    
+  $form['#theme'] = array('fpa_user_admin_permissions');
+  
+  $fpa_module_path = drupal_get_path('module', 'fpa');
+  
+  $form['#attached']['library'][] = array('system', 'jquery.cookie');
+
+  $form['#attached']['css'][] = $fpa_module_path . '/css/fpa.css';
+  $form['#attached']['js'][] = $fpa_module_path . '/js/fpa.min.js';
+  $form['#attached']['js'][] = array(
+    'type' => 'setting',
+    'data' => array(
+      'fpa' => array(
+        'attr' => array(
+          'permission' =>  FPA_ATTR_PERMISSION,
+          'module' =>      FPA_ATTR_MODULE,
+          'role' =>        FPA_ATTR_ROLE,
+          
+          'checked' =>     FPA_ATTR_CHECKED,
+          'not_checked' => FPA_ATTR_NOT_CHECKED,
+
+          'system_name' => FPA_ATTR_SYSTEM_NAME,
+        ),
+      ),
+    ),
+  );
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function fpa_form_filter_perms_admin_perm_filter_alter(&$form, &$form_state) {
+  
+  $form['#submit'][] = '_fpa_reset_filter_defaults';
+}

+ 4 - 4
sites/all/modules/contrib/admin/fpa/fpa.info

@@ -1,10 +1,10 @@
 name = "Fast Permissions Administration"
-description = "Provides quick access to a content type's or field's permissions."
+description = "Fast filtering on permissions administration form."
 core = 7.x
 package = Administration
-; Information added by drupal.org packaging script on 2012-04-22
-version = "7.x-2.2"
+; Information added by Drupal.org packaging script on 2015-05-21
+version = "7.x-2.6+2-dev"
 core = "7.x"
 project = "fpa"
-datestamp = "1335136578"
+datestamp = "1432171983"
 

+ 46 - 0
sites/all/modules/contrib/admin/fpa/fpa.install

@@ -0,0 +1,46 @@
+<?php
+
+/**
+ * @file
+ * Install, update, and uninstall functions for the Fast Permissions Administration module.
+ */
+
+/**
+ * Implements hook_requirements().
+ */
+function fpa_requirements($phase) {
+  $requirements = array();
+  
+  if ($phase == 'runtime') {
+    
+    $permissions_memory_required = _fpa_memory_required();
+    
+    if (!drupal_check_memory_limit($permissions_memory_required . 'b')) {
+      
+      $requirements['fpa_memory'] = array(
+        'severity' => REQUIREMENT_WARNING,
+        'title' => t('Insufficient memory for permissions page'),
+        'value' => t('~@permissions_memory_requiredM of memory required', array('@permissions_memory_required' => (int) ($permissions_memory_required / 1024 / 1024))),
+        'description' => t('It is likely that you will exceed your memory limit when viewing the permissions page for all roles and permissions. If you are unable to load the permissions page, this is most likely the cause.') . '<br />',
+      );
+      
+      $filter_perms_link = l('Filter Permissions', 'https://drupal.org/project/filter_perms');
+        
+      if (!module_exists('filter_perms')) {
+        
+        $requirements['fpa_memory']['description'] .= t('The !filter_perms_link module can work with Fast Permissions Administration by reducing the amount of the permissions form that is rendered and thereby reducing the memory required on the permissions page.', array('!filter_perms_link' => $filter_perms_link)) . '<br />';
+        
+        if ($php_ini_path = get_cfg_var('cfg_file_path')) {
+           $requirements['fpa_memory']['description'] .= t('Increase the memory limit by editing the memory_limit parameter in the file %configuration-file and then restart your web server (or contact your system administrator or hosting provider for assistance).', array('%configuration-file' => $php_ini_path)) . '<br />';
+        }
+      }
+      else {
+        
+        $requirements['fpa_memory']['severity'] = REQUIREMENT_OK;
+        $requirements['fpa_memory']['description'] = t('The !filter_perms_link module installed should prevent a memory issue as long as viewed permissions and roles are limited.', array('!filter_perms_link' => $filter_perms_link));
+      }
+    }
+  }
+  
+  return $requirements;
+}

+ 101 - 46
sites/all/modules/contrib/admin/fpa/fpa.module

@@ -1,5 +1,26 @@
 <?php
 
+/**
+ * @file
+ * Main module file for FPA.
+ */
+
+define('FPA_ATTR_PERMISSION',  'fpa-permission');
+define('FPA_ATTR_MODULE',      'fpa-module');
+define('FPA_ATTR_ROLE',        'fpa-role');
+
+define('FPA_ATTR_CHECKED',     'fpa-checked');
+define('FPA_ATTR_NOT_CHECKED', 'fpa-not-checked');
+
+define('FPA_ATTR_SYSTEM_NAME', 'fpa-system-name');
+
+// Replacement for improper use of hook_hook_info().
+require_once dirname(__FILE__) . '/fpa.theme.inc';
+require_once dirname(__FILE__) . '/fpa.form_alter.inc';
+
+/**
+ * Implements hook_menu().
+ */
 function fpa_menu() {
   $items = array();
   
@@ -7,63 +28,82 @@ function fpa_menu() {
       'title' => 'Permissions',
       'page callback' => '_fpa_ctools',
       'page arguments' => array(1),
-      'access callback' => TRUE,
+      'access arguments' => array('administer permissions'),
       'type' => MENU_CALLBACK,
   );
   
   return $items;
 }
 
+/**
+ * Implements hook_admin_paths().
+ */
 function fpa_admin_paths() {
   return array(
-    'fpa_modal/*/permissions' => FALSE,
+    'fpa_modal/*/permissions' => TRUE,
   );
 }
 
+/**
+ * Implements hook_help().
+ */
+function fpa_help($path, $arg) {
+  if ($path == 'admin/people/permissions') {
+    
+    $output = '';
+    
+    $output .= '<p>' . t('Permissions and Module names will match on the readable or system name. The system name is provided as a togglable column.') . '</p>';
+    
+    $output .= '<p>' . t('Enter in the format of "permission@module", e.g. "admin@system" will show only permissions with the text "admin" in modules with the text "system".') . '</p>';
+    
+    return $output;
+  }
+}
+
+/**
+ * Loader function to wrap ctools loader function.
+ */
 function fpa_js_load($arg) {
+  $return_code = 0;
+  
   if (module_exists('ctools')) {
-    return ctools_js_load($arg);
+    $return_code = ctools_js_load($arg);
   }
-  return 0;
+  
+  return $return_code;
 }
 
 function fpa_l($permission = '', $text = NULL, $options = array()) {
   
-  // available modal modules in order of priority
+  // Available modal module system names 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
+  // Find the most preferred modal method from the available methods.
   $modules = array_intersect($supported_modules, array_keys(module_list()));
   $method = array_shift($modules);
   
   $path = 'fpa_modal/nojs/permissions';
+  
+  $query = array(
+    'fpa_perm' => $permission,
+    'fpa_method' => $method,
+  );
+  
   $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');
-      $attributes['class'] = 'fpa_modalframe';
-      break;
     default:
       $path = 'admin/people/permissions';
       $attributes['target'] = '_blank';
@@ -76,7 +116,7 @@ function fpa_l($permission = '', $text = NULL, $options = array()) {
   return l($text, $path, $options);
 }
 
-function fpa_fieldset($permission, &$parent_item, $group = array()) {
+function fpa_fieldset($filter_string, &$parent_item, $group = array()) {
   
   $parent_item['fpa_fieldset'] = array(
     '#type' => 'fieldset',
@@ -90,39 +130,23 @@ function fpa_fieldset($permission, &$parent_item, $group = array()) {
   
   $parent_item['fpa_fieldset']['fpa'] = array(
     '#type' => 'markup',
-    '#markup' => fpa_l($permission),
+    '#markup' => fpa_l($filter_string),
   );
   
 }
 
-function fpa_form_node_type_form_alter(&$form, &$form_state) {
-  if (!empty($form['type']['#default_value']) && user_access('administer permissions')) {
-    fpa_fieldset($form['#node_type']->type . ':', $form, array('#group' => 'additional_settings'));
-  }
-}
-
-function fpa_form_user_admin_permissions_alter(&$form, &$form_state) {
-  if (isset($_GET['fpa_method']) && $_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');
-  drupal_add_js($fpa_module_path .'/js/fpa.js');
-  drupal_add_js(array('fpa' => array('perm' => isset($_GET['fpa_perm']) && $_GET['fpa_perm'] ? check_plain($_GET['fpa_perm']) : '')), array('type' => 'setting'));
-}
-
-function _fpa_modalframe_submit() {
-  modalframe_close_dialog();
-}
-
+/**
+ * Page callback for CTools modal path.
+ */
 function _fpa_ctools($js = NULL) {
   // Need to include the file that contains the permissions form.
   module_load_include('inc', 'user', 'user.admin');
   
+  $form_id = 'user_admin_permissions';
+  
   // Fall back if $js is not set.
   if (!$js) {
-    return drupal_get_form('user_admin_permissions');
+    return drupal_get_form($form_id);
   }
 
   ctools_include('modal');
@@ -133,11 +157,8 @@ function _fpa_ctools($js = NULL) {
   );
   
   switch ($_GET['fpa_method']) {
-    case 'dialog':
-      $output = dialog_get_form('user_admin_permissions', $form_state);
-      break;
     case 'ctools':
-      $output = ctools_modal_form_wrapper('user_admin_permissions', $form_state);
+      $output = ctools_modal_form_wrapper($form_id, $form_state);
       break;
   }
   
@@ -149,3 +170,37 @@ function _fpa_ctools($js = NULL) {
   echo ajax_render($output);
   exit;
 }
+
+/**
+ * @return int Approximate number of bytes of ram required to render the permissions form.
+ */
+function _fpa_memory_required() {
+  
+  $permissions_count = count(module_invoke_all('permission'));
+  
+  $user_roles_count = count(user_roles());
+  
+  
+  $page_ram_required = (9 * 1024 * 1024);
+  
+  
+  // Takes ~26kb per row without any checkboxes.
+  $permission_row_overhead = 27261.028783658;
+  
+  $permissions_ram_required = $permissions_count * $permission_row_overhead;
+  
+  
+  // Determined by checking peak ram on permissions page, over several different number of visible roles.
+  $bytes_per_checkbox = 18924.508820799;
+  
+  $checkboxes_ram_required = $permissions_count * $user_roles_count * $bytes_per_checkbox;
+  
+  
+  return (int) ($page_ram_required + $permissions_ram_required + $checkboxes_ram_required);
+}
+
+function _fpa_reset_filter_defaults() {
+  setcookie("fpa_module_match", "", time() - 3600, '/');
+  setcookie("fpa_filter",       "", time() - 3600, '/');
+  setcookie("fpa_roles",        "", time() - 3600, '/');
+}

+ 769 - 0
sites/all/modules/contrib/admin/fpa/fpa.theme.inc

@@ -0,0 +1,769 @@
+<?php
+
+/**
+ * @file
+ * Theme callbacks to pre-tag rows for FPA functionality.
+ */
+
+/**
+ * Implements hook_theme().
+ */
+function fpa_theme($existing, $type, $theme, $path) {
+  return array(
+    'fpa_user_admin_permissions' => array(
+      'render element' => 'form',
+      'file' => 'fpa.theme.inc',
+    ),
+  );
+}
+
+/**
+ * Theme function to pre-mark rows with FPA attributes.
+ * 
+ * Based on Drupal Core's permissions form theme function.
+ * 
+ * @see theme_user_admin_permissions().
+ */
+function theme_fpa_user_admin_permissions($variables) {
+  $form = $variables['form'];
+  
+  $nameless_checkbox = array(
+    '#type' => 'html_tag',
+    '#tag' => 'input',
+    '#attributes' => array(
+      'type' => 'checkbox',
+      'class' => array(
+        'rid-1', // Prevents Drupal core Drupal.behaviors.permissions.toggle from applying.
+        'form-checkbox',
+        'fpa-checkboxes-toggle',
+      ),
+    ),
+  );
+  
+  $nameless_checkbox_output = drupal_render($nameless_checkbox);
+  
+  
+  $dummy_checkbox = array(
+    '#type' => 'html_tag',
+    '#tag' => 'input',
+    '#attributes' => array(
+      'type' => 'checkbox',
+      'disabled' => 'disabled',
+      'checked' => 'checked',
+      'title' => t('This permission is inherited from the authenticated user role.'),
+      'class' => array(
+        'dummy-checkbox',
+      ),
+    ),
+  );
+  
+  $dummy_checkbox_output = drupal_render($dummy_checkbox);
+  
+  
+  $permission_col_template = array(
+    '#type' => 'container',
+    '#attributes' => array(
+      'class' => array(
+        'fpa-permission-container',
+      ),
+    ),
+    'description' => array(
+      '#markup' => '',
+    ),
+    'checkbox_cell' => array(
+      '#type' => 'container',
+      '#attributes' => array(
+        'class' => array(
+          'fpa-row-toggle-container',
+        ),
+      ),
+      'checkbox_form_item' => array(
+        '#type' => 'container',
+        '#attributes' => array(
+          'title' => t('Toggle visible checkboxes in this row.'),
+          'class' => array(
+            'form-item',
+            'form-type-checkbox',
+          ),
+        ),
+        'label' => array(
+          '#type' => 'html_tag',
+          '#tag' => 'label',
+          '#attributes' => array(
+            'class' => array(
+              'element-invisible',
+            ),
+          ),
+          '#value' => 'test',
+        ),
+        'checkbox' => array(
+          '#markup' => $nameless_checkbox_output,
+        ),
+      ),
+    ),
+  );
+  
+  $checkboxes_children = element_children($form['checkboxes']);
+  
+  // Prepare role names processed by drupal_html_class() ahead of time.
+  $roles_attr_values = array();
+  
+  foreach ($checkboxes_children as $rid) {
+    $roles_attr_values[$rid] = drupal_html_class($form['role_names'][$rid]['#markup']);
+  }
+  
+  $first_role_index = array_shift($checkboxes_children);
+  
+  // Lists for wrapper.
+  $modules = array();
+  $user_roles = array();
+  
+  // Index of current module row.
+  $module = NULL;
+  
+  // Row counter.
+  $i = 0;
+  
+  $rows = array();
+
+  // Iterate over rows in form table.
+  foreach (element_children($form['permission']) as $key) {
+    
+    // Row template.
+    $row = array(
+      'data' => array(), // Array of table cells.
+      'title' => array(), // HTML attribute on table row tag.
+      FPA_ATTR_MODULE => array(), // HTML attribute on table row tag.
+      FPA_ATTR_PERMISSION => array(), // HTML attribute on table row tag.
+      FPA_ATTR_CHECKED => array(),
+      FPA_ATTR_NOT_CHECKED => array(),
+    );
+    
+    // Determine if row is module or permission.
+    if (is_numeric($key)) {
+      // Module row.
+      
+      $row['class'][] = 'fpa-module-row';
+      
+      // Mark current row with escaped module name.
+      $row[FPA_ATTR_MODULE] = array(
+        // System name
+        0 => $form['permission'][$key]['#id'],
+        // Readable name
+        1 => strip_tags($form['permission'][$key]['#markup']),
+      );
+      
+      // Readable
+      $row['data'][] = array(
+        'data' => drupal_render($form['permission'][$key]),
+        'class' => array('module'),
+        'id' => 'module-' . $form['permission'][$key]['#id'],
+        'colspan' => count($form['role_names']['#value']) + 1,
+      );
+      
+      $row['title'] = array($form['permission'][$key]['#id']);
+      
+      $row[FPA_ATTR_SYSTEM_NAME] = $row[FPA_ATTR_MODULE][0];
+      
+      $row[FPA_ATTR_MODULE] = array_unique(array_map('drupal_html_class', $row[FPA_ATTR_MODULE]));
+      
+      // Add modules to left-side modules list.
+      $modules[$row[FPA_ATTR_MODULE][0]] = array(
+        'text' => strip_tags($form['permission'][$key]['#markup']),
+        'title' => array($form['permission'][$key]['#id']),
+        FPA_ATTR_MODULE => $row[FPA_ATTR_MODULE],
+        FPA_ATTR_PERMISSION => array(),
+      );
+      
+      // Save row number for current module.
+      $module = $i;
+    }
+    else {
+      // Permission row.
+      
+      $row['class'][] = 'fpa-permission-row';
+      
+      $permission_system_name = '';
+      
+      // Might be empty if no modules are displayed in Permissions Filter module.
+      if (!empty($form['checkboxes'][$first_role_index])) {
+        $permission_system_name = $form['checkboxes'][$first_role_index][$key]['#return_value'];
+      }
+      
+      $label = $permission_col_template;
+      
+      $label['description']['#markup'] = drupal_render($form['permission'][$key]);
+      
+      // Permissions filter might cause no Roles to display.
+      if (count(element_children($form['checkboxes'])) == 0) {
+        unset($label['checkbox_cell']);
+      }
+      
+      // Readable
+      $row['data'][] = array(
+        'data' => drupal_render($label),
+        'class' => array('permission'),
+      );
+      
+      foreach (element_children($form['checkboxes']) as $rid) {
+        $form['checkboxes'][$rid][$key]['#title'] = $form['role_names'][$rid]['#markup'] . ': ' . $form['permission'][$key]['#markup'];
+        $form['checkboxes'][$rid][$key]['#title_display'] = 'invisible';
+        
+        // Filter permissions strips role id class from checkbox. Used by Drupal core functionality.
+        $form['checkboxes'][$rid][$key]['#attributes']['class'][] = 'rid-' . $rid;
+        
+        // Set authenticated role behavior class on page load.
+        if ($rid == 2 && $form['checkboxes'][$rid][$key]['#checked'] === TRUE) {
+          $row['class'][] = 'fpa-authenticated-role-behavior';
+        }
+        
+        // For all roles that inherit permissions from 'authenticated user' role, add in dummy checkbox for authenticated role behavior.
+        if ($rid > 2) {
+          $form['checkboxes'][$rid][$key]['#suffix'] = $dummy_checkbox_output; // '#suffix' doesn't have wrapping HTML like '#field_suffix'.
+        }
+        
+        // Add rid's to row attribute for checked status filter.
+        if ($form['checkboxes'][$rid][$key]['#checked'] === TRUE) {
+          $row[FPA_ATTR_CHECKED][] = $rid;
+        }
+        else {
+          $row[FPA_ATTR_NOT_CHECKED][] = $rid;
+        }
+        
+        $row['data'][] = array(
+          'data' => drupal_render($form['checkboxes'][$rid][$key]),
+          'class' => array(
+            'checkbox',
+          ),
+          'title' => array(
+            $form['role_names'][$rid]['#markup'],
+          ),
+          // For role filter
+          FPA_ATTR_ROLE => array(
+            $rid,
+          ),
+        );
+      }
+      
+      if (!empty($rid)) {
+        $row['title'] = array(
+          $form['checkboxes'][$rid][$key]['#return_value'],
+        );
+        
+        $row[FPA_ATTR_SYSTEM_NAME] = array(
+          $form['checkboxes'][$rid][$key]['#return_value'],
+        );
+      }
+      
+      // Mark current row with escaped permission name.
+      $row[FPA_ATTR_PERMISSION] = array(
+        // Permission system name.
+        0 => $permission_system_name,
+        // Readable description.
+        1 => strip_tags($form['permission'][$key]['#markup']),
+      );
+      
+      // Mark current row with current module.
+      $row[FPA_ATTR_MODULE] = $rows[$module][FPA_ATTR_MODULE];
+      
+      $row[FPA_ATTR_PERMISSION] = array_unique(array_map('drupal_html_class', $row[FPA_ATTR_PERMISSION]));
+      
+      // Add current permission to current module row.
+      $rows[$module][FPA_ATTR_PERMISSION] = array_merge($rows[$module][FPA_ATTR_PERMISSION], $row[FPA_ATTR_PERMISSION]);
+      
+      $rows[$module][FPA_ATTR_CHECKED] = array_unique(array_merge($rows[$module][FPA_ATTR_CHECKED], $row[FPA_ATTR_CHECKED]));
+      $rows[$module][FPA_ATTR_NOT_CHECKED] = array_unique(array_merge($rows[$module][FPA_ATTR_NOT_CHECKED], $row[FPA_ATTR_NOT_CHECKED]));
+      
+      $modules[$rows[$module][FPA_ATTR_MODULE][0]][FPA_ATTR_PERMISSION][] = $row[FPA_ATTR_PERMISSION];
+    }
+    
+    $rows[$i++] = $row;
+  }
+  
+  $reset_button = array(
+    '#type' => 'html_tag',
+    '#tag' => 'input',
+    '#attributes' => array(
+      'type' => 'reset',
+      'class' => 'form-submit',
+      'value' => t('Reset changes'),
+    ),
+  );
+  
+  // If there is no submit button, don't add the reset button.
+  if (count(element_children($form['actions'])) > 0) {
+    
+    // Have the reset button appear before the submit button.
+    array_unshift($form['actions'], $reset_button);
+  }
+  
+  $actions_output = drupal_render_children($form['actions']);
+  
+  $header = array();
+  
+  $header[] = array(
+    'data' => t('Permission') . $actions_output,
+  );
+  
+  foreach (element_children($form['role_names']) as $rid) {
+    
+    $header[] = array(
+      'data' => drupal_render($form['role_names'][$rid]) . $nameless_checkbox_output,
+      'class' => array(
+        'checkbox',
+      ),
+      'title' => array(
+        $form['role_names'][$rid]['#markup'],
+      ),
+      FPA_ATTR_ROLE => array(
+        $rid,
+      ),
+    );
+    $user_roles[$rid] = $form['role_names'][$rid]['#markup'];
+  }
+  
+  $table = array(
+    'header' => $header,
+    'rows' => $rows,
+  );
+  
+  $output = _fpa_wrapper($table, $modules, $user_roles, $actions_output);
+  
+  $output .= drupal_render_children($form);
+  
+  return $output;
+}
+
+/**
+ * Wraps table output in the FPA filter.
+ */
+function _fpa_wrapper($permissions_table, $modules, $user_roles, $actions_output) {
+  
+  $same_page = trim(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_PATH), '/') == $_GET['q'];
+  
+  $render = array(
+    '#type' => 'container',
+    '#attributes' => array(
+      'class' => array(
+        'fpa-container',
+      ),
+    ),
+  );
+  
+  $hiders = array(
+    'fpa-hide-descriptions' => array(
+      'hide' => t('Hide descriptions'),
+      'show' => t('Show descriptions'),
+    ),
+    'fpa-hide-system-names' => array(
+      'hide' => t('Hide system names'),
+      'show' => t('Show system names'),
+    ),
+  );
+
+  $render['#attributes']['class'][] = 'fpa-hide-system-names';
+
+  $hide_container = array(
+    '#type' => 'container',
+    '#attributes' => array(
+      'class' => array(
+        'fpa-toggle-container',
+      ),
+    ),
+  );
+  
+  foreach ($hiders as $hide_class => $labels) {
+    
+    $hide_container[$hide_class] = array(
+      '#theme' => 'link',
+      '#text' => '',
+      '#path' => '',
+      '#options' => array(
+        'attributes' => array_merge($labels, array(
+          'fpa-toggle-class' => $hide_class,
+        )),
+        'html' => TRUE,
+        'fragment' => ' ',
+        'external' => TRUE, // Prevent base path from being added to link.
+      ),
+    );
+  }
+  
+  $render['hide_container'] = $hide_container;
+  
+  $wrapper = array(
+    '#type' => 'container',
+    '#attributes' => array(
+      'class' => array(
+        'fpa-wrapper',
+      ),
+    ),
+  );
+  
+  $render['wrapper'] = &$wrapper;
+  
+  /**
+   * <style /> block template.
+   */
+  $style_template = array(
+    '#type' => 'container',
+    '#attributes' => array(
+      'class' => array(
+        'style-wrapper-class-name', // Override on specific block.
+      ),
+    ),
+  );
+  
+  $style_template['style'] = array(
+    '#type' => 'html_tag',
+    '#tag' => 'style',
+    '#attributes' => array(
+      'type' => array(
+        'text/css',
+      ),
+    ),
+    '#value' => '', // #value needed for closing tag.
+  );
+  
+  /**
+   * <style /> block for role filtering.
+   */
+  $wrapper['role_styles'] = $style_template;
+  $wrapper['role_styles']['#attributes']['class'][0] = 'fpa-role-styles';
+  
+  /**
+   * <style /> block for permission filtering.
+   */
+  $wrapper['perm_styles'] = $style_template;
+  $wrapper['perm_styles']['#attributes']['class'][0] = 'fpa-perm-styles';
+  
+  /**
+   * Left section contains module list and form submission button.
+   */
+  $left_section = array(
+    '#type' => 'container',
+    '#attributes' => array(
+      'class' => array(
+        'fpa-left-section',
+      ),
+    ),
+  );
+  
+  $wrapper['left_section'] = &$left_section;
+  
+  
+  /**
+   * Right section contains filter form and permissions table.
+   */
+  $right_section = array(
+    '#type' => 'container',
+    '#attributes' => array(
+      'class' => array(
+        'fpa-right-section',
+      ),
+    ),
+  );
+  
+  $wrapper['right_section'] = &$right_section;
+  
+  $module_template = array(
+    FPA_ATTR_MODULE => array(),
+    FPA_ATTR_PERMISSION => array(),
+    'data' => array(
+      '#type' => 'container',
+      '#attributes' => array(),
+      
+      'link' => array(
+        '#type' => 'markup',
+        '#markup' => '', // l($module['text'], 'admin/people/permissions', $options)
+      ),
+      
+      'counters' => array(),
+      
+      'total' => array(
+        '#type' => 'html_tag',
+        '#tag' => 'span',
+        '#attributes' => array(
+          'class' => array('fpa-perm-total'),
+          'fpa-total' => 0,
+        ),
+        '#value' => '', // #value needed for closing tag.
+      ),
+    ),
+  );
+  
+  $counter_template = array(
+    '#type' => 'html_tag',
+    '#tag' => 'span',
+    '#attributes' => array(
+      'class' => array('fpa-perm-counter'),
+      FPA_ATTR_PERMISSION => array(), // Counters only count permissions match.
+    ),
+    '#value' => '', // #value required for closing tag.
+  );
+  
+  $items = array();
+  
+  $all_modules = array(
+    'text' => t('All modules'),
+    FPA_ATTR_MODULE => array(),
+    FPA_ATTR_PERMISSION => array(),
+  );
+  
+  array_unshift($modules, $all_modules);
+  
+  $all_modules_counters = array();
+  
+  foreach ($modules as $module) {
+    
+    $module_item = $module_template;
+    
+    $module_item[FPA_ATTR_MODULE] = $module[FPA_ATTR_MODULE];
+    $module_item[FPA_ATTR_PERMISSION] = array_reduce(array_pad($module[FPA_ATTR_PERMISSION], 1, array()), 'array_merge', array());
+    
+    // Use link for accessibility and tabability.
+    $options = array(
+      'fragment' => 'all',
+    );
+    
+    if (!empty($module['title'])) {
+      $options['fragment'] = 'module-' . $module['title'][0];
+      $options['attributes']['title'] = $module['title'][0];
+    }
+    
+    $module_item['data']['link']['#markup'] = l($module['text'], 'admin/people/permissions', $options);
+    
+    foreach ($module[FPA_ATTR_PERMISSION] as $module_perm) {
+      
+      $counter_item = $counter_template;
+      
+      $counter_item['#attributes'][FPA_ATTR_PERMISSION] = $module_perm;
+      
+      $all_modules_counters[] = $counter_item;
+      
+      $module_item['data']['counters'][] = $counter_item;
+    }
+    
+    $module_item['data']['total']['#attributes']['fpa-total'] = count($module[FPA_ATTR_PERMISSION]);
+    
+    $items[] = $module_item;
+  }
+  
+  $items[0]['data']['counters'] = $all_modules_counters;
+  $items[0]['data']['total']['#attributes']['fpa-total'] = count($all_modules_counters);
+  
+  foreach ($items as &$item) {
+    $item['data'] = drupal_render($item['data']);
+  }
+  
+  $left_section['list'] = array(
+    '#items' => $items,
+    '#theme' => 'item_list',
+  );
+  
+  $left_section['buttons'] = array(
+    '#type' => 'markup',
+    '#markup' => $actions_output,
+  );
+  
+  $filter_form = array(
+    '#type' => 'container',
+    '#attributes' => array(
+      'class' => array(
+        'fpa-filter-form',
+      ),
+    ),
+  );
+  
+  $clear_button = array(
+    '#type' => 'html_tag',
+    '#tag' => 'input',
+    '#attributes' => array(
+      'type' => array(
+        'button',
+      ),
+      'class' => array(
+        'fpa-clear-search',
+        'form-submit',
+      ),
+      'value' => 'Clear filter',
+    ),
+  );
+  
+  $default_filter = '';
+  
+  if (!empty($_GET['fpa_perm'])) {
+    $default_filter = $_GET['fpa_perm'];
+  }
+  
+  if (!empty($_COOKIE['fpa_filter']) && $same_page) {
+    $default_filter = $_COOKIE['fpa_filter'];
+  }
+  
+  
+  $filter_form['permission_module_filter'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Filter:'),
+    '#description' => t('<p>Enter in the format of "permission@module",</p><p>e.g. <em>admin@system</em> will show only permissions with the<br>text "admin" in modules with the text "system".</p><p>This will also match on system name of a permission.</p>'),
+    '#size' => 25,
+    '#field_suffix' => drupal_render($clear_button),
+    '#attributes' => array(
+      'placeholder' => array(
+        'permission@module',
+      ),
+      'autofocus' => 'autofocus',
+    ),
+    '#value' => $default_filter,
+  );
+  
+  /*
+   * Populate the permission filter styles.
+   */
+  $matches = array();
+  
+  preg_match('/^\s*([^@]*)@?(.*?)\s*$/i', $filter_form['permission_module_filter']['#value'], $matches);
+  
+  array_shift($matches); // Remove whole match item.
+  
+  $safe_matches = array_map('drupal_html_class', $matches);
+  
+  $module_match = !empty($_COOKIE['module_match']) ? $_COOKIE['module_match'] : '*=';
+  
+  $filters = array(
+    drupal_strlen($safe_matches[0]) > 0 ? ('[' . FPA_ATTR_PERMISSION .               '*="' . $safe_matches[0] . '"]') : '',
+    drupal_strlen($safe_matches[1]) > 0 ? ('[' . FPA_ATTR_MODULE     . $module_match . '"' . $safe_matches[1] . '"]') : '',
+  );
+  
+  $filter_styles = array(
+    '.fpa-table-wrapper tr[' . FPA_ATTR_MODULE . ']{display: none;}',
+    
+    '.fpa-table-wrapper tr[' . FPA_ATTR_MODULE . ']',
+    $filters[0],
+    $filters[1],
+    '{display: table-row;}',
+    
+    
+    '.fpa-perm-counter{display: none;}',
+    '.fpa-perm-counter',
+    $filters[0],
+    '{display: inline;}',
+    
+    
+    '.fpa-left-section li[' . FPA_ATTR_MODULE . ']',
+    drupal_strlen($filters[1]) > 0 ? $filters[1] : '[' . FPA_ATTR_MODULE . '=""]',
+    '{margin-right:-1px; background-color: white; border-right: solid 1px transparent;}',
+  );
+  
+  $wrapper['perm_styles']['style']['#value'] = implode('', $filter_styles);
+  
+  
+  $cookie_roles = (!empty($_COOKIE['fpa_roles']) && $same_page) ? json_decode($_COOKIE['fpa_roles']) : array();
+  
+  $options = array(
+    '*' => t('--All Roles'),
+  );
+  
+  if (!empty($user_roles)) {
+    $options += $user_roles; // Preserves keys.
+  }
+  
+  if (in_array('*', $cookie_roles)) {
+    $cookie_roles  = array('*');
+  }
+  
+  $filter_form['role_filter'] = array(
+    '#type' => 'select',
+    '#title' => t('Roles:'),
+    '#description' => t('Select which roles to display.<br>Ctrl+click to select multiple.'),
+    '#size' => 5,
+    '#options' => $options,
+    '#attributes' => array(
+      'multiple' => 'multiple',
+      'autocomplete' => 'off', // Keep browser from populating this from 'cached' input.
+    ),
+    '#value' => count(array_intersect($cookie_roles, array_keys($options))) > 0 ? $cookie_roles : array('*'),
+  );
+  
+  /*
+   * Populate the roles styles.
+   */
+  if (!in_array('*', $filter_form['role_filter']['#value'])) {
+    
+    $role_styles = array(
+      '.fpa-table-wrapper [' . FPA_ATTR_ROLE . '] {display: none;}',
+    );
+    
+    foreach ($filter_form['role_filter']['#value'] as $value) {
+      
+      $role_styles[] = '.fpa-table-wrapper [' . FPA_ATTR_ROLE . '="' . $value . '"] {display: table-cell;}';
+    }
+    
+    $role_styles[] = '.fpa-table-wrapper [' . FPA_ATTR_ROLE . '="' . end($filter_form['role_filter']['#value']) . '"] {border-right: 1px solid #bebfb9;}';
+    
+    $wrapper['role_styles']['style']['#value'] = implode('', $role_styles);
+  }
+  
+  $checked_status = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Display permissions that are:'),
+    '#options' => array(
+      FPA_ATTR_CHECKED => t('Checked'),
+      FPA_ATTR_NOT_CHECKED => t('Not Checked'),
+    ),
+    '#attributes' => array(),
+    '#title_display' => 'before',
+    '#description' => t('Applies to all visible roles.<br />Unsaved changes are not counted.<br />Most effective when a single role is visible.<br />Empty module rows sometimes display when used with permission filter.'),
+  );
+  
+  $checked_status_keys = array_keys($checked_status['#options']);
+  
+  $checked_status['#value'] = array_combine($checked_status_keys, $checked_status_keys);
+  
+  $filter_form['checked_status'] = form_process_checkboxes($checked_status);
+  
+  foreach (element_children($filter_form['checked_status']) as $key) {
+    $filter_form['checked_status'][$key]['#checked'] = TRUE;
+  }
+  
+  $right_section['filter_form'] = $filter_form;
+  
+  $table_wrapper = array(
+    '#type' => 'container',
+    '#attributes' => array(
+      'class' => array(
+        'fpa-table-wrapper',
+      ),
+    ),
+  );
+  
+  $table_wrapper['table'] = array(
+    '#theme' => 'table',
+    '#header' => $permissions_table['header'],
+    '#rows' => $permissions_table['rows'],
+    '#attributes' => array(
+      'id' => 'permissions',
+    ),
+  );
+  
+  // Show after full table HTML is loaded. Reduces progressive table load reflow/repaint.
+  $table_wrapper['show_table'] = array(
+    '#type' => 'html_tag',
+    '#tag' => 'style',
+    '#attributes' => array(
+      'type' => array(
+        'text/css',
+      ),
+    ),
+    '#value' => '#permissions {display: table;} .fpa-table-wrapper {background: none;}',
+  );
+  
+  $table_wrapper['buttons'] = array(
+    '#type' => 'markup',
+    '#markup' => $actions_output,
+  );
+  
+  $right_section['table_wrapper'] = $table_wrapper;
+  
+  
+  return drupal_render($render);
+}

+ 629 - 183
sites/all/modules/contrib/admin/fpa/js/fpa.js

@@ -1,220 +1,666 @@
-(function ($) {
-  var fpa = {
-    selector : {
-      form: '#user-admin-permissions',
-      table : '#permissions',
-      row : 'tbody tr',
-      filter : 'td.permission',
-      grouping : 'td.module'
-    },
-    dom : {
-      table : '', // jquery object for entire permissions table
-      rows : '', // jquery object containing just the rows
-      perm_style : '', // jquery object containing permissions style
-      role_style : '', // jquery object containing roles style
-      section_left: '',
-      section_right: ''
-    },
-    search:''
+
+/**
+ * @file
+ * JS functionality that creates dynamic CSS which hides permission rows and role columns.
+ */
+
+// Wrapper normalizes 'jQuery' to '$'.
+;(function fpa_scope($, Drupal, window, document) {
+  "use strict";
+  
+  var Fpa = function (context, settings) {
+    this.init(context, settings);
+    
+    return this;
+  };
+  
+  Fpa.prototype.selector = {
+    form: '#user-admin-permissions',
+    table : '#permissions'
+  };
+  
+  Fpa.prototype.init = function (context, settings) {
+    
+    this.drupal_html_class_cache = {};
+    
+    this.dom = {};
+    
+    this.attr = settings.attr;
+    
+    this.filter_timeout= null;
+    this.filter_timeout_time = 0;
+    
+    this.module_match = '*=';
+    
+    this.filter_selector_cache = {
+      '*=': {},
+      '~=': {}
+    };
+    
+    this.selector.table_base_selector = '.fpa-table-wrapper tr[' + this.attr.module + ']';
+    this.selector.list_counter_selector = '.fpa-perm-counter';
+    this.selector.list_base_selector = '.fpa-left-section li[' + this.attr.module + ']';
+    
+    if (this.select(context)) {
+      
+      this.prepare();
+      
+      this.authenticated_role_behavior();
+    }
+  };
+  
+  Fpa.prototype.styles = {
+    module_active_style: '{margin-right:-1px; background-color: white; border-right: solid 1px transparent;}'
+  };
+  
+  /**
+   * Select all elements that are used by FPA ahead of time and cache on 'Fpa' instance.
+   */
+  Fpa.prototype.select = function (context) {
+    
+    this.dom.context = $(context);
+    
+    this.dom.form = this.dom.context.find(this.selector.form);
+    
+    // Prevent anything else from running if the form is not found.
+    if (this.dom.form.length === 0) {
+      return false;
+    }
+    
+    this.dom.container = this.dom.form.find('.fpa-container');
+    
+    // Raw element since $().html(); does not work for <style /> elements.
+    this.dom.perm_style = this.dom.container.find('.fpa-perm-styles style').get(0);
+    this.dom.role_style = this.dom.container.find('.fpa-role-styles style').get(0);
+    
+    this.dom.section_left = this.dom.container.find('.fpa-left-section');
+    this.dom.section_right = this.dom.container.find('.fpa-right-section');
+    
+    this.dom.table_wrapper = this.dom.section_right.find('.fpa-table-wrapper');
+    this.dom.table = this.dom.table_wrapper.find(this.selector.table);
+    
+    this.dom.module_list = this.dom.section_left.find('ul');
+    
+    this.dom.filter_form = this.dom.container.find('.fpa-filter-form');
+    
+    this.dom.filter = this.dom.filter_form.find('input[type="text"]');
+    this.dom.role_select = this.dom.filter_form.find('select');
+    this.dom.checked_status = this.dom.filter_form.find('input[type="checkbox"]');
+    
+    return true;
   };
   
   /**
-   * Kick a function to the Back Of The Line.
+   * Prepares a string for use as a CSS identifier (element, class, or ID name).
    * 
-   * Anything that is botl'd will run after other attach events, like sticky table headers.
+   * @see https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_clean_css_identifier/7
    */
-  fpa.botl = function (func) {
-    setTimeout(function () {
-      func();
-    }, 0);
+  Fpa.prototype.drupal_clean_css_identifier = function (str) {
+    
+    return str
+    // replace ' ', '_', '/', '[' with '-'
+    .replace(/[ _\/\[]/g, '-')
+    // replace ']' with ''
+    .replace(/\]/g, '')
+    // Valid characters in a CSS identifier are:
+    // - the hyphen (U+002D)
+    // - a-z (U+0030 - U+0039)
+    // - A-Z (U+0041 - U+005A)
+    // - the underscore (U+005F)
+    // - 0-9 (U+0061 - U+007A)
+    // - ISO 10646 characters U+00A1 and higher
+    // We strip out any character not in the above list.
+    .replace(/[^\u002D\u0030-\u0039\u0041-\u005A\u005F\u0061-\u007A\u00A1-\uFFFF]/, '');
   };
   
   /**
-   * Changes a string into a safe single css class.
+   * Prepares a string for use as a valid class name.
+   * 
+   * @see https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_html_class/7
    */
-  fpa.classify = function (str) {
-    return str.toLowerCase().replace(/ /ig, '-');
+  Fpa.prototype.drupal_html_class = function (str) {
+    
+    if (this.drupal_html_class_cache[str] === undefined) {
+      this.drupal_html_class_cache[str] = this.drupal_clean_css_identifier(str.toLowerCase());
+    }
+    
+    return this.drupal_html_class_cache[str];
   };
   
+  /**
+   * Handles applying styles to <style /> tags.
+   */
+  Fpa.prototype.set_style = (function () {
+    
+    // Feature detection. Mainly for IE8.
+    if ($('<style type="text/css" />').get(0).styleSheet) {
+      return function (element, styles) {
+        
+        element.styleSheet.cssText = styles;
+      };
+    }
+    
+    // Default that works in modern browsers.
+    return function (element, styles) {
+      element.innerHTML = styles;
+    };
+  })();
+  
   /**
    * Callback for click events on module list.
    */
-  fpa.filter_module = function () {
-    var $this = $(this);
-    $('div.fpa-left-section ul li').removeClass('active');
-    $this.parent().addClass('active');
+  Fpa.prototype.filter_module = function (e) {
+    
+    e.preventDefault();
+    e.stopPropagation();
+    
+    var $this = $(e.currentTarget);
+    
+    this.dom.filter.val([
+      
+      // remove current module filter string.
+      this.dom.filter.val().replace(/(@.*)/, ''),
+      
+      // remove trailing @ as that means no module; clean 'All' filter value
+      $this.attr(this.attr.module) !== '' ? '@' + $this.find('a[href]').text() : ''
+      
+    ].join('')); 
+    
+    /**
+     * ~= matches exactly one whitespace separated word in attribute.
+     * 
+     * @see http://www.w3.org/TR/CSS2/selector.html#matching-attrs
+     */
+    this.module_match = '~=';
+    
+    this.filter();
+  };
+  
+  Fpa.prototype.build_filter_selectors = function (filter_string) {
+    
+    // Extracts 'permissions@module', trimming leading and trailing whitespace.
+    var matches = filter_string.match(/^\s*([^@]*)@?(.*?)\s*$/i);
+    
+    matches.shift(); // Remove whole match item.
     
-    var current_perm = fpa.dom.filter.val().split('@');
-    current_perm[1] = $this.attr('title');
-    fpa.dom.filter.val(current_perm.join('@').replace(/@$/ig, '')); // remove trailing @ as that means no module; clean 'All' filter value
-    fpa.filter('=');
+    var safe_matches = $.map(matches, $.proxy(this.drupal_html_class, this));
+    
+    this.filter_selector_cache[this.module_match][filter_string] = [
+      safe_matches[0].length > 0 ? '[' + this.attr.permission          + '*="' + safe_matches[0] + '"]' : '',
+      safe_matches[1].length > 0 ? '[' + this.attr.module + this.module_match + '"' + safe_matches[1] + '"]' : ''
+    ];
+    
+    return this.filter_selector_cache[this.module_match][filter_string];
   };
   
-  fpa.filter = function (module_match) {
-    module_match = module_match || '*=';
-    perm = fpa.dom.filter.val();
-    fpa.botl(function () {
-      if (typeof perm != 'undefined' && ['', '@'].indexOf(perm) == -1) {
-        var perm_copy = fpa.classify(perm).split('@');
-        var selector = fpa.selector.table + ' ' + fpa.selector.row;
+  Fpa.prototype.get_filter_selectors = function (filter_string) {
+    
+    filter_string = filter_string || this.dom.filter.val();
+    
+    return this.filter_selector_cache[this.module_match][filter_string] || this.build_filter_selectors(filter_string);
+  };
+  
+  Fpa.prototype.permission_grid_styles = function (filters) {
+    
+    filters = filters || this.get_filter_selectors();
+    
+    var checked_filters = this.build_checked_selectors();
+    
+    var styles = [
+      this.selector.table_base_selector,
+      '{display: none;}'
+    ];
+    
+    for (var i = 0; i < checked_filters.length; i++) {
+      
+      styles = styles.concat([
+        this.selector.table_base_selector,
         
-        var perm_style_code = selector + '[fpa-module]{display:none;}';
+        checked_filters[i],
         
-        if (perm_copy[0]) selector += '[fpa-permission*="' + perm_copy[0] + '"]';
-        if (perm_copy[1]) selector += '[fpa-module' + module_match + '"' + perm_copy[1] + '"]';
+        filters[0],
+        filters[1],
+        '{display: table-row;}'
+      ]);
+    }
+    
+    return styles.join('');
+    
+  };
+  
+  Fpa.prototype.counter_styles = function (filters) {
+    
+    filters = filters || this.get_filter_selectors();
+    
+    return [
+      this.selector.list_counter_selector,
+      '{display: none;}',
+      
+      this.selector.list_counter_selector,
+      filters[0],
+      '{display: inline;}'
+    ].join('');
+    
+  };
+  
+  Fpa.prototype.module_list_styles = function (filters) {
+    
+    filters = filters || this.get_filter_selectors();
+    
+    return [ 
+      this.selector.list_base_selector,
+      (filters[1].length > 0 ? filters[1] : '[' + this.attr.module + '=""]'),
+      this.styles.module_active_style
+    ].join('');
+    
+  };
+  
+  Fpa.prototype.filter = function () {
+    
+    var perm = this.dom.filter.val();
+    
+    $.cookie('fpa_filter', perm, {path: '/'});
+    $.cookie('fpa_module_match', this.module_match, {path: '/'});
+          
+    this.save_filters();
+    
+    var filter_selector = this.get_filter_selectors(perm);
+    
+    this.set_style(this.dom.perm_style, [
+      
+      this.permission_grid_styles(filter_selector),
+      
+      this.counter_styles(filter_selector),
+      
+      this.module_list_styles(filter_selector)
+      
+    ].join(''));
+  };
+  
+  Fpa.prototype.build_role_selectors = function (roles) {
+    
+    roles = roles || this.dom.role_select.val();
+    
+    var selectors = ['*'];
+    
+    if ($.inArray('*', roles) === -1) {
+      
+      selectors = $.map(roles, $.proxy(function (value, index) {
         
-        perm_style_code += selector + '{display: table-row;}';
+        return '[' + this.attr.role + '="' + value + '"]';
         
-        fpa.dom.perm_style[0].innerHTML = perm_style_code;
+      }, this));
+    }
+    
+    return selectors;
+  };
+  
+  Fpa.prototype.build_checked_selectors = function (roles) {
+    
+    roles = roles || this.dom.role_select.val();
+    
+    var checked_boxes = $.map(this.dom.checked_status, function (element, index) {
+      return element.checked ? $(element).val() : null;
+    });
+    
+    var selectors = [''];
+    
+    if ($.inArray('*', roles) !== -1) {
+      roles = $.map(this.dom.role_select.find('option').not('[value="*"]'), $.proxy(function (element, index) {
+        
+        return $(element).attr('value');
+        
+      }, this));
+    }
+    
+    if (checked_boxes.length != this.dom.checked_status.length) {
+      
+      selectors = $.map(roles, $.proxy(function (value, index) {
+        
+        return $.map(checked_boxes, $.proxy(function (checked_attr, index) {
+          
+          return '[' + checked_attr + '~="' + value + '"]';
+          
+        }, this));
+        
+      }, this));
+    }
+    
+    return selectors;
+  };
+  
+  // Even handler for role selection.
+  Fpa.prototype.filter_roles = function () {
+    
+    this.save_filters();
+    
+    var values = this.dom.role_select.val() || [];
+    var role_style_code = [];
+    
+    $.cookie('fpa_roles', JSON.stringify(values), {path: '/'});
+    
+    // Only filter if "All Roles" is not selected.
+    if ($.inArray('*', values) === -1) {
+      
+      role_style_code.push('.fpa-table-wrapper [' + this.attr.role + '] {display: none;}');
+      
+      if (values.length > 0) {
+        
+        var role_selectors = this.build_role_selectors(values);
+        
+        role_style_code = role_style_code.concat($.map(role_selectors, $.proxy(function (value, index) {
+          
+          return '.fpa-table-wrapper ' + value + ' {display: table-cell;}';
+          
+        }, this)));
+        
+        // Ensure right border on last visible role.
+        role_style_code.push('.fpa-table-wrapper ' + role_selectors.pop() + ' {border-right: 1px solid #bebfb9;}');
       }
       else {
-        fpa.dom.perm_style[0].innerHTML = '';
+        role_style_code.push('td[class="permission"] {border-right: 1px solid #bebfb9;}');
       }
-    });
+      
+    }
+    
+    this.set_style(this.dom.role_style, role_style_code.join(''));
+    
+    this.filter();
   };
   
-  fpa.prepare = function (context) {
-    fpa.dom.form = $(fpa.selector.form, context);
-    if (fpa.dom.form.length == 0) {
-      return;
-    }
-    fpa.dom.table = fpa.dom.form.find(fpa.selector.table);
-    fpa.dom.section_right = fpa.dom.table.wrap('<div class="fpa-right-section" />').parent();
-    fpa.dom.module_list = $('<ul />').insertBefore(fpa.dom.section_right).wrap('<div class="fpa-left-section" />');
-
-    // create module list
-    
-    fpa.dom.all_modules = $('<div />')
-      .appendTo(fpa.dom.module_list)
-      .wrap('<li class="active" />')
-      .text('All modules')
-      .attr('title', '')
-      .click(fpa.filter_module);
-    
-    fpa.dom.table.find(fpa.selector.grouping).each(function () {
-      var module_id = $(this).text();
-      // Add new item to module list.
-      $('<div />')
-        .appendTo(fpa.dom.module_list)
-        .wrap('<li />')
-        .text(module_id.replace(/ module$/ig, ''))
-        .attr('title', module_id)
-        .attr('fpa-module', fpa.classify(module_id))
-        .click(fpa.filter_module);
+  /**
+   * Prevent the current filter from being cleared on form reset.
+   */
+  Fpa.prototype.save_filters = function () {
+    
+    /**
+     * element.defaultValue is what 'text' elements reset to.
+     * 
+     * @link http://www.w3.org/TR/REC-DOM-Level-1/level-one-html.html#ID-26091157
+     */
+    this.dom.filter.get(0).defaultValue = this.dom.filter.val();
+    
+    /**
+     * element.defaultSelected is what 'option' elements reset to.
+     * 
+     * @see http://www.w3.org/TR/REC-DOM-Level-1/level-one-html.html#ID-37770574
+     */
+    this.dom.role_select.find('option').each(function (index, element) {
+      element.defaultSelected = element.selected;
     });
     
-    // tag rows with required classes
-    fpa.botl(function () {
-      fpa.dom.filter_form = $('<div class="fpa-filter-form" />').prependTo(fpa.dom.section_right);
-      fpa.dom.perm_style = $('<style type="text/css" />').prependTo(fpa.dom.section_right);
-      fpa.dom.role_style = $('<style type="text/css" />').prependTo(fpa.dom.section_right);
-      
-      // Put the "Save Permissions" button at the top and bottom.
-      fpa.dom.form.find('input[type="submit"][name="op"]').remove()
-        .clone().insertAfter(fpa.dom.module_list)
-        .clone().insertAfter(fpa.dom.table);
-        
-      fpa.dom.filter = $('<input id="fpa_filter" type="text" class="form-text" placeholder="permission@module" />')
-        .appendTo(fpa.dom.filter_form)
-        .keypress(function (e) {
-          //prevent enter key from submitting form
-          if (e.which == 13) {
-            return false;
-          }
-        })
-        .keyup(function (e) {
-          fpa.filter();
-        })
-        .wrap('<div id="fpa-filter-perm" class="form-item" />')
-        .before('<label for="fpa_filter">Filter:</label>')
-        .after('<div class="description">Enter in the format of permission@module;<br /> E.g. admin@system will show only permissions with<br />the text "admin" in modules with the text "system".</div>')
-        .val(Drupal.settings.fpa.perm);
-        
-      $('<button class="clear-search">Clear Filter</button>')
-        .insertAfter(fpa.dom.filter)
-        .click(function (e) {
+    /**
+     * element.defaultChecked is what 'checkbox' elements reset to.
+     * 
+     * @see http://www.w3.org/TR/REC-DOM-Level-1/level-one-html.html#ID-20509171
+     */
+    this.dom.checked_status.each(function (index, element) {
+      element.defaultChecked = element.checked;
+    });
+  };
+  
+  Fpa.prototype.prepare = function () {
+    
+    this.filter_timeout_time = Math.min(this.dom.table.find('tr').length, 200);
+    
+    this.dom.form
+      .delegate('.fpa-toggle-container a', 'click', $.proxy(function fpa_toggle(e) {
+        e.preventDefault();
+        
+        var toggle_class = $(e.currentTarget).attr('fpa-toggle-class');
+        
+        this.dom.container.toggleClass(toggle_class).hasClass(toggle_class);
+        
+      }, this))
+    ;
+    
+    this.dom.section_left
+      .delegate('li', 'click', $.proxy(this.filter_module, this))
+    ;
+    
+    this.dom.filter
+      // Prevent Enter/Return from submitting form.
+      .keypress(function fpa_prevent_form_submission(e) {
+        if (e.which === 13) {
           e.preventDefault();
-          fpa.dom.filter.val('');
-          fpa.filter();
-          fpa.dom.all_modules.click();
-        });
-        
-      var roles_select = $('<select multiple="multiple" size=5 />')
-        .appendTo(fpa.dom.filter_form)
-        .wrap('<div id="fpa-filter-role" class="form-item" />')
-        .before('<label for="fpa_filter">Roles:</label>')
-        .after('<div class="description">Select which roles to display.<br />Ctrl+click to select multiple.</div>')
-        .change(function () {
-          var values = $(this).val();
-          var selector_array = [];
-          var role_style_code = fpa.selector.table + ' .checkbox{display:none;}';
-          for (i in values) {
-            selector_array.push(fpa.selector.table + ' .checkbox[title^="' + values[i] + '"]');
-          }
-          role_style_code += selector_array.join(',') + '{display: table-cell;}';
+          e.stopPropagation();
+        }
+      })
+      // Prevent non-character keys from triggering filter.
+      .keyup($.proxy(function fpa_filter_keyup(e) {
+        
+        // Prevent ['Enter', 'Shift', 'Ctrl', 'Alt'] from triggering filter.
+        if ($.inArray(e.which, [13, 16, 17, 18]) === -1) {
+          
+          window.clearTimeout(this.filter_timeout);
           
-          fpa.dom.role_style[0].innerHTML = role_style_code;
-        });
-      
-      fpa.dom.rows = fpa.dom.table.find(fpa.selector.row);
-      
-      var roles = fpa.dom.table.find('thead th.checkbox').each(function () {
-        $this = $(this);
-        var role_text = $this.text();
-        var index = $this.attr('title', role_text).index() + 1;
-        fpa.dom.rows.find('td:nth-child(' + index + ')').attr('title', role_text);
-        $('<option />')
-          .appendTo(roles_select)
-          .attr('value', $this.text())
-          .attr('selected', 'selected')
-          .text($this.text());
-      });
-      
-      var module_id = '';
-      var $module_row;
-      
-      // iterate over the rows
-      fpa.dom.rows.each(function () {
-        var $this = $(this);
-        
-        // Is this a module row?
-        var new_module_id = $this.find(fpa.selector.grouping).text();
-        if (new_module_id != '') {
-          $module_row = $this.addClass('module');
-          module_id = fpa.classify(new_module_id);
+          this.filter_timeout = window.setTimeout($.proxy(function () {
+            
+            this.dom.table_wrapper
+              .detach()
+              .each($.proxy(function (index, element) {
+                
+                this.module_match = '*=';
+                
+                this.filter();
+              }, this))
+              .appendTo(this.dom.section_right)
+            ;
+            
+          }, this), this.filter_timeout_time);
         }
-        var perm = $this.find(fpa.selector.filter).clone();
-        perm.find('div.description').remove();
-        var permission = fpa.classify($.trim(perm.text()));
-        $this.attr('fpa-module', module_id).attr('fpa-permission', permission);
-        $module_row.attr('fpa-permission', $module_row.attr('fpa-permission') + ' ' + permission);
-      });
-      
-      if (Drupal.settings.fpa.perm == '' && window.location.hash.indexOf('module-') > -1) {
-        fpa.dom.filter.val('@' + window.location.hash.substring(8));
-        fpa.filter('=');
-      }
+      }, this))
+    ;
+    
+    // Handle links to sections on permission admin page.
+    this.dom.form
+      .delegate('a[href*="admin/people/permissions#"]', 'click', $.proxy(function fpa_inter_page_links_click(e) {
+        e.preventDefault();
+        e.stopPropagation();
+        
+        this.dom.module_list
+          .find('li[' + this.attr.module + '~="' + this.drupal_html_class(e.currentTarget.hash.substring(8)) + '"]')
+          .click()
+        ;
+        
+        $('body').scrollTop(this.dom.container.position().top);
+      }, this))
+    ;
+    
+    // Handler for links that use #hash and can't be capture server side.
+    if(window.location.hash.indexOf('module-') === 1) {
       
-      fpa.filter();
-      fpa.dom.form.addClass('show');
-      fpa.dom.filter.focus();
+      this.dom.module_list
+        .find('li[' + this.attr.module + '~="' + this.drupal_html_class(window.location.hash.substring(8)) + '"]')
+        .click()
+      ;
+    }
+    
+    /**
+     * Reset authenticated role behavior when form resets.
+     * 
+     * @todo should this be synchronous? Would have to trigger reset on elements while detached.
+     */
+    this.dom.form.bind('reset', $.proxy(function fpa_form_reset(e) {
       
-    });
+      // Wait till after the form elements have been reset.
+      window.setTimeout($.proxy(function fpa_fix_authenticated_behavior() {
+        
+        this.dom.table_wrapper
+          .detach() // Don't make numerous changes while elements are in the rendered DOM.
+          .each($.proxy(function (index, element) {
+            
+            $(element)
+              .find('input[type="checkbox"].rid-2')
+              .each(this.dummy_checkbox_behavior)
+            ;
+          }, this))
+          .appendTo(this.dom.section_right)
+        ;
+        
+      }, this), 0);
+    }, this));
+    
+    
+    // Role checkboxes toggle all visible permissions for this column.
+    this.dom.section_right
+      .delegate('th[' + this.attr.role + '] input[type="checkbox"].fpa-checkboxes-toggle', 'change', $.proxy(function fpa_role_permissions_toggle(e) {
+        
+        var $this = $(e.currentTarget);
+        
+        // Get visible rows selectors.
+        var filters = this.get_filter_selectors(this.dom.filter.val());
+        
+        this.dom.table_wrapper
+          .detach()
+          .each($.proxy(function (index, element) {
+            
+            var rid = $this.closest('[' + this.attr.role + ']').attr(this.attr.role);
+            
+            $(element)
+              .find([
+                'tr' + filters.join(''),
+                'td.checkbox[' + this.attr.role + '="' + rid + '"]',
+                'input[type="checkbox"][name]'
+              ].join(' ')) // Array is easier to read, separated for descendant selectors.
+              
+              .attr('checked', $this.attr('checked'))
+              
+              .filter('.rid-2') // Following only applies to "Authenticated User" role.
+              .each(this.dummy_checkbox_behavior)
+            ;
+          }, this))
+          .appendTo(this.dom.section_right)
+        ;
+        
+      }, this))
+    ;
+    
+    // Permission checkboxes toggle all visible permissions for this row.
+    this.dom.section_right
+      .delegate('td.permission input[type="checkbox"].fpa-checkboxes-toggle', 'change', $.proxy(function fpa_role_permissions_toggle(e) {
+        
+        // Get visible rows selectors.
+        
+        var $row = $(e.currentTarget).closest('tr');
+        
+        $row.prev('tr').after(
+          
+          $row
+          .detach()
+          .each($.proxy(function (index, element) {
+            
+            $(element)
+              .find('td.checkbox')
+              .filter(this.build_role_selectors().join(','))
+              .find('input[type="checkbox"][name]')
+              
+              .attr('checked', e.currentTarget.checked)
+              
+              .filter('.rid-2') // Following only applies to "Authenticated User" role.
+              .each(this.dummy_checkbox_behavior)
+            ;
+            
+          }, this))
+        );
+        
+      }, this))
+    ;
+    
+    // Clear contents of search field and reset visible permissions.
+    this.dom.section_right
+      .delegate('.fpa-clear-search', 'click', $.proxy(function (e) {
+        
+        this.dom.filter
+          .val('')
+        ;
+        
+        this.filter();
+      }, this))
+    ;
+    
+    // Change visible roles.
+    this.dom.role_select
+      .bind('change blur', $.proxy(this.filter_roles, this))
+    ;
+    
+    this.dom.checked_status
+      .bind('change', $.proxy(function (e) {
+        
+        this.save_filters();
+        
+        this.filter();
+        
+      }, this))
+    ;
+
+    /**
+     * System name is not normally selectable because its a pseudo-element.
+     *
+     * This detects clicks directly on the TR, which happens when a click is on
+     * the pseudo-element, and displays a prompt() with the system name as the
+     * pre-populated value.
+     */
+    this.dom.table
+      .delegate('tr[' + this.attr.system_name + ']', 'click', $.proxy(function (e) {
+        var $target = $(e.target);
+
+        if ($target.is('tr[' + this.attr.system_name + ']')) {
+
+          window.prompt('You can grab the system name here', $target.attr(this.attr.system_name));
+        }
+      }, this))
+    ;
+    
+    // Focus on element takes long time, bump after normal execution.
+    window.setTimeout($.proxy(function fpa_filter_focus() {
+      this.dom.filter.focus();
+    }, this), 0);
+    
   };
   
-  fpa.modalframe = function (context) {
-    $('a.fpa_modalframe', context).click(function (e) {
-      e.preventDefault();
-      e.stopPropagation();
-      Drupal.modalFrame.open({
-        url: $(this).attr('href'),
-        draggable: false
-      });
-    });
+  /**
+   * Event handler/iterator.
+   * 
+   * Should not be $.proxy()'d.
+   */
+  Fpa.prototype.dummy_checkbox_behavior = function () {
+    // 'this' refers to the element, not the 'Fpa' instance.
+    $(this).closest('tr').toggleClass('fpa-authenticated-role-behavior', this.checked);
   };
   
-  Drupal.behaviors.fpa = {attach: function (context) {
-    fpa.prepare(context);
-    fpa.modalframe(context);
-  }};
-})(jQuery);
+  Fpa.prototype.authenticated_role_behavior = function () {
+    
+    this.dom.table_wrapper
+      .delegate('input[type=checkbox].rid-2', 'mousedown', function (e) {
+        
+        $(e.currentTarget).unbind('click.permissions');
+      })
+      .delegate('input[type=checkbox].rid-2', 'change.fpa_authenticated_role', this.dummy_checkbox_behavior)
+    ;
+  };
+  
+  Drupal.behaviors.fpa = {
+    attach: function (context, settings) {
+      
+      // Add touch-screen styling for checkboxes to make easier to use.
+      if (document.documentElement.ontouchstart !== undefined) {
+        $(document.body).addClass('fpa-mobile');
+      }
+      
+      // Fix table sticky table headers width due to changes in visible roles.
+      $(window)
+        .bind('scroll', function fpa_fix_tableheader(e) {
+          $(e.currentTarget).triggerHandler('resize.drupal-tableheader');
+        })
+      ;
+    
+      new Fpa(context, settings.fpa);
+    }
+  };
+  
+  // Override Drupal core's Authenticated role checkbox behavior.
+  Drupal.behaviors.permissions.attach = $.noop;
+  
+  // Drupal.behaviors.formUpdated.attach = $.noop;
+})(jQuery, Drupal, window, document);

File diff suppressed because it is too large
+ 0 - 0
sites/all/modules/contrib/admin/fpa/js/fpa.min.js


+ 1 - 0
sites/all/modules/features/translations/translations.info

@@ -1,6 +1,7 @@
 name = Translations
 core = 7.x
 package = Materio
+version = 7.x-1.0-beta
 dependencies[] = ctools
 dependencies[] = features
 dependencies[] = features_translations

Some files were not shown because too many files changed in this diff