2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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
39 * @author Iris Studios, Inc.
40 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
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 = '$GLOBALS[\'isso:callback\']->modules[\'localize\']->string';
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=:';
132 * Fields array that is used in this module
137 'tablename' => array(REQ_YES
, null, false),
138 'namecolumn' => array(REQ_YES
, null, false),
139 'datacolumn' => array(REQ_YES
, null, false),
140 'extrawhere' => array(REQ_NO
, null, false),
141 'langcall' => array(REQ_YES
, null, true),
142 'langconst' => array(REQ_YES
, null, true),
143 'pre_parse_hook' => array(REQ_NO
, null, false)
146 // ###################################################################
150 function __construct(&$registry)
152 $this->registry
=& $registry;
155 // ###################################################################
157 * (PHP 4) Constructor
159 function Template(&$registry)
161 $this->__construct($registry);
164 // ###################################################################
166 * Initializes the class and all subclasses under a common package name
170 * @return string The package name
172 function init_as_package()
177 // ###################################################################
183 * @param string Field name
184 * @param mixed Value of the field
186 function set($name, $value)
188 $this->registry
->do_set($name, $value, 'template');
191 // ###################################################################
197 * @param string Field name
199 * @return mixed Value of the field
201 function get($fieldname)
203 return $this->registry
->do_get($fieldname, 'template');
206 // ###################################################################
208 * Takes an array of template names, loads them, and then stores a
209 * parsed version for optimum speed.
213 * @param array List of template names to be cached
215 function cache($namearray)
217 if (sizeof($this->cache
) > 0)
219 trigger_error('You cannot cache templates more than once per initialization', E_USER_WARNING
);
223 $templates = $this->registry
->modules
[ISSO_DB_LAYER
]->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " IN ('" . implode("', '", $namearray) . "')" . ($this->extrawhere
? ' ' . $this->extrawhere
: ''));
224 while ($template = $this->registry
->modules
[ISSO_DB_LAYER
]->fetch_array($templates))
226 $this->cache
[ $template[ $this->namecolumn
] ] = $this->_parse($template[ $this->datacolumn
]);
227 $this->usage
[ $template[ $this->namecolumn
] ] = 0;
232 // ###################################################################
234 * Loads a template from the cache or the _load function and stores the
235 * parsed version of it
239 * @param string The name of the template
241 * @return string A parsed and loaded template
243 function fetch($name)
245 if (isset($this->cache
["$name"]))
247 $template = $this->cache["$name"];
251 $this->uncached
[] = $name;
252 $this->registry
->debug("Manually loading template `$name`");
253 $template = $this->_load($name);
254 $template = $this->_parse($template);
257 if (!isset($this->usage
["$name"]))
259 $this->usage["$name"] = 0;
262 $this->usage
["$name"]++;
267 // ###################################################################
269 * Output a template fully compiled to the browser
273 * @param string Compiled and ready template
275 function flush($template)
279 if (empty($template))
281 trigger_error('There was no output to print', E_USER_ERROR);
285 if ($this->registry->debug AND isset($_GET['query']))
287 if (is_array($this->registry->modules[ISSO_DB_LAYER]->history))
289 foreach ($this->registry->modules[ISSO_DB_LAYER]->history AS $query)
291 echo $this->registry->modules[ISSO_DB_LAYER]->construct_query_debug($query);
297 if ($this->doneflush)
299 trigger_error('A template has already been sent to the output buffer', E_USER_ERROR);
303 $template = str_replace('</body>', $this->registry->construct_debug_block(true) . '</body>', $template);
308 // ###################################################################
310 * Loads an additional template from the database
314 * @param string The name of the template
316 * @return string Template data from the database
318 function _load($name)
320 if ($template = $this->registry->modules[ISSO_DB_LAYER]->query_first("SELECT
* FROM
" . $this->tablename . " WHERE
" . $this->namecolumn . " = '$name'" . ($this->extrawhere ? ' ' . $this->extrawhere : '')))
322 return $template[ $this->datacolumn ];
326 trigger_error("The template '$name' could not be loaded
", E_USER_ERROR);
331 // ###################################################################
333 * A wrapper for all the parsing functions and compiling functins
337 * @param string Unparsed template data
339 * @return string Parsed template data
341 function _parse($template)
343 $this->registry->check_isso_fields(get_class($this));
345 $template = str_replace('"', '\"
', $template);
347 if (function_exists($this->pre_parse_hook))
349 $this->registry->debug("running pre-parse hook: {$this->pre_parse_hook}");
350 $template = call_user_func($this->pre_parse_hook, $template);
353 $template = $this->_parse_phrases($template);
354 $template = $this->_parse_conditionals($template);
358 // ###################################################################
360 * Prepares language and locale information inside templates
364 * @param string Template data to be processed
366 * @return string Language-ready template data
368 function _parse_phrases($template)
370 $tag_start = '<lang
';
371 $tag_start_end = '\"
>';
372 $tag_end = '</lang
>';
374 $location_start = -1;
377 // Process the empty phrase objects -- do this now so we don't have to worry about it when we
're parsing later
378 $template = preg_replace('#\{@\\\"(.*?)\\\"\}#ie', '$this->_phrase_string(\'$1\')', $template);
382 // Find the start language object tag
383 $location_start = strpos($template, $tag_start, $location_end +
1);
384 if ($location_start === false)
390 $location_end = strpos($template, $tag_end, $location_end +
strlen($tag_end));
391 if ($location_end === false)
396 // Extract the language object
397 $phrase_bunch = substr($template, $location_start, ($location_end +
strlen($tag_end)) - $location_start);
399 // Find the close to the opening <lang>
400 $close_of_open = strpos($phrase_bunch, $tag_start_end);
401 if ($close_of_open === false)
406 // Extract the opening tag so it can be parsed
407 $init_tag = substr($phrase_bunch, 0, ($close_of_open +
strlen($tag_start_end)));
408 $init_tag = str_replace($tag_start, '', $init_tag);
409 $init_tag = substr($init_tag, 0, strlen($init_tag) - 1);
411 // Get the args out of the tag
412 $args = preg_split('#([0-9].*?)=#', $init_tag);
413 foreach ($args AS $arg)
415 if ($arg AND $arg != ' ')
418 $arg = substr($arg, 2);
419 $arg = substr($arg, 0, strlen($arg) - 2);
424 // Just get the phrase name
425 $phrase_name = preg_replace('#<lang(.*?)>(.*?)</lang>#i', '$2', $phrase_bunch);
427 // Wrap the parsed data into the build function
428 $function_wrap = '" . ' . $this->langconst . '("' . /*str_replace(array('\"
', "'"), array('"', "\'"),*/ $phrase_name/*)*/ . '", "' . implode('", "', $arglist) . '") . "';
430 // Replace the fully-parsed string back into the template
431 $template = substr_replace($template, $function_wrap, $location_start, $location_end + strlen($tag_end) - $location_start);
439 // ###################################################################
441 * Turns a localized phrase tag into a function call
445 * @param string Phrase text
447 * @return string Function call for phrase text
449 function _phrase_string($text)
451 return '" . ' . $this->langcall . '(\'' . str_replace(array('\\\"', "'"), array('"', "\'
"), $text) . '\') . "';
454 // ###################################################################
456 * Parser for in-line template conditionals
460 * @param string Template data awaiting processing
462 * @return string Parsed template data
464 function _parse_conditionals($template)
467 $tag_start = '<if condition
=\"';
468 $tag_start_end = '\"
>';
469 $tag_else = '<else />';
475 // the information about the current active tag
484 if (strpos($template, $tag_start) === false)
489 for ($i = $offset; $i < strlen($template); $i++)
491 // we've found ourselves a conditional
!
492 if (substr($template, $i, strlen($tag_start)) == $tag_start)
494 // push the position into the tag stack
497 array_push($stack, $i);
501 $tag_full['posi'] = $i;
505 else if (substr($template, $i, strlen($tag_else)) == $tag_else)
507 if (sizeof($stack) == 0 AND !isset($tag_full['else']))
509 $tag_full['else'] = $i;
512 // do we have an end tag?
513 else if (substr($template, $i, strlen($tag_end)) == $tag_end)
515 if (sizeof($stack) != 0)
521 // calculate the position of the end tag
522 $tag_full['posf'] = $i +
strlen($tag_end) - 1;
524 // extract the entire conditional from the template
525 $fullspread = substr($template, $tag_full['posi'], $tag_full['posf'] - $tag_full['posi'] +
1);
527 // remove the beginning tag
528 $conditional = substr($fullspread, strlen($tag_start));
530 // find the end of the expression
531 $temp_end = strpos($conditional, $tag_start_end);
533 // save the expression
534 $parsed[0] = stripslashes(substr($conditional, 0, $temp_end));
536 // remove the expression from the conditional
537 $conditional = substr($conditional, strlen($parsed[0]) +
strlen($tag_start_end));
539 // remove the tailing end tag
540 $conditional = substr($conditional, 0, strlen($conditional) - strlen($tag_end));
543 if (isset($tag_full['else']))
545 // now relative to the start of the <if>
546 $relpos = $tag_full['else'] - $tag_full['posi'];
548 // calculate the length of the expression and opening tag
549 $length = strlen($parsed[0]) +
strlen($tag_start) +
strlen($tag_start_end);
551 // relative to the start of iftrue
552 $elsepos = $relpos - $length;
554 $parsed[1] = substr($conditional, 0, $elsepos);
555 $parsed[2] = substr($conditional, $elsepos +
strlen($tag_else));
560 $parsed[1] = $conditional;
565 // final parsed output
566 $parsed = '" . ((' . stripslashes($parsed[0]) . ') ? "' . $parsed[1] . '" : "' . $parsed[2] . '") . "';
568 // replace the conditional
569 $template = str_replace($fullspread, $parsed, $template);
572 $offset = $tag_full['posi'] +
strlen($tag_start) +
strlen($tag_start_end);
576 unset($fullspread, $conditional, $temp_end, $relpos, $length, $elsepos);
586 // ###################################################################
588 * Debugging function used to print characters in a string that are
589 * around a certain position.
593 * @param string The haystack string
594 * @param integer Position to print around
596 function print_around($str, $pos)
598 echo '>>> PA >>>>>>>>[';
599 echo htmlspecialchars($str[ $pos - 5 ]);
600 echo htmlspecialchars($str[ $pos - 4 ]);
601 echo htmlspecialchars($str[ $pos - 3 ]);
602 echo htmlspecialchars($str[ $pos - 2 ]);
603 echo htmlspecialchars($str[ $pos - 1 ]);
605 echo htmlspecialchars($str[ $pos +
0 ]);
606 echo htmlspecialchars($str[ $pos +
1 ]);
607 echo htmlspecialchars($str[ $pos +
2 ]);
608 echo htmlspecialchars($str[ $pos +
3 ]);
609 echo htmlspecialchars($str[ $pos +
4 ]);
610 echo htmlspecialchars($str[ $pos +
5 ]);
611 echo ']<<<<<<<< PA <<<';
614 /*=====================================================================*\
615 || ###################################################################
618 || ###################################################################
619 \*=====================================================================*/