Move DIMP compose AJAX actions into Horde AJAX framework
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 21 Jan 2010 23:38:31 +0000 (16:38 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 28 Jan 2010 08:34:14 +0000 (01:34 -0700)
framework/Ajax/lib/Horde/Ajax/Application/Base.php
horde/services/ajax.php
imp/compose-dimp.php
imp/js/DimpBase.js
imp/js/DimpCore.js
imp/js/compose-dimp.js
imp/lib/Ajax/Application.php
imp/lib/Dimp.php
imp/templates/chunks/compose.php

index 86b9265..30d5530 100644 (file)
@@ -92,6 +92,18 @@ abstract class Horde_Ajax_Application_Base
     }
 
     /**
+     * Determines the HTTP response output type.
+     *
+     * @see Horde::sendHTTPResponse().
+     *
+     * @return string  The output type.
+     */
+    public function responseType()
+    {
+        return 'json';
+    }
+
+    /**
      * AJAX action: Logout.
      *
      * This needs to be done here (server), rather than on the browser,
index 571cc35..9d469ea 100644 (file)
@@ -41,7 +41,7 @@ try {
         $notification = Horde_Notification::singleton();
 
         $notification->push(str_replace('&amp;', '&', Horde_Auth::getLogoutUrl(array('reason' => Horde_Auth::REASON_SESSION))), 'horde.ajaxtimeout', array('content.raw'));
-        Horde::sendHTTPResponse(Horde::prepareResponse(null, $ajax->notificationHandler()), 'json');
+        Horde::sendHTTPResponse(Horde::prepareResponse(null, $ajax->notificationHandler()), $ajax->responseType());
         exit;
     }
 
@@ -62,4 +62,4 @@ if (ob_get_length()) {
 }
 
 // Send the final result.
-Horde::sendHTTPResponse(Horde::prepareResponse($result, $ajax->notificationHandler()), 'json');
+Horde::sendHTTPResponse(Horde::prepareResponse($result, $ajax->notificationHandler()), $ajax->responseType());
index 3f2bc95..3432e61 100644 (file)
@@ -29,7 +29,6 @@ foreach (array('to', 'cc', 'bcc', 'subject') as $v) {
     $header[$v] = rawurldecode(Horde_Util::getFormData($v, ''));
 }
 
-$action = Horde_Util::getFormData('action');
 $get_sig = true;
 $msg = '';
 
@@ -47,144 +46,6 @@ $imp_compose = IMP_Compose::singleton(Horde_Util::getFormData('composeCache'));
 /* Init IMP_Ui_Compose:: object. */
 $imp_ui = new IMP_Ui_Compose();
 
