Request #7656: Add auto-save draft to IMP
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 10 Dec 2009 19:06:47 +0000 (12:06 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 10 Dec 2009 21:10:07 +0000 (14:10 -0700)
imp/compose.php
imp/config/prefs.php.dist
imp/docs/CHANGES
imp/js/compose.js

index 1dcd29c..2b8822c 100644 (file)
@@ -368,8 +368,11 @@ case 'redirect_send':
     }
     break;
 
+case 'auto_save_draft':
+case 'save_draft':
 case 'send_message':
-    if ($compose_disable) {
+    // Drafts readonly is handled below.
+    if (($actionID == 'send_message') && $compose_disable) {
         break;
     }
 
@@ -381,7 +384,6 @@ case 'send_message':
         $notification->push($e);
         break;
     }
-    $header['replyto'] = $identity->getValue('replyto_addr');
 
     $header['to'] = $imp_ui->getAddressList(Horde_Util::getFormData('to'));
     if ($prefs->getValue('compose_cc')) {
@@ -391,8 +393,57 @@ case 'send_message':
         $header['bcc'] = $imp_ui->getAddressList(Horde_Util::getFormData('bcc'));
     }
 
-    $message = Horde_Util::getFormData('message');
     $header['subject'] = Horde_Util::getFormData('subject', '');
