<?php

/**
 * Message class.
 * This class implements a simply but nice way to show messages to the user. Note that this class uses Dojo's features, 
 * so it's dependent on that toolkit. 
 *
 * @author Eduardo Bonfandini [eduardo@solis.coop.br],
 *         Armando Taffarel Neto [taffarel@solis.coop.br],
 *         Vilson Cristiano Gartner [vilson@solis.coop.br]
 *
 * @version $id$
 *
 * \b Maintainers: \n
 * Vilson Cristiano Gartner [vilson@solis.coop.br]
 *
 * @since
 * Creation date 2009/04/17
 *
 * \b Organization: \n
 * SOLIS - Cooperativa de Solucoes Livres \n
 *
 * \b Copyright: \n
 * Copyright (c) 2009 SOLIS - Cooperativa de Solucoes Livres \n
 *
 * \b License: \n
 * Licensed under GPLv2 (for further details read the COPYING file or http://www.gnu.org/licenses/gpl.html)
 *
 * \b History: \n
 * See history in SVN repository: http://www.miolo.org.br
 *
 */

/*
 * Class to show a message.
 * This classes can be used to show messages to the user, using Dojo's features. On your form, 
 * you need only to add a field, which will be used to show the message, and than when you want, 
 * create a new MMessage object and it's done.  
 * 
 * @example
 * <code>
 * ...
 * $fields[] = MMessage::getMessageContainer();
 * $this->setFields($fields);
 * ...
 * new MMessage("Error message string here.", MMessage::TYPE_ERROR);
 * </code>
 * 
 */
class MMessage
{
    const TYPE_INFORMATION  = 'Information';
    const TYPE_WARNING  = 'Warning';
    const TYPE_ERROR    = 'Error';
    const MSG_CLASS     = 'mMessage';
    const MSG_CONTAINER = 'messageDiv';

    /**
     * Returns a message, formated according the type.
     *
     * @param (string) $mensagem Message to show
     * @param (string) $type Message type, TYPE_INFORMATION, TYPE_WARNING or TYPE_ERROR
     * @param (boolean) $ajaxResponse If true message is send to cliente, otherwise, message is registered in session. 
     */
    public function __construct($message, $type = self::TYPE_INFORMATION, $doSendToClient = true)
    {
        $MIOLO = MIOLO::getInstance();
        
        if ( is_array($message) )
        {
            $message = implode($message, '<br>');
        }

        // @todo if no field exists on form, whe need to add a child on it!
        $box = new MDiv( 'miolo_message_div', $message, self::MSG_CLASS . $type );

        if ( $doSendToClient )
        {
            $jsCode = "dojo.fx.wipeOut({node: '" . self::MSG_CONTAINER . "', duration: 0}).play();
                      dojo.fx.wipeIn({node: '" . self::MSG_CONTAINER . "', duration: 500}).play();";

            $MIOLO->page->onload( $jsCode );
            $MIOLO->ajax->setResponse( $box, self::MSG_CONTAINER );
        }
        else
        {
            $session = new MSession( 'miolo_message_div' );
            $session->setValue( 'lastMessage', $message );
            $session->setValue( 'lastType', $type );
        }
    }

    /**
     * Method do return the last message.
     *
     * @return (string) The last registered message
     */
    public static function getLastMessage()
    {
        $session = new MSession( 'miolo_message_div' );
        $value = $session->getValue( 'lastMessage' );
        return $value;
    }

    /**
     * This method return the last type of the message.
     *
     * @return (string) Last type of message
     */
    public static function getLastMessageType()
    {
        $session = new MSession( 'miolo_message_session' );
        $value = $session->getValue( 'lastType' );
        return $value;
    }

    /**
     * Clear the last registered message in session
     */
    public static function clearLastMessage()
    {
        $session = new MSession( 'miolo_message_session' );
        $session->setValue( 'lastMessage', '' );
        $session->setValue( 'lastType', '' );
    }

    /**
     * Create a container for the messages. If a message is registered in session, returns it in the container.
     *
     * @return (object) Container (MDiv) with a message, if it's registered in session.
     */
    public static function getMessageContainer()
    {
        $MIOLO = MIOLO::getInstance();

        $type = self::getLastMessageType();
        $message = self::getLastMessage();

        //if a message exists in session, return it
        if ( $type && $message )
        {
            $content = new MDiv( self::MSG_CONTAINER, $message, self::MSG_CLASS . $type );
            self::clearLastMessage();
            $jsCode = "dojo.fx.wipeOut({node: '" . self::MSG_CONTAINER . "', duration: 0}).play();
                      dojo.fx.wipeIn({node: '" . self::MSG_CONTAINER . "', duration: 500}).play();";
        }

        $div = new MDiv( self::MSG_CONTAINER, $content ? $content : '' );

        $jsCode .= "function hideMessageDiv()
                    {
                        dojo.fx.wipeOut({node: '" . self::MSG_CONTAINER . "', duration: 500}).play();
                        document.onchange = '';
                        document.onkeypress = '';
                    }
                    document.onchange = function() {hideMessageDiv()};
                    document.onkeypress = function() {hideMessageDiv()};";

        $MIOLO->page->addDojoRequire( 'dojo.fx' ); // Effects
        $MIOLO->page->onload( $jsCode );

        return $div;
    }
}

?>