-if (count($_POST)) {
-    $result = new stdClass;
-    $result->action = $action;
-    $result->success = 0;
-
-    /* Update the file attachment information. */
-    if ($action == 'add_attachment') {
-        if ($_SESSION['imp']['file_upload'] &&
-            $imp_compose->addFilesFromUpload('file_')) {
-            $info = IMP_Dimp::getAttachmentInfo($imp_compose);
-            $result->success = 1;
-            $result->info = end($info);
-            $result->imp_compose = $imp_compose->getCacheId();
-        }
-        Horde::sendHTTPResponse(Horde::prepareResponse($result, $GLOBALS['imp_notify']), 'js-json');
-        exit;
-    }
-
-    /* Set the default charset. */
-    $charset = Horde_Nls::getEmailCharset();
-    if (!$prefs->isLocked('sending_charset')) {
-        $charset = Horde_Util::getFormData('charset', $charset);
-    }
-
-    switch ($action) {
-    case 'auto_save_draft':
-    case 'save_draft':
-    case 'send_message':
-        /* Set up the From address based on the identity. */
-        try {
-            $from = $identity->getFromLine(null, Horde_Util::getFormData('from'));
-        } catch (Horde_Exception $e) {
-            $notification->push($e);
-            break;
-        }
-        $header['from'] = $from;
-        $header['to'] = $imp_ui->getAddressList(Horde_Util::getFormData('to'));
-        if ($prefs->getValue('compose_cc')) {
-            $header['cc'] = $imp_ui->getAddressList(Horde_Util::getFormData('cc'));
-        }
-        if ($prefs->getValue('compose_bcc')) {
-            $header['bcc'] = $imp_ui->getAddressList(Horde_Util::getFormData('bcc'));
-        }
-
-        /* Save the draft. */
-        if (($action == 'auto_save_draft') ||
-            ($action == 'save_draft')) {
-            try {
-                $res = $imp_compose->saveDraft($header, Horde_Util::getFormData('message', ''), Horde_Nls::getCharset(), Horde_Util::getFormData('html'));
-                $result->success = 1;
-
-                if ($action == 'auto_save_draft') {
-                    $notification->push(_("Draft automatically saved."), 'horde.message');
-                } else {
-                    $notification->push($res);
-                    if ($prefs->getValue('close_draft')) {
-                        $imp_compose->destroy();
-                    }
-                }
-            } catch (IMP_Compose_Exception $e) {
-                $notification->push($e, 'horde.error');
-            }
-            break;
-        }
-
-        if ($compose_disable) {
-            break;
-        }
-
-        $header['replyto'] = $identity->getValue('replyto_addr');
-
-        $message = Horde_Util::getFormData('message');
-        $html = Horde_Util::getFormData('html');
-
-        $result->uid = $imp_compose->getMetadata('uid');
-
-        if ($reply_type = $imp_compose->getMetadata('reply_type')) {
-            $result->reply_folder = $imp_compose->getMetadata('mailbox');
-            $result->reply_type = $reply_type;
-        }
-
-        /* Use IMP_Tree to determine whether the sent mail folder was
-         * created. */
-        $imptree = IMP_Imap_Tree::singleton();
-        $imptree->eltDiffStart();
-
-        $options = array(
-            'priority' => Horde_Util::getFormData('priority'),
-            'readreceipt' => Horde_Util::getFormData('request_read_receipt'),
-            'save_attachments' => Horde_Util::getFormData('save_attachments_select'),
-            'save_sent' => (($prefs->isLocked('save_sent_mail'))
-                            ? $identity->getValue('save_sent_mail')
-                            : (bool)Horde_Util::getFormData('save_sent_mail')),
-            'sent_folder' => (($prefs->isLocked('save_sent_mail'))
-                              ? $identity->getValue('sent_mail_folder')
-                              : Horde_Util::getFormData('save_sent_mail_folder', $identity->getValue('sent_mail_folder')))
-        );
-
-        try {
-            $sent = $imp_compose->buildAndSendMessage($message, $header, $charset, $html, $options);
-        } catch (IMP_Compose_Exception $e) {
-            $notification->push($e->getMessage(), 'horde.error');
-            break;
-        }
-        $result->success = 1;
-
-        /* Remove any auto-saved drafts. */
-        if ($prefs->getValue('auto_save_drafts') ||
-            $prefs->getValue('auto_delete_drafts')) {
-            $result->draft_delete = 1;
-        }
-
-        if ($sent && $prefs->getValue('compose_confirm')) {
-            $notification->push(empty($header['subject']) ? _("Message sent successfully.") : sprintf(_("Message \"%s\" sent successfully."), Horde_String::truncate($header['subject'])), 'horde.success');
-        }
-
-        /* Update maillog information. */
-        if (!empty($GLOBALS['conf']['maillog']['use_maillog'])) {
-            $in_reply_to = $imp_compose->getMetadata('in_reply_to');
-            if (!empty($in_reply_to) &&
-                ($tmp = IMP_Dimp::getMsgLogInfo($in_reply_to))) {
-                $result->log = $tmp;
-            }
-        }
-
-        $imp_compose->destroy();
-
-        $res = IMP_Dimp::getFolderResponse($imptree);
-        if (!empty($res)) {
-            $result->folder = $res['a'][0];
-        }
-        break;
-    }
-
-    Horde::sendHTTPResponse(Horde::prepareResponse($result, $GLOBALS['imp_notify']), 'json');
-    exit;
-}
-
 /* Attach spellchecker & auto completer. */
 $imp_ui->attachAutoCompleter(array('to', 'cc', 'bcc'));
 $imp_ui->attachSpellChecker('dimp');
index e061955..372a569 100644 (file)
@@ -725,7 +725,7 @@ var DimpBase = {
         case 'ctx_vfolder_delete':
             tmp = baseelt.up('LI');
             if (window.confirm(DIMP.text.delete_folder.replace(/%s/, tmp.readAttribute('title')))) {
-                DimpCore.doAction('DeleteMailbox', { mbox: tmp.retrieve('mbox') }, { callback: this._mailboxCallback.bind(this) });
+                DimpCore.doAction('DeleteMailbox', { mbox: tmp.retrieve('mbox') }, { callback: this.mailboxCallback.bind(this) });
             }
             break;
 
@@ -1582,7 +1582,7 @@ var DimpBase = {
             dropbase = (drop == $('dropbase'));
             if (dropbase ||
                 (ftype != 'special' && !this.isSubfolder(drag, drop))) {
-                DimpCore.doAction('RenameMailbox', { old_name: drag.retrieve('mbox'), new_parent: dropbase ? '' : foldername, new_name: drag.retrieve('l') }, { callback: this._mailboxCallback.bind(this) });
+                DimpCore.doAction('RenameMailbox', { old_name: drag.retrieve('mbox'), new_parent: dropbase ? '' : foldername, new_name: drag.retrieve('l') }, { callback: this.mailboxCallback.bind(this) });
             }
         } else if (ftype != 'container') {
             sel = this.viewport.getSelected();
@@ -2204,13 +2204,13 @@ var DimpBase = {
             }
 
             if (action) {
-                DimpCore.doAction(action, params, { callback: this._mailboxCallback.bind(this) });
+                DimpCore.doAction(action, params, { callback: this.mailboxCallback.bind(this) });
             }
         }
     },
 
     /* Mailbox action callback functions. */
