Source for file Header.class.php

Documentation is available at Header.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Template
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Special Template Header Class
  22.  * 
  23.  * This class will represent the Header.  If using HTML, it provides methods for
  24.  * adding Stylesheets and JavaScript dynamically.  This will format two Template
  25.  * variables 'header_styles' and 'header_javascript'.
  26.  *
  27.  * @category Gumbo
  28.  * @package Template
  29.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  30.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  31.  * @author Michael Luster <mluster79@yahoo.com>
  32.  * @link http://sourceforge.net/projects/phpgumbo
  33.  * @desc Special Template Header Class
  34.  * @version 0.0.1
  35.  */
  36.  
  37. gumbo_load ("Interface_Singleton");
  38. gumbo_load ("Template_Special");
  39.  
  40.     
  41.     /** @var Gumbo_Template_Header $_instance */
  42.     private static $_instance null;
  43.     
  44.     /** @var Gumbo_Interface_List $_styles list of extra stylesheets */
  45.     private $_styles;
  46.     /** @var Gumbo_Interface_List $_javascript list of javascript files */
  47.     private $_javascript;
  48.     
  49.     
  50.     
  51.     /**
  52.      * Constructor
  53.      * @uses Gumbo_Template
  54.      */
  55.     private function __construct ({
  56.         gumbo_load ("Template");
  57.         gumbo_load ("List");
  58.         
  59.         $fact new Gumbo_Template ();
  60.         $this->setEngine ($fact->factory ());
  61.         $this->getEngine ()->loadGlobals ();
  62.         
  63.         $this->_styles = new Gumbo_List ();
  64.         $this->_javascript = new Gumbo_List ();
  65.     }
  66.     
  67.     /**
  68.      * Singleton Method
  69.      * @return Gumbo_Interface_Special_Header 
  70.      */
  71.     public static function instance ({
  72.         if (self::$_instance == null{
  73.             self::$_instance new Gumbo_Template_Header ();
  74.         }
  75.         return self::$_instance;
  76.     }
  77.     
  78.     
  79.     
  80.     /** ACTION METHODS **/
  81.     /**
  82.      * Returns the formatted text to the browser
  83.      * @param bool $reload reloads the output
  84.      * @return string 
  85.      */
  86.     public function fetch ($reload=false{
  87.         $this->getEngine ()->setFile ($this->getFile ());
  88.         if (!$this->isActive (&& $this->getFileBasic ()) {
  89.             $this->getEngine ()->setFile ($this->getFileBasic ());
  90.         else {
  91.             return;
  92.         }
  93.         
  94.         // assign extra styles
  95.         $txt null;
  96.         foreach ($this->getStyles (as $style{
  97.             $txt .= "\t<link href=\"" gumbo_appendLocation ($style ['url']$style ['file'"\" rel=\"stylesheet\" type=\"text/css\" media=\"" $style ['media'"\" />\n";
  98.         }
  99.         $this->assign ("header_styles"$txt);
  100.         
  101.         // assign Javascript
  102.         $txt null;
  103.         foreach ($this->getJavascript (as $js{
  104.             $txt .= "\t<script type=\"text/javascript\" ";
  105.             if (!$js ['file']{
  106.                 $txt .= "src=\"{$js ['js']}\" />";
  107.             else {
  108.                 $txt .= ">{$js ['js']}</style>";
  109.             }
  110.             $txt .= "\n";
  111.         }
  112.         $this->assign ("header_javascript"$txt);
  113.         
  114.         // assign the Elements
  115.         foreach ($this->getElements (as $key=>$val{
  116.             $val->loadGlobals ();
  117.             $this->assign ($key$val);
  118.         }
  119.         
  120.         return $this->getEngine ()->fetch ($reload);
  121.     }
  122.     
  123.     /**
  124.      * Adds a style sheet (path not necessary)
  125.      * @param string $file file name of the style sheet
  126.      * @param string $media media type (screen|print|etc.)
  127.      * @param string $url URL to style sheet
  128.      * @throws Gumbo_Exception
  129.      */
  130.     public function addStyle ($file$media=null$url=null{
  131.         try {
  132.             // verify precondition
  133.             if (!is_string ($file)) {
  134.                 throw new Gumbo_Exception ("Invalid Argument 'file:str' => {$file}:gettype ($file));
  135.             }
  136.             
  137.             try {
  138.                 // verify precondition
  139.                 if (!is_null ($url&& !is_string ($url)) {
  140.                     throw new Gumbo_Exception ("Invalid Argument 'url:str' => {$url}:gettype ($url));
  141.                 }
  142.             catch (Gumbo_Exception $e{
  143.                 $e->setFunction (__METHOD__);
  144.                 gumbo_trigger ($e);
  145.                 $url null;
  146.             }
  147.             
  148.             if (is_null ($media)) $media "screen"}
  149.             if (!is_string ($media)) {
  150.                 throw new Gumbo_Exception ("Invalid Argument 'media:str' => {$media}:gettype ($media));
  151.             }
  152.             $media strtolower ($media);
  153.             
  154.             switch ($media{
  155.                 case "all" break;
  156.                 case "screen" break;
  157.                 case "print" break;
  158.                 case "handheld" break;
  159.                 case "projection" break;
  160.                 case "tty" break;
  161.                 case "tv" break;
  162.                 case "braille" break;
  163.                 case "aural" break;
  164.                 default : throw new Gumbo_Exception ("Invalid Media Value [all|screen|print|handheld|projection|tty|tv|braille|aural]: {$media}");
  165.             }
  166.             
  167.             $this->getStyles ()->add (array ("file"=>$file"media"=>$media"url"=>$url));
  168.         catch (Gumbo_Exception $e{
  169.             $e->setFunction (__METHOD__);
  170.             gumbo_trigger ($e);
  171.         }
  172.     }
  173.     
  174.     /**
  175.      * Adds Javascript to the Header
  176.      * @param string $js file name or javascript text
  177.      * @param bool $as_string 
  178.      */
  179.     public function addJavascript ($js$as_string=false{
  180.         try {
  181.             // verify precondition
  182.             if (!is_string ($js)) {
  183.                 throw new Gumbo_Exception ("Invalid Argument 'js:str' => {$js}:gettype ($js));
  184.             }
  185.             try {
  186.                 if (!is_bool ($as_string)) {
  187.                     throw new Gumbo_Exception ("Invalid Argument 'as_string:bool' => {$as_string}:gettype ($as_string));
  188.                 }
  189.             catch (Gumbo_Exception $e{
  190.                 $e->setFunction (__METHOD__);
  191.                 gumbo_trigger ($e);
  192.                 $as_string false;
  193.             }
  194.             
  195.             $this->getJavascript ()->add (array ("js"=>$js"file"=>$as_string));
  196.         catch (Gumbo_Exception $e{
  197.             $e->setFunction (__METHOD__);
  198.             gumbo_trigger ($e);
  199.         }
  200.     }
  201.  
  202.     
  203.     
  204.     /** ACCESSOR METHODS **/
  205.     /**
  206.      * Returns the extra style sheets to include in the template file
  207.      * @return Gumbo_Interface_List 
  208.      */
  209.     public function getStyles ({
  210.         return $this->_styles;
  211.     }
  212.     
  213.     /**
  214.      * Returns any Javascript Files
  215.      * @return Gumbo_Interface_List 
  216.      */
  217.     public function getJavascript ({
  218.         return $this->_javascript;
  219.     }
  220.     
  221. }
  222.  
  223. ?>