| 1 |
<?php
|
| 2 |
// $Id: form_restore.module,v 1.1 2006/07/28 04:42:11 tema Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Restores non-submitted form values when user is back.
|
| 7 |
* Use it with add_n_reference.module
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_help().
|
| 12 |
*/
|
| 13 |
function form_restore_help($section) {
|
| 14 |
switch ($section) {
|
| 15 |
case 'admin/modules#description':
|
| 16 |
return t('Restores non-submitted form values. <em>Note: Use it with add_n_reference.module</em>');
|
| 17 |
}
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Implementation of hook_menu().
|
| 22 |
*/
|
| 23 |
function form_restore_menu($may_cache) {
|
| 24 |
$items = array();
|
| 25 |
if ($may_cache) {
|
| 26 |
// Settings page
|
| 27 |
$items[] = array(
|
| 28 |
'path' => 'admin/settings/form_restore/reset',
|
| 29 |
'title' => t('empty stored form data'),
|
| 30 |
'callback' => 'form_restore_reset_form',
|
| 31 |
'access' => user_access('empty form_restore data'),
|
| 32 |
'type' => MENU_NORMAL_ITEM,
|
| 33 |
);
|
| 34 |
}
|
| 35 |
return $items;
|
| 36 |
}
|
| 37 |
|
| 38 |
function form_restore_reset_form() {
|
| 39 |
return confirm_form('form_restore_reset_form', array(), t('Are you sure you want to empty all stored forms data?'), 'admin/settings', t('All filled in non-submitted data will be lost.'), t('Empty all'));
|
| 40 |
}
|
| 41 |
|
| 42 |
function form_restore_reset_form_submit() {
|
| 43 |
if (isset($_SESSION['post_stack'])) {
|
| 44 |
unset($_SESSION['post_stack']);
|
| 45 |
drupal_set_message(t('all saved form data was emptied'));
|
| 46 |
}
|
| 47 |
else {
|
| 48 |
drupal_set_message(t('there are no stored forms'));
|
| 49 |
}
|
| 50 |
return 'admin/settings';
|
| 51 |
}
|
| 52 |
|
| 53 |
/**
|
| 54 |
* Implementation of hook_form_alter().
|
| 55 |
*/
|
| 56 |
function form_restore_form_alter($form_id, &$form) {
|
| 57 |
if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
|
| 58 |
if (!isset($_POST['op'])) {
|
| 59 |
if (isset($_SESSION['post_stack'][$_REQUEST['q']])
|
| 60 |
&& _form_restore_set_default_values(&$form, $_SESSION['post_stack'][$_REQUEST['q']]['edit'])) {
|
| 61 |
$_SESSION['post_stack'][$_REQUEST['q']]['restored'] = true;
|
| 62 |
}
|
| 63 |
}
|
| 64 |
else if ($_POST['op'] == t('Submit') && isset($_SESSION['post_stack'][$_REQUEST['q']]['restored'])) {
|
| 65 |
if (isset($_SESSION['post_stack'][$_REQUEST['q']]['destination'])) {
|
| 66 |
$_REQUEST['destination'] = $_SESSION['post_stack'][$_REQUEST['q']]['destination'];
|
| 67 |
}
|
| 68 |
unset($_SESSION['post_stack'][$_REQUEST['q']]);
|
| 69 |
}
|
| 70 |
}
|
| 71 |
}
|
| 72 |
|
| 73 |
function _form_restore_set_default_values(&$form_item, $stored_item, $success = false) {
|
| 74 |
if (is_array($form_item) && is_array($stored_item)) {
|
| 75 |
$keys = array_intersect(array_keys($form_item), array_keys($stored_item));
|
| 76 |
if (!empty($keys)) {
|
| 77 |
foreach ($keys as $key) {
|
| 78 |
if (array_key_exists('#default_value', $form_item[$key])) {
|
| 79 |
if (is_array($stored_item[$key]) && !empty($stored_item[$key])) {
|
| 80 |
$form_item[$key]['#default_value'] = array_merge_recursive($form_item[$key]['#default_value'], $stored_item[$key]);
|
| 81 |
$success = true;
|
| 82 |
}
|
| 83 |
else if ($stored_item[$key] != '' && $form_item[$key]['#default_value'] != $stored_item[$key]) {
|
| 84 |
$form_item[$key]['#default_value'] = $stored_item[$key];
|
| 85 |
}
|
| 86 |
}
|
| 87 |
else {
|
| 88 |
$success = _form_restore_set_default_values($form_item[$key], $stored_item[$key], $success);
|
| 89 |
}
|
| 90 |
}
|
| 91 |
}
|
| 92 |
}
|
| 93 |
return $success;
|
| 94 |
}
|