+    $message = Horde_Util::getFormData('message');
+
+    /* Save the draft. */
+    if (($actionID == 'auto_save_draft') || ($actionID == 'save_draft')) {
+        if (!$readonly_drafts) {
+            try {
+                $old_uid = $imp_compose->getMetadata('draft_uid');
+
+                $result = $imp_compose->saveDraft($header, $message, Horde_Nls::getCharset(), $rtemode);
+
+                /* Delete existing draft. */
+                $imp_ui->removeDraft($old_uid);
+
+                /* Closing draft if requested by preferences. */
+                if ($actionID == 'save_draft') {
+                    $imp_compose->destroy();
+
+                    if ($isPopup) {
+                        if ($prefs->getValue('close_draft')) {
+                            Horde_Util::closeWindowJS();
+                            exit;
+                        } else {
+                            $notification->push($result, 'horde.success');
+                        }
+                    } else {
+                        $notification->push($result);
+                        header('Location: ' . _mailboxReturnURL(false));
+                        exit;
+                    }
+                }
+            } catch (IMP_Compose_Exception $e) {
+                if ($actionID == 'save_draft') {
+                    $notification->push($e, 'horde.error');
+                }
+            }
+        }
+
+        if ($actionID == 'auto_save_draft') {
+            $request = new stdClass;
+            $request->requestToken = Horde::getRequestToken('imp.compose');
+            $request->formToken = Horde_Token::generateId('compose');
+            Horde::sendHTTPResponse(Horde::prepareResponse($request), 'json');
+            exit;
+        }
+
+        $get_sig = false;
+        break;
+    }
+
+    $header['replyto'] = $identity->getValue('replyto_addr');
 
     if ($smf = Horde_Util::getFormData('sent_mail_folder')) {
         $sent_mail_folder = $smf;
@@ -410,7 +461,8 @@ case 'send_message':
     try {
         $sent = $imp_compose->buildAndSendMessage($message, $header, $charset, $rtemode, $options);
 
-        if ($prefs->getValue('auto_delete_drafts')) {
+        if ($prefs->getValue('auto_save_drafts') ||
+            $prefs->getValue('auto_delete_drafts')) {
             $imp_ui->removeDraft($imp_compose->getMetadata('draft_uid'));
         }
 
@@ -454,50 +506,6 @@ case 'send_message':
     }
     exit;
 
-case 'save_draft':
-    if ($readonly_drafts) {
-        break;
-    }
-
-    /* Set up the From address based on the identity. */
-    try {
-        $header['from'] = $identity->getFromLine(null, Horde_Util::getFormData('from'));
-    } catch (Horde_Exception $e) {
-        $header['from'] = '';
-        $get_sig = false;
-        $notification->push($e);
-        break;
-    }
-    foreach (array('to', 'cc', 'bcc', 'subject') as $val) {
-        $header[$val] = Horde_Util::getFormData($val);
-    }
-    $message = Horde_Util::getFormData('message', '');
-
-    /* Save the draft. */
-    try {
-        $result = $imp_compose->saveDraft($header, $message, Horde_Nls::getCharset(), $rtemode);
-        $imp_compose->destroy();
-
-        /* Closing draft if requested by preferences. */
-        if ($isPopup) {
-            if ($prefs->getValue('close_draft')) {
-                Horde_Util::closeWindowJS();
-                exit;
-            } else {
-                $notification->push($result, 'horde.success');
-            }
-        } else {
-            $notification->push($result);
-            header('Location: ' . _mailboxReturnURL(false));
-            exit;
-        }
-    } catch (IMP_Compose_Exception $e) {
-        $notification->push($e, 'horde.error');
-    }
-
-    $get_sig = false;
-    break;
-
 case 'fwd_digest':
     $indices = Horde_Util::getFormData('fwddigest');
     if (!empty($indices)) {
@@ -772,15 +780,16 @@ if (!$rtemode) {
 
 /* Define some variables used in the javascript code. */
 $js_code = array(
+    'ImpCompose.auto_save = ' . intval($prefs->getValue('auto_save_drafts')),
     'ImpCompose.cancel_url = \'' . $cancel_url . '\'',
-    'ImpCompose.spellcheck = ' . intval($spellcheck && $prefs->getValue('compose_spellcheck')),
     'ImpCompose.cursor_pos = ' . (is_null($cursor_pos) ? 'null' : $cursor_pos),
     'ImpCompose.max_attachments = ' . (($max_attach === true) ? 'null' : $max_attach),
     'ImpCompose.popup = ' . intval($isPopup),
     'ImpCompose.redirect = ' . intval($redirect),
+    'ImpCompose.reloaded = ' . intval($token),
     'ImpCompose.rtemode = ' . intval($rtemode),
     'ImpCompose.smf_check = ' . intval($smf_check),
-    'ImpCompose.reloaded = ' . intval($token)
+    'ImpCompose.spellcheck = ' . intval($spellcheck && $prefs->getValue('compose_spellcheck'))
 );
 
 /* Create javascript identities array. */
index 545829a..ad76550 100644 (file)
@@ -782,7 +782,6 @@ $_prefs['unseen_drafts'] = array(
     'desc' => _("Save drafts as unseen?"));
 
 // auto-save drafts? value is in minutes, 0 == don't save.
-// Only works w/DIMP
 $_prefs['auto_save_drafts'] = array(
     'value' => 5,
     // Locked by default
index ff77a6a..8596e32 100644 (file)
@@ -2,6 +2,7 @@
 v5.0-git
 --------
 
+[mms] Add auto-save draft to IMP (Request #7656).
 [mms] Add hook to dynamically change mailbox label (Request #6734).
 [mms] Improved address expansion in MIMP.
 [mms] Load folders on-demand in sidebar (DIMP).
index 84e9591..3b47835 100644 (file)
@@ -7,8 +7,9 @@
 
 var ImpCompose = {
     // Variables defined in compose.php:
-    //   cancel_url, spellcheck, cursor_pos, identities, max_attachments,
-    //   popup, redirect, reloaded, rtemode, smf_check, skip_spellcheck
+    //   cancel_url, spellcheck, cursor_pos, identities, last_msg,
+    //   max_attachments, popup, redirect, reloaded, rtemode, smf_check,
+    //   skip_spellcheck
     display_unload_warning: true,
 
     confirmCancel: function(e)
@@ -149,7 +150,7 @@ var ImpCompose = {
 
     uniqSubmit: function(actionID, e)
     {
-        var form;
+        var cur_msg, form;
 
         if (!Object.isUndefined(e)) {
             e.stop();
@@ -194,6 +195,22 @@ var ImpCompose = {
             $('actionID').setValue(actionID);
             break;
 
+        case 'auto_save_draft':
+            // Move HTML text to textarea field for submission.
+            if (this.rtemode) {
+                CKEDITOR.instances.composeMessage.updateElement();
+            }
+
+            cur_msg = $F('composeMessage').replace(/\r/g, '');
+            if (!cur_msg.empty() && this.last_msg != cur_msg) {
+                // Use an AJAX submit here so that the page doesn't reload.
+                $('actionID').setValue(actionID);
+                $('compose').request({ onComplete: this._autoSaveDraft.bind(this) });
+
+                this.last_msg = cur_msg;
+            }
+            return;
+
         case 'toggle_editor':
             form = $('compose');
             break;
@@ -217,6 +234,15 @@ var ImpCompose = {
         this.uniqSubmit(actionID, e);
     },
 
+    _autoSaveDraft: function(r, o)
+    {
+        if (r.responseJSON && r.responseJSON.response) {
+            r = r.responseJSON.response;
+            $('compose_formToken').setValue(r.formToken);
+            $('compose_requestToken').setValue(r.requestToken);
+        }
+    },
+
     attachmentChanged: function()
     {
         var fields = [],
@@ -384,6 +410,10 @@ var ImpCompose = {
         document.observe('click', this.clickHandler.bindAsEventListener(this));
         document.observe('change', this.changeHandler.bindAsEventListener(this));
 
+        if (this.auto_save) {
+            new PeriodicalExecuter(this.uniqSubmit.bind(this, 'auto_save_draft'), this.auto_save * 60);
+        }
+
         this.resize.bind(this).delay(0.25);
     },