Location: PHPKode > scripts > RogePHP > RoguePHP-0.9.4/RoguePHP-0.9.4/views/Skin.php
<?

/**
 * This class renders the head and footer.   It "wraps" the content in a "skin" 
 * 
 * @author Cory Marsh
 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
 */
class Skin
{
    /* @var string $_script the text markup of <Script src=""> tags */
	protected $_script = '';
	protected $_scriptList;

    /* @var string $_code javascript code to place inside a <script> tag in the footer */
	protected $_code = '';

    /* @var array $_style an array of css links */
	protected $_style;
    /* @var string $_title the <title> tag value */
	protected $_title = '';
    /* @var string $_head additional markup to place in the <head> */
	protected $_head = '';
    /* @var string $_description the <meta name="description"> tag value */
	protected $_description = '';
    /* $var Skin $_instance singleton */
	protected static $_instance = null;
    /* @var boolean $_render true if the skin has been rendered */
	private $_render = false;
    /* @var boolean $_waitForLoad true if the skin should wait for load methods before rendering the <head> */
	private $_waitForLoad = false;
    /* $var XhtmlView $_view the actual XhtmlView instance */
    private $_view;
    /* $var CalUser $_user the currently logged in user, or null if not logged in */
    private $_user;


    /* $var array $_header list links in the header */
    private $_header = array();
    /* $var array $_menu list links in the menu */
    private $_menu = array();

    /**
     * create the Skin and set the default skin 
     */
    private function __construct($defaultSkin = 'defaultSkin.xhtml')
	{
		$this->_viewFile = $defaultSkin;
        $this->_view = new XhtmlView($this->_viewFile);
        $this->_user = CalUserPersistence::getInstance()->getCurrentUser();
        

        // create header links based on who is logged in
        // the vendor header link  
        $this->_header[] = array('class' => 'vendors', 
                'name' => Text::get('vendors', 'head'),
                'href' => '/vendors',
                'title' => Text::get('vendors-title', 'head'));
        // links for logged in users
        if ($this->_user)
        {
            $this->_header[] = array('class' => 'settings', 
                'name' => Text::get('settings', 'head'),
                'href' => '/update-settings',
                'title' => Text::get('settings-title', 'head'));
            if (!$this->_user->isVendor())
            {
                // the menu navigation
                $this->_menu[] = array('name' => Text::get('dashboard', 'head'),
                        'href' => '/dashboard',
                        'title' => Text::get('dashboard-title', 'head'));

                $this->_menu[] = array('name' => Text::get('profile', 'head'),
                        'href' => '/member/'.$this->_user->getFirstName() . '/' . $this->_user->getEntityId(),
                        'title' => Text::get('profile-title', 'head'));

                $this->_menu[] = array('name' => Text::get('loyalty', 'head'),
                        'href' => '/module/campaign/Loyalty',
                        'title' => Text::get('loyalty-title', 'head'));

                $this->_header[] = array('class' => 'username',
                    'name' => $this->_user->getFirstName() . ' dashboard',// . $this->_user->getLastName(),
                    'href' => '/dashboard',
                    'title' => Text::get('dashboard-title', 'head'));
            }
            else
            {
                $this->_menu[] = array('name' => Text::get('dashboard-vendor', 'head'),
                        'href' => '/vendor-dashboard',
                        'title' => Text::get('dashboard-vendor', 'head'));
            }

            $this->_header[] = array('class' => 'logout',
                'name' => Text::get('logout', 'head'),
                'href' => '/signout',
                'title' => Text::get('logout-title', 'head'));

        }
        // links for not logged in users
        else
        {
            $this->_header[] = array('class' => 'login',
                'name' => Text::get('login', 'head'),
                'href' => '/signin',
                'title' => Text::get('login-title', 'head'));

            // the menu navigation
            $this->_menu[] = array('name' => Text::get('dashboard', 'head'),
                    'href' => '/dashboard',
                    'title' => Text::get('dashboard-title', 'head'));
        }

    // the menu navigation
    $this->_menu[] = array('name' => Text::get('browse', 'head'),
            'href' => '/browse?id=1',
            'title' => Text::get('browse-title', 'head'));

    $this->_menu[] = array('name' => Text::get('members', 'head'),
            'href' => '/module/list/Members?page=0',
            'title' => Text::get('members-title', 'head'));


        $this->_style = array();
        $this->_style[] = '/styles/base.css?v='.HTML_VER;
        $this->_style[] = '/styles/skeleton.css?v='.HTML_VER;
        $this->_style[] = '/styles/basic.css?v='.HTML_VER;
        $this->_style[] = '/styles/style.css?v='.HTML_VER;

        $this->_scriptList = array();
        $this->_scriptList[] = '/scripts/jquery-1.6.1.min.js';
        $this->_scriptList[] = '/scripts/reqs.js?v='.HTML_VER;
    }
	

