2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
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
54 * Name of the database table templates are in
60 * Name of the table column template names are in
66 * Name of the table column templates are in
72 * Additional WHERE clauses for the template fetch SQL
78 * The name of the function phrases are fetched with
81 var $langcall = '$GLOBALS[\'isso:null-framework\']->modules[\'localize\']->string';
84 * The name of the function phrases are sprintf() parsed with
87 var $langconst = 'sprintf';
90 * Array of pre-compiled templates that are stored to decrease server load
96 * A list of the number of times each template has been used
102 * A list of templates that weren't cached, but are still used
105 var $uncached = array();
108 * Whether or not the page has been flush()'d already
111 var $doneflush = false
;
114 * The name of a function that is called before template parsing of phrases and conditionals occurs
117 var $pre_parse_hook = ':=NO METHOD=:';
119 // ###################################################################
123 function __construct(&$registry)
125 $this->registry
=& $registry;
128 // ###################################################################
130 * (PHP 4) Constructor
132 function Template(&$registry)
134 $this->__construct($registry);
137 // ###################################################################
139 * Takes an array of template names, loads them, and then stores a
140 * parsed version for optimum speed.
144 * @param array List of template names to be cached
146 function cache($namearray)
148 if (sizeof($this->cache
) > 0)
150 trigger_error('You cannot cache templates more than once per initialization', E_USER_WARNING
);
154 $templates = $this->registry
->modules
[ISSO_DB_LAYER
]->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " IN ('" . implode("', '", $namearray) . "')" . ($this->extrawhere ?
$this->extrawhere
: ''));
155 while ($template = $this->registry
->modules
[ISSO_DB_LAYER
]->fetch_array($templates))
157 $this->cache
[ $template[ $this->namecolumn
] ] = $this->_parse($template[ $this->datacolumn
]);
158 $this->usage
[ $template[ $this->namecolumn
] ] = 0;
163 // ###################################################################
165 * Loads a template from the cache or the _load function and stores the
166 * parsed version of it
170 * @param string The name of the template
172 * @return string A parsed and loaded template
174 function fetch($name)
176 if (isset($this->cache
["$name"]))
178 $template = $this->cache
["$name"];
182 $this->uncached
[] = $name;
183 $this->registry
->debug("Manually loading template `$name`");
184 $template = $this->_load($name);
185 $template = $this->_parse($template);
188 if (!isset($this->usage
["$name"]))
190 $this->usage
["$name"] = 0;
193 $this->usage
["$name"]++
;
198 // ###################################################################
200 * Output a template fully compiled to the browser
204 * @param string Compiled and ready template
206 function flush($template)
210 if (empty($template))
212 trigger_error('There was no output to print', E_USER_ERROR
);
216 if ($this->registry
->debug
AND isset($_GET['query']))
218 if (is_array($this->registry
->modules
[ISSO_DB_LAYER
]->history
))
221 foreach ($this->registry
->modules
[ISSO_DB_LAYER
]->history
AS $query)
223 echo $query . "\n\n<hr />\n\n";
230 if ($this->doneflush
)
232 trigger_error('A template has already been sent to the output buffer', E_USER_ERROR
);
236 $template = str_replace('</body>', $this->registry
->construct_debug_block(true
) . '</body>', $template);
241 // ###################################################################
243 * Loads an additional template from the database
247 * @param string The name of the template
249 * @return string Template data from the database
251 function _load($name)
253 if ($template = $this->registry
->modules
[ISSO_DB_LAYER
]->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " = '$name'" . ($this->extrawhere ?
$this->extrawhere
: '')))
255 return $template[ $this->datacolumn
];
259 trigger_error("The template '$name' could not be loaded", E_USER_ERROR
);
264 // ###################################################################
266 * A wrapper for all the parsing functions and compiling functins
270 * @param string Unparsed template data
272 * @return string Parsed template data
274 function _parse($template)
276 $template = str_replace('"', '\"', $template);
278 if (function_exists($this->pre_parse_hook
))
280 $this->registry
->debug("running pre-parse hook: {$this->pre_parse_hook}");
281 $template = call_user_func($this->pre_parse_hook
, $template);
284 $template = $this->_parse_phrases($template);
285 $template = $this->_parse_conditionals($template);
289 // ###################################################################
291 * Prepares language and locale information inside templates
295 * @param string Template data to be processed
297 * @return string Language-ready template data
299 function _parse_phrases($template)
301 $tag_start = '<lang ';
302 $tag_start_end = '\">';
303 $tag_end = '</lang>';
305 $location_start = -1;
308 // Process the empty phrase objects -- do this now so we don't have to worry about it when we're parsing later
309 $template = preg_replace('#\{@\\\"(.*?)\\\"\}#ie', '$this->_phrase_string(\'$1\')', $template);
313 // Find the start language object tag
314 $location_start = strpos($template, $tag_start, $location_end +
1);
315 if ($location_start === false
)
321 $location_end = strpos($template, $tag_end, $location_end +
strlen($tag_end));
322 if ($location_end === false
)
327 // Extract the language object
328 $phrase_bunch = substr($template, $location_start, ($location_end +
strlen($tag_end)) - $location_start);
330 // Find the close to the opening <lang>
331 $close_of_open = strpos($phrase_bunch, $tag_start_end);
332 if ($close_of_open === false
)
337 // Extract the opening tag so it can be parsed
338 $init_tag = substr($phrase_bunch, 0, ($close_of_open +
strlen($tag_start_end)));
339 $init_tag = str_replace($tag_start, '', $init_tag);
340 $init_tag = substr($init_tag, 0, strlen($init_tag) - 1);
342 // Get the args out of the tag
343 $args = preg_split('#([0-9].*?)=#', $init_tag);
344 foreach ($args AS $arg)
346 if ($arg AND $arg != ' ')
349 $arg = substr($arg, 2);
350 $arg = substr($arg, 0, strlen($arg) - 2);
355 // Just get the phrase name
356 $phrase_name = preg_replace('#<lang(.*?)>(.*?)</lang>#i', '$2', $phrase_bunch);
358 // Wrap the parsed data into the build function
359 $function_wrap = '" . ' . $this->langconst
. '("' . /*str_replace(array('\"', "'"), array('"', "\'"),*/ $phrase_name/*)*/ . '", "' . implode('", "', $arglist) . '") . "';
361 // Replace the fully-parsed string back into the template
362 $template = substr_replace($template, $function_wrap, $location_start, $location_end +
strlen($tag_end) - $location_start);
370 // ###################################################################
372 * Turns a localized phrase tag into a function call
376 * @param string Phrase text
378 * @return string Function call for phrase text
380 function _phrase_string($text)
382 return '" . ' . $this->langcall
. '(\'' . str_replace(array('\\\"', "'"), array('"', "\'"), $text) . '\') . "';
385 // ###################################################################
387 * Parser for in-line template conditionals
391 * @param string Template data awaiting processing
393 * @return string Parsed template data
395 function _parse_conditionals($template)
398 $tag_start = '<if condition=\"';
399 $tag_start_end = '\">';
400 $tag_else = '<else />';
406 // the information about the current active tag
415 if (strpos($template, $tag_start) === false
)
420 for ($i = $offset; $i < strlen($template); $i++
)
422 // we've found ourselves a conditional!
423 if (substr($template, $i, strlen($tag_start)) == $tag_start)
425 // push the position into the tag stack
428 array_push($stack, $i);
432 $tag_full['posi'] = $i;
436 else if (substr($template, $i, strlen($tag_else)) == $tag_else)
438 if (count($stack) == 0 AND !isset($tag_full['else']))
440 $tag_full['else'] = $i;
443 // do we have an end tag?
444 else if (substr($template, $i, strlen($tag_end)) == $tag_end)
446 if (count($stack) != 0)
452 // calculate the position of the end tag
453 $tag_full['posf'] = $i +
strlen($tag_end) - 1;
455 // extract the entire conditional from the template
456 $fullspread = substr($template, $tag_full['posi'], $tag_full['posf'] - $tag_full['posi'] +
1);
458 // remove the beginning tag
459 $conditional = substr($fullspread, strlen($tag_start));
461 // find the end of the expression
462 $temp_end = strpos($conditional, $tag_start_end);
464 // save the expression
465 $parsed[0] = stripslashes(substr($conditional, 0, $temp_end));
467 // remove the expression from the conditional
468 $conditional = substr($conditional, strlen($parsed[0]) +
strlen($tag_start_end));
470 // remove the tailing end tag
471 $conditional = substr($conditional, 0, strlen($conditional) - strlen($tag_end));
474 if (isset($tag_full['else']))
476 // now relative to the start of the <if>
477 $relpos = $tag_full['else'] - $tag_full['posi'];
479 // calculate the length of the expression and opening tag
480 $length = strlen($parsed[0]) +
strlen($tag_start) +
strlen($tag_start_end);
482 // relative to the start of iftrue
483 $elsepos = $relpos - $length;
485 $parsed[1] = substr($conditional, 0, $elsepos);
486 $parsed[2] = substr($conditional, $elsepos +
strlen($tag_else));
491 $parsed[1] = $conditional;
496 // final parsed output
497 $parsed = '" . ((' . stripslashes($parsed[0]) . ') ? "' . $parsed[1] . '" : "' . $parsed[2] . '") . "';
499 // replace the conditional
500 $template = str_replace($fullspread, $parsed, $template);
503 $offset = $tag_full['posi'] +
strlen($tag_start) +
strlen($tag_start_end);
507 unset($fullspread, $conditional, $temp_end, $relpos, $length, $elsepos);
517 // ###################################################################
519 * Debugging function used to print characters in a string that are
520 * around a certain position.
524 * @param string The haystack string
525 * @param integer Position to print around
527 function print_around($str, $pos)
529 echo '>>> PA >>>>>>>>[';
530 echo htmlspecialchars($str[ $pos - 5 ]);
531 echo htmlspecialchars($str[ $pos - 4 ]);
532 echo htmlspecialchars($str[ $pos - 3 ]);
533 echo htmlspecialchars($str[ $pos - 2 ]);
534 echo htmlspecialchars($str[ $pos - 1 ]);
536 echo htmlspecialchars($str[ $pos +
0 ]);
537 echo htmlspecialchars($str[ $pos +
1 ]);
538 echo htmlspecialchars($str[ $pos +
2 ]);
539 echo htmlspecialchars($str[ $pos +
3 ]);
540 echo htmlspecialchars($str[ $pos +
4 ]);
541 echo htmlspecialchars($str[ $pos +
5 ]);
542 echo ']<<<<<<<< PA <<<';
545 /*=====================================================================*\
546 || ###################################################################
549 || ###################################################################
550 \*=====================================================================*/