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.
*/
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)) {
*/
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));
}
$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());
}
}
return $count;
} else {
- return @count($_SESSION[$this->_stack][$this->_listeners[$my_listener]->getName()]);
+ return @count($_SESSION[$this->_stack][$this->_listeners[strtolower($my_listener)]->getName()]);
}
}
* @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
*/
public function getName()
{
- return get_class($this);
+ return $this->_name;
}
/**
* @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
public function __construct()
{
$this->_handles = array('audio' => '');
+ $this->_name = 'audio';
}
/**
{
if (count($messageStack)) {
while ($message = array_shift($messageStack)) {
- $this->getMessage($message);
+ echo $this->getMessage($message);
}
}
}
* 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" />';
}
}
'javascript' => '',
'javascript-file' => ''
);
+ $this->_name = 'javascript';
}
/**
}
$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;
}
}
}
}
+ /**
+ * 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);
+ }
+
}
'horde.warning' => _("WARN"),
'horde.message' => _("MSG")
);
+ $this->_name = 'status';
}
/**
* 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();
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';
}
/**
*
* @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>';
}
}
* 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;
}
}