    /**
     * @param string $defaultSkin set the path to the skin.  If absolute (begins with '/') then the
     * full path is used, otherwise the path is relative the VIEW_DIR directory.
     * 
     * NOTE: please use relative paths.  relative to the VIEW_DIR directory.  it creates much
     * shorter paths.
     */
    private function setSkin($skin = 'defaultSkin.xhtml')
    {
        if ($skin[0] == '/')
            $this->_viewFile = $skin;
        else
		    $this->_viewFile = VIEW_DIR . '/' . $skin;
        $this->_view->setViewFile($skin);
    }


    /**
     * this method is called after all modules are created but before they have loaded their data.
     * If non of the modules have called Skin::getInstance()->waitForLoad(), then the <head> is rendered
     * first.  This is the case when cookies are not set.
     * 
     * If the skin is instructed to "waitForLoad()" then this method will do nothing, and will be called
     * again AFTER the loadData() methods.  During the second call, this method will FORCE the <head> render.
     * 
     * @see waitForLoad()
     * @param boolean $force  true if we are forcing the <head> rednder regardless of the waitForLoad setting
     */
	public function renderHead($force = false)
	{
        // make sure we only render once, if we should wait for load and this is before
		// load has happened, then dont render
		if (!$this->_render && (!$this->_waitForLoad || $force))
		{
            $doc = '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:rogue="http://roguephp.sourceforge.net">';

            echo $doc;
            $this->_view->add('language', Text::getLanguage());
            $this->_view->add('title', $this->_title);
            $this->_view->add('description', $this->_description);
            $this->_view->add('GOOGLE_ANALYTICS', GOOGLE_ANALYTICS);
            $this->_view->add('htmlver', HTML_VER);
            $this->_view->add('dbver', DB_VER);
            $this->_view->add('style', $this->_style);

            echo $this->_view->render('head', 'head');
            flush();
			$this->_render = true;
		}
	}

	/**
	 * usually the header is sent as soon as possible, but sometimes we may need to
	 * send cookies, etc... in that case the module should tell us to "waitForLoad"
	 * @param boolean $wait
	 */
	public function waitForLoad($wait = true)
	{
		$this->_waitForLoad = $wait;
	}

	/**
	 * @return Skin
	 */
	public static function getInstance()
	{
		if (self::$_instance == null)
			self::$_instance = new Skin();
		return self::$_instance;
	}

	public function addScript($script)
	{
        // add version number for local files
        if (!stristr($script, "http://"))
        {
            if (stristr($script, '?'))
                $script .= '&v='.HTML_VER;
            else
                $script .= '?v='.HTML_VER;
        }
        if (!in_array($script, $this->_scriptList))
            $this->_scriptList[] = $script;
	}
    public function removeScript($script)
    {
        unset($this->_scriptList[$script]);
    }
	public function addJsCode($code)
	{
		$this->_code .= $code;
	}
    /**
     * @param String headMarkup raw html markup to add to the HEAD of the rendered document
     */
    public function addHead($headMarkup)
    {
		$this->_head .= $headMarkup;
    }
    /**
     * @param String $style an href link to a script to add to the <head>
     */
	public function addStyle($style)
	{
        // add version number for local files
        if (!stristr($style, "http://"))
            $style .= '?='.HTML_VER;
        if (array_search($style, $this->_style))
            Logger::getLogger('mvc')->error("adding the same style 2x $style");
        else
            $this->_style[] = $style;
	}

    /**
     * set the <title> for the rendered page
     * @param string $title 
     */
	public function setTitle($title)
	{
		$this->_title = $title;
	}

    /**
     * set the <meta description> for the rendered page
     * @param string $description 
     */
	public function setDescription($description)
	{
		$this->_description = $description;
	}

    /**
     * @return string the meta description or ''
     */
    public function getTitle()
    {
        return $this->_description;
    }

    /**
     * @return string the meta description or ''
     */
    public function getDescription()
    {
        return $this->_description;
    }

	public function render()
	{
        $user = CalUserPersistence::getInstance()->getCurrentUser();
        $this->_view->add('user', $user);
        $this->_view->add('frnd_ver', "0");
        if ($user)
            $this->_view->add('frnd_ver', $user->getVersion('frnd', 0));

        $this->_view->add('style', $this->_style);
        $this->_view->add('header', $this->_header);
        $this->_view->add('menu', $this->_menu);
        $this->_view->add('script', $this->_scriptList);
        $this->_view->add('code', $this->_code);
        $this->_view->add('content', $this->content);
        $this->_view->add('ver', HTML_VER);
        $this->_view->add('user', $user);

        echo $this->_view->render('body', 'body');
        echo "</html>";
	}
}
Return current item: RogePHP