You are here

function autosave_save in Autosave 7

Same name and namespace in other branches
  1. 5.3 autosave.module \autosave_save()
  2. 5 autosave.module \autosave_save()
  3. 5.2 autosave.module \autosave_save()
  4. 6.2 autosave.module \autosave_save()
  5. 6 autosave.module \autosave_save()
  6. 7.2 autosave.module \autosave_save()

Menu callback; autosaves the node.

1 string reference to 'autosave_save'
autosave_menu in ./autosave.module
Implements hook_menu().

File

./autosave.module, line 134
Does background saves of node being edited.

Code

function autosave_save() {
  global $user;
  $path = $_POST['q'];
  $form_id = $_POST['form_id'];

  // Not all variables need to be serialized.
  //    - for Drupal 6 version need to remove op and form_build_id
  unset($_POST['q'], $_POST['op'], $_POST['form_build_id']);
  $serialized = serialize($_POST['serialized']);

  // check if node has just been saved - if it has then it's because AS ajax fired off as user was submitting
  // if it had just been submitted - no need to AS now
  //    - easy to figure out if we are submitting an edit to existing node
  //    - little harder if we have just added a node
  $path_args = explode("/", $path);

  // update case
  if (is_numeric($path_args[1])) {
    $submitted = node_load($path_args[1]);
  }
  else {

    // add case
    $submitted->changed = db_query("SELECT created FROM {node} WHERE uid = :uid and type = :type ORDER BY created DESC LIMIT 1", array(
      ':uid' => $user->uid,
      ':type' => str_replace("-", "_", $path_args[2]),
    ))
      ->fetchField();
  }
  if (!$submitted || REQUEST_TIME - $submitted->changed > 10) {

    // Currently, each user can have only one autosave form at a particular path.
    db_delete('autosaved_forms')
      ->condition('form_id', $form_id)
      ->condition('path', $path)
      ->condition('uid', $user->uid)
      ->execute();
    $id = db_insert('autosaved_forms')
      ->fields(array(
      'form_id' => $form_id,
      'path' => $path,
      'uid' => $user->uid,
      'timestamp' => REQUEST_TIME,
      'serialized' => $serialized,
    ))
      ->execute();
  }
  exit;
}