[Cs-content-commits] SF.net SVN: cs-content:[440] trunk/1.0
PHP Templating & Includes System
Brought to you by:
crazedsanity
From: <cra...@us...> - 2009-08-18 18:40:30
|
Revision: 440 http://cs-content.svn.sourceforge.net/cs-content/?rev=440&view=rev Author: crazedsanity Date: 2009-08-18 18:40:18 +0000 (Tue, 18 Aug 2009) Log Message: ----------- Cleanup, modifications for unit testing. /contentSystem.class.php: * intitialize_locals(): -- set the SITE_ROOT a bit more dynamically, so it should work even if the constant SITE_ROOT is not defined. /cs_genericPage.class.php: * MAIN::: -- a bit of formatting -- added templateFiles var as an array. -- dropped "showEditableLink" var. * __construct(): -- ARG CHANGE: ARG REMOVED: #3 ($allowRedirects=TRUE) -- removed unnecessary (and apparently unused) var for optionally disallowing redirects. -- NOTE: this was easily bypassed by using a method in cs_globalFunctions of the same name. -- remove code for showing editable link (this is NOT something that should be handled at this level; a CMS should handle that part). * initialize_locals(): -- attempt to set siteRoot from the mainTemplateFile value before trying to use SITE_ROOT (arguments should ALWAYS be able to override constant definitions). -- if no SITE_ROOT constant, try to find a templates directory beneath the server's DOCUMENT_ROOT. -- throw an exception if tmplDir isn't a directory (otherwise the Template class will exit with an error). * add_template_file(): -- dropped code that dealt with showEditableLink -- updates the internal templateFiles array. * conditional_header(): -- ARG CHANGE: NEW ARG: #3 ($isPermRedir=FALSE) -- now it calls cs_globalFunctions::conditional_header() so there is really only one set of code for this operation. * __get() [NEW]: -- magic PHP method for retrieving the values of protected/private vars (includes non-existent ones). * __set() [NEW]: -- magic PHP method for setting the values of protected/private vars (including non-existent ones). Modified Paths: -------------- trunk/1.0/contentSystem.class.php trunk/1.0/cs_genericPage.class.php Modified: trunk/1.0/contentSystem.class.php =================================================================== --- trunk/1.0/contentSystem.class.php 2009-08-14 19:40:09 UTC (rev 439) +++ trunk/1.0/contentSystem.class.php 2009-08-18 18:40:18 UTC (rev 440) @@ -145,7 +145,11 @@ //build the templating engine: this may cause an immediate redirect, if they need to be logged-in. //TODO: find a way to define this on a per-page basis. Possibly have templateObj->check_login() // run during the "finish" stage... probably using GenericPage{}->check_login(). - $this->templateObj = new cs_genericPage(FALSE, "main.shared.tmpl"); + $root = $_SERVER['DOCUMENT_ROOT']; + if(defined('SITE_ROOT')) { + $root = constant('SITE_ROOT'); + } + $this->templateObj = new cs_genericPage(FALSE, $root ."/main.shared.tmpl"); //setup some default template vars. $this->templateObj->add_template_var('date', date('m-d-Y')); Modified: trunk/1.0/cs_genericPage.class.php =================================================================== --- trunk/1.0/cs_genericPage.class.php 2009-08-14 19:40:09 UTC (rev 439) +++ trunk/1.0/cs_genericPage.class.php 2009-08-18 18:40:18 UTC (rev 440) @@ -12,9 +12,10 @@ class cs_genericPage extends cs_contentAbstract { public $templateObj; //template object to parse the pages - public $templateVars = array(); //our copy of the global templateVars - public $templateRows = array(); //array of block rows & their contents. - public $mainTemplate; //the default layout of the site + public $templateVars = array(); //our copy of the global templateVars + public $templateFiles = array(); //our list of template files... + public $templateRows = array(); //array of block rows & their contents. + public $mainTemplate; //the default layout of the site public $unhandledVars=array(); public $printOnFinish=true; @@ -23,17 +24,13 @@ private $siteRoot; private $allowRedirect; - private $showEditableLink = FALSE; - private $allowInvalidUrls=NULL; //--------------------------------------------------------------------------------------------- /** * The constructor. */ - public function __construct($restrictedAccess=TRUE, $mainTemplateFile=NULL, $allowRedirect=TRUE) { - //handle some configuration. - $this->allowRedirect = $allowRedirect; + public function __construct($restrictedAccess=TRUE, $mainTemplateFile=NULL) { //initialize stuff from our parent... parent::__construct(); @@ -47,11 +44,6 @@ if(!defined('CS-CONTENT_SESSION_NAME')) { define("CS-CONTENT_SESSION_NAME", ini_get('session.name')); } - - //TODO: if the end page doesn't want to allow the "edit" links, will this still work? - if(defined("CS_CONTENT_MODIFIABLE") && constant("CS_CONTENT_MODIFIABLE") === TRUE) { - $this->showEditableLink = TRUE; - } }//end __construct() //--------------------------------------------------------------------------------------------- @@ -72,16 +64,29 @@ $mainTemplateFile = preg_replace('/^\//', '', $mainTemplateFile); } - - if(defined('SITE_ROOT')) { + if(isset($mainTemplateFile) && strlen($mainTemplateFile) && is_dir(dirname($mainTemplateFile))) { + $this->siteRoot = dirname($mainTemplateFile); + if(preg_match('/\//', $this->siteRoot) && preg_match('/templates/', $this->siteRoot)) { + $this->siteRoot .= "/.."; + } + } + elseif(defined('SITE_ROOT') && is_dir(constant('SITE_ROOT'))) { $this->siteRoot = constant('SITE_ROOT'); } + elseif(is_dir($_SERVER['DOCUMENT_ROOT'] .'/templates')) { + $this->siteRoot = $_SERVER['DOCUMENT_ROOT'] .'/templates'; + } else { - throw new exception(__METHOD__ .": required constant 'SITE_ROOT' not set"); + throw new exception(__METHOD__ .": cannot locate siteRoot from main template file (". $mainTemplateFile .")"); } $this->tmplDir = $this->siteRoot .'/templates'; $this->libDir = $this->siteRoot .'/lib'; + if(!is_dir($this->tmplDir)) { + $this->gfObj->debug_print(func_get_args(),1); + throw new exception(__METHOD__ .": invalid templates folder (". $this->tmplDir ."), pwd=(". getcwd() .")"); + } + //if there have been some global template vars (or files) set, read 'em in here. if(is_array($GLOBALS['templateVars']) && count($GLOBALS['templateVars'])) { foreach($GLOBALS['templateVars'] as $key=>$value) { @@ -176,13 +181,8 @@ * TODO: check if $fileName exists before blindly trying to parse it. */ public function add_template_file($handleName, $fileName){ - if($this->showEditableLink) { - $prefix = '[<a href="#NULL_page='. $fileName .'"><font color="red"><b>Edit "'. $handleName .'"</b></font></a>]<BR>'; - $this->add_template_var($handleName, $prefix .$this->file_to_string($fileName)); - } - else { - $this->add_template_var($handleName, $this->file_to_string($fileName)); - } + $this->templateFiles[$handleName] = $fileName; + $this->add_template_var($handleName, $this->file_to_string($fileName)); }//end add_template_file() //--------------------------------------------------------------------------------------------- @@ -489,35 +489,8 @@ /** * Performs redirection, provided it is allowed. */ - function conditional_header($url, $exitAfter=TRUE) { - if($this->allowRedirect) { - //checks to see if headers were sent; if yes: use a meta redirect. - // if no: send header("location") info... - if(headers_sent()) { - //headers sent. Use the meta redirect. - print " - <HTML> - <HEAD> - <TITLE>Redirect Page</TITLE> - <META HTTP-EQUIV='refresh' content='0; URL=$url'> - </HEAD> - <a href=\"$url\"></a> - </HTML> - "; - } - else { - header("location:$url"); - } - - if($exitAfter) { - //redirecting without exitting is bad, m'kay? - exit; - } - } - else { - //TODO: should an exception be thrown, or maybe exit here anyway? - throw new exception(__METHOD__ .": auto redirects not allowed...?"); - } + function conditional_header($url, $exitAfter=TRUE,$isPermRedir=FALSE) { + $this->gfObj->conditional_header($url, $exitAfter, $isPermRedir); }//end conditional_header() //--------------------------------------------------------------------------------------------- @@ -691,6 +664,31 @@ return($this->templateVars[$section]); }//strip_undef_template_vars_from_section() //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + /** + * Magic PHP method for retrieving the values of private/protected vars. + */ + public function __get($var) { + return($this->$var); + }//end __get() + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + /** + * Magic PHP method for changing the values of private/protected vars (or + * creating new ones). + */ + public function __set($var, $val) { + + //TODO: set some restrictions on internal vars... + $this->$var = $val; + }//end __set() + //------------------------------------------------------------------------- }//end cs_genericPage{} ?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |