<?php

require_once 'Horde/Notification/Listener.php';

/**
 * The Notification_Listener_javascript:: class provides functionality for
 * inserting javascript code from the message stack into the page.
 *
 * $Horde: framework/Notification/Notification/Listener/javascript.php,v 1.7.10.6 2006/01/01 21:28:30 jan Exp $
 *
 * Copyright 2004-2006 Jan Schneider <jan@horde.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  Jan Schneider <jan@horde.org>
 * @since   Horde 3.0
 * @package Horde_Notification
 */
class Notification_Listener_javascript extends Notification_Listener {

    /**
     * Constructor
     */
    function Notification_Listener_javascript()
    {
        $this->_handles = array('javascript' => '');
    }

    /**
     * Return a unique identifier for this listener.
     *
     * @return string  Unique id.
     */
    function getName()
    {
        return 'javascript';
    }

    /**
     * Outputs the javascript code if there are any messages on the
     * 'javascript' message stack and if the 'notify_javascript' option is set.
     *
     * @param array &$messageStack     The stack of messages.
     * @param array $options  An array of options. Options: 'noscript'
     */
    function notify(&$messageStack, $options = array())
    {
        if (count($messageStack)) {
            if (empty($options['noscript'])) {
                echo '<script type="text/javascript">';
                echo "\n<!--\n";
            }
            while ($message = array_shift($messageStack)) {
                $this->getMessage($message);
            }
            if (empty($options['noscript'])) {
                echo "//-->\n</script>\n";
            }
        }
    }

    /**
     * Outputs one message.
     *
     * @param array $message  One message hash from the stack.
     */
    function getMessage($message)
    {
        $event = $this->getEvent($message);
        echo $event->getMessage() . "\n";
    }

}
