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

/**
 * «Singleton» View
 *
 * A manager class to automate searching and loading of templates, belonging to view layer.
 *
 * @access      public
 * @package     yana
 * @subpackage  core
 *
 * @ignore
 */
class View extends Singleton
{
    /**#@+
     * @ignore
     * @access  private
     */

    /** @var String */       var $baseView = null;
    /** @var String */       var $selected = null;

    /**#@-*/

    /**
     * readonly
     * @access public
     * @var smartTemplate */ var $document = null;

    /**
    * «Singleton» Constructor
    *
    * @access public
    */
    function View()
    {
        $this->checkSingletonConstraint();
        $this->document =& new smartTemplate();
    }

    /**
    * selectView
    *
    * @access public
    * @param string $viewName
    * @return boolean
    */
    function selectView($viewName)
    {
        assert('is_string($viewName);');
        settype($viewName, "string");

        global $YANA;
        $view = $YANA->skin->getFile($viewName);

        if (!empty($view)) {
            $this->selected = $view;
            return true;
        } else {
            trigger_error("Invalid view name \"$viewName\" supplied for opperation \"selectView\". View is not registered.", E_USER_WARNING);
            return false;
        }
    }

    /**
    * setView
    *
    * @access public
    * @param string $viewName
    * @return boolean
    */
    function setView($viewName)
    {
        assert('is_string($viewName);');
        settype($viewName, "string");

        if (file_exists($viewName)) {
            $this->selected = $viewName;
            return true;
        } else {
            trigger_error("Invalid filename \"$viewName\" supplied for opperation \"setView\". File does not exist.", E_USER_WARNING);
            return false;
        }
    }
    
    /**
    * getView
    *
    * @access public
    * @return string
    */
    function getView()
    {
        assert('is_string($this->selected);');
        return $this->selected;
    }

    /**
    * unsetView
    *
    * @access public
    */
    function unsetView()
    {
        $this->selected = "";
    }

    /**
    * selectBaseView
    *
    * @access public
    * @param string $viewName
    * @return boolean
    */
    function selectBaseView($viewName)
    {
        assert('is_string($viewName);');
        settype($viewName, "string");

        global $YANA;
        $baseView = $YANA->skin->getFile($viewName);

        if (!empty($baseView)) {
            $this->baseView = $baseView;
            return true;
        } else {
            trigger_error("Invalid view name \"$viewName\" supplied for opperation \"selectBaseView\". View is not registered.", E_USER_WARNING);
            return false;
        }
    }

    /**
    * setBaseView
    *
    * @access public
    * @param string $viewName
    * @return boolean
    */
    function setBaseView($viewName) 
    {
        assert('is_string($viewName);');
        settype($viewName, "string");

        if (file_exists($viewName)) {
            $this->baseView = $viewName;
            return true;
        } else {
            trigger_error("Invalid filename \"$viewName\" supplied for opperation \"setBaseView\". File does not exist.", E_USER_WARNING);
            return false;
        }
    }

    /**
    * unsetBaseView
    *
    * @access public
    */
    function unsetBaseView()
    {
        $this->baseView = "";
    }

    /**
    * getBaseView
    *
    * @access public
    * @return string
    */
    function getBaseView()
    {
        assert('is_string($this->baseView);');
        return $this->baseView;
    }

    /**
    * buildPage
    *
    * @access public
    * @return boolean
    */
    function buildPage() 
    {
         if ($this->baseView) {
             $this->document->setFilename($this->baseView);
             if ($this->selected) {
                 $this->document->insertFile('*',$this->selected);
             }
             return true;
         } else {
             return false;
         }
    }
    
    /**
    * show
    *
    * @access public
    * @return boolean
    */
    function show()
    {
        if (!is_null($this->document) && $this->document->exists()) {
            $this->document->write();
            return true;
        } else {
            return false;
        }
    }
    
    /**
    * setOptions
    *
    * @access public
    * @param string $key
    * @return boolean
    */
    function setOptions($key)
    {
        settype($key, "string");
        if (!is_null($this->document) && $this->document->exists()) {
            global $YANA;
            $this->document->insert('*', $YANA->getVar($key));
            return true;
        } else {
            return false;
        }
    }

    /**
    * message
    *
    * NOTE: This function will terminate program execution
    *
    * @access public
    * @param string $type
    * @param string $errcode
    * @param string $event
    */
    function message($type, $errcode, $event)
    {
        settype($type, "string");
        settype($errcode, "string");
        settype($event, "string");

        if (!$this->selectBaseView('message')) {
            trigger_error("Invalid filename supplied. Unable to create page.", E_USER_ERROR);
            exit;
        }

        $this->unsetView();
        $this->buildPage();

        global $YANA;
        $YANA->registry->mount('language_message_file');

        $YANA->setVar('ACTION', $event);
        $YANA->setVar('MESSAGE_TYPE', strtolower($type));

        $this->setOptions("TEXT.$errcode");
        foreach ($_GET as $a => $b)
        {
            if (is_null($this->document->get(strtoupper($a)))) {
                $this->document->insert(strtoupper($a), $b);
            }
        }
        foreach ($_POST as $a => $b)
        {
            if (is_null($this->document->get(strtoupper($a)))) {
                $this->document->insert(strtoupper($a), $b);
            }
        }
        $this->setOptions("*");
        $this->show();
        exit;
    }
}

?>