You are here

autosave.module in Autosave 5.2

Automatically saves a node after a period of time.

File

autosave.module
View source
<?php

/**
 * @file
 * Automatically saves a node after a period of time.
 */

/**
 * Implementation of hook_help().
 */
function autosave_help($section) {
  switch ($section) {
    case 'admin/help#autosave':
      $output = '<p>' . t('The autosave module automatically saves a form after a period of time.') . '</p>';
      return $output;
  }
}

/**
 * Implementation of hook_menu().
 */
function autosave_menu($may_cache) {
  global $user;
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/autosave',
      'title' => t('Autosave save'),
      'callback' => 'autosave_save',
      'access' => TRUE,
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/settings/autosave',
      'title' => t('Autosave'),
      'description' => t('Configure autosave settings.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'autosave_admin_settings',
      ),
      'access' => user_access('administer nodes'),
    );
  }
  return $items;
}

/**
 * Implementation of hook_form_alter().
 */
function autosave_form_alter($form_id, &$form) {

  // TODO: Allow user to configure which forms can be autosaved.
  if (end(explode('/', $_GET['q'])) == 'configure-autosave') {
    drupal_add_js('var configure_autosave = true;', 'inline');
    drupal_add_js(drupal_get_path('module', 'autosave') . '/autosave.js');
    drupal_add_js(drupal_get_path('module', 'autosave') . '/jquery.field.js');
    drupal_add_css(drupal_get_path('module', 'autosave') . '/autosave.css');
  }
  if ($form_id != 'search_theme_form') {
    global $user;
    $path = $_GET['q'];
    drupal_add_js(drupal_get_path('module', 'autosave') . '/autosave.js');
    drupal_add_js(drupal_get_path('module', 'autosave') . '/jquery.field.js');
    drupal_add_css(drupal_get_path('module', 'autosave') . '/autosave.css');
    if ($form['type']['#value'] . '_node_form' == $form_id) {
      $settings['form_id'] = 'node_form';
    }
    else {
      $settings['form_id'] = $form_id;
    }
    $settings['period'] = variable_get('autosave_period', 10);
    $settings['autosave_url'] = url('admin/autosave');
    $settings['q'] = $path;

    // If an autosaved version of the form exists, make it available via javascript.
    if ($autosaved_form = autosave_get_autosaved_form($form_id, $path, $user->uid)) {
      $autosaved_form_id = $form['type']['#value'] . '_node_form' == $form_id ? 'node_form' : $form_id;
      $settings['autosaved_form'][] = array(
        'form_id' => $autosaved_form_id,
        'serialized' => unserialize($autosaved_form['serialized']),
        'saved_date' => format_date($autosaved_form['timestamp'], 'medium'),
      );
    }
    drupal_add_js($settings, 'setting');
  }
}

/**
 * Menu callback; return the autosave module settings form.
 */
function autosave_admin_settings() {
  $types = node_get_types();
  $path = drupal_get_path('module', 'autosave');
  if (!file_exists($path . '/jquery.field.js')) {
    drupal_set_message(t('Unable to find the jQuery Field Plugin in !path. Please <a href="http://plugins.jquery.com/files/jquery.field.js_4.txt">download jquery.field.js</a> and place it into !path.', array(
      '!path' => $path,
    )), 'error');
  }
  $form['autosave_period'] = array(
    '#type' => 'textfield',
    '#title' => t('Autosave after this amount seconds has passed'),
    '#default_value' => variable_get('autosave_period', 10),
  );
  return system_settings_form($form);
}

/**
 * Menu callback; autosaves the node.
 */
function autosave_save() {
  global $user;
  $path = $_POST['q'];
  $form_id = $_POST['form_id'];

  // Not all variables need to be serialized.
  unset($_POST['form_token'], $_POST['q']);
  $serialized = serialize($_POST);

  // Currently, each user can have only one autosave form at a particular path.
  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;
}

/**
 * Get the autosaved form at a particular path for a user.
 *
 * @param string $form_id
 *   The form_id of the form.
 * @param string $path 
 *   The the internal Drupal path where the form is located
 * @param string $uid 
 *   Drupal UID of the user
 * @return
 *   An array containing the serialized values of the autosaved form and the timestamp of when the form was autosaved.
 */
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;
}

Functions

Namesort descending Description
autosave_admin_settings Menu callback; return the autosave module settings form.
autosave_form_alter Implementation of hook_form_alter().
autosave_get_autosaved_form Get the autosaved form at a particular path for a user.
autosave_help Implementation of hook_help().
autosave_menu Implementation of hook_menu().
autosave_save Menu callback; autosaves the node.