Abstract stationery code out into IMP_Compose_Stationery
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 1 Oct 2010 21:11:12 +0000 (15:11 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 1 Oct 2010 22:41:10 +0000 (16:41 -0600)
imp/compose.php
imp/lib/Compose/Stationery.php [new file with mode: 0644]
imp/lib/Prefs/Ui.php
imp/templates/prefs/stationery.html

index 0495bf7..a82de6e 100644 (file)
@@ -31,10 +31,8 @@ $vars = Horde_Variables::getDefaultVariables();
 
 /* Set the current identity. */
 $identity = $injector->getInstance('IMP_Identity');
-if (!$prefs->isLocked('default_identity')) {
-    if (!is_null($vars->identity)) {
-        $identity->setDefault($vars->identity);
-    }
+if (!$prefs->isLocked('default_identity') && !is_null($vars->identity)) {
+    $identity->setDefault($vars->identity);
 }
 
 /* Catch submits if javascript is not present. */
@@ -123,8 +121,9 @@ try {
     $notification->push($e);
 }
 
-/* Init IMP_Ui_Compose:: object. */
+/* Init objects. */
 $imp_ui = new IMP_Ui_Compose();
+$stationery = $injector->getInstance('IMP_Compose_Stationery');
 
 /* Set the default charset & encoding.
  * $charset - charset to use when sending messages
@@ -157,25 +156,6 @@ if ($_SESSION['imp']['rteavail']) {
     }
 }
 
-/* Load stationery. */
-$stationery_list = array();
-if (!$prefs->isLocked('stationery')) {
-    $stationery = null;
-    $all_stationery = @unserialize($prefs->getValue('stationery', false));
-    if (is_array($all_stationery)) {
-        $all_stationery = Horde_String::convertCharset($all_stationery, $prefs->getCharset());
-        foreach ($all_stationery as $id => $choice) {
-            if (($choice['t'] == 'plain') ||
-                (($choice['t'] == 'html') && $rtemode)) {
-                if ($rtemode && $choice['t'] == 'plain') {
-                    $choice['c'] = $imp_compose->text2html($choice['c']);
-                }
-                $stationery_list[$id] = $choice;
-            }
-        }
-    }
-}
-
 /* Update the file attachment information. */
 if ($_SESSION['imp']['file_upload']) {
     /* Only notify if we are reloading the compose screen. */
@@ -536,28 +516,12 @@ case 'selectlist_process':
     break;
 
 case 'change_stationery':
-    if (empty($stationery_list)) {
+    if (!count($stationery)) {
         break;
     }
-    $stationery = $vars->stationery;
-    if (strlen($stationery)) {
-        $stationery = (int)$stationery;
-        $stationery_content = $stationery_list[$stationery]['c'];
-        $msg = strval($vars->message);
-        if (strpos($stationery_content, '%s') !== false) {
-            $sig = $identity->getSignature();
-            if ($rtemode) {
-                $sig = $imp_compose->text2html($sig);
-                $stationery_content = $imp_compose->text2html($stationery_content);
-            }
-            $msg = str_replace(array("\r\n", $sig), array("\n", ''), $msg);
-            $stationery_content = str_replace('%s', $sig, $stationery_content);
-        }
-        if (strpos($stationery_content, '%c') === false) {
-            $msg .= $stationery_content;
-        } else {
-            $msg = str_replace('%c', $msg, $stationery_content);
-        }
+
+    if (isset($vars->stationery)) {
+        $msg = $stationery->getContent(intval($vars->stationery), $identity, strval($vars->message), $rtemode);
     }
     $get_sig = false;
     break;
@@ -865,12 +829,16 @@ if ($redirect) {
         $t->set('pri_opt', $priority_option);
     }
 
-    $t->set('stationery', !empty($stationery_list));
+    $t->set('stationery', count($stationery));
     if ($t->get('stationery')) {
         $t->set('stationery_label', Horde::label('stationery', _("Stationery")));
         $stationeries = array();
-        foreach ($stationery_list as $id => $choice) {
-            $stationeries[] = array('val' => $id, 'label' => $choice['n'], 'selected' => ($stationery === $id));
+        foreach ($stationery as $id => $choice) {
+            $stationeries[] = array(
+                'label' => $choice['n'],
+                'selected' => ($stationery === $id),
+                'val' => $id
+            );
         }
         $t->set('stationeries', $stationeries);
     }
diff --git a/imp/lib/Compose/Stationery.php b/imp/lib/Compose/Stationery.php
new file mode 100644 (file)
index 0000000..1a4f52c
--- /dev/null
@@ -0,0 +1,159 @@
+<?php
+/**
+ * This class provides access to IMP stationery data.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @license  http://www.fsf.org/copyleft/gpl.html GPL
+ * @package  IMP
+ */
+class IMP_Compose_Stationery implements ArrayAccess, Countable, Iterator
+{
+    /**
+     * Stationery list.
+     * Each entry has the following properties:
+     * <pre>
+     * 'c' => (string) Content.
+     * 'n' => (string) Name.
+     * 't' => (string) Type.
+     * </pre>
+     *
+     * @var array
+     */
+    protected $_stationery;
+
+    /**
+     * Constructor.
+     */
+    public function __construct()
+    {
+        $slist = @unserialize($GLOBALS['prefs']->getValue('stationery', false));
+        $this->_stationery = is_array($slist)
+            ? Horde_String::convertCharset($slist, $GLOBALS['prefs']->getCharset(), $GLOBALS['registry']->getCharset())
+            : array();
+    }
+
+    /**
+     * Substitute variables in stationery content.
+     *
+     * @param integer $id                   The stationery ID.
+     * @param IMP_Prefs_Identity $identity  The identity object.
+     * @param string $msg                   The message text.
+     * @param boolean $html                 Output HTML code?
+     *
+     * @return string  Stationery content.
+     */
+    public function getContent($id, IMP_Prefs_Identity $identity, $msg,
+                               $html = false)
+    {
+        $s_content = $this[$id]['c'];
+
+        if (strpos($s_content, '%s') !== false) {
+            $sig = $identity->getSignature($html ? 'html' : 'text');
+
+            switch ($this[$id]['t']) {
+            case 'html':
+                if (!$html) {
+                    $s_content = $GLOBALS['injector']->getInstance('Horde_Text_Filter')->filter($s_content, 'Html2text', array('charset' => $GLOBALS['registry']->getCharset()));
+                }
+                break;
+
+            case 'text':
+                if ($html) {
+                    $s_content = IMP_Compose::text2html($s_content);
+                }
+                break;
+            }
+
+            $msg = str_replace(array("\r\n", $sig), array("\n", ''), $msg);
+            $s_content = str_replace('%s', $sig, $s_content);
+        }
+
+        return (strpos($s_content, '%c') === false)
+            ? $s_content
+            : str_replace('%c', $msg, $s_content);
+    }
+
+    /**
+     * Save the current stationery list to preferences.
+     */
+    protected function _save()
+    {
+        $GLOBALS['prefs']->setValue('stationery', serialize(Horde_String::convertCharset($this->_stationery, $GLOBALS['registry']->getCharset(), $GLOBALS['prefs']->getCharset())), false);
+    }
+
+    /* ArrayAccess methods. */
+
+    public function offsetExists($offset)
+    {
+        return isset($this->_stationery[$offset]);
+    }
+
+    public function offsetGet($offset)
+    {
+        return isset($this->_stationery[$offset])
+            ? $this->_stationery[$offset]
+            : null;
+    }
+
+    public function offsetSet($offset, $value)
+    {
+        if (is_null($offset)) {
+            $this->_stationery[] = $value;
+        } else {
+            $this->_stationery[$offset] = $value;
+        }
+
+        $this->_save();
+    }
+
+    /* NOTE: this function reindexes the internal array. */
+    public function offsetUnset($offset)
+    {
+        if (isset($this->_stationery[$offset])) {
+            unset($this->_stationery[$offset]);
+            $this->_stationery = array_values($this->_stationery);
+            $this->_save();
+        }
+    }
+
+    /* Countable methods. */
+
+    public function count()
+    {
+        return count($this->_stationery);
+    }
+
+    /* Iterator methods. */
+
+    public function current()
+    {
+        return current($this->_stationery);
+    }
+
+    public function key()
+    {
+        return key($this->_stationery);
+    }
+
+    public function next()
+    {
+        next($this->_stationery);
+    }
+
+    public function rewind()
+    {
+        reset($this->_stationery);
+    }
+
+    public function valid()
+    {
+        return (key($this->_stationery) !== null);
+    }
+
+}
index d961c5a..c09004f 100644 (file)
@@ -1720,8 +1720,9 @@ class IMP_Prefs_Ui
     protected function _stationeryManagement($ui)
     {
         $ob = $this->_parseStationeryVars($ui);
+        $stationery = $GLOBALS['injector']->getInstance('IMP_Compose_Stationery');
 
-        if ($ob->stationery['t'] == 'html') {
+        if ($ob->type == 'html') {
             $GLOBALS['injector']->getInstance('Horde_Editor')->getEditor('Ckeditor', array('id' => 'content'));
         }
 
@@ -1729,7 +1730,7 @@ class IMP_Prefs_Ui
         $t->setOption('gettext', true);
 
         $slist = array();
-        foreach ($ob->stationery_list as $key => $choice) {
+        foreach ($stationery as $key => $choice) {
             $slist[] = array(
                 'selected' => ($ob->selected === $key),
                 'text' => $choice['n'] . ' ' . ($choice['t'] == 'html' ? _("(HTML)") : _("(Plain Text)")),
@@ -1738,15 +1739,16 @@ class IMP_Prefs_Ui
         }
         $t->set('slist', $slist);
 
-        $t->set('selected', strlen($ob->selected));
-        $t->set('last_type', $ob->stationery['t']);
+        $t->set('selected', $ob->selected);
+        $t->set('show_delete', ($ob->selected != -1));
+        $t->set('last_type', $ob->type);
         $t->set('name_label', Horde::label('name', _("Stationery name:")));
-        $t->set('name', $ob->stationery['n']);
+        $t->set('name', $ob->name);
         $t->set('type_label', Horde::label('name', _("Stationery type:")));
-        $t->set('plain', $ob->stationery['t'] == 'plain');
-        $t->set('html', $ob->stationery['t'] == 'html');
+        $t->set('plain', $ob->type == 'plain');
+        $t->set('html', $ob->type == 'html');
         $t->set('content_label', Horde::label('content', _("Stationery:")));
-        $t->set('content', $ob->stationery['c']);
+        $t->set('content', $ob->content);
 
         return $t->fetch(IMP_TEMPLATES . '/prefs/stationery.html');
     }
@@ -1758,31 +1760,34 @@ class IMP_Prefs_Ui
      */
     protected function _updateStationeryManagement($ui)
     {
+        $ob = $this->_parseStationeryVars($ui);
+        $stationery = $GLOBALS['injector']->getInstance('IMP_Compose_Stationery');
         $updated = false;
 
         if ($ui->vars->delete) {
             /* Delete stationery. */
-            $ob = $this->_parseStationeryVars($ui);
-
-            if (isset($ob->stationery_list[$ob->selected])) {
-                $updated = sprintf(_("The stationery \"%s\" has been deleted."), $ob->stationery_list[$ob->selected]['n']);
-                unset($ob->stationery_list[$ob->selected]);
+            if (isset($stationery[$ob->selected])) {
+                $updated = sprintf(_("The stationery \"%s\" has been deleted."), $stationery[$ob->selected]['n']);
+                unset($stationery[$ob->selected]);
             }
         } elseif ($ui->vars->save) {
             /* Saving stationery. */
-            $ob = $this->_parseStationeryVars($ui);
+            $entry = array(
+                'c' => $ob->content,
+                'n' => $ob->name,
+                't' => $ob->type
+            );
 
-            if (strlen($ob->selected)) {
-                $ob->stationery_list[$ob->selected] = $ob->stationery;
-                $updated = sprintf(_("The stationery \"%s\" has been updated."), $ob->stationery['n']);
+            if ($ob->selected == -1) {
+                $stationery[] = $entry;
+                $updated = sprintf(_("The stationery \"%s\" has been added."), $ob->name);
             } else {
-                $ob->stationery_list[] = $ob->stationery;
-                $updated = sprintf(_("The stationery \"%s\" has been added."), $ob->stationery['n']);
+                $stationery[$ob->selected] = $entry;
+                $updated = sprintf(_("The stationery \"%s\" has been updated."), $ob->name);
             }
         }
 
         if ($updated) {
-            $GLOBALS['prefs']->setValue('stationery', serialize(Horde_String::convertCharset(array_values($ob->stationery_list), $GLOBALS['registry']->getCharset(), $GLOBALS['prefs']->getCharset())), false);
             $GLOBALS['notification']->push($updated, 'horde.success');
         }
     }
@@ -1794,40 +1799,47 @@ class IMP_Prefs_Ui
      *
      * @return object  Object with the following properties:
      * <pre>
+     * 'content' - (string) Content.
+     * 'name' - (string) Name.
      * 'selected' - (integer) The currently selected value.
-     * 'stationery' - (array) The current stationery entry.
-     * 'stationery_list' - (array) The list of all stationery values.
+     * 'type' - (string) Type.
      * </pre>
      */
     protected function _parseStationeryVars($ui)
     {
-        $ob = new stdClass;
-
-        $ob->selected = strlen($ui->vars->stationery)
+        $selected = strlen($ui->vars->stationery)
             ? intval($ui->vars->stationery)
-            : null;
-
-        $stationery_list = @unserialize($GLOBALS['prefs']->getValue('stationery', false));
-        $ob->stationery_list = is_array($stationery_list)
-            ? Horde_String::convertCharset($stationery_list, $GLOBALS['prefs']->getCharset())
-            : array();
-
-        $content = strval($ui->vars->content);
-        $type = isset($ui->vars->type)
-            ? $ui->vars->type
-            : 'plain';
-
-        if ($content && ($ui->vars->last_type != $type)) {
-            $content = ($type == 'plain')
-                ? $GLOBALS['injector']->getInstance('Horde_Text_Filter')->filter($content, 'Html2text')
-                : nl2br(htmlspecialchars(htmlspecialchars($content)));
+            : -1;
+        $stationery = $GLOBALS['injector']->getInstance('IMP_Compose_Stationery');
+
+        if ($ui->vars->last_selected == $selected) {
+            $content = strval($ui->vars->content);
+            $name = strval($ui->vars->name);
+            $type = isset($ui->vars->type)
+                ? $ui->vars->type
+                : 'plain';
+
+            if ($content && ($ui->vars->last_type != $type)) {
+                $content = ($type == 'plain')
+                    ? $GLOBALS['injector']->getInstance('Horde_Text_Filter')->filter($content, 'Html2text')
+                    : IMP_Compose::text2html($content);
+            }
+        } elseif ($selected == -1) {
+            $content = $name = '';
+            $type = 'plain';
+        } else {
+            $entry = $stationery[$selected];
+            $content = $entry['c'];
+            $name = $entry['n'];
+            $type = $entry['t'];
         }
 
-        $ob->stationery = array(
-            'c' => $content,
-            'n' => strval($ui->vars->name),
-            't' => $type
-        );
+
+        $ob = new stdClass;
+        $ob->content = $content;
+        $ob->name = $name;
+        $ob->selected = $selected;
+        $ob->type = $type;
 
         return $ob;
     }
index a03a0e9..889ddf8 100644 (file)
@@ -1,3 +1,4 @@
+<input type="hidden" name="last_selected" value="<tag:selected />" />
 <input type="hidden" name="last_type" value="<tag:last_type />" />
 
 <div>
@@ -45,7 +46,7 @@
 
 <div>
  <input type="submit" name="save" class="button" value="<gettext>Save Stationery</gettext>" />
-<if:selected>
+<if:show_delete>
  <input type="submit" name="delete" class="button" value="<gettext>Delete Stationery</gettext>" />
-</if:selected>
+</if:show_delete>
 </div>