Source for file Head.php
Documentation is available at Head.php
* A Framework for Creating and Using Complex Web Elements
* The purpose of this framework is to provide a library of high-level objects
* to facilitate common HTML coding tasks, such as menus, tables, and forms.
* The intent is to reduce repetitive HTML coding as much as possible, replacing
* it with a combination of configuration files and style sheets with
* standardized naming conventions.
* This framework is built on and requires the
* {@link http://framework.zend.com/ Zend Framework}.
* @author Lyle Frost <lfrost@cnz.com>
* @copyright Copyright (c) 2006-2007 Citadel Network <{@link http://www.citadelnetwork.com/}>
* @license http://www.citadelnetwork.com/license/cnzframework New BSD License
* @version $Id: Head.php 27 2007-07-19 18:47:54Z lfrost $
Zend_Loader::loadClass('Cnz_Html_Element');
* This class automates the creation and display of a header block.
* Where the lists are filenames, the file extension should not be included.
* Style filenames must have the extension .css, and client-side script files
* must have the extension .js.
* Scripts for a given page should be stored underneath
* <var>scriptDirLocal</var>. Scripts for an entire site should be stored
* underneath <var>scriptDirSite</var>. Scripts to be shared across sites on a
* server should be stored in <var>scriptDirServer</var>. Script names may also
* be code-generated and fed to the constructor. Similary for styles.
* Each script and style may be specified as either an absolute path or a
* relative path. If a relative path, then the file is taken from the site
* directory if the file exists, else the server directory. If scriptAuto/
* styleAuto is enabled, then all script/style files (judged by the file
* extension) in the local script/style directory are used.
* Styles may be followed by a field delimiter (<kbd>:</kbd>) and a value for
* the media attribute. If no media attribute is specified, the default is
* Example INI (defaults shown as comments):
* ;author = <organization>
* ;copyright = "Copyright © <author>"
* description = Description for this Site
* keywords = keyword list
* organization = Organization Name
* ;scriptDirLocal = script/
* ;scriptDirServer = /slib/script/
* ;scriptDirSite = /lib/script/
* styles = main:all,another:all
* ;styleDirLocal = style/
* ;styleDirServer = /slib/style/
* ;styleDirSite = /lib/script/
* $htmlHead = Cnz_Html::loadAndDisplay('Cnz_Html_Head');
private $subtitleArray = array();
private $scriptArray = array();
/** Associative array stylename => media */
private $styleArray = array();
private $scriptAutoFlag = true;
private $styleAutoFlag = true;
/** Default value $this->organization */
/** Default value DEFAULT_COPYRIGHT . $this->organization or author */
private $copyright = NULL;
private $description = NULL;
private $distribution = self::DEFAULT_DISTRIBUTION;
private $keywords = NULL;
private $scriptList = NULL;
private $styleList = NULL;
private $organization = NULL;
private $revisit = self::DEFAULT_REVISIT;
private $robots = self::DEFAULT_ROBOTS;
private $scriptDirLocal = self::DEFAULT_SCRIPT_DIR_LOCAL;
private $scriptDirServer = self::DEFAULT_SCRIPT_DIR_SERVER;
private $scriptDirSite = self::DEFAULT_SCRIPT_DIR_SITE;
private $styleDirLocal = self::DEFAULT_STYLE_DIR_LOCAL;
private $styleDirServer = self::DEFAULT_STYLE_DIR_SERVER;
private $styleDirSite = self::DEFAULT_STYLE_DIR_SITE;
/* Getters/Setters ===================================================*/
/** @return string Copyright */
/** @return string Organization */
return $this->organization;
/** @return string Title */
/* Methods ===========================================================*/
* string keywords List of additional keywords
* string scripts List of additional client-side scripts
* string styles List of additional CSS styles
* string subtitles List of page subtitles
* author Value for author meta tag (default organization)
* copyright Value for copyright meta tag (default Copyright © author)
* description Value for description meta tag (default none)
* distribution Value for distribution meta tag (default global)
* keywords Value for keywords meta tag (default none)
* organization Value for organization meta tag (required)
* revisit Value for revisit meta tag (default 7 days)
* robots Value for robots meta tag (default index,follow)
* scripts List of client-side scripts to include (default none)
* scriptAuto Automatically include scripts in local script dir (default yes).
* scriptDirLocal Local script directory (default script/)
* scriptDirServer Server script directory (default /slib/script/)
* scriptDirSite Site script directory (default /lib/script/)
* styles List of styles to include (default none)
* styleAuto Automatically include stypes in local style dir (default yes).
* styleDirLocal Local style directory (default style/)
* styleDirServer Server style directory (default /slib/style/)
* styleDirSite Site style directory (default /lib/style/)
* title Site title (default none)
* @param array $options Options
if (isset ($options['keywords'])) $this->keywords .= ',' . $options['keywords'];
if (isset ($options['scripts'])) $this->scriptList .= ',' . $options['scripts'];
if (isset ($options['styles'])) $this->styleList .= ',' . $options['styles'];
if (isset ($options['subtitles'])) $this->subtitleArray = Cnz_Html::listToArray($options['subtitles']);
// Process configuration.
if (isset ($this->config->author)) $this->author = $this->config->author;
if (isset ($this->config->copyright)) $this->copyright = $this->config->copyright;
if (isset ($this->config->description)) $this->description = $this->config->description;
if (isset ($this->config->distribution)) $this->distribution = $this->config->distribution;
if (isset ($this->config->keywords)) $this->keywords = $this->config->keywords;
if (isset ($this->config->organization)) $this->organization = $this->config->organization;
if (isset ($this->config->revisit)) $this->revisit = $this->config->revisit;
if (isset ($this->config->robots)) $this->robots = $this->config->robots;
if (isset ($this->config->scriptDirLocal)) $this->scriptDirLocal = $this->config->scriptDirLocal;
if (isset ($this->config->scriptDirServer)) $this->scriptDirServer = $this->config->scriptDirServer;
if (isset ($this->config->scriptDirSite)) $this->scriptDirSite = $this->config->scriptDirSite;
if (isset ($this->config->styleDirLocal)) $this->styleDirLocal = $this->config->styleDirLocal;
if (isset ($this->config->styleDirServer)) $this->styleDirServer = $this->config->styleDirServer;
if (isset ($this->config->styleDirSite)) $this->styleDirSite = $this->config->styleDirSite;
if (isset ($this->config->title)) $this->title = $this->config->title;
if (empty($this->author)) $this->author = $this->organization;
if (empty($this->copyright)) $this->copyright = self::DEFAULT_COPYRIGHT . $this->author;
if ($this->scriptAutoFlag && is_dir($scriptDirLocal))
$scripts = scandir($scriptDirLocal);
Cnz_Html::getLogger()->warn(__METHOD__ . '[' . __LINE__ . '] ' . 'Failed scanning local script directory.');
foreach ($scripts as $script)
$this->scriptArray['local:' . $filebase] = NULL;
if ($this->styleAutoFlag && is_dir($styleDirLocal))
Cnz_Html::getLogger()->warn(__METHOD__ . '[' . __LINE__ . '] ' . 'Failed scanning local style directory.');
foreach ($styles as $style)
$this->styleArray['local:' . $filebase] = self::DEFAULT_MEDIA;
* @param string $style Unused
public function display($style = NULL)
echo $indent, '<head>', "\n";
foreach ($this->subtitleArray as $subtitle => $value)
$this->title .= self::TITLE_SEPARATOR . $subtitle;
echo $indent, $is, '<title>', $this->title, '</title>', "\n";
// This is for compatibility with mod_rewrite.
echo $indent, $is, '<base href = "', Cnz_Html::uriRoot(), $base, '"/>', "\n";
foreach ($this->styleArray as $src => $media)
echo $indent, $is, '<link href = "';
if (strncmp($src, 'local:', 6) == 0)
echo $this->styleDirLocal;
elseif (substr($src, 0, 1) != '/')
// if file exists in site dir, use it, else use server dir.
if (is_file($_SERVER['DOCUMENT_ROOT'] . $this->styleDirSite . $src . '.' . Cnz_Html::STYLE_FILE_EXT)) echo $this->styleDirSite;
else echo $this->styleDirServer;
if (empty($media)) $media = self::DEFAULT_MEDIA;
echo '.', Cnz_Html::STYLE_FILE_EXT, '" media = "' . $media . '" rel = "stylesheet" type = "', Cnz_Html::STYLE_TYPE, '"/>', "\n";
if (!empty($this->author)) echo $indent, $is, '<meta name = "author" content = "', $this->author, '"/>', "\n";
if (!empty($this->copyright)) echo $indent, $is, '<meta name = "copyright" content = "', str_replace('©', '(c)', $this->copyright), '"/>', "\n";
if (!empty($this->description)) echo $indent, $is, '<meta name = "description" content = "', $this->description, '"/>', "\n";
if (!empty($this->distribution)) echo $indent, $is, '<meta name = "distribution" content = "', $this->distribution, '"/>', "\n";
if (!empty($this->keywords)) echo $indent, $is, '<meta name = "keywords" content = "', $this->keywords, '"/>', "\n";
if (!empty($this->organization)) echo $indent, $is, '<meta name = "organization" content = "', $this->organization, '"/>', "\n";
if (!empty($this->revisit)) echo $indent, $is, '<meta name = "revisit-after" content = "', $this->revisit, '"/>', "\n";
if (!empty($this->robots)) echo $indent, $is, '<meta name = "robots" content = "', $this->robots, '"/>', "\n";
foreach ($this->scriptArray as $src => $value)
echo $indent, $is, '<script src = "';
if (strncmp($src, 'local:', 6) == 0)
echo $this->scriptDirLocal;
elseif (substr($src, 0, 1) != '/')
// if file exists in site dir, use it, else use server dir.
if (is_file($_SERVER['DOCUMENT_ROOT'] . $this->scriptDirSite . $src . '.' . Cnz_Html::SCRIPT_FILE_EXT)) echo $this->scriptDirSite;
else echo $this->scriptDirServer;
echo $indent, '</head>', "\n";
|