You are here

function theme_perms_fieldsets_theme in Util 6.2

Same name and namespace in other branches
  1. 6.3 perms_fieldsets.module \theme_perms_fieldsets_theme()
  2. 6 perms_fieldsets.module \theme_perms_fieldsets_theme()

File

./perms_fieldsets.module, line 84
Set default collapsed/expanded state for Access Permissions

Code

function theme_perms_fieldsets_theme($form) {
  $fieldset_output = '<p>' . l(t('Set collapsed state'), 'admin/settings/util/apfm') . '</p>';
  $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();
  $collapse_all = variable_get('perms_fieldsets_collapsed', 0);
  $selected = array_filter(variable_get('perms_fieldsets_cfg', array()));
  $roles = user_roles();

  // Fieldset weight counter.
  $fieldset_weight = 0;

  // Module watcher.
  // Because role information is not grouped, we need to emulate grouping.
  // 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'];

        // Final decision on collapsing this field set.
        if ($collapse_all) {
          $collapsed = isset($selected[$fieldset_module_watcher]) ? FALSE : TRUE;
        }
        else {
          $collapsed = isset($selected[$fieldset_module_watcher]) ? TRUE : FALSE;
        }
        $fieldset_form[$fieldset_module_watcher] = array(
          '#type' => 'fieldset',
          '#title' => '<a name="module-' . $fieldset_module_watcher . '">' . t('@module module', array(
            '@module' => $fieldset_module_watcher,
          )) . '</a>',
          '#collapsible' => TRUE,
          '#collapsed' => $collapsed,
          '#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;
}