Daeva
In Runtime
- Messages
- 407
Ok, here is the main source. It wouldn't fit in the previous one.
PHP:
<?php
/*
FILE: pageHeader.php
CREATED: 2/24/2009
LAST MODIFIED: 3/19/2009
VERSION: 2.1.1
AUTHOR: David Kyle (dkyle)
PURPOSE: the pageHeader class handles the creation of the top of an html page. It allows you to specify the doctype, characterset, meta tags, stylesheets, javascripts, title and almost all other aspects of the page.
This file generates documents that can be validated against the W3's reccomendation for several different document types.
It is useful for creating websites using a content management system, or for creating the same look-and-feel for each page, without having to re-type or copy paste the code.
---ChangeLog---
// ******* 03/19/2009 ******* \\
--03/19/2009 : Added support for html4
+ -- stripped single-line close tags ( />) from metas
+ -- stripped single-line close tags ( />) from stylesheet links
--03/19/2009 : Added functionality
+ -- added ability to exclue javascripts from the <head> tag
--03/19/2009 : Bug-fixes
--03/19/2009 : Todo
+ -- Add support for embedding data driven content into the head structure
+ -- Add support for favicon
+ -- Provide Script lookup and style lookup capabilities
+ -- Improve formatting support for inline stylesheet and javascript code
+ -- Create a new package that will serialize this class to php, html, php/html for the creation of static or dynamic pages for Content Management Systems
+ -- Provide secure support for insertion of php tags
// ******* ******* \\
*/
//Builds the top of an html page
class pageHeader {
private $_title; // page title value
private $_charset; // character set, used in xml declaration (xhtml) and meta http-equiv
private $_doctype; // dtd
private $_scripts = array(); // list of javascript classes
public $scripts; // public access to the scripts array
public $styles; // public access to the styles array
private $_styles = array();
public $metas = array();
private $_metas = array();
private $spacing;
/// DTD associative Array. To support more document types, simply add the name and the value for the dtd
private $docTypes = array("xhtmlSt"=>"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">","xhtmlTr"=>"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">","html4"=>"<!DOCTYPE HTML PUBLIC\"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
function __construct($title,$charset) { // require the title and the charset and the minimum
$this->spacing = new formatting(0);
$this->setTitle($title);
$this->setCharset($charset);
$this->scripts = $this->_scripts; // public access to the jScripts array object
$this->styles = $this->_styles; // public access to the styles array object
} // end constructor
public function addScript($linkType,$src,$code,$inHeader) { // add to the current javascript objects
array_push($this->_scripts,new jScripts($linkType,$src,$code,$inHeader));//append the newly created jScript object to the array
$newNumber = count($this->_scripts)-1;
$this->_scripts[$newNumber]->setSpacing(2);
$this->scripts = $this->_scripts; // assign the private array to the public one
} // end function addScript
private function setTitle($title) { // set the page's title
if (strlen($title) > 0) { // make sure they entered a title
$this->_title = $title;
} else {
$this->_title = "Untitled Document"; // default page title
} // end if/else
} // end function setTitle
public function addMeta($name,$value) { // add a meta tag
array_push($this->_metas,array($name=>$value));
$this->metas = $this->_metas;
} // end function addMeta
private function setCharset($charset) {
if (strlen($charset) > 0) { // to maintain validity, check to make sure this exists
$this->_charset = $charset;
} else {
$this->_charset = "utf-8"; // default character set
} // end if/else
} // end class setCharset
public function addStyle($styleType,$src,$code) { // add to the current stylesheet objects
array_push($this->_styles,new style($styleType,$src,$code)); // append to the styles array the newly created style
$newNumber = count($this->_styles)-1;
$this->_styles[$newNumber]->setSpacing(2);
$this->styles = $this->_styles; // assign the public to the private
} // end function addStyle
public function setDocType($docType) {// apply the correct document type declaration
$this->_doctype = $docType; // set the Document Type to be used later to determine output type
} // end function setDocType
function writePage() { // output the page
$returnText = "";
switch($this->_doctype) {
case "xhtmlSt":
$returnText .= $this->getXhtmlDTD();
break;
case "xhtmlTr":
$returnText .= $this->getXhtmlDTD();
break;
case "html4":
$returnText .= $this->getHtmlDTD();
break;
default:
$returnText .= $this->getHtmlDTD();
break;
} // end switch
$this->spacing->setSpacing(1);
$returnText .= $this->spacing->getSpaces() . "<head>\n";
$this->spacing->setSpacing(2);
$returnText .= $this->spacing->getSpaces() . "<title>".$this->_title."</title>\n";
$returnText .= $this->spacing->getSpaces() . "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$this->_charset."\"";
if($this->_doctype == "html4") {
$returnText .= ">\n";
} else {
$returnText .= " />\n";
} // end if/else
// include javascripts
for($i=0;$i<count($this->_scripts);$i++) {
if ($this->_scripts[$i]->showInHeader() == true) {
$returnText .= $this->_scripts[$i]->getScript();
} else {
// Do nothing because we don't want it shown in the header
} // end if/else
} // end for loop
// include stylesheets
for($i=0;$i<count($this->_styles);$i++) {
if ($this->_doctype == "html4") {
$returnText .= str_replace(" />",">",$this->_styles[$i]->getStyle());
} else {
$returnText .= $this->_styles[$i]->getStyle();
} // end if/else
} // end for loop
for($i = 0;$i<count($this->_metas);$i++) {
foreach($this->_metas[$i] as $name=>$value) {
$returnText .= $this->spacing->getSpaces() . "<meta name=\"". $name . "\" content=\"". $value . "\"";
if($this->_doctype == "html4") {
$returnText .= ">\n";
} else {
$returnText .= " />\n";
} // end if/else
} // end for loop
} // end for loop
$this->spacing->setSpacing(1);
$returnText .= $this->spacing->getSpaces() . "</head>\n";
$returnText .= $this->spacing->getSpaces() . "<body>\n";
return $returnText;
} // end writePage
private function getXhtmlDTD() {
$returnString = $this->spacing->getSpaces() . "<?xml version=\"1.0\" encoding=\"".$this->_charset."\"?>\n";
$returnString .= $this->spacing->getSpaces() . $this->docTypes[$this->_doctype] . "\n";
$returnString .= $this->spacing->getSpaces() . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-us\" dir=\"ltr\" lang=\"en-us\">\n";
return $returnString;
} // end function getXhtmlDTD
private function getHtmlDTD() {
$returnString = $this->spacing->getSpaces() . $this->docTypes[$this->_doctype] . "\n";
$returnString .= $this->spacing->getSpaces() . "<html>\n";
return $returnString;
} // end function getHtmlDTD
} // end class pageHeader
// Class that other classes can use to apply formatting (tab indentation) to the source output
class formatting {
private $_spacing = ""; // spaces to prefix the line with
private $_spacingNum = 0; // used to calculate the $_spacing
function __construct($spacing) {
$this->setSpacing($spacing);
} // end constructor
public function setSpacing($num) { //Set the spacing directly
$this->_spacingNum = $num;
} // end function setSpacing
public function addSpaces($num) { // Append to the spaces. No different from setSpacing at this point
$this->setSpacing($num);
} // end function addSpaces
public function getSpaces() { // return the nuber of spaces to append
if(strlen($this->_spacing) == $this->_spacingNum) { // if the number hasn't change, don't re-compute every time to minimize overhead
} else {
$this->_spacing = "";
for($i=0;$i<$this->_spacingNum;$i++) {
$this->_spacing .=" ";
} // end for loop
} // end if/else
return $this->_spacing;
} // end function getSpaces
} // end function formatting
?>