| 1 |
<?php
|
| 2 |
// $Id: perms_fieldsets.module,v 1.4 2008/05/30 15:26:00 litwol Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
*/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Implementation of hook_menu().
|
| 10 |
*/
|
| 11 |
function perms_fieldsets_menu() {
|
| 12 |
$menu['admin/settings/util/apfm'] = array(
|
| 13 |
'title' => 'Manage Access Permissions Fieldsets',
|
| 14 |
'description' => 'Manage Access Permissions Fieldsets',
|
| 15 |
'page callback' => 'drupal_get_form',
|
| 16 |
'page arguments' => array('perms_fieldsets_settings'),
|
| 17 |
'access arguments' => array('administer site configuration'),
|
| 18 |
'type' => MENU_LOCAL_TASK,
|
| 19 |
);
|
| 20 |
return $menu;
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Implementation of hook_theme().
|
| 25 |
*/
|
| 26 |
function perms_fieldsets_theme() {
|
| 27 |
return array(
|
| 28 |
'perms_fieldsets_theme' => array(
|
| 29 |
'arguments' => array('form' => null),
|
| 30 |
),
|
| 31 |
);
|
| 32 |
}
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Form builder; generate settings form.
|
| 36 |
*
|
| 37 |
* @ingroup forms
|
| 38 |
* @see system_settings_form()
|
| 39 |
*/
|
| 40 |
function perms_fieldsets_settings() {
|
| 41 |
$form['todo'] = array(
|
| 42 |
'#type' => 'fieldset',
|
| 43 |
'#title' => 'TODO list',
|
| 44 |
);
|
| 45 |
$form['todo']['item1'] = array(
|
| 46 |
'#type' => 'item',
|
| 47 |
'#value' => 'create settings for default module permission coppalsing / expanding',
|
| 48 |
);
|
| 49 |
$form['todo']['item2'] = array(
|
| 50 |
'#type' => 'item',
|
| 51 |
'#value' => 'Enable permission counter per role ( administrator[3/10] )',
|
| 52 |
);
|
| 53 |
return system_settings_form($form);
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Implementation of hook_form_alter().
|
| 58 |
*/
|
| 59 |
function perms_fieldsets_form_alter(&$form, $form_state, $form_id) {
|
| 60 |
// drupal_set_message($form_id);
|
| 61 |
// drupal_set_message('<pre>'. print_r($form, 1) .'</pre>');
|
| 62 |
switch ($form_id) {
|
| 63 |
case 'system_modules':
|
| 64 |
//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.
|
| 65 |
$form['description']['perms_fieldsets']['#value'] = t('Set default collapsed/expanded state for <a href="@url">Access Permissions</a>', array('@url' => url('admin/user/permissions')));
|
| 66 |
break;
|
| 67 |
case 'user_admin_perm':
|
| 68 |
$form['#theme'] = 'perms_fieldsets_theme';
|
| 69 |
break;
|
| 70 |
}
|
| 71 |
}
|
| 72 |
|
| 73 |
function theme_perms_fieldsets_theme($form) {
|
| 74 |
$header[] = array('data' => t('Permission'), 'width' => '100%');
|
| 75 |
foreach (element_children($form['role_names']) as $rid) {
|
| 76 |
if (is_array($form['role_names'][$rid])) {
|
| 77 |
$header[] = array('data' => drupal_render($form['role_names'][$rid]), 'class' => 'checkbox', 'nowrap' => 'nowrap');
|
| 78 |
}
|
| 79 |
}
|
| 80 |
//This is my custom fieldset-enabled form
|
| 81 |
$fieldset_form = array();
|
| 82 |
|
| 83 |
$roles = user_roles();
|
| 84 |
|
| 85 |
//fieldset weight counter
|
| 86 |
$fieldset_weight = 0;
|
| 87 |
|
| 88 |
//module watcher
|
| 89 |
//because role information is not grouped, we need to emulate groupping
|
| 90 |
//watching this variable will help us determine which module's permissions are being processed now
|
| 91 |
$fieldset_module_watcher = '';
|
| 92 |
$fieldset_module_watcher_old = '';
|
| 93 |
foreach (element_children($form['permission']) as $key) {
|
| 94 |
// Don't take form control structures
|
| 95 |
if (is_array($form['permission'][$key])) {
|
| 96 |
$row = array();
|
| 97 |
// Module name
|
| 98 |
if (is_numeric($key)) {
|
| 99 |
$fieldset_module_watcher = $form['permission'][$key]['#value'];
|
| 100 |
//fieldset test
|
| 101 |
$fieldset_form[$fieldset_module_watcher] = array(
|
| 102 |
'#type' => 'fieldset',
|
| 103 |
'#title' => t('@module module', array('@module' => $fieldset_module_watcher)),
|
| 104 |
'#collapsible' => TRUE,
|
| 105 |
'#collapsed' => FALSE,
|
| 106 |
'#weight' => $fieldset_weight++,
|
| 107 |
);
|
| 108 |
|
| 109 |
$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);
|
| 110 |
}
|
| 111 |
else {
|
| 112 |
|
| 113 |
$row[] = array('data' => drupal_render($form['permission'][$key]), 'class' => 'permission');
|
| 114 |
foreach (element_children($form['checkboxes']) as $rid) {
|
| 115 |
if (is_array($form['checkboxes'][$rid])) {
|
| 116 |
$row[] = array('data' => drupal_render($form['checkboxes'][$rid][$key]), 'class' => 'checkbox', 'title' => $roles[$rid] .' : '. t($key));
|
| 117 |
}
|
| 118 |
}
|
| 119 |
$fieldset_rows[] = $row;
|
| 120 |
}
|
| 121 |
|
| 122 |
if ($fieldset_module_watcher != $fieldset_module_watcher_old) {
|
| 123 |
//this should mean our module changed
|
| 124 |
//but first check if this is not the first iteration where _old will be empty and always != the current _watcher
|
| 125 |
if (!empty($fieldset_module_watcher_old)) {
|
| 126 |
//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.
|
| 127 |
$fieldset_form[$fieldset_module_watcher_old][$fieldset_module_watcher_old .'perms'] = array(
|
| 128 |
'#type' => 'item',
|
| 129 |
'#value' => theme('table', $header, $fieldset_rows, array('id' => 'permissions')),
|
| 130 |
);
|
| 131 |
unset($fieldset_rows);
|
| 132 |
$fieldset_module_watcher_old = $fieldset_module_watcher;
|
| 133 |
}
|
| 134 |
else {
|
| 135 |
//because this is first iteration we simply set the _watcher_old
|
| 136 |
$fieldset_module_watcher_old = $fieldset_module_watcher;
|
| 137 |
}
|
| 138 |
}
|
| 139 |
$rows[] = $row;
|
| 140 |
}
|
| 141 |
}
|
| 142 |
//this step is necessary because the loop above exits before last module's permissions get added to our fieldsets
|
| 143 |
//i dont like the whole approach on principle and would like a cleaner and more streamlined solution
|
| 144 |
//TODO: come up with a better algorithm to do this whole theme
|
| 145 |
$fieldset_form[$fieldset_module_watcher_old][$fieldset_module_watcher_old .'perms'] = array(
|
| 146 |
'#type' => 'item',
|
| 147 |
'#value' => theme('table', $header, $fieldset_rows, array('id' => 'permissions')),
|
| 148 |
);
|
| 149 |
|
| 150 |
$fieldset_output = drupal_render($fieldset_form);
|
| 151 |
$fieldset_output .= drupal_render($form);
|
| 152 |
return $fieldset_output;
|
| 153 |
// return $output;
|
| 154 |
}
|