View source
<?php
define('AUTOSAVE_PATH', drupal_get_path('module', 'autosave'));
function autosave_help($path, $arg) {
$output = '';
switch ($path) {
case 'admin/help#autosave':
$output = '<p>' . t('The autosave module automatically saves a form after a period of time.') . '</p>';
break;
}
return $output;
}
function autosave_menu() {
$items['autosave/handler/%'] = array(
'title' => 'Autosave save',
'page callback' => 'autosave_save',
'access callback' => 'autosave_save_access',
'type' => MENU_CALLBACK,
);
$items['admin/settings/autosave'] = array(
'title' => 'Autosave',
'description' => 'Configure autosave settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'autosave_admin_settings',
),
'access arguments' => array(
'administer nodes',
),
);
return $items;
}
function autosave_save_access() {
$path = trim($_POST['autosave_path'], '/');
$menu_item = menu_get_item($path);
$token = isset($_POST['form_token'], $_POST['form_id']) && drupal_valid_token($_POST['form_token'], $_POST['form_id']);
$menu = isset($menu_item['access']) ? $menu_item['access'] : FALSE;
return $token && $menu;
}
function autosave_admin_settings() {
$form['autosave_period'] = array(
'#type' => 'textfield',
'#title' => t('Autosave after this amount seconds has passed'),
'#default_value' => variable_get('autosave_period', 10),
);
$form['autosave_hidden'] = array(
'#prefix' => '<div class="form-item"><label for="edit-autosave-hidden">' . t('Stealth Mode') . '</label>',
'#type' => 'checkbox',
'#title' => t('Run in stealth mode'),
'#description' => t('If this check box is selected no popup will appear notifying user that the form has been autosaved.'),
'#default_value' => variable_get('autosave_hidden', 0),
'#suffix' => "</div>",
);
return system_settings_form($form);
}
function autosave_form_node_type_form_alter(&$form, $form_state) {
$form['workflow']['autosave'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Autosave to add/edit forms for this node type'),
'#default_value' => variable_get('autosave_' . $form['#node_type']->type, 0),
'#description' => t('Check this box to enable Autosave for this node type.'),
);
}
function autosave_form_alter(&$form, &$form_state, $form_id) {
global $user;
$path = $_GET['q'];
if (stristr($form_id, '_node_form') && arg(0) != 'admin') {
if (variable_get('autosave_' . $form['type']['#value'], 0)) {
drupal_add_js(AUTOSAVE_PATH . '/autosave.js');
drupal_add_js(AUTOSAVE_PATH . '/jquery.field.js');
drupal_add_css(AUTOSAVE_PATH . '/autosave.css');
if (module_exists('wysiwyg')) {
$settings['autosave']['wysiwyg'] = 1;
}
else {
$settings['autosave']['wysiwyg'] = 0;
}
$token = drupal_get_token($form_id);
$settings['autosave']['url'] = url('autosave/handler/' . $token);
$settings['autosave']['period'] = variable_get('autosave_period', 10);
$settings['autosave']['autosave_path'] = $path;
$settings['autosave']['hidden'] = variable_get('autosave_hidden', 0);
if ($autosaved_form = autosave_get_autosaved_form($form_id, $path, $user->uid)) {
$settings['autosave'] = array_merge($settings['autosave'], array(
'serialized' => unserialize($autosaved_form['serialized']),
'saved_date' => format_date($autosaved_form['timestamp'], 'medium'),
));
}
drupal_add_js($settings, 'setting');
}
}
}
function autosave_save() {
global $user;
$path = $_POST['autosave_path'];
$form_id = $_POST['form_id'];
unset($_POST['autosave_path'], $_POST['op'], $_POST['form_build_id']);
$serialized = serialize($_POST);
$path_args = explode("/", $path);
if (is_numeric($path_args[1])) {
$submitted = node_load($path_args[1]);
}
else {
$submitted->changed = db_result(db_query("SELECT created FROM {node} WHERE uid = %d and type = '%s' ORDER BY created DESC LIMIT 1", $user->uid, str_replace("-", "_", $path_args[2])));
}
if (!$submitted || time() - $submitted->changed > 10) {
db_query("DELETE FROM {autosaved_forms} WHERE form_id = '%s' AND path = '%s' AND uid = %d", $form_id, $path, $user->uid);
db_query("INSERT INTO {autosaved_forms} (form_id, path, uid, timestamp, serialized) VALUES ('%s', '%s', %d, %d, '%s')", $form_id, $path, $user->uid, time(), $serialized);
}
exit;
}
function autosave_get_autosaved_form($form_id, $path, $uid) {
$result = db_query("SELECT form_id, serialized, timestamp FROM {autosaved_forms} WHERE form_id = '%s' AND path = '%s' AND uid = %d", $form_id, $path, $uid);
while ($data = db_fetch_object($result)) {
$form['serialized'] = $data->serialized;
$form['timestamp'] = $data->timestamp;
}
return $form;
}
function autosave_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($op == 'presave') {
db_query("DELETE FROM {autosaved_forms} WHERE form_id = '%s' AND path = '%s'", $node->form_id, $_GET['q']);
}
}