You are here

function _fpa_wrapper in Fast Permissions Administration 8.2

Same name and namespace in other branches
  1. 7.2 fpa.theme.inc \_fpa_wrapper()

Wraps table output in the FPA filter.

1 call to _fpa_wrapper()
theme_fpa_user_admin_permissions in ./fpa.theme.inc
Theme function to pre-mark rows with FPA attributes.

File

./fpa.theme.inc, line 345
Theme callbacks to pre-tag rows for FPA functionality.

Code

function _fpa_wrapper($permissions_table, $modules, $user_roles, $actions_output) {
  $same_page = trim(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_PATH), '/') == $_GET['q'];
  $render = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'fpa-container',
      ],
    ],
  ];
  $hiders = [
    'fpa-hide-descriptions' => [
      'hide' => t('Hide descriptions'),
      'show' => t('Show descriptions'),
    ],
    'fpa-hide-system-names' => [
      'hide' => t('Hide system names'),
      'show' => t('Show system names'),
    ],
  ];
  $render['#attributes']['class'][] = 'fpa-hide-system-names';
  $hide_container = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'fpa-toggle-container',
      ],
    ],
  ];
  foreach ($hiders as $hide_class => $labels) {
    $hide_container[$hide_class] = [
      '#theme' => 'link',
      '#text' => '',
      '#path' => '',
      '#options' => [
        'attributes' => array_merge($labels, [
          'fpa-toggle-class' => $hide_class,
        ]),
        'html' => TRUE,
        'fragment' => ' ',
        'external' => TRUE,
      ],
    ];
  }
  $render['hide_container'] = $hide_container;
  $wrapper = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'fpa-wrapper',
      ],
    ],
  ];
  $render['wrapper'] =& $wrapper;

  /**
   * <style /> block template.
   */
  $style_template = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'style-wrapper-class-name',
      ],
    ],
  ];
  $style_template['style'] = [
    '#type' => 'html_tag',
    '#tag' => 'style',
    '#attributes' => [
      'type' => [
        'text/css',
      ],
    ],
    '#value' => '',
  ];

  /**
   * <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 = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'fpa-left-section',
      ],
    ],
  ];
  $wrapper['left_section'] =& $left_section;

  /**
   * Right section contains filter form and permissions table.
   */
  $right_section = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'fpa-right-section',
      ],
    ],
  ];
  $wrapper['right_section'] =& $right_section;
  $module_template = [
    FPA_ATTR_MODULE => [],
    FPA_ATTR_PERMISSION => [],
    'data' => [
      '#type' => 'container',
      '#attributes' => [],
      'link' => [
        '#type' => 'markup',
        '#markup' => '',
      ],
      'counters' => [],
      'total' => [
        '#type' => 'html_tag',
        '#tag' => 'span',
        '#attributes' => [
          'class' => [
            'fpa-perm-total',
          ],
          'fpa-total' => 0,
        ],
        '#value' => '',
      ],
    ],
  ];
  $counter_template = [
    '#type' => 'html_tag',
    '#tag' => 'span',
    '#attributes' => [
      'class' => [
        'fpa-perm-counter',
      ],
      FPA_ATTR_PERMISSION => [],
    ],
    '#value' => '',
  ];
  $items = [];
  $all_modules = [
    'text' => t('All modules'),
    FPA_ATTR_MODULE => [],
    FPA_ATTR_PERMISSION => [],
  ];
  array_unshift($modules, $all_modules);
  $all_modules_counters = [];
  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_merge', []);

    // Use link for accessibility and tabability.
    $options = [
      '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::service('renderer')
      ->render($item['data']);
  }
  $left_section['list'] = [
    '#items' => $items,
    '#theme' => 'item_list',
  ];
  $left_section['buttons'] = [
    '#type' => 'markup',
    '#markup' => $actions_output,
  ];
  $filter_form = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'fpa-filter-form',
      ],
    ],
  ];
  $clear_button = [
    '#type' => 'html_tag',
    '#tag' => 'input',
    '#attributes' => [
      'type' => [
        'button',
      ],
      'class' => [
        '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'] = [
    '#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::service('renderer')
      ->render($clear_button),
    '#attributes' => [
      'placeholder' => [
        'permission@module',
      ],
      'autofocus' => 'autofocus',
    ],
    '#value' => $default_filter,
  ];

  /*
   * Populate the permission filter styles.
   */
  $matches = [];
  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 = [
    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 = [
    '.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']) : [];
  $options = [
    '*' => t('--All Roles'),
  ];
  if (!empty($user_roles)) {
    $options += $user_roles;

    // Preserves keys.
  }
  if (in_array('*', $cookie_roles)) {
    $cookie_roles = [
      '*',
    ];
  }
  $filter_form['role_filter'] = [
    '#type' => 'select',
    '#title' => t('Roles:'),
    '#description' => t('Select which roles to display.<br>Ctrl+click to select multiple.'),
    '#size' => 5,
    '#options' => $options,
    '#attributes' => [
      'multiple' => 'multiple',
      'autocomplete' => 'off',
    ],
    '#value' => count(array_intersect($cookie_roles, array_keys($options))) > 0 ? $cookie_roles : [
      '*',
    ],
  ];

  /*
   * Populate the roles styles.
   */
  if (!in_array('*', $filter_form['role_filter']['#value'])) {
    $role_styles = [
      '.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 = [
    '#type' => 'checkboxes',
    '#title' => t('Display permissions that are:'),
    '#options' => [
      FPA_ATTR_CHECKED => t('Checked'),
      FPA_ATTR_NOT_CHECKED => t('Not Checked'),
    ],
    '#attributes' => [],
    '#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;
    $filter_form['checked_status'][$key]['#id'] = drupal_html_id('edit-checkboxes-' . $key);
  }
  $right_section['filter_form'] = $filter_form;
  $table_wrapper = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'fpa-table-wrapper',
      ],
    ],
  ];
  $table_wrapper['table'] = [
    '#theme' => 'table',
    '#header' => $permissions_table['header'],
    '#rows' => $permissions_table['rows'],
    '#attributes' => [
      'id' => 'permissions',
    ],
  ];

  // Show after full table HTML is loaded. Reduces progressive table load reflow/repaint.
  $table_wrapper['show_table'] = [
    '#type' => 'html_tag',
    '#tag' => 'style',
    '#attributes' => [
      'type' => [
        'text/css',
      ],
    ],
    '#value' => '#permissions {display: table;} .fpa-table-wrapper {background: none;}',
  ];
  $table_wrapper['buttons'] = [
    '#type' => 'markup',
    '#markup' => $actions_output,
  ];
  $right_section['table_wrapper'] = $table_wrapper;
  return \Drupal::service('renderer')
    ->render($render);
}