You are here

function theme_perms_fieldsets_theme in Util 6

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

File

./perms_fieldsets.module, line 56

Code

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;
}