2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2002-[#]year[#] Blue Static
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
23 * Database-Driven Template System
30 * Database-Driven Template System
32 * This framework is a backend to the database template engine and
33 * contains all the parsing algorithms.
36 * $this->pre_parse_hook - Name of the function to execute on
37 * a template before the parsing occurs
40 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
48 * Framework registry object
55 * Name of the database table templates are in
62 * Name of the table column template names are in
69 * Name of the table column templates are in
76 * Additional WHERE clauses for the template fetch SQL
83 * The name of the function phrases are fetched with
87 var $langcall = 'gettext';
90 * The name of the function phrases are sprintf() parsed with
94 var $langconst = 'sprintf';
97 * Array of pre-compiled templates that are stored to decrease server load
101 var $cache = array();
104 * A list of the number of times each template has been used
108 var $usage = array();
111 * A list of templates that weren't cached, but are still used
115 var $uncached = array();
118 * Whether or not the page has been flush()'d already
122 var $doneflush = false;
125 * The name of a function that is called before template parsing of phrases and conditionals occurs
129 var $pre_parse_hook = ':=NO METHOD=:';
131 // ###################################################################
135 function __construct(&$registry)
137 $this->registry
=& $registry;
140 // ###################################################################
142 * (PHP 4) Constructor
144 function Template(&$registry)
146 $this->__construct($registry);
149 // ###################################################################
151 * Initializes the class and all subclasses under a common package name
155 * @return string The package name
157 function init_as_package()
162 // ###################################################################
168 * @param string Table name
170 function setTableName($name)
172 $this->tablename
= $name;
175 // ###################################################################
181 * @param string Name column name
183 function setNameColumn($name)
185 $this->namecolumn
= $name;
188 // ###################################################################
194 * @param string Data column name
196 function setDataColumn($name)
198 $this->datacolumn
= $name;
201 // ###################################################################
207 * @param string Extra WHERE SQL clause
209 function setExtraWhere($sql)
211 $this->extrawhere
= $sql;
214 // ###################################################################
216 * Sets the pre-parse hook
220 * @param string Method name
222 function setPreParseHook($name)
224 $this->pre_parse_hook
= $name;
227 // ###################################################################
229 * Takes an array of template names, loads them, and then stores a
230 * parsed version for optimum speed.
234 * @param array List of template names to be cached
236 function cache($namearray)
238 if (sizeof($this->cache
) > 0)
240 trigger_error('You cannot cache templates more than once per initialization', E_USER_WARNING
);
244 $templates = $this->registry
->modules
[ISSO_DB_LAYER
]->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " IN ('" . implode("', '", $namearray) . "')" . ($this->extrawhere
? ' ' . $this->extrawhere
: ''));
245 while ($template = $this->registry
->modules
[ISSO_DB_LAYER
]->fetch_array($templates))
247 $this->cache
[ $template[ $this->namecolumn
] ] = $this->_parse($template[ $this->datacolumn
]);
248 $this->usage
[ $template[ $this->namecolumn
] ] = 0;
253 // ###################################################################
255 * Loads a template from the cache or the _load function and stores the
256 * parsed version of it
260 * @param string The name of the template
262 * @return string A parsed and loaded template
264 function fetch($name)
266 if (isset($this->cache
["$name"]))
268 $template = $this->cache["$name"];
272 $this->uncached
[] = $name;
273 $this->registry
->debug("Manually loading template `$name`");
274 $template = $this->_load($name);
275 $template = $this->_parse($template);
278 if (!isset($this->usage
["$name"]))
280 $this->usage["$name"] = 0;
283 $this->usage
["$name"]++;
288 // ###################################################################
290 * Output a template fully compiled to the browser
294 * @param string Compiled and ready template
296 function flush($template)
300 if (empty($template))
302 trigger_error('There was no output to print', E_USER_ERROR);
306 if ($this->registry->debug AND isset($_GET['query']))
308 if (is_array($this->registry->modules[ISSO_DB_LAYER]->history))
310 foreach ($this->registry->modules[ISSO_DB_LAYER]->history AS $query)
312 echo $this->registry->modules[ISSO_DB_LAYER]->construct_query_debug($query);
318 if ($this->doneflush)
320 trigger_error('A template has already been sent to the output buffer', E_USER_ERROR);
324 $template = str_replace('</body>', $this->registry->construct_debug_block(true) . '</body>', $template);
329 // ###################################################################
331 * Loads an additional template from the database
335 * @param string The name of the template
337 * @return string Template data from the database
339 function _load($name)
341 if ($template = $this->registry->modules[ISSO_DB_LAYER]->query_first("SELECT
* FROM
" . $this->tablename . " WHERE
" . $this->namecolumn . " = '$name'" . ($this->extrawhere ? ' ' . $this->extrawhere : '')))
343 return $template[ $this->datacolumn ];
347 trigger_error("The template '$name' could not be loaded
", E_USER_ERROR);
352 // ###################################################################
354 * A wrapper for all the parsing functions and compiling functins
358 * @param string Unparsed template data
360 * @return string Parsed template data
362 function _parse($template)
364 $template = str_replace('"', '\"
', $template);
366 if (function_exists($this->pre_parse_hook))
368 $template = call_user_func($this->pre_parse_hook, $template);
371 $template = $this->_parse_phrases($template);
372 $template = $this->_parse_conditionals($template);
376 // ###################################################################
378 * Prepares language and locale information inside templates
382 * @param string Template data to be processed
384 * @return string Language-ready template data
386 function _parse_phrases($template)
388 $tag_start = '<lang
';
389 $tag_start_end = '\"
>';
390 $tag_end = '</lang
>';
392 $location_start = -1;
397 // Find the start language object tag
398 $location_start = strpos($template, $tag_start, $location_end + 1);
399 if ($location_start === false)
405 $location_end = strpos($template, $tag_end, $location_end + strlen($tag_end));
406 if ($location_end === false)
411 // Extract the language object
412 $phrase_bunch = substr($template, $location_start, ($location_end + strlen($tag_end)) - $location_start);
414 // Find the close to the opening <lang>
415 $close_of_open = strpos($phrase_bunch, $tag_start_end);
416 if ($close_of_open === false)
421 // Extract the opening tag so it can be parsed
422 $init_tag = substr($phrase_bunch, 0, ($close_of_open + strlen($tag_start_end)));
423 $init_tag = str_replace($tag_start, '', $init_tag);
424 $init_tag = substr($init_tag, 0, strlen($init_tag) - 1);
426 // Get the args out of the tag
427 $args = preg_split('#([0-9].*?)=#', $init_tag);
428 foreach ($args AS $arg)
430 if ($arg AND $arg != ' ')
433 $arg = substr($arg, 2);
434 $arg = substr($arg, 0, strlen($arg) - 2);
439 // Just get the phrase name
440 $phrase_name = preg_replace('#<lang(.*?)>(.*?)</lang>#i', '$2', $phrase_bunch);
442 // Wrap the parsed data into the build function
443 $function_wrap = '" . ' . $this->langconst . '("' . /*str_replace(array('\"
', "'"), array('"', "\'"),*/ $phrase_name/*)*/ . '", "' . implode('", "', $arglist) . '") . "';
445 // Replace the fully-parsed string back into the template
446 $template = substr_replace($template, $function_wrap, $location_start, $location_end + strlen($tag_end) - $location_start);
451 // Process the empty phrase objects -- do this now so we don't have to worry about it when we
're parsing later
452 $template = preg_replace('#\{@\\\"(.*?)\\\"\}#ise', '$this->_phrase_string(\'$1\')', $template);
457 // ###################################################################
459 * Turns a localized phrase tag into a function call
463 * @param string Phrase text
465 * @return string Function call for phrase text
467 function _phrase_string($text)
469 return '" . ' . $this->langcall
. '(\'' . str_replace(array('\\\"', "'"), array('"', "\'"), $text) . '\') . "';
472 // ###################################################################
474 * Parser for in-line template conditionals
478 * @param string Template data awaiting processing
480 * @return string Parsed template data
482 function _parse_conditionals($template)
485 $tag_start = '<if condition=\"';
486 $tag_start_end = '\">';
487 $tag_else = '<else />';
493 // the information about the current active tag
502 if (strpos($template, $tag_start) === false)
507 for ($i = $offset; $i < strlen($template); $i++
)
509 // we've found ourselves a conditional!
510 if (substr($template, $i, strlen($tag_start)) == $tag_start)
512 // push the position into the tag stack
515 array_push($stack, $i);
519 $tag_full['posi'] = $i;
523 else if (substr($template, $i, strlen($tag_else)) == $tag_else)
525 if (sizeof($stack) == 0 AND !isset($tag_full['else']))
527 $tag_full['else'] = $i;
530 // do we have an end tag?
531 else if (substr($template, $i, strlen($tag_end)) == $tag_end)
533 if (sizeof($stack) != 0)
539 // calculate the position of the end tag
540 $tag_full['posf'] = $i +
strlen($tag_end) - 1;
542 // extract the entire conditional from the template
543 $fullspread = substr($template, $tag_full['posi'], $tag_full['posf'] - $tag_full['posi'] +
1);
545 // remove the beginning tag
546 $conditional = substr($fullspread, strlen($tag_start));
548 // find the end of the expression
549 $temp_end = strpos($conditional, $tag_start_end);
551 // save the expression
552 $parsed[0] = stripslashes(substr($conditional, 0, $temp_end));
554 // remove the expression from the conditional
555 $conditional = substr($conditional, strlen($parsed[0]) +
strlen($tag_start_end));
557 // remove the tailing end tag
558 $conditional = substr($conditional, 0, strlen($conditional) - strlen($tag_end));
561 if (isset($tag_full['else']))
563 // now relative to the start of the <if>
564 $relpos = $tag_full['else'] - $tag_full['posi'];
566 // calculate the length of the expression and opening tag
567 $length = strlen($parsed[0]) +
strlen($tag_start) +
strlen($tag_start_end);
569 // relative to the start of iftrue
570 $elsepos = $relpos - $length;
572 $parsed[1] = substr($conditional, 0, $elsepos);
573 $parsed[2] = substr($conditional, $elsepos +
strlen($tag_else));
578 $parsed[1] = $conditional;
583 // final parsed output
584 $parsed = '" . ((' . stripslashes($parsed[0]) . ') ? "' . $parsed[1] . '" : "' . $parsed[2] . '") . "';
586 // replace the conditional
587 $template = str_replace($fullspread, $parsed, $template);
590 $offset = $tag_full['posi'] +
strlen($tag_start) +
strlen($tag_start_end);
594 unset($fullspread, $conditional, $temp_end, $relpos, $length, $elsepos);
604 // ###################################################################
606 * Debugging function used to print characters in a string that are
607 * around a certain position.
611 * @param string The haystack string
612 * @param integer Position to print around
614 function print_around($str, $pos)
616 echo '>>> PA >>>>>>>>[';
617 echo htmlspecialchars($str[ $pos - 5 ]);
618 echo htmlspecialchars($str[ $pos - 4 ]);
619 echo htmlspecialchars($str[ $pos - 3 ]);
620 echo htmlspecialchars($str[ $pos - 2 ]);
621 echo htmlspecialchars($str[ $pos - 1 ]);
623 echo htmlspecialchars($str[ $pos +
0 ]);
624 echo htmlspecialchars($str[ $pos +
1 ]);
625 echo htmlspecialchars($str[ $pos +
2 ]);
626 echo htmlspecialchars($str[ $pos +
3 ]);
627 echo htmlspecialchars($str[ $pos +
4 ]);
628 echo htmlspecialchars($str[ $pos +
5 ]);
629 echo ']<<<<<<<< PA <<<';