diff -u b/autosave.module b/autosave.module --- b/autosave.module +++ b/autosave.module @@ -172,20 +172,22 @@ // Store form arguments in db only if the form has not been autosaved // before. - $key = array( - 'form_id' => $form_id, - 'uid' => $GLOBALS['user']->uid, - 'path' => $path, - ); - db_merge('autosaved_forms') - ->key($key) - ->insertFields(array_merge($key, array( - 'timestamp' => 0, - 'serialized' => '', - 'args' => serialize($form_state['build_info']['args']) - ))) - ->updateFields($key) - ->execute(); + if (!empty($form_state['build_info']['args']) && strpos($path, 'autosave/restore/') !== 0) { + $key = array( + 'form_id' => $form_id, + 'uid' => $GLOBALS['user']->uid, + 'path' => $path, + ); + db_merge('autosaved_forms') + ->key($key) + ->insertFields(array_merge($key, array( + 'timestamp' => 0, + 'serialized' => '', + 'args' => serialize($form_state['build_info']['args']) + ))) + ->updateFields($key) + ->execute(); + } $settings['autosave']['formid'] = $form_id; $settings['autosave']['url'] = url('autosave/handler'); @@ -341,11 +343,15 @@ } /** - * Menu callback; autosaves the node. + * Menu callback; autosaves the form. */ function autosave_save() { global $user; + // Possibility to prevent autosaving. + $prevent_autosave = FALSE; + drupal_alter('autosave_prevent', $prevent_autosave); + $path = $_POST['autosave_form_path']; $form_id = $_POST['form_id']; // Not all variables need to be serialized. @@ -353,23 +359,7 @@ unset($_POST['autosave_form_path'], $_POST['op'], $_POST['form_build_id']); $serialized = serialize($_POST); - // 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 = db_query_range("SELECT created AS changed FROM {node} WHERE uid = :uid and type = :type ORDER BY created DESC", 0, 1, array( - ':uid' => $user->uid, - ':type' => str_replace("-", "_", $path_args[2])))->fetchObject(); - } - - if (!$submitted || (REQUEST_TIME - $submitted->changed) > 10) { + if (!$prevent_autosave) { // Currently, each user can have only one autosave form at a particular path. db_merge('autosaved_forms')->key(array( 'form_id' => $form_id, @@ -386,6 +376,30 @@ exit(); } +/** + * Implements hook_autosave_prevent(). + */ +function autosave_autosave_prevent(&$prevent_autosave) { + $path = $_POST['autosave_form_path']; + $path_args = explode("/", $path); + // 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 + if ($path_args[0] == 'node') { + // update case + if (is_numeric($path_args[1])) { + $submitted = node_load($path_args[1]); + } + else { + // add case + $submitted = db_query_range("SELECT created AS changed FROM {node} WHERE uid = :uid and type = :type ORDER BY created DESC", 0, 1, array( + ':uid' => $user->uid, + ':type' => str_replace("-", "_", $path_args[2])))->fetchObject(); + } + $prevent_autosave = $submitted ? TRUE : $prevent_autosave; + } +} /** * Get the autosaved form at a particular path for a user.