2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©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 ©2002 - [#]year[#], Blue Static
48 * Framework registry object
51 private $registry = null
;
54 * Name of the database table templates are in
57 private $tablename = '';
60 * Name of the table column template names are in
63 private $namecolumn = '';
66 * Name of the table column templates are in
69 private $datacolumn = '';
72 * Additional WHERE clauses for the template fetch SQL
75 private $extrawhere = '';
78 * The name of the function phrases are fetched with
81 private $langcall = '$GLOBALS[\'isso:callback\']->modules[\'localize\']->string';
84 * The name of the function phrases are sprintf() parsed with
87 private $langconst = 'sprintf';
90 * Array of pre-compiled templates that are stored to decrease server load
93 private $cache = array();
96 * A list of the number of times each template has been used
99 private $usage = array();
102 * A list of templates that weren't cached, but are still used
105 private $uncached = array();
108 * Whether or not the page has been flush()'d already
111 private $doneflush = false
;
114 * The name of a function that is called before template parsing of phrases and conditionals occurs
117 private $pre_parse_hook = ':=NO METHOD=:';
120 * Fields array that is used in this module
123 private $fields = array(
124 'tablename' => array(REQ_YES
, null
, false
),
125 'namecolumn' => array(REQ_YES
, null
, false
),
126 'datacolumn' => array(REQ_YES
, null
, false
),
127 'extrawhere' => array(REQ_NO
, null
, false
),
128 'langcall' => array(REQ_YES
, null
, true
),
129 'langconst' => array(REQ_YES
, null
, true
),
130 'pre_parse_hook' => array(REQ_NO
, null
, false
)
133 // ###################################################################
137 function __construct(&$registry)
139 $this->registry
=& $registry;
142 // ###################################################################
144 * (PHP 4) Constructor
146 function Template(&$registry)
148 $this->__construct($registry);
151 // ###################################################################
153 * Initializes the class and all subclasses under a common package name
157 * @return string The package name
159 function init_as_package()
164 // ###################################################################
170 * @param string Field name
171 * @param mixed Value of the field
173 function set($name, $value)
175 $this->registry
->do_set($name, $value, 'template');
178 // ###################################################################
184 * @param string Field name
186 * @return mixed Value of the field
188 function get($fieldname)
190 return $this->registry
->do_get($fieldname, 'template');
193 // ###################################################################
195 * Takes an array of template names, loads them, and then stores a
196 * parsed version for optimum speed.
200 * @param array List of template names to be cached
202 function cache($namearray)
204 if (sizeof($this->cache
) > 0)
206 trigger_error('You cannot cache templates more than once per initialization', E_USER_WARNING
);
210 $templates = $this->registry
->modules
[ISSO_DB_LAYER
]->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " IN ('" . implode("', '", $namearray) . "')" . ($this->extrawhere ?
' ' . $this->extrawhere
: ''));
211 while ($template = $this->registry
->modules
[ISSO_DB_LAYER
]->fetch_array($templates))
213 $this->cache
[ $template[ $this->namecolumn
] ] = $this->_parse($template[ $this->datacolumn
]);
214 $this->usage
[ $template[ $this->namecolumn
] ] = 0;
219 // ###################################################################
221 * Loads a template from the cache or the _load function and stores the
222 * parsed version of it
226 * @param string The name of the template
228 * @return string A parsed and loaded template
230 function fetch($name)
232 if (isset($this->cache
["$name"]))
234 $template = $this->cache
["$name"];
238 $this->uncached
[] = $name;
239 $this->registry
->debug("Manually loading template `$name`");
240 $template = $this->_load($name);
241 $template = $this->_parse($template);
244 if (!isset($this->usage
["$name"]))
246 $this->usage
["$name"] = 0;
249 $this->usage
["$name"]++
;
254 // ###################################################################
256 * Output a template fully compiled to the browser
260 * @param string Compiled and ready template
262 function flush($template)
266 if (empty($template))
268 trigger_error('There was no output to print', E_USER_ERROR
);
272 if ($this->registry
->debug
AND isset($_GET['query']))
274 if (is_array($this->registry
->modules
[ISSO_DB_LAYER
]->history
))
276 foreach ($this->registry
->modules
[ISSO_DB_LAYER
]->history
AS $query)
278 echo $this->registry
->modules
[ISSO_DB_LAYER
]->construct_query_debug($query);
284 if ($this->doneflush
)
286 trigger_error('A template has already been sent to the output buffer', E_USER_ERROR
);
290 $template = str_replace('</body>', $this->registry
->construct_debug_block(true
) . '</body>', $template);
295 // ###################################################################
297 * Loads an additional template from the database
301 * @param string The name of the template
303 * @return string Template data from the database
305 function _load($name)
307 if ($template = $this->registry
->modules
[ISSO_DB_LAYER
]->query_first("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " = '$name'" . ($this->extrawhere ?
' ' . $this->extrawhere
: '')))
309 return $template[ $this->datacolumn
];
313 trigger_error("The template '$name' could not be loaded", E_USER_ERROR
);
318 // ###################################################################
320 * A wrapper for all the parsing functions and compiling functins
324 * @param string Unparsed template data
326 * @return string Parsed template data
328 function _parse($template)
330 $this->registry
->check_isso_fields(get_class($this));
332 $template = str_replace('"', '\"', $template);
334 if (function_exists($this->pre_parse_hook
))
336 $template = call_user_func($this->pre_parse_hook
, $template);
339 $template = $this->_parse_phrases($template);
340 $template = $this->_parse_conditionals($template);
344 // ###################################################################
346 * Prepares language and locale information inside templates
350 * @param string Template data to be processed
352 * @return string Language-ready template data
354 function _parse_phrases($template)
356 $tag_start = '<lang ';
357 $tag_start_end = '\">';
358 $tag_end = '</lang>';
360 $location_start = -1;
365 // Find the start language object tag
366 $location_start = strpos($template, $tag_start, $location_end +
1);
367 if ($location_start === false
)
373 $location_end = strpos($template, $tag_end, $location_end +
strlen($tag_end));
374 if ($location_end === false
)
379 // Extract the language object
380 $phrase_bunch = substr($template, $location_start, ($location_end +
strlen($tag_end)) - $location_start);
382 // Find the close to the opening <lang>
383 $close_of_open = strpos($phrase_bunch, $tag_start_end);
384 if ($close_of_open === false
)
389 // Extract the opening tag so it can be parsed
390 $init_tag = substr($phrase_bunch, 0, ($close_of_open +
strlen($tag_start_end)));
391 $init_tag = str_replace($tag_start, '', $init_tag);
392 $init_tag = substr($init_tag, 0, strlen($init_tag) - 1);
394 // Get the args out of the tag
395 $args = preg_split('#([0-9].*?)=#', $init_tag);
396 foreach ($args AS $arg)
398 if ($arg AND $arg != ' ')
401 $arg = substr($arg, 2);
402 $arg = substr($arg, 0, strlen($arg) - 2);
407 // Just get the phrase name
408 $phrase_name = preg_replace('#<lang(.*?)>(.*?)</lang>#i', '$2', $phrase_bunch);
410 // Wrap the parsed data into the build function
411 $function_wrap = '" . ' . $this->langconst
. '("' . /*str_replace(array('\"', "'"), array('"', "\'"),*/ $phrase_name/*)*/ . '", "' . implode('", "', $arglist) . '") . "';
413 // Replace the fully-parsed string back into the template
414 $template = substr_replace($template, $function_wrap, $location_start, $location_end +
strlen($tag_end) - $location_start);
419 // Process the empty phrase objects -- do this now so we don't have to worry about it when we're parsing later
420 $template = preg_replace('#\{@\\\"(.*?)\\\"\}#ise', '$this->_phrase_string(\'$1\')', $template);
425 // ###################################################################
427 * Turns a localized phrase tag into a function call
431 * @param string Phrase text
433 * @return string Function call for phrase text
435 function _phrase_string($text)
437 return '" . ' . $this->langcall
. '(\'' . str_replace(array('\\\"', "'"), array('"', "\'"), $text) . '\') . "';
440 // ###################################################################
442 * Parser for in-line template conditionals
446 * @param string Template data awaiting processing
448 * @return string Parsed template data
450 function _parse_conditionals($template)
453 $tag_start = '<if condition=\"';
454 $tag_start_end = '\">';
455 $tag_else = '<else />';
461 // the information about the current active tag
470 if (strpos($template, $tag_start) === false
)
475 for ($i = $offset; $i < strlen($template); $i++
)
477 // we've found ourselves a conditional!
478 if (substr($template, $i, strlen($tag_start)) == $tag_start)
480 // push the position into the tag stack
483 array_push($stack, $i);
487 $tag_full['posi'] = $i;
491 else if (substr($template, $i, strlen($tag_else)) == $tag_else)
493 if (sizeof($stack) == 0 AND !isset($tag_full['else']))
495 $tag_full['else'] = $i;
498 // do we have an end tag?
499 else if (substr($template, $i, strlen($tag_end)) == $tag_end)
501 if (sizeof($stack) != 0)
507 // calculate the position of the end tag
508 $tag_full['posf'] = $i +
strlen($tag_end) - 1;
510 // extract the entire conditional from the template
511 $fullspread = substr($template, $tag_full['posi'], $tag_full['posf'] - $tag_full['posi'] +
1);
513 // remove the beginning tag
514 $conditional = substr($fullspread, strlen($tag_start));
516 // find the end of the expression
517 $temp_end = strpos($conditional, $tag_start_end);
519 // save the expression
520 $parsed[0] = stripslashes(substr($conditional, 0, $temp_end));
522 // remove the expression from the conditional
523 $conditional = substr($conditional, strlen($parsed[0]) +
strlen($tag_start_end));
525 // remove the tailing end tag
526 $conditional = substr($conditional, 0, strlen($conditional) - strlen($tag_end));
529 if (isset($tag_full['else']))
531 // now relative to the start of the <if>
532 $relpos = $tag_full['else'] - $tag_full['posi'];
534 // calculate the length of the expression and opening tag
535 $length = strlen($parsed[0]) +
strlen($tag_start) +
strlen($tag_start_end);
537 // relative to the start of iftrue
538 $elsepos = $relpos - $length;
540 $parsed[1] = substr($conditional, 0, $elsepos);
541 $parsed[2] = substr($conditional, $elsepos +
strlen($tag_else));
546 $parsed[1] = $conditional;
551 // final parsed output
552 $parsed = '" . ((' . stripslashes($parsed[0]) . ') ? "' . $parsed[1] . '" : "' . $parsed[2] . '") . "';
554 // replace the conditional
555 $template = str_replace($fullspread, $parsed, $template);
558 $offset = $tag_full['posi'] +
strlen($tag_start) +
strlen($tag_start_end);
562 unset($fullspread, $conditional, $temp_end, $relpos, $length, $elsepos);
572 // ###################################################################
574 * Debugging function used to print characters in a string that are
575 * around a certain position.
579 * @param string The haystack string
580 * @param integer Position to print around
582 function print_around($str, $pos)
584 echo '>>> PA >>>>>>>>[';
585 echo htmlspecialchars($str[ $pos - 5 ]);
586 echo htmlspecialchars($str[ $pos - 4 ]);
587 echo htmlspecialchars($str[ $pos - 3 ]);
588 echo htmlspecialchars($str[ $pos - 2 ]);
589 echo htmlspecialchars($str[ $pos - 1 ]);
591 echo htmlspecialchars($str[ $pos +
0 ]);
592 echo htmlspecialchars($str[ $pos +
1 ]);
593 echo htmlspecialchars($str[ $pos +
2 ]);
594 echo htmlspecialchars($str[ $pos +
3 ]);
595 echo htmlspecialchars($str[ $pos +
4 ]);
596 echo htmlspecialchars($str[ $pos +
5 ]);
597 echo ']<<<<<<<< PA <<<';
600 /*=====================================================================*\
601 || ###################################################################
604 || ###################################################################
605 \*=====================================================================*/