Go Back   Computer Forums > Web Design | Website Development > Web Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 03-19-2009, 11:42 AM   #11
In Runtime
 
Daeva's Avatar
 
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: PHP Code simplifier

Ok, here is the main source. It wouldn't fit in the previous one.
PHP Code:
<?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
?>
__________________

__________________
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
http://www.twitter.com/xDaevax
Daeva is offline   Reply With Quote
Old 03-19-2009, 11:46 AM   #12
In Runtime
 
Daeva's Avatar
 
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: PHP Code simplifier

Grrr. It still didn't fit. I'll have to upload the code elsewhere and link to it next time.

Here is the final part of the main class file
PHP Code:
<?php
//Represents a stylesheet
class style {

  private 
$_linkStyle true;
  private 
$_linkSrc;
  private 
$_styleCode;
  private 
$_writeTags true;
  private 
$spacing;
  private 
$_txtSpacing "";

  function 
__construct($link,$src,$code) {
    
$this->spacing = new formatting(0);
    if (
$link === true || $link === false) {
      
$this->_linkStyle $link;
    } else {
      throw new 
Exception("Link type on style is invalid.");
    } 
// end if/else
    
$this->setSRC($src);
    
$this->_styleCode $code;
  } 
// end constructor
  
  
private function setSRC($src) {
    
$this->_linkSrc addslashes(str_replace("<?","",str_replace("?>","",$src)));
  } 
// end function setSRC
  
  
public function getStyle() { // return the style tag formatted later for html4 compliance
    
if($this->_linkStyle == true) {
      
$returnText .= $this->spacing->getSpaces() . "<style type=\"text/css\">\n";
      
$this->setSpacing(strlen($this->spacing->getSpaces())+1);
      
$returnText .= $this->spacing->getSpaces() . addslashes(str_replace("<?","",str_replace("?>","",$this->_styleCode)));
      
$this->setSpacing(strlen($this->spacing->getSpaces())-1);
      
$returnText .= "\n" $this->spacing->getSpaces() . "</style>\n";
    } else {
      
$returnText .= $this->spacing->getSpaces() . "<link rel=\"stylesheet\" type=\"text/css\" href=\"";
      if(
strlen($this->_linkSrc) > 0) {
        
$returnText .= $this->_linkSrc "\" />\n";
      } else {
        
$returnText .= "\"\" />\n";
      } 
// end if/else
    
// end if/else
    
return $returnText;  
  } 
// end function getStyle
  
  
public function setSpacing($num) { // needs modification!  setSpacing in class formatting used to be private so this provided access to it via the addSpaces method
    
$this->spacing->addSpaces($num);
  } 
// end function setSpacing

// end class style

//Represents a javascript element in the head, or other
class jScripts {

  private 
$_linkScript true;
  private 
$_linkSrc;
  private 
$_showInHeader true;
  private 
$_linkCode;
  private 
$_writeTags true// not yet implemented
  
private $spacing// object of the formatting class
  
private $_txtSpacing "";
  
  function 
__construct($link$src$code,$showHeader) {
    
$this->spacing = new formatting(0);
    if(
$link === true || $link === false) {
      
$this->_linkScript $link;
    } else {
      throw new 
Exception("Link type on script is invalid.");
    } 
// end if/else
    
if($showHeader === true || $showHeader === false) {
      
$this->_showInHeader $showHeader;
    } else {
      
$this->_showInHeader true;
    } 
// end if/else
    
$this->_linkSrc $this->setSRC($src);
    
$this->_linkCode $code;
  } 
// end constructor
  
  
public function setSpacing($num) { // apply formatting to output
    
$this->spacing->addSpaces($num);
  } 
// end function setSpacing
  
  
private function setSRC($src) { // remove dangerous things from the script source
   
return addslashes(str_replace("<?","",str_replace("?>","",$src))); 
  } 
// end function setSRC
  
  //Returns the full script element one of either <script><!--//--></script> or <script src=""></script>
  
public function getScript() {
    if(
$this->_writeTags === true) {
      
$returnText $this->spacing->getSpaces() ."<script type=\"text/javascript\"";
      if (
$this->_linkScript === true) {
        
$returnText .= " src=\"".$this->_linkSrc."\"></script>\n";
      } else {
        if(
strlen($this->_linkCode) > 0) {
          
$returnText .=">\n";
          
$returnText .= $this->spacing->getSpaces() . "<!--\n";
          
$this->setSpacing(strlen($this->spacing->getSpaces())+1);
          
$returnText .= $this->spacing->getSpaces() . addslashes(str_replace("<?","",str_replace("?>","",$this->_linkCode)));
          
$this->setSpacing(strlen($this->spacing->getSpaces())-1);
          
$returnText .= "\n" $this->spacing->getSpaces() . "//-->\n";
          
$returnText .= $this->spacing->getSpaces() . "</script>\n";
        } else {
          
$returnText .= "></script>\n";
        } 
// end if/else
      
// end if/else
    
} else {
      
// NOT IMPLEMENTED
    
// end if/else
    
return $returnText;
  } 
// end function getScript
  
  
public function showInHeader() {
    return 
$this->_showInHeader;
  } 
// end function showInHeader
// end class jScripts
?>
Any suggestions and/or ideas are greatly welcome and appreciated (positive or negative).
__________________

__________________
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
http://www.twitter.com/xDaevax
Daeva is offline   Reply With Quote
Old 03-19-2009, 11:07 PM   #13
In Runtime
 
Join Date: Mar 2009
Posts: 171
Send a message via AIM to burn420 Send a message via Yahoo to burn420
Default Re: PHP Code simplifier

I didn't give it much of a look, but from the looks of things looks alright...
If I wasn't in the middle of my project at the moment I would probably give it a better look...

Edit: So I took another look at it, seems pretty good, a little bit clustered but I definitely seen worse lol... So what else have you got left to do?
__________________

__________________
http://tetralogica.com
burn420 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 01:16 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0