From: Michael M Slusarz Date: Thu, 25 Jun 2009 01:00:46 +0000 (-0600) Subject: Improvements to notification/status listener. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=59fa30018ae20160422119959133aa2f7a64d78f;p=horde.git Improvements to notification/status listener. 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. --- diff --git a/framework/Notification/lib/Horde/Notification.php b/framework/Notification/lib/Horde/Notification.php index ba9a201b9..d97ccd6f7 100644 --- a/framework/Notification/lib/Horde/Notification.php +++ b/framework/Notification/lib/Horde/Notification.php @@ -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()]); } } diff --git a/framework/Notification/lib/Horde/Notification/Listener.php b/framework/Notification/lib/Horde/Notification/Listener.php index 679f3c543..b9ff0a1a7 100644 --- a/framework/Notification/lib/Horde/Notification/Listener.php +++ b/framework/Notification/lib/Horde/Notification/Listener.php @@ -11,9 +11,16 @@ * @author Chuck Hagenbuch * @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 diff --git a/framework/Notification/lib/Horde/Notification/Listener/Audio.php b/framework/Notification/lib/Horde/Notification/Listener/Audio.php index 30ca2986a..c399750f4 100644 --- a/framework/Notification/lib/Horde/Notification/Listener/Audio.php +++ b/framework/Notification/lib/Horde/Notification/Listener/Audio.php @@ -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 ''; + return ''; } } diff --git a/framework/Notification/lib/Horde/Notification/Listener/Javascript.php b/framework/Notification/lib/Horde/Notification/Listener/Javascript.php index 6c18449ca..9702a7e04 100644 --- a/framework/Notification/lib/Horde/Notification/Listener/Javascript.php +++ b/framework/Notification/lib/Horde/Notification/Listener/Javascript.php @@ -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); + } + } diff --git a/framework/Notification/lib/Horde/Notification/Listener/Mobile.php b/framework/Notification/lib/Horde/Notification/Listener/Mobile.php index 890114260..0684e752b 100644 --- a/framework/Notification/lib/Horde/Notification/Listener/Mobile.php +++ b/framework/Notification/lib/Horde/Notification/Listener/Mobile.php @@ -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(); diff --git a/framework/Notification/lib/Horde/Notification/Listener/Status.php b/framework/Notification/lib/Horde/Notification/Listener/Status.php index 4a4b5ca4f..39fecc52a 100644 --- a/framework/Notification/lib/Horde/Notification/Listener/Status.php +++ b/framework/Notification/lib/Horde/Notification/Listener/Status.php @@ -14,17 +14,27 @@ 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. + *
+     * 'store' - (boolean) If false, outputs message stack to page. If true,
+     *                     stores the message stack for subsequent retrieval
+     *                     via getStack(). DEFAULT: false
+     * 
*/ public function notify(&$messageStack, $options = array()) { - if (count($messageStack)) { + if (!count($messageStack)) { + return; + } + + $store = !empty($options['store']); + + if (!$store) { echo ''; } } @@ -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. + *
+     * 'data' - (boolean) If false, outputs HTML code. If true, outputs an
+     *                    array of message information. DEFAULT: false
+     * 
+ * + * @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 '
  • ' . Horde::img($this->_handles[$message['type']][0], $this->_handles[$message['type']][1], '', $GLOBALS['registry']->getImageDir('horde')) . $text . '
  • '; + return empty($options['data']) + ? '
  • ' . Horde::img($this->_handles[$message['type']][0], $this->_handles[$message['type']][1], '', '') . $text . '
  • ' + : 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; } }