From b8a750aea787b22627099997914a2f13498f1687 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 6 Dec 2006 05:07:12 +0000 Subject: [PATCH] Refactoring template.php --- template.php | 124 +++++++++++++++++++++------------------------------ 1 file changed, 52 insertions(+), 72 deletions(-) diff --git a/template.php b/template.php index 278af76..ba656e6 100644 --- a/template.php +++ b/template.php @@ -20,8 +20,7 @@ \*=====================================================================*/ /** -* Database-Driven Template System -* template.php +* Database-Driven Template System (template.php) * * @package ISSO */ @@ -32,53 +31,49 @@ * This framework is a backend to the database template engine and * contains all the parsing algorithms. * -* Hooks: -* $this->pre_parse_hook - Name of the function to execute on -* a template before the parsing occurs -* * @author Blue Static * @copyright Copyright ©2002 - [#]year[#], Blue Static * @version $Revision$ * @package ISSO * */ -class Template +class BSTemplate { /** - * Framework registry object - * @var object + * The database object to use + * @var object */ - private $registry = null; + private $db; /** * Name of the database table templates are in * @var string */ - private $tablename = ''; + private $tableName = ''; /** * Name of the table column template names are in * @var string */ - private $namecolumn = ''; + private $nameColumn = ''; /** * Name of the table column templates are in * @var string */ - private $datacolumn = ''; + private $dataColumn = ''; /** * Additional WHERE clauses for the template fetch SQL * @var string */ - private $extrawhere = ''; + private $extraWhere = ''; /** * The name of the function phrases are fetched with * @var string */ - private $langcall = '$GLOBALS[\'isso:callback\']->modules[\'localize\']->string'; + private $langcall = 'gettext'; /** * The name of the function phrases are sprintf() parsed with @@ -114,65 +109,53 @@ class Template * The name of a function that is called before template parsing of phrases and conditionals occurs * @var string */ - private $pre_parse_hook = ':=NO METHOD=:'; - - /** - * Fields array that is used in this module - * @var array - */ - private $fields = array( - 'tablename' => array(REQ_YES, null, false), - 'namecolumn' => array(REQ_YES, null, false), - 'datacolumn' => array(REQ_YES, null, false), - 'extrawhere' => array(REQ_NO, null, false), - 'langcall' => array(REQ_YES, null, true), - 'langconst' => array(REQ_YES, null, true), - 'pre_parse_hook' => array(REQ_NO, null, false) - ); + private $preParseHook = ':undefined:'; // ################################################################### /** - * Constructor + * Constructor: check required modules */ - public function __construct(&$registry) + public function __construct() { - $this->registry =& $registry; + BSRegister::RequiredModules(array('Db')); } // ################################################################### /** - * Initializes the class and all subclasses under a common package name + * Sets the database object to use * - * @return string The package name + * @param object Database object */ - protected function init_as_package() + public function setDatabase($db) { - return 'template'; + $this->db = $db; } // ################################################################### /** - * Sets an ISSO field + * Sets the table name and column information * - * @param string Field name - * @param mixed Value of the field + * @param string The database table name + * @param string Name of the column that stores the name of the template + * @param string Name of the column that stores template data */ - public function set($name, $value) + public function setDatabaseTable($tableName, $nameColumn, $dataColumn) { - $this->registry->do_set($name, $value, 'template'); + $this->tableName = $tableName; + $this->nameColumn = $nameColumn; + $this->dataColumn = $dataColumn; } // ################################################################### /** - * Gets an ISSO field + * Sets the pre-parse hook method which is called before any other + * processing is done on the template. * - * @param string Field name - * - * @return mixed Value of the field + * @param string Method name */ - public function get($fieldname) + public function setPreParseHook($hook) { - return $this->registry->do_get($fieldname, 'template'); + $this->preParseHook = $hook; } // ################################################################### @@ -190,11 +173,11 @@ class Template } else { - $templates = $this->registry->modules[ISSO_DB_LAYER]->query("SELECT * FROM " . $this->tablename . " WHERE " . $this->namecolumn . " IN ('" . implode("', '", $namearray) . "')" . ($this->extrawhere ? ' ' . $this->extrawhere : '')); - while ($template = $this->registry->modules[ISSO_DB_LAYER]->fetch_array($templates)) + $templates = $this->db->query("SELECT * FROM " . $this->tableName . " WHERE " . $this->nameColumn . " IN ('" . implode("', '", $namearray) . "')" . ($this->extrawhere ? ' ' . $this->extrawhere : '')); + while ($template = $this->db->fetch_array($templates)) { - $this->cache[ $template[ $this->namecolumn ] ] = $this->_parse($template[ $this->datacolumn ]); - $this->usage[ $template[ $this->namecolumn ] ] = 0; + $this->cache[ $template[ $this->nameColumn ] ] = $this->_parse($template[ $this->dataColumn ]); + $this->usage[ $template[ $this->nameColumn ] ] = 0; } } } @@ -217,9 +200,9 @@ class Template else { $this->uncached[] = $name; - $this->registry->debug("Manually loading template `$name`"); - $template = $this->_load($name); - $template = $this->_parse($template); + BSRegister::Debug("Manually loading template `$name`"); + $template = $this->_loadTemplate($name); + $template = $this->_parseTemplate($template); } if (!isset($this->usage["$name"])) @@ -248,7 +231,7 @@ class Template exit; } - if ($this->registry->debug AND isset($_GET['query'])) + if (BSRegister::GetDebug() AND isset($_GET['query'])) { if (is_array($this->registry->modules[ISSO_DB_LAYER]->history)) { @@ -279,11 +262,11 @@ class Template * * @return string Template data from the database */ - private function _load($name) + private function _loadTemplate($name) { - if ($template = $this->registry->modules[ISSO_DB_LAYER]->query_first("SELECT * FROM " . $this->tablename . " WHERE " . $this->namecolumn . " = '$name'" . ($this->extrawhere ? ' ' . $this->extrawhere : ''))) + if ($template = $this->db->query_first("SELECT * FROM " . $this->tableName . " WHERE " . $this->nameColumn . " = '$name'" . ($this->extrawhere ? ' ' . $this->extrawhere : ''))) { - return $template[ $this->datacolumn ]; + return $template[ $this->dataColumn ]; } else { @@ -300,19 +283,17 @@ class Template * * @return string Parsed template data */ - protected function _parse($template) + protected function _parseTemplate($template) { - $this->registry->check_isso_fields(get_class($this)); - $template = str_replace('"', '\"', $template); - if (function_exists($this->pre_parse_hook)) + if (function_exists($this->preParseHook)) { - $template = call_user_func($this->pre_parse_hook, $template); + $template = call_user_func($this->preParseHook, $template); } - $template = $this->_parse_phrases($template); - $template = $this->_parse_conditionals($template); + $template = $this->_parsePhrases($template); + $template = $this->_parseConditionals($template); return $template; } @@ -324,7 +305,7 @@ class Template * * @return string Language-ready template data */ - private function _parse_phrases($template) + private function _parsePhrases($template) { $tag_start = '(.*?)#i', '$2', $phrase_bunch); // Wrap the parsed data into the build function - $function_wrap = '" . ' . $this->langconst . '("' . /*str_replace(array('\"', "'"), array('"', "\'"),*/ $phrase_name/*)*/ . '", "' . implode('", "', $arglist) . '") . "'; + $function_wrap = '" . ' . $this->langconst . '("' . $phrase_name . '", "' . implode('", "', $arglist) . '") . "'; // Replace the fully-parsed string back into the template $template = substr_replace($template, $function_wrap, $location_start, $location_end + strlen($tag_end) - $location_start); @@ -390,7 +371,7 @@ class Template } // Process the empty phrase objects -- do this now so we don't have to worry about it when we're parsing later - $template = preg_replace('#\{@\\\"(.*?)\\\"\}#ise', '$this->_phrase_string(\'$1\')', $template); + $template = preg_replace('#\{@\\\"(.*?)\\\"\}#ise', '$this->_templateString(\'$1\')', $template); return $template; } @@ -403,7 +384,7 @@ class Template * * @return string Function call for phrase text */ - private function _phrase_string($text) + private function _templateString($text) { return '" . ' . $this->langcall . '(\'' . str_replace(array('\\\"', "'"), array('"', "\'"), $text) . '\') . "'; } @@ -416,7 +397,7 @@ class Template * * @return string Parsed template data */ - private function _parse_conditionals($template) + private function _parseConditionals($template) { // tag data $tag_start = '