10.30 php saved
Jippi_
Tags add more
HeadHelper js css  
Note
Modified RosSoft's HeadHelper

Added feature to have default and custom css / js folder - usefull when using on alot of websites where you need some basic css / js - but want to have the option to make your own custom without breaking the original source
  1. <?php
  2. /**
  3. * Head Helper
  4. * Register <head> tags from helpers, then print them
  5. * in head through layout.
  6. * @author RosSoft
  7. * @license MIT
  8. * @version 0.2
  9. */
  10. class HeadHelper extends Helper
  11. {
  12.     var $helpers=array('html','javascript');
  13.  
  14.     var $_library; //static array of items to be included
  15.  
  16.     function __construct()
  17.     {
  18.         static $library=array()//for php4 compat
  19.         $this->_library=& $library;
  20.     }
  21.  
  22.     /**
  23.      * Adds a css file to array
  24.      * @param string $file CSS file to be included
  25.      * @param string $param Array of htmlAttributes
  26.      */
  27.     function register_css($file,$htmlAttributes=null)
  28.     {
  29.         $this->_register(array($file,'css',$htmlAttributes));
  30.     }
  31.  
  32.     /**
  33.      * Adds an inline css block to array
  34.      * @param string $css CSS tags to be included
  35.      * @param string $param Array of htmlAttributes
  36.      */
  37.     function register_cssblock($css,$htmlAttributes=null)
  38.     {
  39.         $this->_register(array($css,'cssblock',$htmlAttributes));
  40.     }
  41.  
  42.  
  43.     /**
  44.      * Adds a js file to array
  45.      * @param string $file CSS file to be included
  46.      * @param string $param Array of htmlAttributes
  47.      */
  48.     function register_js($file)
  49.     {
  50.         $this->_register(array($file,'js'));
  51.     }
  52.  
  53.     /**
  54.      * Adds a javascript block to array
  55.      * @param string $javascript Javascript block to be included
  56.      * @param string $param Array of htmlAttributes
  57.      */
  58.     function register_jsblock($javascript)
  59.     {
  60.         $this->_register(array($javascript,'jsblock'));
  61.     }
  62.  
  63.     /**
  64.      * Adds a meta tag to array
  65.      * @param array $htmlAttributes Array of html attributes of meta tag
  66.      */
  67.     function register_meta($htmlAttributes)
  68.     {
  69.         $this->_register(array($htmlAttributes,'meta'));
  70.     }
  71.  
  72.  
  73.     /**
  74.      * Adds a link tag to array
  75.      * @param array $htmlAttributes Array of html attributes of meta tag
  76.      */
  77.     function register_link($htmlAttributes)
  78.     {
  79.         $this->_register(array($htmlAttributes,'link'));
  80.     }
  81.  
  82.     /**
  83.      * Adds a raw sequence of html tags to array
  84.      * @param string $raw Sequence of html tags
  85.      */
  86.     function register_raw($raw)
  87.     {
  88.         $this->_register(array($raw,'raw'));
  89.     }
  90.  
  91.  
  92. //-------------------------------------------------
  93. // Jippi at dork.dk  HeadHacks
  94. //-------------------------------------------------
  95.  
  96.     // Var to store our css search paths ( relative to webroot )
  97.     var $searchPaths = array(
  98.         'custom',
  99.         'default'
  100.     );
  101.  
  102.     // Find our CSS files
  103.     function __findCSS( $file ) { return $this->__find( $file, 'css', '.css'    ); }
  104.     // Find our JS files
  105.     function __findJS( $file )  { return $this->__find( $file, 'js', '.js'    ); }
  106.  
  107.     /**
  108.      * Try to find our file in webroot
  109.      *
  110.      * @param string $file   File to find
  111.      * @param string $folder    Folder to search in ( without trailing / )
  112.      * @param string $extension Extension of the file we want
  113.      * @return string         Path to the file if we found it - else the original file
  114.      */
  115.     function __find( $file, $folder, $extension )
  116.     {
  117.         foreach ( $this->searchPaths AS $key => $path )
  118.         {
  119.             if( is_file( $this->__getWebRoot() . $folder . DS . $path . DS . $file  . $extension ))
  120.                 return $path . DS . $file;
  121.         }
  122.         return $file;
  123.     }
  124.  
  125.     /**
  126.      * Get our webroot with full path !
  127.      *
  128.      * @return string FULL path to our webroot directory
  129.      */
  130.     function __getWebRoot()
  131.     {
  132.         return ROOT . DS . APP_DIR . DS . WEBROOT_DIR . DS;
  133.     }
  134.  
  135.     /**
  136.      * Prints the html for all of the items registered
  137.      * @return string
  138.      */
  139.     function print_registered()
  140.     {
  141.         if( is_null( $this->searchPaths ) )
  142.             $this->getSearchPaths();
  143.  
  144.         foreach ($this->_library as $l)
  145.         {
  146.             echo "\n\t";
  147.             switch ($l[1])
  148.             {
  149.                 case 'css':
  150. /* changed */      echo $this->html->css( $this->__findCSS( $l[0] ),'stylesheet',$l[2]);
  151.                     break;
  152.                 case 'js':
  153. /* changed */      echo $this->javascript->link( $this->__findJS( $l[0] ) );
  154.                     break;
  155.                 case 'jsblock':
  156.                     echo $this->javascript->codeBlock($l[0]);
  157.                     break;
  158.                 case 'meta':
  159.                     echo "<meta " . $this->_parseAttributes($l[0]) . " />";
  160.                     break;
  161.                 case 'link':
  162.                     echo "<link " . $this->_parseAttributes($l[0]) . " />";
  163.                     break;
  164.                 case 'raw':
  165.                     echo $l[0];
  166.                     break;
  167.                 case 'cssblock':
  168.                     echo '<style type="text/css" '$this->_parseAttributes($l[2]) . " ><!--{$l[0]}--></style>";
  169.                     break;
  170.                 default:
  171.                     die('Internal error on HeadHelper: Unknown type registered.');
  172.             }
  173.         }
  174.     }
  175.  
  176. //------------------------------------------------
  177. // End Jippi hacks
  178. //------------------------------------------------
  179.  
  180.     /**
  181.      * Adds the item in the array if it doesn't already exist
  182.      * @param array $item Item to be added
  183.      * @access private
  184.      */
  185.     function _register($item)
  186.     {
  187.         if (! in_array($item,$this->_library))
  188.         {
  189.             $this->_library[]=$item;
  190.         }
  191.     }
  192.  
  193.  
  194.  
  195.     /**
  196.      * This is a copy of the same function in HtmlHelper
  197.      * Returns a space-delimited string with items of the $options array. If a
  198.      * key of $options array happens to be one of:
  199.      *    + 'compact'
  200.      *    + 'checked'
  201.      *    + 'declare'
  202.      *    + 'readonly'
  203.      *    + 'disabled'
  204.      *    + 'selected'
  205.      *    + 'defer'
  206.      *    + 'ismap'
  207.      *    + 'nohref'
  208.      *    + 'noshade'
  209.      *    + 'nowrap'
  210.      *    + 'multiple'
  211.      *    + 'noresize'
  212.      *
  213.      * And its value is one of:
  214.      *    + 1
  215.      *    + true
  216.      *    + 'true'
  217.      *
  218.      * Then the value will be reset to be identical with key's name.
  219.      * If the value is not one of these 3, the parameter is not output.
  220.      *
  221.      * @param  array  $options      Array of options.
  222.      * @param  array  $exclude      Array of options to be excluded.
  223.      * @param  string $insertBefore String to be inserted before options.
  224.      * @param  string $insertAfter  String to be inserted ater options.
  225.      * @return string
  226.      */
  227.     function _parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null)
  228.     {
  229.         $minimizedAttributes = array(
  230.             'compact',
  231.             'checked',
  232.             'declare',
  233.             'readonly',
  234.             'disabled',
  235.             'selected',
  236.             'defer',
  237.             'ismap',
  238.             'nohref',
  239.             'noshade',
  240.             'nowrap',
  241.             'multiple',
  242.             'noresize'
  243.         );
  244.  
  245.         if (!is_array($exclude))
  246.         {
  247.             $exclude = array();
  248.         }
  249.  
  250.         if (is_array($options))
  251.         {
  252.             $out = array();
  253.  
  254.             foreach ($options as $key => $value)
  255.             {
  256.                 if (!in_array($key, $exclude))
  257.                 {
  258.                     if (in_array($key, $minimizedAttributes) && ($value === 1 ||
  259.                     $value === true || $value === 'true' || in_array($value,
  260.                     $minimizedAttributes)))
  261.                     {
  262.                         $value = $key;
  263.                     }
  264.                     elseif (in_array($key, $minimizedAttributes))
  265.                     {
  266.                         continue;
  267.                     }
  268.                     $out[] = "{$key}=\"{$value}\"";
  269.                 }
  270.             }
  271.             $out = join(' ', $out);
  272.             return $out? $insertBefore.$out.$insertAfter: null;
  273.         }
  274.         else
  275.         {
  276.             return $options? $insertBefore.$options.$insertAfter: null;
  277.         }
  278.     }
  279. }
  280. ?>
Parsed in 0.235 seconds, using GeSHi 1.0.7.14

Modify this Paste