From 0ce2126ac91f634b0bc5bf7f3567acd2f87f9972 Mon Sep 17 00:00:00 2001 From: Thomas Bruederli Date: Thu, 29 Aug 2013 17:29:18 +0200 Subject: New settings section to manage canned responses --- program/include/rcmail.php | 25 +++++++ program/js/app.js | 84 +++++++++++++++++++++++- program/localization/en_US/labels.inc | 1 + program/localization/en_US/messages.inc | 1 + program/steps/mail/compose.inc | 15 ++--- program/steps/settings/edit_response.inc | 108 +++++++++++++++++++++++++++++++ program/steps/settings/func.inc | 3 + program/steps/settings/responses.inc | 81 +++++++++++++++++++++-- skins/larry/includes/settingstabs.html | 1 + skins/larry/templates/responseedit.html | 22 +++++++ skins/larry/templates/responses.html | 41 ++++++++++++ 11 files changed, 364 insertions(+), 18 deletions(-) create mode 100644 program/steps/settings/edit_response.inc create mode 100644 skins/larry/templates/responseedit.html create mode 100644 skins/larry/templates/responses.html diff --git a/program/include/rcmail.php b/program/include/rcmail.php index 02287d312..9713cdb4e 100644 --- a/program/include/rcmail.php +++ b/program/include/rcmail.php @@ -346,6 +346,31 @@ class rcmail extends rcube return $list; } + /** + * Getter for compose responses. + * These are stored in local config and user preferences. + * + * @param boolean True to sort the list alphabetically + * @return array List of the current user's stored responses + */ + public function get_compose_responses($sorted = false) + { + foreach ($this->config->get('compose_responses', array()) as $response) { + if (empty($response['key'])) + $response['key'] = substr(md5($response['name']), 0, 16); + $k = $sorted ? strtolower($response['name']) : $response['key']; + $responses[$k] = $response; + } + + if ($sorted) { + // sort list by name + ksort($responses, SORT_LOCALE_STRING); + return array_values($responses); + } + + return $responses; + } + /** * Init output object for GUI and add common scripts. diff --git a/program/js/app.js b/program/js/app.js index a7a92175d..cef688204 100644 --- a/program/js/app.js +++ b/program/js/app.js @@ -399,7 +399,7 @@ function rcube_webmail() break; case 'settings': - this.enable_command('preferences', 'identities', 'save', 'folders', true); + this.enable_command('preferences', 'identities', 'responses', 'save', 'folders', true); if (this.env.action == 'identities') { this.enable_command('add', this.env.identities_level < 2); @@ -420,6 +420,9 @@ function rcube_webmail() parent.rcmail.enable_command('purge', this.env.messagecount); $("input[type='text']").first().select(); } + else if (this.env.action == 'responses') { + this.enable_command('add', true); + } if (this.gui_objects.identitieslist) { this.identity_list = new rcube_list_widget(this.gui_objects.identitieslist, {multiselect:false, draggable:false, keyboard:false}); @@ -436,8 +439,22 @@ function rcube_webmail() this.sections_list.init(); this.sections_list.focus(); } - else if (this.gui_objects.subscriptionlist) + else if (this.gui_objects.subscriptionlist) { this.init_subscription_list(); + } + else if (this.gui_objects.responseslist) { + this.responses_list = new rcube_list_widget(this.gui_objects.responseslist, {multiselect:false, draggable:false, keyboard:false}); + this.responses_list.addEventListener('select', function(list){ + var win, id = list.get_single_selection(); + p.enable_command('delete', !!id); + if (id && (win = p.get_frame_window(p.env.contentframe))) { + p.set_busy(true); + p.location_href({ _action:'edit-response', _key:id, _framed:1 }, win); + } + }); + this.responses_list.init(); + this.responses_list.focus(); + } break; @@ -743,6 +760,13 @@ function rcube_webmail() case 'add': if (this.task == 'addressbook') this.load_contact(0, 'add'); + else if (this.task == 'settings' && this.env.action == 'responses') { + var frame; + if ((frame = this.get_frame_window(this.env.contentframe))) { + this.set_busy(true); + this.location_href({ _action:'add-response', _framed:1 }, frame); + } + } else if (this.task == 'settings') { this.identity_list.clear_selection(); this.load_identity(0, 'add-identity'); @@ -806,7 +830,10 @@ function rcube_webmail() // addressbook task else if (this.task == 'addressbook') this.delete_contacts(); - // user settings task + // settings: canned response + else if (this.task == 'settings' && this.env.action == 'responses') + this.delete_response(); + // settings: user identities else if (this.task == 'settings') this.delete_identity(); break; @@ -1191,6 +1218,7 @@ function rcube_webmail() // user settings commands case 'preferences': case 'identities': + case 'responses': case 'folders': this.goto_url('settings/' + command); break; @@ -3428,6 +3456,27 @@ function rcube_webmail() } }; + this.edit_responses = function() + { + // TODO: decide what to do here... + }; + + this.delete_response = function(key) + { + if (!key && this.responses_list) { + var selection = this.responses_list.get_selection(); + key = selection[0]; + } + + // submit delete request + if (key && confirm(this.get_label('deleteresponseconfirm'))) { + this.http_post('settings/delete-response', { _key: key }, false); + return true; + } + + return false; + }; + this.stop_spellchecking = function() { var ed; @@ -5343,6 +5392,35 @@ function rcube_webmail() } }; + this.update_response_row = function(response, oldkey) + { + var list = this.responses_list; + + if (list && oldkey) { + list.update_row(oldkey, [ response.name ], response.key, true); + } + else if (list) { + list.insert_row({ id:'rcmrow'+response.key, cols:[ { className:'name', innerHTML:response.name } ] }); + list.select(response.key); + } + }; + + this.remove_response = function(key) + { + var frame; + + if (this.env.textresponses) { + delete this.env.textresponses[key]; + } + + if (this.responses_list) { + this.responses_list.remove_row(key); + if (this.env.contentframe && (frame = this.get_frame_window(this.env.contentframe))) { + frame.location.href = this.env.blankpage; + } + } + }; + /*********************************************************/ /********* folder manager methods *********/ diff --git a/program/localization/en_US/labels.inc b/program/localization/en_US/labels.inc index 8cda30990..1f0697ccb 100644 --- a/program/localization/en_US/labels.inc +++ b/program/localization/en_US/labels.inc @@ -237,6 +237,7 @@ $labels['insertresponse'] = 'Insert a response'; $labels['manageresponses'] = 'Manage responses'; $labels['savenewresponse'] = 'Save new response'; $labels['editresponses'] = 'Edit responses'; +$labels['editresponse'] = 'Edit response'; $labels['responsename'] = 'Name'; $labels['responsetext'] = 'Response Text'; diff --git a/program/localization/en_US/messages.inc b/program/localization/en_US/messages.inc index e9feb243d..c2a7b6e29 100644 --- a/program/localization/en_US/messages.inc +++ b/program/localization/en_US/messages.inc @@ -47,6 +47,7 @@ $messages['savingmessage'] = 'Saving message...'; $messages['messagesaved'] = 'Message saved to Drafts.'; $messages['successfullysaved'] = 'Successfully saved.'; $messages['savingresponse'] = 'Saving response text...'; +$messages['deleteresponseconfirm'] = 'Do you really want to delete this response text?'; $messages['addedsuccessfully'] = 'Contact added successfully to address book.'; $messages['contactexists'] = 'A contact with the same e-mail address already exists.'; $messages['contactnameexists'] = 'A contact with the same name already exists.'; diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc index efc0cc8e0..282a2fd3e 100644 --- a/program/steps/mail/compose.inc +++ b/program/steps/mail/compose.inc @@ -1707,23 +1707,16 @@ function rcmail_compose_responses_list($attrib) $attrib += array('id' => 'rcmresponseslist', 'tagname' => 'ul', 'cols' => 1); $jsenv = array(); - $items = array(); - foreach ($RCMAIL->config->get('compose_responses', array()) as $response) { - $key = $response['key'] ? $response['key'] : substr(md5($response['name']), 0, 16); - $items[strtolower($response['name'])] = html::a(array( + $list = new html_table($attrib); + foreach ($RCMAIL->get_compose_responses(true) as $response) { + $key = $response['key']; + $item = html::a(array( 'href '=> '#'.urlencode($response['name']), 'class' => rtrim('insertresponse ' . $attrib['itemclass']), 'rel' => $key, ), Q($response['name'])); $jsenv[$key] = $response; - } - - // sort list by name - ksort($items, SORT_LOCALE_STRING); - - $list = new html_table($attrib); - foreach ($items as $item) { $list->add(array(), $item); } diff --git a/program/steps/settings/edit_response.inc b/program/steps/settings/edit_response.inc new file mode 100644 index 000000000..26f7e6e48 --- /dev/null +++ b/program/steps/settings/edit_response.inc @@ -0,0 +1,108 @@ + | + +-----------------------------------------------------------------------+ +*/ + +$responses = $RCMAIL->get_compose_responses(); + +// edit-response +if (($key = get_input_value('_key', RCUBE_INPUT_GPC))) { + foreach ($responses as $i => $response) { + if (empty($response['key'])) + $response['key'] = substr(md5($response['name']), 0, 16); + if ($response['key'] == $key) { + $RESPONSE_RECORD = $response; + $RESPONSE_RECORD['index'] = $i; + break; + } + } +} + +// save response +if ($RCMAIL->action == 'save-response' && isset($_POST['_name'])) { + $name = trim(get_input_value('_name', RCUBE_INPUT_POST)); + $text = trim(get_input_value('_text', RCUBE_INPUT_POST)); + + if (!empty($_REQUEST['_framed'])) + $RCMAIL->output->framed = 1; + + if (!empty($name) && !empty($text)) { + $dupes = 0; + foreach ($responses as $i => $resp) { + if ($RESPONSE_RECORD && $RESPONSE_RECORD['index'] === $i) + continue; + if (strcasecmp($name, preg_replace('/\s\(\d+\)$/', '', $resp['name'])) == 0) + $dupes++; + } + if ($dupes) { // require a unique name + $name .= ' (' . ++$dupes . ')'; + } + + $response = array('name' => $name, 'text' => $text, 'format' => 'text', 'key' => substr(md5($name), 0, 16)); + if ($RESPONSE_RECORD && $responses[$RESPONSE_RECORD['index']]) { + $responses[$RESPONSE_RECORD['index']] = $response; + } + else { + $responses[] = $response; + } + + if ($RCMAIL->user->save_prefs(array('compose_responses' => $responses))) { + $RCMAIL->output->show_message('successfullysaved', 'confirmation'); + $RCMAIL->output->command('update_response_row', $response, $key); + $RESPONSE_RECORD = $response; + } + } + else { + $RCMAIL->output->show_message('formincomplete', 'error'); + } +} + + +function rcube_response_form($attrib) +{ + global $RCMAIL, $OUTPUT, $RESPONSE_RECORD; + + // Set form tags and hidden fields + $key = $RESPONSE_RECORD['key']; + list($form_start, $form_end) = get_form_tags($attrib, 'save-response', $key, array('name' => '_key', 'value' => $key)); + unset($attrib['form'], $attrib['id']); + + // return the complete edit form as table + $out = "$form_start\n"; + + $table = new html_table(array('cols' => 2)); + $label = rcube_label('responsename'); + + $table->add('title', html::label('ffname', Q(rcube_label('responsename')))); + $table->add(null, rcube_output::get_edit_field('name', $RESPONSE_RECORD['name'], array('id' => 'ffname', 'size' => $attrib['size']), 'text')); + + $table->add('title', html::label('fftext', Q(rcube_label('responsetext')))); + $table->add(null, rcube_output::get_edit_field('text', $RESPONSE_RECORD['text'], array('id' => 'fftext', 'size' => $attrib['textareacols'], 'rows' => $attrib['textarearows']), 'textarea')); + + $out .= $table->show($attrib); + $out .= $form_end; + + return $out; +} + +$OUTPUT->add_handler('responseform', 'rcube_response_form'); +$OUTPUT->set_pagetitle(rcube_label(($RCMAIL->action=='add-response' ? 'savenewresponse' : 'editresponse'))); + +$OUTPUT->send('responseedit'); + diff --git a/program/steps/settings/func.inc b/program/steps/settings/func.inc index fdc07be9e..39a925e0e 100644 --- a/program/steps/settings/func.inc +++ b/program/steps/settings/func.inc @@ -1253,4 +1253,7 @@ $RCMAIL->register_action_map(array( 'purge' => 'folders.inc', 'folder-size' => 'folders.inc', 'add-identity' => 'edit_identity.inc', + 'add-response' => 'edit_response.inc', + 'save-response' => 'edit_response.inc', + 'delete-response' => 'responses.inc', )); diff --git a/program/steps/settings/responses.inc b/program/steps/settings/responses.inc index 5a7db5687..330b4fde4 100644 --- a/program/steps/settings/responses.inc +++ b/program/steps/settings/responses.inc @@ -21,12 +21,12 @@ if (!empty($_POST['_insert'])) { - $name = get_input_value('_name', RCUBE_INPUT_POST); + $name = trim(get_input_value('_name', RCUBE_INPUT_POST)); $text = trim(get_input_value('_text', RCUBE_INPUT_POST)); if (!empty($name) && !empty($text)) { $dupes = 0; - $responses = $RCMAIL->config->get('compose_responses', array()); + $responses = $RCMAIL->get_compose_responses(); foreach ($responses as $resp) { if (strcasecmp($name, preg_replace('/\s\(\d+\)$/', '', $resp['name'])) == 0) $dupes++; @@ -46,8 +46,81 @@ if (!empty($_POST['_insert'])) { $RCMAIL->output->command('display_message', rcube_label('errorsaving'), 'error'); } } + + // send response + $RCMAIL->output->send(); +} + + +if ($RCMAIL->action == 'delete-response') { + if ($key = get_input_value('_key', RCUBE_INPUT_GPC)) { + $responses = $RCMAIL->get_compose_responses(); + foreach ($responses as $i => $response) { + if (empty($response['key'])) + $response['key'] = substr(md5($response['name']), 0, 16); + if ($response['key'] == $key) { + unset($responses[$i]); + $deleted = $RCMAIL->user->save_prefs(array('compose_responses' => $responses)); + break; + } + } + } + + if ($deleted) { + $RCMAIL->output->command('display_message', rcube_label('successfullydeleted'), 'confirmation'); + $RCMAIL->output->command('remove_response', $key); + } + + if ($RCMAIL->output->ajax_call) { + $RCMAIL->output->send(); + } +} + + +$OUTPUT->set_pagetitle(rcube_label('responses')); +$OUTPUT->include_script('list.js'); + + +/** + * + */ +function rcmail_responses_list($attrib) +{ + global $RCMAIL, $OUTPUT; + + $attrib += array('id' => 'rcmresponseslist', 'tagname' => 'table', 'cols' => 1); + + $plugin = $RCMAIL->plugins->exec_hook('responses_list', array( + 'list' => $RCMAIL->get_compose_responses(true), + 'cols' => array('name') + )); + + $out = rcube_table_output($attrib, $plugin['list'], $plugin['cols'], 'key'); + + // set client env + $OUTPUT->add_gui_object('responseslist', $attrib['id']); + + return $out; +} + + +// similar function as /steps/addressbook/func.inc::rcmail_contact_frame() +function rcmail_response_frame($attrib) +{ + global $OUTPUT; + + if (!$attrib['id']) { + $attrib['id'] = 'rcmResponseFrame'; + } + + $OUTPUT->set_env('contentframe', $attrib['id']); + return $OUTPUT->frame($attrib, true); } -// send response -$RCMAIL->output->send(); +$OUTPUT->add_handlers(array( + 'responseframe' => 'rcmail_response_frame', + 'responseslist' => 'rcmail_responses_list', +)); +$OUTPUT->add_label('deleteresponseconfirm'); +$OUTPUT->send('responses'); diff --git a/skins/larry/includes/settingstabs.html b/skins/larry/includes/settingstabs.html index bb26fc6a6..14d875696 100644 --- a/skins/larry/includes/settingstabs.html +++ b/skins/larry/includes/settingstabs.html @@ -4,6 +4,7 @@ + diff --git a/skins/larry/templates/responseedit.html b/skins/larry/templates/responseedit.html new file mode 100644 index 000000000..d2f031b34 --- /dev/null +++ b/skins/larry/templates/responseedit.html @@ -0,0 +1,22 @@ + + + +<roundcube:object name="pagetitle" /> + + + + +

+ +
+ +
+ +
+ +
+ + + + + diff --git a/skins/larry/templates/responses.html b/skins/larry/templates/responses.html new file mode 100644 index 000000000..fb40048c8 --- /dev/null +++ b/skins/larry/templates/responses.html @@ -0,0 +1,41 @@ + + + +<roundcube:object name="pagetitle" /> + + + + + + +
+ + + +
+ +
+

+
+ +
+
+ +
+
+ +
+
+ +
+ +
+ +
+ +
+ + + + + -- cgit v1.2.3