| 1 |
<?php
|
| 2 |
// $Id: update_form_enhancement.module,v 1.1 2008/09/20 19:57:36 markuspetrux Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Enhances update.php user interface by separating modules that are up to date
|
| 7 |
* from those that are not.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_help().
|
| 12 |
*/
|
| 13 |
function update_form_enhancement_help($path, $arg) {
|
| 14 |
switch ($path) {
|
| 15 |
case 'admin/help#update_form_enhancement':
|
| 16 |
return t('Enhances update.php user interface by separating modules that are up to date from those that are not.');
|
| 17 |
}
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Implementation of hook_form_alter().
|
| 22 |
*
|
| 23 |
* This function is used to alter the form that is displayed on second step of
|
| 24 |
* update.php script to select modules that need to be updated.
|
| 25 |
*
|
| 26 |
* When all modules are up to date, a message is displayed for the site admin
|
| 27 |
* and the update form remains unchanged. The site admin can still reapply any
|
| 28 |
* update task should (s)he needs to.
|
| 29 |
*
|
| 30 |
* When all modules have pending update tasks, a message is displayed for the
|
| 31 |
* site admin and the update form remains unchanged.
|
| 32 |
* This particular scenario may never happen, but anyway...
|
| 33 |
*
|
| 34 |
* When some modules have pending updates and some are up to date, they are
|
| 35 |
* grouped into separated collapsible fieldset. The fieldset used to group
|
| 36 |
* modules with pending updates is displayed first and expanded, so site admins
|
| 37 |
* can easilly check what's pending. The fieldset used to group up to date
|
| 38 |
* modules is rendered last and collapsed.
|
| 39 |
*/
|
| 40 |
function update_form_enhancement_form_alter(&$form, $form_state, $form_id) {
|
| 41 |
if ($form_id == 'update_script_selection_form' && is_array($form['start'])) {
|
| 42 |
$modules = array(
|
| 43 |
'changed' => array(),
|
| 44 |
'unchanged' => array(),
|
| 45 |
);
|
| 46 |
foreach ($form['start'] as $module => $select_box) {
|
| 47 |
// Ignore FAPI attributes.
|
| 48 |
if ($module[0] == '#') {
|
| 49 |
continue;
|
| 50 |
}
|
| 51 |
// We cannot be sure if someone else may have altered update form, so we quit and
|
| 52 |
// do nothing if this is not a selection box to prevent from being the cause of
|
| 53 |
// unexpected results.
|
| 54 |
if ($select_box['#type'] != 'select' || !is_array($select_box['#options'])) {
|
| 55 |
return;
|
| 56 |
}
|
| 57 |
// If default value is equal to last option, then this module is up to date.
|
| 58 |
$last_option = array_pop(array_keys($select_box['#options']));
|
| 59 |
$module_state = ($select_box['#default_value'] == $last_option ? 'unchanged' : 'changed');
|
| 60 |
$modules[$module_state][$module] = $select_box;
|
| 61 |
}
|
| 62 |
|
| 63 |
// If no modules require update, then just advise the site admin and quit.
|
| 64 |
if (empty($modules['changed'])) {
|
| 65 |
drupal_set_message(t('All installed modules are up to date. You may still reapply any update task should you need to.'));
|
| 66 |
return;
|
| 67 |
}
|
| 68 |
|
| 69 |
// If all modules require update, then just warn the site admin and quit.
|
| 70 |
// This may never happen, but anyway...
|
| 71 |
if (empty($modules['unchanged'])) {
|
| 72 |
drupal_set_message(t('All installed modules have pending update tasks.'));
|
| 73 |
return;
|
| 74 |
}
|
| 75 |
|
| 76 |
// We have pending updates, so we can open the Select module fieldset.
|
| 77 |
$form['start']['#collapsed'] = FALSE;
|
| 78 |
$form['start']['#description'] = t('Modules with pending update tasks are grouped first. You may still reapply any update task to any module should you need to.');
|
| 79 |
|
| 80 |
// Create a new fieldset to group modules that need updates.
|
| 81 |
$form['start']['changed'] = array(
|
| 82 |
'#type' => 'fieldset',
|
| 83 |
'#title' => 'Pending updates',
|
| 84 |
'#description' => t('All modules in this group have pending update tasks.'),
|
| 85 |
'#collapsible' => TRUE,
|
| 86 |
'#collapsed' => FALSE,
|
| 87 |
'#weight' => -10,
|
| 88 |
);
|
| 89 |
foreach ($modules['changed'] as $module => $select_box) {
|
| 90 |
unset($form['start'][$module]);
|
| 91 |
$form['start']['changed'][$module] = $select_box;
|
| 92 |
}
|
| 93 |
|
| 94 |
// Finally, group unchanged modules into a separate fieldset, if any.
|
| 95 |
if (!empty($modules['unchanged'])) {
|
| 96 |
$form['start']['unchanged'] = array(
|
| 97 |
'#type' => 'fieldset',
|
| 98 |
'#title' => 'Up to date modules',
|
| 99 |
'#description' => t('All modules in this group are up to date.'),
|
| 100 |
'#collapsible' => TRUE,
|
| 101 |
'#collapsed' => TRUE,
|
| 102 |
'#weight' => -9,
|
| 103 |
);
|
| 104 |
foreach ($modules['unchanged'] as $module => $select_box) {
|
| 105 |
unset($form['start'][$module]);
|
| 106 |
$form['start']['unchanged'][$module] = $select_box;
|
| 107 |
}
|
| 108 |
}
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
/**
|
| 113 |
* Implementation of hook_boot().
|
| 114 |
*
|
| 115 |
* This function is used to undo the changes made by our own implementation of
|
| 116 |
* hook_form_alter() before the POST data is processed by the next step in
|
| 117 |
* update.php script. We could do this in several places, but hook_boot() looks
|
| 118 |
* like a good enough place to do so.
|
| 119 |
* Note that FormAPI is not invoked to process the form. Rather, the $_POST
|
| 120 |
* array is directly accessed by function update_batch() in update.php where
|
| 121 |
* it is expected to have the same structure it was initially built with.
|
| 122 |
*/
|
| 123 |
function update_form_enhancement_boot() {
|
| 124 |
if (isset($_POST['form_id']) && $_POST['form_id'] === 'update_script_selection_form' && isset($_POST['start'])) {
|
| 125 |
if (!empty($_POST['start']['changed'])) {
|
| 126 |
$_POST['start'] += $_POST['start']['changed'];
|
| 127 |
}
|
| 128 |
if (!empty($_POST['start']['unchanged'])) {
|
| 129 |
$_POST['start'] += $_POST['start']['unchanged'];
|
| 130 |
}
|
| 131 |
unset($_POST['start']['changed']);
|
| 132 |
unset($_POST['start']['unchanged']);
|
| 133 |
}
|
| 134 |
}
|