You are here

perms_fieldsets.module in Util 6

Same filename and directory in other branches
  1. 6.3 perms_fieldsets.module
  2. 6.2 perms_fieldsets.module

File

perms_fieldsets.module
View source
<?php

function perms_fieldsets_menu() {
  $menu['admin/settings/util/apfm'] = array(
    'title' => 'Manage Access Permissions Fieldsets',
    'description' => 'Manage Access Permissions Fieldsets',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'perms_fieldsets_settings',
    ),
  );
  return $menu;
}

// the theme registry
function perms_fieldsets_theme() {
  return array(
    'perms_fieldsets_theme' => array(
      'arguments' => array(
        'form' => null,
      ),
    ),
  );
}
function perms_fieldsets_settings() {
  $form['todo'] = array(
    '#type' => 'fieldset',
    '#title' => 'TODO list',
  );
  $form['todo']['item1'] = array(
    '#type' => 'item',
    '#value' => 'create settings for default module permission coppalsing / expanding',
  );
  $form['todo']['item2'] = array(
    '#type' => 'item',
    '#value' => 'Enable permission counter per role ( administrator[3/10] )',
  );
  return $form;
}
function perms_fieldsets_form_alter(&$form, $form_state, $form_id) {

  //  drupal_set_message($form_id);
  //  drupal_set_message('<pre>'. print_r($form, 1) .'</pre>');
  switch ($form_id) {
    case 'system_modules':

      //a neat idea to allow access to the important module settings directly from the modules page, saves page loads from navigting to site configuration etc etc.
      $form['description']['perms_fieldsets']['#value'] = t('Set default collapsed/expanded state for <a href="@url">Access Permissions</a>', array(
        '@url' => url('admin/user/permissions'),
      ));
      break;
    case 'user_admin_perm':
      $form['#theme'] = 'perms_fieldsets_theme';
      break;
  }
}
function theme_perms_fieldsets_theme($form) {
  $header[] = array(
    'data' => t('Permission'),
    'width' => '100%',
  );
  foreach (element_children($form['role_names']) as $rid) {
    if (is_array($form['role_names'][$rid])) {
      $header[] = array(
        'data' => drupal_render($form['role_names'][$rid]),
        'class' => 'checkbox',
        'nowrap' => 'nowrap',
      );
    }
  }

  //This is my custom fieldset-enabled form
  $fieldset_form = array();
  $roles = user_roles();

  //fieldset weight counter
  $fieldset_weight = 0;

  //module watcher

  //because role information is not grouped, we need to emulate groupping

  //watching this variable will help us determine which module's permissions are being processed now
  $fieldset_module_watcher = '';
  $fieldset_module_watcher_old = '';
  foreach (element_children($form['permission']) as $key) {

    // Don't take form control structures
    if (is_array($form['permission'][$key])) {
      $row = array();

      // Module name
      if (is_numeric($key)) {
        $fieldset_module_watcher = $form['permission'][$key]['#value'];

        //fieldset test
        $fieldset_form[$fieldset_module_watcher] = array(
          '#type' => 'fieldset',
          '#title' => t('@module module', array(
            '@module' => $fieldset_module_watcher,
          )),
          '#collapsible' => TRUE,
          '#collapsed' => FALSE,
          '#weight' => $fieldset_weight++,
        );
        $row[] = array(
          'data' => t('@module module', array(
            '@module' => drupal_render($form['permission'][$key]),
          )),
          'class' => 'module',
          'id' => 'module-' . $form['permission'][$key]['#value'],
          'colspan' => count($form['role_names']) + 1,
        );
      }
      else {
        $row[] = array(
          'data' => drupal_render($form['permission'][$key]),
          'class' => 'permission',
        );
        foreach (element_children($form['checkboxes']) as $rid) {
          if (is_array($form['checkboxes'][$rid])) {
            $row[] = array(
              'data' => drupal_render($form['checkboxes'][$rid][$key]),
              'class' => 'checkbox',
              'title' => $roles[$rid] . ' : ' . t($key),
            );
          }
        }
        $fieldset_rows[] = $row;
      }
      if ($fieldset_module_watcher != $fieldset_module_watcher_old) {

        //this should mean our module changed

        //but first check if this is not the first iteration where _old will be empty and always != the current _watcher
        if (!empty($fieldset_module_watcher_old)) {

          //if we got here then it means that old module watcher was changed and this is not first iteration. so we must build the table.
          $fieldset_form[$fieldset_module_watcher_old][$fieldset_module_watcher_old . 'perms'] = array(
            '#type' => 'item',
            '#value' => theme('table', $header, $fieldset_rows, array(
              'id' => 'permissions',
            )),
          );
          unset($fieldset_rows);
          $fieldset_module_watcher_old = $fieldset_module_watcher;
        }
        else {

          //because this is first iteration we simply set the _watcher_old
          $fieldset_module_watcher_old = $fieldset_module_watcher;
        }
      }
      $rows[] = $row;
    }
  }

  //this step is necessary because the loop above exits before last module's permissions get added to our fieldsets

  //i dont like the whole approach on principle and would like a cleaner and more streamlined solution

  //TODO: come up with a better algorithm to do this whole theme
  $fieldset_form[$fieldset_module_watcher_old][$fieldset_module_watcher_old . 'perms'] = array(
    '#type' => 'item',
    '#value' => theme('table', $header, $fieldset_rows, array(
      'id' => 'permissions',
    )),
  );
  $fieldset_output = drupal_render($fieldset_form);
  $fieldset_output .= drupal_render($form);
  return $fieldset_output;

  //  return $output;
}