1: <?php
2: /**
3: * Copyright 2013-2014 Horde LLC (http://www.horde.org/)
4: *
5: * See the enclosed file COPYING for license information (GPL). If you
6: * did not receive this file, see http://www.horde.org/licenses/gpl.
7: *
8: * @category Horde
9: * @copyright 2013-2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * Defines AJAX actions used for saving compose drafts.
16: *
17: * @author Michael Slusarz <slusarz@horde.org>
18: * @category Horde
19: * @copyright 2013-2014 Horde LLC
20: * @license http://www.horde.org/licenses/gpl GPL
21: * @package IMP
22: */
23: class IMP_Ajax_Application_Handler_Draft extends Horde_Core_Ajax_Application_Handler
24: {
25: /**
26: * The list of disabled draft actions.
27: *
28: * @var array
29: */
30: public $disabled = array();
31:
32: /**
33: * AJAX action: Auto save a draft message.
34: *
35: * @return object See _draftAction().
36: */
37: public function autoSaveDraft()
38: {
39: return $this->_draftAction('autoSaveDraft');
40: }
41:
42: /**
43: * AJAX action: Save a draft message.
44: *
45: * @return object See _draftAction().
46: */
47: public function saveDraft()
48: {
49: return $this->_draftAction('saveDraft');
50: }
51:
52: /**
53: * AJAX action: Save a template message.
54: *
55: * @return object See _draftAction().
56: */
57: public function saveTemplate()
58: {
59: return $this->_draftAction('saveTemplate');
60: }
61:
62: /* Protected methods. */
63:
64: /**
65: * Save a draft composed message.
66: *
67: * See the list of variables needed for
68: * IMP_Ajax_Application#composeSetup(). Additional variables used:
69: * - html: (integer) In HTML compose mode?
70: * - message: (string) The message text.
71: * - priority: (string) The priority of the message.
72: * - request_read_receipt: (boolean) Add request read receipt header?
73: *
74: * @param string $action AJAX action.
75: *
76: * @return object An object with the following entries:
77: * - action: (string) The AJAX action string
78: * - success: (integer) 1 on success, 0 on failure.
79: */
80: protected function _draftAction($action)
81: {
82: if (in_array($action, $this->disabled)) {
83: return false;
84: }
85:
86: try {
87: list($result, $imp_compose, $headers, ) = $this->_base->composeSetup($action);
88: } catch (Horde_Exception $e) {
89: $GLOBALS['notification']->push($e);
90:
91: $result = new stdClass;
92: $result->action = $action;
93: $result->success = 0;
94: return $result;
95: }
96:
97: $opts = array(
98: 'autosave' => ($action == 'autoSaveDraft'),
99: 'html' => $this->vars->html,
100: 'priority' => $this->vars->priority,
101: 'readreceipt' => $this->vars->request_read_receipt
102: );
103:
104: try {
105: switch ($action) {
106: case 'saveTemplate':
107: $res = $imp_compose->saveTemplate($headers, $this->vars->message, $opts);
108: break;
109:
110: default:
111: $res = $imp_compose->saveDraft($headers, $this->vars->message, $opts);
112: break;
113: }
114:
115: switch ($action) {
116: case 'autoSaveDraft':
117: $GLOBALS['notification']->push(_("Draft automatically saved."), 'horde.message');
118: break;
119:
120: case 'saveDraft':
121: if ($GLOBALS['prefs']->getValue('close_draft')) {
122: $imp_compose->destroy('save_draft');
123: }
124: // Fall-through
125:
126: default:
127: $GLOBALS['notification']->push($res);
128: break;
129: }
130: } catch (IMP_Compose_Exception $e) {
131: $result->success = 0;
132: $GLOBALS['notification']->push($e);
133: }
134:
135: return $result;
136: }
137:
138: }
139: