/home/coolpkct/www/websites/cake3.cool.rocks/admin/classes/board.php
<?php
/**
 * Part of Showkase web site management package
 *
 * @package Showkase
 * @author Jack Hardie {@link http://www.jhardie.com}
 * @copyright Copyright (c) 2012, SimpleViewer Inc.
 */
defined('SK_ACCESS')||die('<h1>403: Forbidden</h1>');
 
/**
 * Pass messages between commands and views
 *
 * @package Showkase
 */
class Board
{
    
    /**   
     * @var object
     */
    private static $instance;
    
    /**   
     * @var array
     */
    private $messages = array();
        
    /**   
     * Constructor
     *
     */
    private function __construct()
    {}
        
    /**   
     * Gets instance of this class
     *
     * @return object
     */
    public static function getInstance()
    {
        if (empty(self::$instance)) {
            self::$instance = new Board();
        }
        return self::$instance;
    }
        
    /**   
     * Sets user feedback message
     *
     * @param string
     */
    private function add($msg, $class)
    {
        if (!is_array($msg)) {
            array_push($this->messages, array('content'=>$msg, 'class'=>$class));
            return;
        }
        if (count($msg) > 0) {
            $this->messages = array_merge($this->messages, $msg);
        }
    }
    
    /**   
     * Sets feedback message from Exception
     *
     * @param object
     * @return void
     */
    private function addException($e, $class)
    {
        if (DEMO_MODE) {
                $this->messages = array(
                        array(
                                'content'=>'Demo version, changes have not been saved',
                                'class'=>'warning'
                        )
                );
                return;
        }
        $msg = $e->getMessage();
        if (DEBUG) {
                $msg = $msg.' in '.basename($e->getFile()).' line '.$e->getLine();
        }
        array_push($this->messages, array('content'=>$msg, 'class'=>$class));
    }
    
    /**   
     * returns user messages
     *
     * @returns string
     */
    private function getHtml()
    {
        if (count ($this->messages) == 0) return '';
        $messageHtml = '<ul class="alert unstyled">';
        foreach ($this->messages as $message) {
            $messageHtml .= '<li class="'.$message['class'].'">'.$message['content'].'</li>';
        }
        $messageHtml .= '</ul>';        
        return $messageHtml;
    }
    
    /**
     * public interface to add method
     *
     * @param string message
     * @param string message class
     * @return void
     */
    public static function addMessage($msg, $class='notice')
    {
        self::getInstance()->add($msg, $class);
    }
    
    /**
     * Public interface to addException method
     *
     * @param object Exception
     * @param string message class
     * @return void
     */
    public static function addExceptionMessage(Exception $e, $class="warning")
    {
        self::getInstance()->addException($e, $class);
    }
    
    /*
     * Public interface to list method
     *
     * @return string
     */
    public static function getMessages()
    {
        return self::getInstance()->getHtml();
    }
   
}