-    _mailboxCallback: function(r)
+    mailboxCallback: function(r)
     {
         r = r.response.mailbox;
 
@@ -2297,7 +2297,7 @@ var DimpBase = {
 
     _folderLoadCallback: function(r, callback)
     {
-        this._mailboxCallback(r);
+        this.mailboxCallback(r);
 
         if (callback) {
             callback();
index d8d5115..6d5a9aa 100644 (file)
@@ -102,14 +102,9 @@ var DimpCore = {
         return mlist;
     },
 
-    /* 'action' -> if action begins with a '*', the exact string will be used
-     *  instead of sending the action to the IMP handler.
-     *  'opts' -> ajaxopts, callback, uids */
+    // 'opts' -> ajaxopts, callback, uids
     doAction: function(action, params, opts)
     {
-        action = action.startsWith('*')
-            ? action.substring(1)
-            : DIMP.conf.URI_AJAX + action;
         params = $H(params);
         opts = opts || {};
 
@@ -138,7 +133,7 @@ var DimpCore = {
         ajaxopts.parameters = this.addRequestParams(params);
         ajaxopts.onComplete = function(t, o) { this.doActionComplete(t, opts.callback); }.bind(this);
 
-        new Ajax.Request(action, ajaxopts);
+        new Ajax.Request(DIMP.conf.URI_AJAX + action, ajaxopts);
     },
 
     // params - (Hash)
index 2c82d9c..0576285 100644 (file)
@@ -194,9 +194,9 @@ var DimpCompose = {
             this.skip_spellcheck = true;
         }
 
-        if (action == 'send_message' || action == 'save_draft') {
+        if (action == 'SendMessage' || action == 'SaveDraft') {
             switch (action) {
-            case 'send_message':
+            case 'SendMessage':
                 if (($F('subject') == '') &&
                     !window.confirm(DIMP.text_compose.nosubject)) {
                     return;
@@ -222,9 +222,8 @@ var DimpCompose = {
 
         c.setStyle({ cursor: 'wait' });
         this.skip_spellcheck = false;
-        $('action').setValue(action);
 
-        if (action == 'add_attachment') {
+        if (action == 'AddAttachment') {
             // We need a submit action here because browser security models
             // won't let us access files on user's filesystem otherwise.
             this.uploading = true;
@@ -237,11 +236,11 @@ var DimpCompose = {
 
             // Use an AJAX submit here so that we can do javascript-y stuff
             // before having to close the window on success.
-            DimpCore.doAction('*' + DIMP.conf.URI_COMPOSE, c.serialize(true), { callback: this.uniqueSubmitCallback.bind(this) });
+            DimpCore.doAction(action, c.serialize(true), { callback: this.uniqueSubmitCallback.bind(this) });
 
             // Can't disable until we send the message - or else nothing
             // will get POST'ed.
-            if (action == 'send_message' || action == 'save_draft') {
+            if (action == 'SendMessage' || action == 'SaveDraft') {
                 this.setDisabled(true);
             }
         }
@@ -260,15 +259,15 @@ var DimpCompose = {
             $('composeCache').setValue(d.imp_compose);
         }
 
-        if (d.success || d.action == 'add_attachment') {
+        if (d.success || d.action == 'AddAttachment') {
             switch (d.action) {
-            case 'auto_save_draft':
-            case 'save_draft':
+            case 'AutoSaveDraft':
+            case 'SaveDraft':
                 this.setDisabled(false);
 
                 this.updateDraftsMailbox();
 
-                if (d.action == 'save_draft') {
+                if (d.action == 'SaveDraft') {
                     if (this.is_popup && !DIMP.conf_compose.qreply) {
                         DIMP.baseWindow.DimpCore.showNotifications(r.msgs);
                         r.msgs = [];
@@ -279,15 +278,14 @@ var DimpCompose = {
                 }
                 break;
 
-            case 'send_message':
+            case 'SendMessage':
                 if (this.is_popup && DIMP.baseWindow.DimpBase) {
                     if (d.reply_type) {
                         DIMP.baseWindow.DimpBase.flag(d.reply_type == 'reply' ? '\\answered' : '$forwarded', true, { uid: d.uid, mailbox: d.reply_folder, noserver: true });
                     }
 
-                    // @TODO: Needed?
-                    if (d.folder) {
-                        DIMP.baseWindow.DimpBase.createFolder(d.folder);
+                    if (d.mailbox) {
+                        DIMP.baseWindow.DimpBase.mailboxCallback(d.mailbox);
                     }
 
                     if (d.draft_delete) {
@@ -305,7 +303,7 @@ var DimpCompose = {
                 }
                 return this.closeCompose();
 
-            case 'add_attachment':
+            case 'AddAttachment':
                 this.uploading = false;
                 if (d.success) {
                     this.addAttach(d.info.number, d.info.name, d.info.type, d.info.size);
@@ -499,7 +497,7 @@ var DimpCompose = {
                     : $F(msgval);
                 cur_msg = cur_msg.replace(/\r/g, '');
                 if (!cur_msg.empty() && this.last_msg != cur_msg) {
-                    this.uniqueSubmit('auto_save_draft');
+                    this.uniqueSubmit('AutoSaveDraft');
                     this.last_msg = cur_msg;
                 }
             }.bind(this), DIMP.conf_compose.auto_save_interval_val * 60);
@@ -637,7 +635,7 @@ var DimpCompose = {
     uploadAttachment: function()
     {
         var u = $('upload');
-        this.uniqueSubmit('add_attachment');
+        this.uniqueSubmit('AddAttachment');
         u.next().hide();
         u.replace(new Element('SPAN', { id: 'upload_wait' }).insert(DIMP.text_compose.uploading + ' (' + $F(u) + ')'));
     },
@@ -735,7 +733,7 @@ var DimpCompose = {
             case 'draft_button':
             case 'send_button':
                 if (!this.disabled) {
-                    this.uniqueSubmit(id == 'send_button' ? 'send_message' : 'save_draft');
+                    this.uniqueSubmit(id == 'send_button' ? 'SendMessage' : 'SaveDraft');
                 }
                 break;
 
index 8359b8b..156f49a 100644 (file)
@@ -33,6 +33,20 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
     }
 
     /**
+     * Determines the HTTP response output type.
+     *
+     * @see Horde::sendHTTPResponse().
+     *
+     * @return string  The output type.
+     */
+    public function responseType()
+    {
+        return ($this->_action == 'AddAttachment')
+            ? 'js-json'
+            : parent::responseType();
+    }
+
+    /**
      * AJAX action: Create a mailbox.
      *
      * @param Horde_Variables $vars  Variables used:
@@ -67,7 +81,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
             $new = $imptree->createMailboxName($vars->parent, $new);
             if ($result = $imp_folder->create($new, $GLOBALS['prefs']->getValue('subscribe'))) {
                 $result = new stdClass;
-                $result->mailbox = IMP_Dimp::getFolderResponse($imptree);
+                $result->mailbox = $this->_getMailboxResponse($imptree);
             }
         } catch (Horde_Exception $e) {
             $GLOBALS['notification']->push($e, 'horde.error');
@@ -115,7 +129,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
 
         if ($result) {
             $result = new stdClass;
-            $result->mailbox = IMP_Dimp::getFolderResponse($imptree);
+            $result->mailbox = $this->_getMailboxResponse($imptree);
         }
 
         return $result;
@@ -160,7 +174,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
                 $imp_folder->rename($vars->old_name, $new)) {
 
                 $result = new stdClass;
-                $result->mailbox = IMP_Dimp::getFolderResponse($imptree);
+                $result->mailbox = $this->_getMailboxResponse($imptree);
             }
         } catch (Horde_Exception $e) {
             $GLOBALS['notification']->push($e, 'horde.error');
@@ -309,7 +323,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
         }
 
         $result = new stdClass;
-        $result->mailbox = IMP_Dimp::getFolderResponse($imptree, array(
+        $result->mailbox = $this->_getMailboxResponse($imptree, array(
             'a' => array_values($folder_list),
             'c' => array(),
             'd' => array()
@@ -1327,6 +1341,258 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
     }
 
     /**
+     * AJAX action: Add an attachment to a compose message.
+     *
+     * @param Horde_Variables $vars  Variables used:
+     * <pre>
+     * 'composeCache' - (string) The IMP_Compose cache identifier.
+     * </pre>
+     *
+     * @return object  An object with the following entries:
+     * <pre>
+     * 'error' - (string) An error message.
+     * 'success' - (integer) 1 on success, 0 on failure.
+     * </pre>
+     */
+    public function AddAttachment($vars)
+    {
+        $imp_compose = IMP_Compose::singleton($vars->composeCache);
+
+        $result = new stdClass;
+        $result->action = 'AddAttachment';
+        $result->success = 0;
+
+        if ($_SESSION['imp']['file_upload'] &&
+            $imp_compose->addFilesFromUpload('file_')) {
+            $info = IMP_Dimp::getAttachmentInfo($imp_compose);
+            $result->success = 1;
+            $result->info = end($info);
+            $result->imp_compose = $imp_compose->getCacheId();
+        }
+
+        return $result;
+    }
+
+    /**
+     * AJAX action: Auto save a draft message.
+     *
+     * @param Horde_Variables $vars  See self::_dimpComposeSetup().
+     *
+     * @return object  See self::_dimpDraftAction().
+     */
+    public function AutoSaveDraft($vars)
+    {
+        return $this->_dimpDraftAction($vars);
+    }
+
+    /**
+     * AJAX action: Save a draft message.
+     *
+     * @param Horde_Variables $vars  See self::_dimpComposeSetup().
+     *
+     * @return object  See self::_dimpDraftAction().
+     */
+    public function SaveDraft($vars)
+    {
+        return $this->_dimpDraftAction($vars);
+    }
+
+    /**
+     * AJAX action: Send a message.
+     *
+     * @param Horde_Variables $vars  See the list of variables needed for
+     *                               _dimpComposeSetup(). Additional
+     *                               variables used:
+     * <pre>
+     * 'html' - (integer) In HTML compose mode?
+     * 'message' - (string) The message text.
+     * 'priority' - TODO
+     * 'request_read_receipt' - TODO
+     * 'save_attachments_select' - TODO
+     * 'save_sent_mail' - TODO
+     * 'save_sent_mail_folder' - (string) TODO
+     * </pre>
+     *
+     * @return object  An object with the following entries:
+     * <pre>
+     * 'action' - (string) The AJAX action string
+     * 'draft_delete' - (integer) TODO
+     * 'log' - (array) TODO
+     * 'mailbox' - (array) TODO
+     * 'reply_folder' - (string) TODO
+     * 'reply_type' - (string) TODO
+     * 'success' - (integer) 1 on success, 0 on failure.
+     * 'uid' - (integer) TODO
+     * </pre>
+     */
+    public function SendMessage($vars)
+    {
+        list($result, $imp_compose, $headers, $identity) = $this->_dimpComposeSetup($vars);
+        if (!IMP::canCompose()) {
+            $result->success = 0;
+        }
+        if (!$result->success) {
+            return $result;
+        }
+
+        $headers['replyto'] = $identity->getValue('replyto_addr');
+
+        $result->uid = $imp_compose->getMetadata('uid');
+
+        if ($reply_type = $imp_compose->getMetadata('reply_type')) {
+            $result->reply_folder = $imp_compose->getMetadata('mailbox');
+            $result->reply_type = $reply_type;
+        }
+
+        /* Use IMP_Tree to determine whether the sent mail folder was
+         * created. */
+        $imptree = IMP_Imap_Tree::singleton();
+        $imptree->eltDiffStart();
+
+        $options = array(
+            'priority' => $vars->priority,
+            'readreceipt' => $vars->request_read_receipt,
+            'save_attachments' => $vars->save_attachments_select,
+            'save_sent' => (($GLOBALS['prefs']->isLocked('save_sent_mail'))
+                            ? $identity->getValue('save_sent_mail')
+                            : (bool)$vars->save_sent_mail),
+            'sent_folder' => (($GLOBALS['prefs']->isLocked('save_sent_mail'))
+                              ? $identity->getValue('sent_mail_folder')
+                              : (isset($vars->save_sent_mail_folder) ? $vars->save_sent_mail_folder : $identity->getValue('sent_mail_folder')))
+        );
+
+        try {
+            // TODO: Use 'sending_charset'
+            $sent = $imp_compose->buildAndSendMessage($vars->message, $headers, Horde_Nls::getEmailCharset(), $vars->html, $options);
+        } catch (IMP_Compose_Exception $e) {
+            $result->success = 0;
+            $GLOBALS['notification']->push($e, 'horde.error');
+            return $result;
+        }
+
+        /* Remove any auto-saved drafts. */
+        if ($GLOBALS['prefs']->getValue('auto_save_drafts') ||
+            $GLOBALS['prefs']->getValue('auto_delete_drafts')) {
+            $result->draft_delete = 1;
+        }
+
+        if ($sent && $GLOBALS['prefs']->getValue('compose_confirm')) {
+            $GLOBALS['notification']->push(empty($headers['subject']) ? _("Message sent successfully.") : sprintf(_("Message \"%s\" sent successfully."), Horde_String::truncate($headers['subject'])), 'horde.success');
+        }
+
+        /* Update maillog information. */
+        if (!empty($GLOBALS['conf']['maillog']['use_maillog'])) {
+            $in_reply_to = $imp_compose->getMetadata('in_reply_to');
+            if (!empty($in_reply_to) &&
+                ($tmp = IMP_Dimp::getMsgLogInfo($in_reply_to))) {
+                $result->log = $tmp;
+            }
+        }
+
+        $imp_compose->destroy();
+
+        $result->mailbox = $this->_getMailboxResponse($imptree);
+
+        return $result;
+    }
+
+    /**
+     * Setup environment for dimp compose actions.
+     *
+     * <pre>
+     * 'composeCache' - (string) The IMP_Compose cache identifier.
+     * </pre>
+     * from, identity, composeCache
+     *
+     * @return array  An array with the following values:
+     * <pre>
+     * [0] (object) AJAX base return object (with action and success
+     *     parameters defined).
+     * [1] (IMP_Compose) The IMP_Compose object for the message.
+     * [2] (array) The list of headers for the object.
+     * [3] (Horde_Prefs_Identity) The identity used for the composition.
+     * </pre>
+     */
+    protected function _dimpComposeSetup($vars)
+    {
+        $result = new stdClass;
+        $result->action = $this->_action;
+        $result->success = 1;
+
+        /* Set up identity. */
+        $identity = Horde_Prefs_Identity::singleton(array('imp', 'imp'));
+        if (isset($vars->identity) &&
+            !$GLOBALS['prefs']->isLocked('default_identity')) {
+            $identity->setDefault($vars->identity);
+        }
+
+        /* Set up the From address based on the identity. */
+        $headers = array();
+        try {
+            $headers['from'] = $identity->getFromLine(null, $vars->from);
+        } catch (Horde_Exception $e) {
+            $GLOBALS['notification']->push($e);
+            $result->success = 1;
+            return array($result);
+        }
+
+        $imp_ui = new IMP_Ui_Compose();
+        $headers['to'] = $imp_ui->getAddressList($vars->to);
+        if ($GLOBALS['prefs']->getValue('compose_cc')) {
+            $headers['cc'] = $imp_ui->getAddressList($vars->cc);
+        }
+        if ($GLOBALS['prefs']->getValue('compose_bcc')) {
+            $headers['bcc'] = $imp_ui->getAddressList($vars->bcc);
+        }
+
+        $imp_compose = IMP_Compose::singleton($vars->composeCache);
+
+        return array($result, $imp_compose, $headers, $identity);
+    }
+
+    /**
+     * Save a draft composed message.
+     *
+     * @param Horde_Variables $vars  See the list of variables needed for
+     *                               _dimpComposeSetup(). Additional
+     *                               variables used:
+     * <pre>
+     * 'html' - (integer) In HTML compose mode?
+     * 'message' - (string) The message text.
+     * </pre>
+     *
+     * @return object  An object with the following entries:
+     * <pre>
+     * 'action' - (string) The AJAX action string
+     * 'success' - (integer) 1 on success, 0 on failure.
+     * </pre>
+     */
+    protected function _dimpDraftAction($vars)
+    {
+        list($result, $imp_compose, $headers, $identity) = $this->_dimpComposeSetup($vars);
+        if (!$result->success) {
+            return $result;
+        }
+
+        try {
+            $res = $imp_compose->saveDraft($headers, $vars->message, Horde_Nls::getCharset(), $vars->html);
+            if ($this->_action == 'AutoSaveDraft') {
+                $GLOBALS['notification']->push(_("Draft automatically saved."), 'horde.message');
+            } else {
+                $GLOBALS['notification']->push($res);
+                if ($GLOBALS['prefs']->getValue('close_draft')) {
+                    $imp_compose->destroy();
+                }
+            }
+        } catch (IMP_Compose_Exception $e) {
+            $result->success = 0;
+            $GLOBALS['notification']->push($e, 'horde.error');
+        }
+
+        return $result;
+    }
+
+    /**
      * Check the UID validity of the mailbox.
      *
      * @param Horde_Variables $vars  See the list of variables needed for
@@ -1536,4 +1802,144 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
         return null;
     }
 
+    /**
+     * Formats the response to send to javascript code when dealing with
+     * mailbox operations.
+     *
+     * @param IMP_Tree $imptree  An IMP_Tree object.
+     * @param array $changes     An array with three sub arrays - to be used
+     *                           instead of the return from
+     *                           $imptree->eltDiff():
+     *                           'a' - a list of mailboxes/objects to add
+     *                           'c' - a list of changed mailboxes
+     *                           'd' - a list of mailboxes to delete
+     *
+     * @return array  The object used by the JS code to update the folder
+     *                tree.
+     */
+    protected function _getMailboxResponse($imptree, $changes = null)
+    {
+        if (is_null($changes)) {
+            $changes = $imptree->eltDiff();
+        }
+        if (empty($changes)) {
+            return false;
+        }
+
+        $result = array();
+
+        if (!empty($changes['a'])) {
+            $result['a'] = array();
+            foreach ($changes['a'] as $val) {
+                $result['a'][] = $this->_createMailboxElt(is_array($val) ? $val : $imptree->element($val));
+            }
+        }
+
+        if (!empty($changes['c'])) {
+            $result['c'] = array();
+            foreach ($changes['c'] as $val) {
+                // Skip the base element, since any change there won't ever be
+                // updated on-screen.
+                if ($val != IMP_Imap_Tree::BASE_ELT) {
+                    $result['c'][] = $this->_createMailboxElt($imptree->element($val));
+                }
+            }
+        }
+
+        if (!empty($changes['d'])) {
+            $result['d'] = array_reverse($changes['d']);
+        }
+
+        return $result;
+    }
+
+    /**
+     * Create an object used by DimpCore to generate the folder tree.
+     *
+     * @param array $elt  The output from IMP_Tree::element().
+     *
+     * @return stdClass  The element object. Contains the following items:
+     * <pre>
+     * 'ch' (children) = Does the mailbox contain children? [boolean]
+     *                   [DEFAULT: no]
+     * 'cl' (class) = The CSS class. [string] [DEFAULT: 'base']
+     * 'co' (container) = Is this mailbox a container element? [boolean]
+     *                    [DEFAULT: no]
+     * 'i' (icon) = A user defined icon to use. [string] [DEFAULT: none]
+     * 'l' (label) = The mailbox display label. [string] [DEFAULT: 'm' val]
+     * 'm' (mbox) = The mailbox value. [string]
+     * 'n' (non-imap) = A non-IMAP element? [boolean] [DEFAULT: no]
+     * 'pa' (parent) = The parent element. [string] [DEFAULT:
+     *                 DIMP.conf.base_mbox]
+     * 'po' (polled) = Is the element polled? [boolean] [DEFAULT: no]
+     * 's' (special) = Is this a "special" element? [boolean] [DEFAULT: no]
+     * 't' (title) = The title value. [string] [DEFAULT: 'm' val]
+     * 'u' (unseen) = The number of unseen messages. [integer]
+     * 'un' (unsubscribed) = Is this mailbox unsubscribed? [boolean]
+     *                       [DEFAULT: no]
+     * 'v' (virtual) = Virtual folder? 0 = not vfolder, 1 = system vfolder,
+     *                 2 = user vfolder [integer] [DEFAULT: 0]
+     * </pre>
+     */
+    protected function _createMailboxElt($elt)
+    {
+        $ob = new stdClass;
+
+        if ($elt['children']) {
+            $ob->ch = 1;
+        }
+        $ob->cl = $elt['class'];
+        $ob->m = $elt['value'];
+        if ($ob->m != $elt['name']) {
+            $ob->l = $elt['name'];
+        }
+        if ($elt['parent'] != IMP_Imap_Tree::BASE_ELT) {
+            $ob->pa = $elt['parent'];
+        }
+        if ($elt['polled']) {
+            $ob->po = 1;
+        }
+        if ($elt['vfolder']) {
+            $ob->v = $GLOBALS['imp_search']->isEditableVFolder($elt['value']) ? 2 : 1;
+        }
+        if (!$elt['sub']) {
+            $ob->un = 1;
+        }
+
+        $tmp = IMP::getLabel($ob->m);
+        if ($tmp != $ob->m) {
+            $ob->t = $tmp;
+        }
+
+        if ($elt['container']) {
+            $ob->cl = 'exp';
+            $ob->co = 1;
+            if ($elt['nonimap']) {
+                $ob->n = 1;
+            }
+        } else {
+            if ($elt['polled']) {
+                $ob->u = intval($elt['unseen']);
+            }
+
+            if ($elt['special']) {
+                $ob->s = 1;
+            } elseif (!$elt['vfolder'] && $elt['children']) {
+                $ob->cl = 'exp';
+            }
+        }
+
+        if ($elt['user_icon']) {
+            $ob->cl = 'customimg';
+            $dir = empty($elt['icondir'])
+                ? $GLOBALS['registry']->getImageDir()
+                : $elt['icondir'];
+            $ob->i = empty($dir)
+                ? $elt['icon']
+                : $dir . '/' . $elt['icon'];
+        }
+
+        return $ob;
+    }
+
 }
index 7e9657c..bd79708 100644 (file)
@@ -98,145 +98,6 @@ class IMP_Dimp
     }
 
     /**
-     * Formats the response to send to javascript code when dealing with
-     * folder operations.
-     *
-     * @param IMP_Tree $imptree  An IMP_Tree object.
-     * @param array $changes     An array with three sub arrays - to be used
-     *                           instead of the return from
-     *                           $imptree->eltDiff():
-     *                           'a' - a list of folders/objects to add
-     *                           'c' - a list of changed folders
-     *                           'd' - a list of folders to delete
-     *
-     * @return array  The object used by the JS code to update the folder tree.
-     */
-    static public function getFolderResponse($imptree, $changes = null)
-    {
-        if (is_null($changes)) {
-            $changes = $imptree->eltDiff();
-        }
-        if (empty($changes)) {
-            return false;
-        }
-
-        $result = array();
-
-        if (!empty($changes['a'])) {
-            $result['a'] = array();
-            foreach ($changes['a'] as $val) {
-                $result['a'][] = self::_createFolderElt(is_array($val) ? $val : $imptree->element($val));
-            }
-        }
-
-        if (!empty($changes['c'])) {
-            $result['c'] = array();
-            foreach ($changes['c'] as $val) {
-                // Skip the base element, since any change there won't ever be
-                // updated on-screen.
-                if ($val != IMP_Imap_Tree::BASE_ELT) {
-                    $result['c'][] = self::_createFolderElt($imptree->element($val));
-                }
-            }
-        }
-
-        if (!empty($changes['d'])) {
-            $result['d'] = array_reverse($changes['d']);
-        }
-
-        return $result;
-    }
-
-    /**
-     * Create an object used by DimpCore to generate the folder tree.
-     *
-     * @param array $elt  The output from IMP_Tree::element().
-     *
-     * @return stdClass  The element object. Contains the following items:
-     * <pre>
-     * 'ch' (children) = Does the folder contain children? [boolean]
-     *                   [DEFAULT: no]
-     * 'cl' (class) = The CSS class. [string] [DEFAULT: 'base']
-     * 'co' (container) = Is this folder a container element? [boolean]
-     *                    [DEFAULT: no]
-     * 'i' (icon) = A user defined icon to use. [string] [DEFAULT: none]
-     * 'l' (label) = The folder display label. [string] [DEFAULT: 'm' val]
-     * 'm' (mbox) = The mailbox value. [string]
-     * 'n' (non-imap) = A non-IMAP element? [boolean] [DEFAULT: no]
-     * 'pa' (parent) = The parent element. [string] [DEFAULT:
-     *                 DIMP.conf.base_mbox]
-     * 'po' (polled) = Is the element polled? [boolean] [DEFAULT: no]
-     * 's' (special) = Is this a "special" element? [boolean] [DEFAULT: no]
-     * 't' (title) = The title value. [string] [DEFAULT: 'm' val]
-     * 'u' (unseen) = The number of unseen messages. [integer]
-     * 'un' (unsubscribed) = Is this folder unsubscribed? [boolean]
-     *                       [DEFAULT: no]
-     * 'v' (virtual) = Virtual folder? 0 = not vfolder, 1 = system vfolder,
-     *                 2 = user vfolder [integer] [DEFAULT: 0]
-     * </pre>
-     */
-    static private function _createFolderElt($elt)
-    {
-        $ob = new stdClass;
-
-        if ($elt['children']) {
-            $ob->ch = 1;
-        }
-        $ob->cl = $elt['class'];
-        $ob->m = $elt['value'];
-        if ($ob->m != $elt['name']) {
-            $ob->l = $elt['name'];
-        }
-        if ($elt['parent'] != IMP_Imap_Tree::BASE_ELT) {
-            $ob->pa = $elt['parent'];
-        }
-        if ($elt['polled']) {
-            $ob->po = 1;
-        }
-        if ($elt['vfolder']) {
-            $ob->v = $GLOBALS['imp_search']->isEditableVFolder($elt['value']) ? 2 : 1;
-        }
-        if (!$elt['sub']) {
-            $ob->un = 1;
-        }
-
-        $tmp = IMP::getLabel($ob->m);
-        if ($tmp != $ob->m) {
-            $ob->t = $tmp;
-        }
-
-        if ($elt['container']) {
-            $ob->cl = 'exp';
-            $ob->co = 1;
-            if ($elt['nonimap']) {
-                $ob->n = 1;
-            }
-        } else {
-            if ($elt['polled']) {
-                $ob->u = intval($elt['unseen']);
-            }
-
-            if ($elt['special']) {
-                $ob->s = 1;
-            } elseif (!$elt['vfolder'] && $elt['children']) {
-                $ob->cl = 'exp';
-            }
-        }
-
-        if ($elt['user_icon']) {
-            $ob->cl = 'customimg';
-            $dir = empty($elt['icondir'])
-                ? $GLOBALS['registry']->getImageDir()
-                : $elt['icondir'];
-            $ob->i = empty($dir)
-                ? $elt['icon']
-                : $dir . '/' . $elt['icon'];
-        }
-
-        return $ob;
-    }
-
-    /**
      * Return information about the current attachments for a message
      *
      * @param IMP_Compose $imp_compose  An IMP_Compose object.
index ad38a51..34cc09c 100644 (file)
@@ -19,9 +19,8 @@ $save_attach = $GLOBALS['prefs']->getValue('save_attachments');
 $compose_disable = !IMP::canCompose();
 
 ?>
-<form id="compose" name="compose" enctype="multipart/form-data" action="compose-dimp.php" method="post" target="submit_frame">
+<form id="compose" name="compose" enctype="multipart/form-data" action="<?php echo Horde::getServiceLink('ajax', 'imp') ?>AddAttachment" method="post" target="submit_frame">
 <?php echo Horde_Util::formInput() ?>
-<input type="hidden" id="action" name="action" />
 <input type="hidden" id="last_identity" name="last_identity" value="<?php echo (int)$selected_identity ?>" />
 <input type="hidden" id="html" name="html" value="<?php echo intval($rte && $compose_html) ?>" />
 <input type="hidden" id="composeCache" name="composeCache" value="<?php echo $composeCache ?>" />