Improvements to notification/status listener.
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 25 Jun 2009 01:00:46 +0000 (19:00 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 25 Jun 2009 01:55:29 +0000 (19:55 -0600)
Fix some more case-related listener name issues.
Allow notify() to store messages internally rather than outputting to
the page.
Refactor code to allow extending drivers to easily add custom icon
entries for new notification types.

framework/Notification/lib/Horde/Notification.php
framework/Notification/lib/Horde/Notification/Listener.php
framework/Notification/lib/Horde/Notification/Listener/Audio.php
framework/Notification/lib/Horde/Notification/Listener/Javascript.php
framework/Notification/lib/Horde/Notification/Listener/Mobile.php
framework/Notification/lib/Horde/Notification/Listener/Status.php

index ba9a201..d97ccd6 100644 (file)
@@ -105,13 +105,13 @@ class Horde_Notification
      */
     public function attach($listener, $params = array(), $class = null)
     {
-        $listener = ucfirst(basename($listener));
+        $listener = strtolower(basename($listener));
         if (!empty($this->_listeners[$listener])) {
             return $this->_listeners[$listener];
         }
 
         if (is_null($class)) {
-            $class = 'Horde_Notification_Listener_' . $listener;
+            $class = 'Horde_Notification_Listener_' . ucfirst($listener);
         }
 
         if (class_exists($class)) {
@@ -134,7 +134,7 @@ class Horde_Notification
      */
     public function detach($listener)
     {
-        $listener = ucfirst(basename($listener));
+        $listener = strtolower(basename($listener));
         if (!isset($this->_listeners[$listener])) {
             throw new Horde_Exception(sprintf('Notification listener %s not found.', $listener));
         }
@@ -202,6 +202,8 @@ class Horde_Notification
             $options['listeners'] = array($options['listeners']);
         }
 
+        $options['listeners'] = array_map('strtolower', $options['listeners']);
+
         if ($this->_alarm && in_array('status', $options['listeners'])) {
             $this->_alarm->notify(Auth::getAuth());
         }
@@ -233,7 +235,7 @@ class Horde_Notification
             }
             return $count;
         } else {
-            return @count($_SESSION[$this->_stack][$this->_listeners[$my_listener]->getName()]);
+            return @count($_SESSION[$this->_stack][$this->_listeners[strtolower($my_listener)]->getName()]);
         }
     }
 
index 679f3c5..b9ff0a1 100644 (file)
  * @author  Chuck Hagenbuch <chuck@horde.org>
  * @package Horde_Notification
  */
-class Horde_Notification_Listener
+abstract class Horde_Notification_Listener
 {
     /**
+     * The base type of this listener.
+     *
+     * @var string
+     */
+    protected $_name;
+
+    /**
      * Array of message types that this listener handles.
      *
      * @var array
@@ -39,7 +46,7 @@ class Horde_Notification_Listener
      */
     public function getName()
     {
-        return get_class($this);
+        return $this->_name;
     }
 
     /**
@@ -49,18 +56,17 @@ class Horde_Notification_Listener
      * @param array &$messageStack  The stack of messages.
      * @param array $options        An array of options.
      */
-    public function notify(&$messageStacks, $options = array())
-    {
-    }
+    abstract public function notify(&$messageStacks, $options = array());
 
     /**
      * Processes one message from the message stack.
      *
      * @param array $message  One message hash from the stack.
+     * @param array $options  An array of options.
+     *
+     * @return mixed  TODO
      */
-    public function getMessage($message)
-    {
-    }
+    abstract public function getMessage($message, $options = array());
 
     /**
      * Unserialize an event from the message stack, checking to see if the
index 30ca298..c399750 100644 (file)
@@ -19,6 +19,7 @@ class Horde_Notification_Listener_Audio extends Horde_Notification_Listener
     public function __construct()
     {
         $this->_handles = array('audio' => '');
+        $this->_name = 'audio';
     }
 
     /**
@@ -32,7 +33,7 @@ class Horde_Notification_Listener_Audio extends Horde_Notification_Listener
     {
         if (count($messageStack)) {
             while ($message = array_shift($messageStack)) {
-                $this->getMessage($message);
+                echo $this->getMessage($message);
             }
         }
     }
@@ -41,12 +42,15 @@ class Horde_Notification_Listener_Audio extends Horde_Notification_Listener
      * Outputs one message.
      *
      * @param array $message  One message hash from the stack.
+     * @param array $options  An array of options (not used).
+     *
+     * @return text  The message representation.
      */
-    public function getMessage($message)
+    public function getMessage($message, $options = array())
     {
         $event = $this->getEvent($message);
-        echo '<embed src="', htmlspecialchars($event->getMessage()),
-             '" width="0" height="0" autostart="true" />';
+        return '<embed src="' . htmlspecialchars($event->getMessage()) .
+               '" width="0" height="0" autostart="true" />';
     }
 
 }
index 6c18449..9702a7e 100644 (file)
@@ -22,6 +22,7 @@ class Horde_Notification_Listener_Javascript extends Horde_Notification_Listener
             'javascript' => '',
             'javascript-file' => ''
         );
