<?php
/**
 * YANA library
 *
 * @package yana
 * @licence http://creativecommons.org/licenses/by-nc-sa/2.5/
 */

/**
 * «abstract» Yana_log
 *
 * @abstract
 * @access      public
 * @package     yana
 * @subpackage  error_reporting
 */
class Yana_log
{
    /**#@+
     * @access  public
     */

    /** @var string */ var $message = "";
    /** @var mixed */  var $data    = null;

    /**#@-*/

    /**
     * constructor
     *
     * @param string $message
     * @param mixed $data (optional)
     */ 
    function Yana_log($message, $data = null) 
    {
        assert('is_string($message);');
        settype($message, "string");
        $this->message = $message;
        $this->data    = $data;
    }
    
    /**
     * create log
     *
     * @access public
     * @return string
     */
     function getLog()
     {
         global $YANA;
         $report['IP']       = $_SERVER['REMOTE_ADDR'];
         if (isset($YANA)) {
             $report['USER'] = $YANA->getVar("SESSION_USER_ID");
         }
         if (empty($report['USER'])) {
             $report['USER'] = '*GUEST';
         }
         $report['TIME']     = date("d.m.Y H:i:s", time());
         $report['MESSAGE']  = $this->getMessage();
         $result = "";
         foreach ($report as $key => $value)
         {
             $result .= ((!empty($result)) ?', ':'') . $key . '="' . str_replace('"', '&quot;',$value).'"';
         }
         return $result;
     }
     
    /**
     * returns the message string of this event
     *
     * @access public
     * @return string
     */
     function getMessage()
     {
         $message = $this->message;
         if (!empty($this->data)) {
             if (is_scalar($this->data)) {
                 $message .= ";".$this->data;
             } else if (is_array($this->data)) {
                 $message .= ";".str_replace("\n", '', makeConfig($this->data));
             } else {
                 $message .= ";".print_r($this->data, true);
             }
         }
         $message = substr(preg_replace('/\s/', ' ', $message),0,1000);
         return $message;
     }

}

/**
 * Log
 *
 * @access      public
 * @package     yana
 * @subpackage  error_reporting
 */
class Log extends Yana_log
{
}

/**
 * Message
 *
 * @access      public
 * @package     yana
 * @subpackage  error_reporting
 */
class Message extends Yana_log
{
    /**
     * constructor
     *
     * @name    message::__construct()
     * @param   array|string  $message
     * @param   mixed         $data    (optional)
     */ 
    function message($message, $data = null)
    {
        if (is_string($message)) {
            $this->message = $message;
        } else if (is_array($message)) {
            if (count($message) == 2 && isset($message[0]) && isset($message[1])) {
                $this->message = array('H'=>$message[0],'P'=>$message[1]);
            } else {
                $message = changeCaseArray($message,CASE_UPPER);
                if (count($message) == 2 && isset($message['H']) && isset($message['P'])) {
                    $this->message = $message;
                } else {
                    trigger_error("Expect argument 1 to be a string or an array with the 2 elements, the first being the message's name and the second being the message description. Instead found '".gettype($message)."(".print_r($message,1).")'");
                }
            }
        } else {
            trigger_error("Expect argument 1 to be a string or an array, found '".gettype($message).")' instead.");
        }
        $this->data = $data;
    }
    
    /**
     * getMessage
     *
     * returns the message string of this event
     *
     * <ol>
     *     <li>    checks whether message is fulltext or id and resolves id,            </li>
     *     <li>    checks whether data is array or empty or other and replaces token    </li>
     * </ol>
     *
     * @access  public
     * @name    message::getmessage()
     * @return  mixed
     */
    function getMessage()
    {
        global $YANA;
        $message = "";

        /* 1) */
        if (is_string($this->message) && preg_match('/^[\w_\d]+$/', $this->message)) {
            $message = $YANA->getVar('TEXT.'.strtoupper($this->message));
        } else {
            $message = $this->message;
        }
        if (empty($message)) {
            $message = $this->message;
        /* 2) */
        } else if (is_array($message) && !empty($this->data) && is_array($this->data)) {
            if (!empty($message['H'])) {
                $message['H'] = Layout::fillTemplate($message['H'], $this->data);
            }
            if (!empty($message['P'])) {
                $message['P'] = Layout::fillTemplate($message['P'], $this->data);
            }
        }
        return $message;
    }
}

/**
 * Warning
 *
 * @access      public
 * @package     yana
 * @subpackage  error_reporting
 */
class Warning extends Message
{
}

/**
 * Alert
 *
 * @access      public
 * @package     yana
 * @subpackage  error_reporting
 */
class Alert extends Message
{
}

/**
 * Error
 *
 * @access      public
 * @package     yana
 * @subpackage  error_reporting
 */
class Error extends Message
{
}

?>