Cnz_Html
[ class tree: Cnz_Html ] [ index: Cnz_Html ] [ all elements ]

Source for file Head.php

Documentation is available at Head.php

  1. <?php
  2. /**
  3.  * CNZ Framework
  4.  *
  5.  * A Framework for Creating and Using Complex Web Elements
  6.  *
  7.  * The purpose of this framework is to provide a library of high-level objects
  8.  * to facilitate common HTML coding tasks, such as menus, tables, and forms.
  9.  * The intent is to reduce repetitive HTML coding as much as possible, replacing
  10.  * it with a combination of configuration files and style sheets with
  11.  * standardized naming conventions.
  12.  *
  13.  * This framework is built on and requires the
  14.  * {@link http://framework.zend.com/ Zend Framework}.
  15.  *
  16.  * @category Cnz
  17.  * @package  Cnz_Html
  18.  *
  19.  * @author    Lyle Frost <lfrost@cnz.com>
  20.  * @copyright Copyright (c) 2006-2007 Citadel Network <{@link http://www.citadelnetwork.com/}>
  21.  * @filesource
  22.  * @license   http://www.citadelnetwork.com/license/cnzframework New BSD License
  23.  * @version   $Id: Head.php 27 2007-07-19 18:47:54Z lfrost $
  24.  */
  25.  
  26. /**
  27.  * Required classes.
  28.  *
  29.  * @ignore
  30.  */
  31. Zend_Loader::loadClass('Cnz_Html_Element');
  32.  
  33. /**
  34.  * HTML Head class
  35.  *
  36.  * This class automates the creation and display of a header block.
  37.  *
  38.  * Where the lists are filenames, the file extension should not be included.
  39.  * Style filenames must have the extension .css, and client-side script files
  40.  * must have the extension .js.
  41.  *
  42.  * Scripts for a given page should be stored underneath
  43.  * <var>scriptDirLocal</var>.  Scripts for an entire site should be stored
  44.  * underneath <var>scriptDirSite</var>.  Scripts to be shared across sites on a
  45.  * server should be stored in <var>scriptDirServer</var>.  Script names may also
  46.  * be code-generated and fed to the constructor.  Similary for styles.
  47.  *
  48.  * Each script and style may be specified as either an absolute path or a
  49.  * relative path.  If a relative path, then the file is taken from the site
  50.  * directory if the file exists, else the server directory.  If scriptAuto/
  51.  * styleAuto is enabled, then all script/style files (judged by the file
  52.  * extension) in the local script/style directory are used.
  53.  *
  54.  * Styles may be followed by a field delimiter (<kbd>:</kbd>) and a value for
  55.  * the media attribute.  If no media attribute is specified, the default is
  56.  * <kbd>all</kbd>.
  57.  *
  58.  * Example INI (defaults shown as comments):
  59.  *
  60.  * <pre><samp>
  61.  * [htmlHead]
  62.  * ;author          = <organization>
  63.  * ;copyright       = "Copyright &copy; <author>"
  64.  * description      = Description for this Site
  65.  * ;distribution    = global
  66.  * keywords         = keyword list
  67.  * organization     = Organization Name
  68.  * ;revisit         = 7 days
  69.  * ;robots          = index,follow
  70.  * scripts          = mootools
  71.  * ;scriptAuto      = yes
  72.  * ;scriptDirLocal  = script/
  73.  * ;scriptDirServer = /slib/script/
  74.  * ;scriptDirSite   = /lib/script/
  75.  * styles           = main:all,another:all
  76.  * ;styleAuto       = yes
  77.  * ;styleDirLocal   = style/
  78.  * ;styleDirServer  = /slib/style/
  79.  * ;styleDirSite    = /lib/script/
  80.  * ;title           = <domain>
  81.  * </samp></pre>
  82.  *
  83.  * Example PHP:
  84.  *
  85.  * <code>
  86.  * $htmlHead = Cnz_Html::loadAndDisplay('Cnz_Html_Head');
  87.  * </code>
  88.  *
  89.  * @category Cnz
  90.  * @package  Cnz_Html
  91.  */
  92. {
  93.     const HAS_CONFIG        = true;
  94.     const PREFIX            = 'HtmlHead';
  95.     const DEFAULT_COPYRIGHT        = 'Copyright &copy; ';
  96.     const DEFAULT_DISTRIBUTION    = 'global';
  97.     const DEFAULT_MEDIA        = 'all';
  98.     const DEFAULT_REVISIT        = '7 days';
  99.     const DEFAULT_ROBOTS        = 'index,follow';
  100.     const DEFAULT_SCRIPT_DIR_SERVER    = '/slib/script/';
  101.     const DEFAULT_SCRIPT_DIR_SITE    = '/lib/script/';
  102.     const DEFAULT_SCRIPT_DIR_LOCAL    = 'script/';
  103.     const DEFAULT_STYLE_DIR_SERVER    = '/slib/style/';
  104.     const DEFAULT_STYLE_DIR_SITE    = '/lib/style/';
  105.     const DEFAULT_STYLE_DIR_LOCAL    = 'style/';
  106.     const TITLE_SEPARATOR        = ' :: ';
  107.  
  108.     /**#@+ @var array */
  109.     private    $subtitleArray        array();
  110.     private    $scriptArray        array();
  111.     /** Associative array stylename => media */
  112.     private    $styleArray        array();
  113.     /**#@-*/
  114.  
  115.     /**#@+ @var boolean */
  116.     private    $scriptAutoFlag        true;
  117.     private    $styleAutoFlag        true;
  118.     /**#@-*/
  119.  
  120.     /**#@+ @var string */
  121.     private    $title            NULL;
  122.     /** Default value $this->organization */
  123.     private    $author            NULL;
  124.     /** Default value DEFAULT_COPYRIGHT . $this->organization or author */
  125.     private    $copyright        NULL;
  126.     private    $description        NULL;
  127.     private    $distribution        self::DEFAULT_DISTRIBUTION;
  128.     private    $keywords        NULL;
  129.     private    $scriptList        NULL;
  130.     private    $styleList        NULL;
  131.     private    $organization        NULL;
  132.     private    $revisit        self::DEFAULT_REVISIT;
  133.     private    $robots            self::DEFAULT_ROBOTS;
  134.     private    $scriptDirLocal        self::DEFAULT_SCRIPT_DIR_LOCAL;
  135.     private    $scriptDirServer    self::DEFAULT_SCRIPT_DIR_SERVER;
  136.     private    $scriptDirSite        self::DEFAULT_SCRIPT_DIR_SITE;
  137.     private    $styleDirLocal        self::DEFAULT_STYLE_DIR_LOCAL;
  138.     private    $styleDirServer        self::DEFAULT_STYLE_DIR_SERVER;
  139.     private    $styleDirSite        self::DEFAULT_STYLE_DIR_SITE;
  140.     /**#@-*/
  141.  
  142.     /* Getters/Setters ===================================================*/
  143.  
  144.     /** @return string Copyright */
  145.     public function getCopyright()
  146.     {
  147.         return $this->copyright;
  148.     }
  149.  
  150.     /** @return string Organization */
  151.     public function getOrganization()
  152.     {
  153.         return $this->organization;
  154.     }
  155.  
  156.     /** @return string Title */
  157.     public function getTitle()
  158.     {
  159.         return $this->title;
  160.     }
  161.  
  162.     /* Methods ===========================================================*/
  163.  
  164.     /**
  165.      * Option fields:
  166.      *   string keywords   List of additional keywords
  167.      *   string scripts    List of additional client-side scripts
  168.      *   string styles     List of additional CSS styles
  169.      *   string subtitles  List of page subtitles
  170.      *
  171.      * Configuration fields:
  172.      *   author           Value for author meta tag (default organization)
  173.      *   copyright        Value for copyright meta tag (default Copyright &copy; author)
  174.      *   description      Value for description meta tag (default none)
  175.      *   distribution     Value for distribution meta tag (default global)
  176.      *   keywords         Value for keywords meta tag (default none)
  177.      *   organization     Value for organization meta tag (required)
  178.      *   revisit          Value for revisit meta tag (default 7 days)
  179.      *   robots           Value for robots meta tag (default index,follow)
  180.      *   scripts          List of client-side scripts to include (default none)
  181.      *   scriptAuto       Automatically include scripts in local script dir (default yes).
  182.      *   scriptDirLocal   Local script directory (default script/)
  183.      *   scriptDirServer  Server script directory (default /slib/script/)
  184.      *   scriptDirSite    Site script directory (default /lib/script/)
  185.      *   styles           List of styles to include (default none)
  186.      *   styleAuto        Automatically include stypes in local style dir (default yes).
  187.      *   styleDirLocal    Local style directory (default style/)
  188.      *   styleDirServer   Server style directory (default /slib/style/)
  189.      *   styleDirSite     Site style directory (default /lib/style/)
  190.      *   title            Site title (default none)
  191.      *
  192.      * @param  array $options Options
  193.      */
  194.     public function __construct(array $options array())
  195.     {
  196.         parent::__construct($options);
  197.  
  198.         // Process options.
  199.         if (isset($options['keywords'])) $this->keywords .= ',' $options['keywords'];
  200.         if (isset($options['scripts'])) $this->scriptList .= ',' $options['scripts'];
  201.         if (isset($options['styles'])) $this->styleList .= ',' $options['styles'];
  202.         if (isset($options['subtitles'])) $this->subtitleArray Cnz_Html::listToArray($options['subtitles']);
  203.  
  204.         // Process configuration.
  205.         if (isset($this->config->author)) $this->author $this->config->author;
  206.         if (isset($this->config->copyright)) $this->copyright $this->config->copyright;
  207.         if (isset($this->config->description)) $this->description $this->config->description;
  208.         if (isset($this->config->distribution)) $this->distribution $this->config->distribution;
  209.         if (isset($this->config->keywords)) $this->keywords $this->config->keywords;
  210.         if (isset($this->config->organization)) $this->organization $this->config->organization;
  211.         if (isset($this->config->revisit)) $this->revisit $this->config->revisit;
  212.         if (isset($this->config->robots)) $this->robots $this->config->robots;
  213.         if (isset($this->config->scripts)) $this->scriptArray Cnz_Html::listToArray($this->config->scripts);
  214.         if (isset($this->config->scriptAuto)) $this->scriptAutoFlag Cnz_Html::configFlag($this->config->scriptAuto);
  215.         if (isset($this->config->scriptDirLocal)) $this->scriptDirLocal $this->config->scriptDirLocal;
  216.         if (isset($this->config->scriptDirServer)) $this->scriptDirServer $this->config->scriptDirServer;
  217.         if (isset($this->config->scriptDirSite)) $this->scriptDirSite $this->config->scriptDirSite;
  218.         if (isset($this->config->styles)) $this->styleArray Cnz_Html::listToArray($this->config->styles);
  219.         if (isset($this->config->styleAuto)) $this->styleAutoFlag Cnz_Html::configFlag($this->config->styleAuto);
  220.         if (isset($this->config->styleDirLocal)) $this->styleDirLocal $this->config->styleDirLocal;
  221.         if (isset($this->config->styleDirServer)) $this->styleDirServer $this->config->styleDirServer;
  222.         if (isset($this->config->styleDirSite)) $this->styleDirSite $this->config->styleDirSite;
  223.         if (isset($this->config->title)) $this->title $this->config->title;
  224.  
  225.         if (empty($this->author)) $this->author $this->organization;
  226.         if (empty($this->copyright)) $this->copyright self::DEFAULT_COPYRIGHT $this->author;
  227.         if (empty($this->title)) $this->title Cnz_Html::getDomain();
  228.  
  229.         Cnz_Html::listAppendToArray($this->scriptArray$this->scriptList);
  230.         Cnz_Html::listAppendToArray($this->styleArray$this->styleList);
  231.  
  232.         // Automatic scripts
  233.         $scriptDirLocal Cnz_Html::docDir($this->scriptDirLocal;
  234.         if ($this->scriptAutoFlag && is_dir($scriptDirLocal))
  235.         {
  236.             $scripts scandir($scriptDirLocal);
  237.             if ($scripts === false)
  238.             {
  239.                 Cnz_Html::getLogger()->warn(__METHOD__ . '[' . __LINE__ . '] ' 'Failed scanning local script directory.');
  240.             }
  241.             foreach ($scripts as $script)
  242.             {
  243.                 $filebase Cnz_Html::checkFileExtension($scriptCnz_Html::SCRIPT_FILE_EXT);
  244.                 if ($filebase)
  245.                 {
  246.                     $this->scriptArray['local:' $filebaseNULL;
  247.                 }
  248.             }
  249.         }
  250.  
  251.         // Automatic styles
  252.         $styleDirLocal Cnz_Html::docDir($this->styleDirLocal;
  253.         if ($this->styleAutoFlag && is_dir($styleDirLocal))
  254.         {
  255.             $styles scandir($styleDirLocal);
  256.             if ($styles === false)
  257.             {
  258.                 Cnz_Html::getLogger()->warn(__METHOD__ . '[' . __LINE__ . '] ' 'Failed scanning local style directory.');
  259.             }
  260.             foreach ($styles as $style)
  261.             {
  262.                 $filebase Cnz_Html::checkFileExtension($styleCnz_Html::STYLE_FILE_EXT);
  263.                 if ($filebase)
  264.                 {
  265.                     $this->styleArray['local:' $filebaseself::DEFAULT_MEDIA;
  266.                 }
  267.             }
  268.         }
  269.  
  270.         return;
  271.     }
  272.  
  273.     /**
  274.      * @param  string $style Unused
  275.      * @return void 
  276.      */
  277.     public function display($style NULL)
  278.     {
  279.         $is Cnz_Html::getIndentString();
  280.         $indent Cnz_Html::indentGenerate();
  281.  
  282.         echo $indent'<head>'"\n";
  283.  
  284.         //
  285.         // subtitles
  286.         if (is_array($this->subtitleArray))
  287.         {
  288.             foreach ($this->subtitleArray as $subtitle => $value)
  289.             {
  290.                 $this->title .= self::TITLE_SEPARATOR $subtitle;
  291.             }
  292.         }
  293.         echo $indent$is'<title>'$this->title'</title>'"\n";
  294.  
  295.         // base
  296.         // This is for compatibility with mod_rewrite.
  297.         $base Cnz_Html::docDir(true);
  298.         if (substr($base-1!= '/'$base dirname($base);
  299.         echo $indent$is'<base href = "'Cnz_Html::uriRoot()$base'"/>'"\n";
  300.  
  301.         // styles
  302.         foreach ($this->styleArray as $src => $media)
  303.         {
  304.             $src trim($src);
  305.             echo $indent$is'<link href = "';
  306.             if (strncmp($src'local:'6== 0)
  307.             {
  308.                 echo $this->styleDirLocal;
  309.                 $src substr($src6);
  310.             }
  311.             elseif (substr($src01!= '/')
  312.             {
  313.                 // if file exists in site dir, use it, else use server dir.
  314.                 if (is_file($_SERVER['DOCUMENT_ROOT'$this->styleDirSite $src '.' Cnz_Html::STYLE_FILE_EXT)) echo $this->styleDirSite;
  315.                 else echo $this->styleDirServer;
  316.             }
  317.             if (empty($media)) $media self::DEFAULT_MEDIA;
  318.             echo $src;
  319.             echo '.'Cnz_Html::STYLE_FILE_EXT'" media = "' $media '" rel = "stylesheet" type = "'Cnz_Html::STYLE_TYPE'"/>'"\n";
  320.         }
  321.  
  322.         // meta tags
  323.         if (!empty($this->author)) echo $indent$is'<meta name = "author" content = "'$this->author'"/>'"\n";
  324.         if (!empty($this->copyright)) echo $indent$is'<meta name = "copyright" content = "'str_replace('&copy;''(c)'$this->copyright)'"/>'"\n";
  325.         if (!empty($this->description)) echo $indent$is'<meta name = "description" content = "'$this->description'"/>'"\n";
  326.         if (!empty($this->distribution)) echo $indent$is'<meta name = "distribution" content = "'$this->distribution'"/>'"\n";
  327.         if (!empty($this->keywords)) echo $indent$is'<meta name = "keywords" content = "'$this->keywords'"/>'"\n";
  328.         if (!empty($this->organization)) echo $indent$is'<meta name = "organization" content = "'$this->organization'"/>'"\n";
  329.         if (!empty($this->revisit)) echo $indent$is'<meta name = "revisit-after" content = "'$this->revisit'"/>'"\n";
  330.         if (!empty($this->robots)) echo $indent$is'<meta name = "robots" content = "'$this->robots'"/>'"\n";
  331.  
  332.         // JavaScript sources
  333.         foreach ($this->scriptArray as $src => $value)
  334.         {
  335.             $src trim($src);
  336.             echo $indent$is'<script src = "';
  337.             if (strncmp($src'local:'6== 0)
  338.             {
  339.                 echo $this->scriptDirLocal;
  340.                 $src substr($src6);
  341.             }
  342.             elseif (substr($src01!= '/')
  343.             {
  344.                 // if file exists in site dir, use it, else use server dir.
  345.                 if (is_file($_SERVER['DOCUMENT_ROOT'$this->scriptDirSite $src '.' Cnz_Html::SCRIPT_FILE_EXT)) echo $this->scriptDirSite;
  346.                 else echo $this->scriptDirServer;
  347.             }
  348.             echo $src;
  349.              echo '.'Cnz_Html::SCRIPT_FILE_EXT'" type = "'Cnz_Html::getJavascriptType()'"></script>'"\n";
  350.         }
  351.  
  352.         echo $indent'</head>'"\n";
  353.  
  354.         return;
  355.     }
  356. }

Documentation generated on Thu, 19 Jul 2007 15:02:05 -0400 by phpDocumentor 1.4.0RC2