+        $this->_name = 'javascript';
     }
 
     /**
@@ -42,12 +43,13 @@ class Horde_Notification_Listener_Javascript extends Horde_Notification_Listener
         }
 
         $files = array();
+
         while ($message = array_shift($messageStack)) {
-            $event = $this->getEvent($message);
+            $msg_text = $this->getMessage($message);
             if ($message['type'] == 'javascript') {
-                echo $event->getMessage() . "\n";
+                echo $msg_text . "\n";
             } elseif ($message['type'] == 'javascript-file') {
-                $files[] = $event->getMessage();
+                $files[] = $msg_text;
             }
         }
 
@@ -61,4 +63,17 @@ class Horde_Notification_Listener_Javascript extends Horde_Notification_Listener
         }
     }
 
+    /**
+     * Outputs one message.
+     *
+     * @param array $message  One message hash from the stack.
+     * @param array $options  An array of options (not used).
+     *
+     * @return string  The message text.
+     */
+    public function getMessage($message, $options = array())
+    {
+        return $this->getEvent($message);
+    }
+
 }
index 8901142..0684e75 100644 (file)
@@ -31,6 +31,7 @@ class Horde_Notification_Listener_Mobile extends Horde_Notification_Listener_Sta
             'horde.warning' => _("WARN"),
             'horde.message' => _("MSG")
         );
+        $this->_name = 'status';
     }
 
     /**
@@ -70,8 +71,9 @@ class Horde_Notification_Listener_Mobile extends Horde_Notification_Listener_Sta
      * Outputs one message.
      *
      * @param array $message  One message hash from the stack.
+     * @param array $options  An array of options (not used).
      */
-    public function getMessage($message)
+    public function getMessage($message, $options = array())
     {
         if (!$this->_mobile) {
             $p = new Horde_Notification_Listener_Status();
index 4a4b5ca..39fecc5 100644 (file)
 class Horde_Notification_Listener_Status extends Horde_Notification_Listener
 {
     /**
+     * The notified message stack.
+     *
+     * @var array
+     */
+    protected $_notifiedStack = array();
+
+    /**
      * Constructor.
      */
     public function __construct()
     {
+        $image_dir = $GLOBALS['registry']->getImageDir('horde');
+
         $this->_handles = array(
-            'horde.error' => array('alerts/error.png', _("Error")),
-            'horde.success' => array('alerts/success.png', _("Success")),
-            'horde.warning' => array('alerts/warning.png', _("Warning")),
-            'horde.message' => array('alerts/message.png', _("Message")),
-            'horde.alarm' => array('alerts/alarm.png', _("Alarm"))
+            'horde.error' => array($image_dir . '/alerts/error.png', _("Error")),
+            'horde.success' => array($image_dir . '/alerts/success.png', _("Success")),
+            'horde.warning' => array($image_dir . '/alerts/warning.png', _("Warning")),
+            'horde.message' => array($image_dir . '/alerts/message.png', _("Message")),
+            'horde.alarm' => array($image_dir . '/alerts/alarm.png', _("Alarm"))
         );
+        $this->_name = 'status';
     }
 
     /**
@@ -33,16 +43,34 @@ class Horde_Notification_Listener_Status extends Horde_Notification_Listener
      *
      * @param array &$messageStack  The stack of messages.
      * @param array $options        An array of options.
+     * <pre>
+     * 'store' - (boolean) If false, outputs message stack to page. If true,
+     *                     stores the message stack for subsequent retrieval
+     *                     via getStack(). DEFAULT: false
+     * </pre>
      */
     public function notify(&$messageStack, $options = array())
     {
-        if (count($messageStack)) {
+        if (!count($messageStack)) {
+            return;
+        }
+
+        $store = !empty($options['store']);
+
+        if (!$store) {
             echo '<ul class="notices">';
-            while ($message = array_shift($messageStack)) {
-                $message = $this->getMessage($message);
-                $message = preg_replace('/^<p class="notice">(.*)<\/p>$/', '<li>$1</li>', $message);
-                echo $message;
+        }
+
+        while ($message = array_shift($messageStack)) {
+            $message = $this->getMessage($message, array('data' => $store));
+            if ($store) {
+                $this->_notifiedStack[] = $message;
+            } else {
+                echo preg_replace('/^<p class="notice">(.*)<\/p>$/', '<li>$1</li>', $message);
             }
+        }
+
+        if (!$store) {
             echo '</ul>';
         }
     }
@@ -51,17 +79,42 @@ class Horde_Notification_Listener_Status extends Horde_Notification_Listener
      * Outputs one message.
      *
      * @param array $message  One message hash from the stack.
+     * @param array $options  An array of options.
+     * <pre>
+     * 'data' - (boolean) If false, outputs HTML code. If true, outputs an
+     *                    array of message information. DEFAULT: false
+     * </pre>
+     *
+     * @return mixed  TODO
      */
-    public function getMessage($message)
+    public function getMessage($message, $options = array())
     {
         $event = $this->getEvent($message);
         $text = $event->getMessage();
 
         if (!in_array('content.raw', $this->getFlags($message))) {
-            $text = htmlspecialchars($text);
+            $text = htmlspecialchars($text, ENT_COMPAT, NLS::getCharset());
         }
 
-        return '<li>' . Horde::img($this->_handles[$message['type']][0], $this->_handles[$message['type']][1], '', $GLOBALS['registry']->getImageDir('horde')) . $text . '</li>';
+        return empty($options['data'])
+            ? '<li>' . Horde::img($this->_handles[$message['type']][0], $this->_handles[$message['type']][1], '', '') . $text . '</li>'
+            : array('message' => $text, 'type' => $message['type']);
+    }
+
+    /**
+     * Returns all status messages stored via the 'store' option to notify().
+     *
+     * @param boolean $clear  Clear the entries off the internal stack?
+     *
+     * @return array  An array of data items.
+     */
+    public function getStack($clear = true)
+    {
+        $info = $this->_notifiedStack;
+        if ($clear) {
+            $this->_notifiedStack = array();
+        }
+        return $info;
     }
 
 }