From 0ce2126ac91f634b0bc5bf7f3567acd2f87f9972 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Wed, 04 Sep 2013 03:32:01 -0400
Subject: [PATCH] New settings section to manage canned responses

---
 skins/larry/templates/responseedit.html  |   22 +++
 program/steps/settings/responses.inc     |   81 +++++++++++
 program/steps/mail/compose.inc           |   15 -
 program/include/rcmail.php               |   25 +++
 program/localization/en_US/messages.inc  |    1 
 program/steps/settings/func.inc          |    3 
 program/localization/en_US/labels.inc    |    1 
 program/steps/settings/edit_response.inc |  108 +++++++++++++++
 program/js/app.js                        |   84 +++++++++++
 skins/larry/includes/settingstabs.html   |    1 
 skins/larry/templates/responses.html     |   41 +++++
 11 files changed, 364 insertions(+), 18 deletions(-)

diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index 02287d3..9713cdb 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -346,6 +346,31 @@
     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 a7a9217..cef6882 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -399,7 +399,7 @@
         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 @@
           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 @@
           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 @@
       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 @@
         // 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 @@
       // user settings commands
       case 'preferences':
       case 'identities':
+      case 'responses':
       case 'folders':
         this.goto_url('settings/' + command);
         break;
@@ -3428,6 +3456,27 @@
     }
   };
 
+  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 @@
     }
   };
 
+  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 8cda309..1f0697c 100644
--- a/program/localization/en_US/labels.inc
+++ b/program/localization/en_US/labels.inc
@@ -237,6 +237,7 @@
 $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 e9feb24..c2a7b6e 100644
--- a/program/localization/en_US/messages.inc
+++ b/program/localization/en_US/messages.inc
@@ -47,6 +47,7 @@
 $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 efc0cc8..282a2fd 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -1707,23 +1707,16 @@
     $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 0000000..26f7e6e
--- /dev/null
+++ b/program/steps/settings/edit_response.inc
@@ -0,0 +1,108 @@
+<?php
+
+/*
+ +-----------------------------------------------------------------------+
+ | program/steps/settings/edit_response.inc                              |
+ |                                                                       |
+ | This file is part of the Roundcube Webmail client                     |
+ | Copyright (C) 2013, The Roundcube Dev Team                            |
+ |                                                                       |
+ | Licensed under the GNU General Public License version 3 or            |
+ | any later version with exceptions for skins & plugins.                |
+ | See the README file for a full license statement.                     |
+ |                                                                       |
+ | PURPOSE:                                                              |
+ |   Show edit form for a canned response record or to add a new one     |
+ |                                                                       |
+ +-----------------------------------------------------------------------+
+ | Author: Thomas Bruederli <roundcube@gmail.com>                        |
+ +-----------------------------------------------------------------------+
+*/
+
+$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 fdc07be..39a925e 100644
--- a/program/steps/settings/func.inc
+++ b/program/steps/settings/func.inc
@@ -1253,4 +1253,7 @@
     '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 5a7db56..330b4fd 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 @@
             $RCMAIL->output->command('display_message', rcube_label('errorsaving'), 'error');
         }
     }
+
+    // send response
+    $RCMAIL->output->send();
 }
 
-// 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);
+}
+
+$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 bb26fc6..14d8756 100644
--- a/skins/larry/includes/settingstabs.html
+++ b/skins/larry/includes/settingstabs.html
@@ -4,6 +4,7 @@
 	<span id="settingstabpreferences" class="listitem preferences"><roundcube:button command="preferences" type="link" label="preferences" title="editpreferences" /></span>
 	<span id="settingstabfolders" class="listitem folders"><roundcube:button command="folders" type="link" label="folders" title="managefolders" /></span>
 	<span id="settingstabidentities" class="listitem identities"><roundcube:button command="identities" type="link" label="identities" title="manageidentities" /></span>
+	<span id="settingstabresponses" class="listitem responses"><roundcube:button command="responses" type="link" label="responses" title="editresponses" /></span>
 	<roundcube:container name="tabs" id="settings-tabs" />
 </div>
 </div>
diff --git a/skins/larry/templates/responseedit.html b/skins/larry/templates/responseedit.html
new file mode 100644
index 0000000..d2f031b
--- /dev/null
+++ b/skins/larry/templates/responseedit.html
@@ -0,0 +1,22 @@
+<roundcube:object name="doctype" value="html5" />
+<html>
+<head>
+<title><roundcube:object name="pagetitle" /></title>
+<roundcube:include file="/includes/links.html" />
+</head>
+<body class="iframe">
+
+<h1 class="boxtitle"><roundcube:object name="steptitle" /></h1>
+
+<div id="preferences-details" class="boxcontent">
+<roundcube:object name="responseform" class="propform" size="60" textareacols="60" textarearows="18" />
+</div>
+
+<div class="footerleft formbuttons">
+	<roundcube:button command="save" type="input" class="button mainaction" label="save" />
+</div>
+
+<roundcube:include file="/includes/footer.html" />
+
+</body>
+</html>
diff --git a/skins/larry/templates/responses.html b/skins/larry/templates/responses.html
new file mode 100644
index 0000000..fb40048
--- /dev/null
+++ b/skins/larry/templates/responses.html
@@ -0,0 +1,41 @@
+<roundcube:object name="doctype" value="html5" />
+<html>
+<head>
+<title><roundcube:object name="pagetitle" /></title>
+<roundcube:include file="/includes/links.html" />
+</head>
+<body class="noscroll">
+
+<roundcube:include file="/includes/header.html" />
+
+<div id="mainscreen" class="offset">
+
+<roundcube:include file="/includes/settingstabs.html" />
+
+<div id="settings-right">
+
+<div id="identitieslist" class="uibox listbox">
+<h2 class="boxtitle"><roundcube:label name="responses" /></h2>
+<div class="scroller withfooter">
+<roundcube:object name="responsesList" id="identities-table" class="listing" cellspacing="0" summary="Responses list" noheader="true" />
+</div>
+<div class="boxfooter">
+<roundcube:button command="add" type="link" title="savenewresponse" class="listbutton add disabled" classAct="listbutton add" innerClass="inner" content="+" /><roundcube:button command="delete" type="link" title="delete" class="listbutton delete disabled" classAct="listbutton delete" innerClass="inner" content="-" />
+</div>
+</div>
+
+<div id="identity-details" class="uibox contentbox">
+	<div class="iframebox">
+		<roundcube:object name="responseframe" id="preferences-frame" style="width:100%; height:100%" frameborder="0" src="/watermark.html" />
+	</div>
+	<roundcube:object name="message" id="message" class="statusbar" />
+</div>
+
+</div>
+
+</div>
+
+<roundcube:include file="/includes/footer.html" />
+
+</body>
+</html>

--
Gitblit v1.9.1