2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # All parts of this file are ©2003-[#]year[#] Iris Studios, Inc. No # ||
7 || # part of this file may be reproduced in any way: part or whole. # ||
8 || # --------------------------------------------------------------- # ||
9 || # ©2003 - [#]year[#] Iris Studios, Inc. | http://www.iris-studios.com # ||
10 || ################################################################### ||
11 \*=====================================================================*/
13 $OBJECT = 'Database Template System';
14 $CLASS = 'DB_Template';
18 * Database-Driven Template System
20 * This framework is a backend to the database template engine and
21 * contains all the parsing algorithms.
23 * @author Iris Studios, Inc.
24 * @copyright Copyright ©2003 - [#]year[#], Iris Studios, Inc.
31 * Global environment variables
33 * This stores all of the cached and parsed template data
35 * @var tablename The name of the database table templates are in
36 * @var namecolumn The name of the database column template names are in
37 * @var datacolumn The name of the database column template data is stored in
38 * @var extrawhere Additional information for the WHERE clause in template loading
39 * @var langcall Function wrapper for phrase fetching system
40 * @var langconst Function that constructs a phrase with sprintf()
41 * @var cache Stores all fettched templates
42 * @var usage Counter for template usage
43 * @var uncached List of uncached templates
44 * @var doneflush Variable to check and see if output has been sent
46 var $templatetable = '';
50 var $langcall = 'fetch_phrase';
51 var $langconst = 'construct_phrase';
54 var $uncached = array();
55 var $doneflush = false
;
58 * Takes an array of template names, loads them, and then stores
59 * a parsed version for optimum speed.
61 * @var array List of template names to be cached
63 function cache($namearray)
67 if (sizeof($this->cache
) > 0)
69 trigger_error('You cannot cache templates more than once per initialization', ERR_WARNING
);
73 $templates = $_isso->db
->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " IN ('" . implode("', '", $namearray) . "')" . iff($this->extrawhere
, $this->extrawhere
));
74 while ($template = $_isso->db
->fetch_array($templates))
76 $template = $this->_parse($template);
77 $this->cache
[ $template[ $this->namecolumn
] ] = $template[ $this->datacolumn
];
78 $this->usage
["$name"] = 0;
84 * Loads a template from the cache or the _load function and
85 * stores the parsed version of it
87 * @param str The name of the template
89 * @return str A parsed and loaded template
95 if (isset($this->cache
["$name"]))
97 $template = $this->cache
["$name"];
101 $this->uncached
[] = $name;
102 $_isso->debug("Manually loading template `$name`");
103 $template = $this->_load($name);
104 $template = $this->_parse($template);
107 if (!isset($this->usage
["$name"]))
109 $this->usage
["$name"] = 0;
112 $this->usage
["$name"]++
;
118 * Output a template fully compiled to the browser
120 * @param str Compiled and ready template
122 function flush($template)
128 if (empty($template))
130 trigger_error('There was no output to print', ERR_FATAL
);
134 if ($_isso->debug
AND isset($_GET['query']))
136 if (is_array($_isso->db
->history
))
139 foreach ($_isso->db
->history
AS $query)
141 echo $query . "\n\n<hr />\n\n";
148 if ($this->doneflush
)
150 trigger_error('A template has already been sent to the output buffer', ERR_FATAL
);
157 foreach ($this->usage
AS $name => $count)
159 if (isset($this->uncached
["$name"]))
161 $optlist[] = $name . '[' . $count . ']';
169 $sizeof = sizeof($this->uncached
);
172 $debug .= "\n\t<li><strong style=\"color: red\">Uncached Template(s):</strong> $sizeof ( " . implode(' ', $optlist) . " )</li>";
176 $scinfo = 'Not Under Source Control';
177 $possiblescms = array('cvs', 'svn', 'cvs_information', 'svn_information', 'scm', 'sc_information', 'scm_information');
178 foreach ($possiblescms AS $scm)
180 if (defined(strtoupper($scm)))
182 $scinfo = constant(strtoupper($scm));
186 $debug .= "\n\t<li><strong>Source Control:</strong> $scinfo</li>";
189 $debug .= "\n\t<li><strong>Total Queries:</strong> " . sizeof($_isso->db
->history
) . " (<a href=\"" . $_SERVER['SCRIPT_URL'] . iff(strpos($_SERVER['SCRIPT_URL'], '?') !== false
, '&query=1', '?query=1') . "\">?<a/>)</li>";
192 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Debug Notices (" . sizeof($_isso->debuginfo
) . ")</option>";
193 foreach ((array)$_isso->debuginfo
AS $msg)
195 $debug .= "\n\t\t\t<option>$msg</option>";
197 $debug .= "\n\t</select>\n\t</li>";
200 $modules = $_isso->show_modules(true
);
201 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Loaded Modules (" . sizeof($modules) . ")</option>";
202 foreach ($modules AS $mod)
204 $debug .= "\n\t\t\t<option>$mod</option>";
206 $debug .= "\n\t</select>\n\t</li>";
211 $debug = "\n<hr />\n" . $_isso->_message('Debug Information', $debug, 1, true
);
212 $template = str_replace('</body>', "\n\n<!-- dev debug -->$debug\n<!-- / dev debug -->\n\n</body>", $template);
215 print(stripslashes($template));
219 * Loads an additional template from the database
221 * @param str The name of the template
223 * @return str Template data from the database
225 function _load($name)
228 if ($template = $_isso->db
->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " = '$name'" . iff($this->extrawhere
, $this->extrawhere
)))
230 return $template[ $this->datacolumn
];
234 trigger_error("The template '$name' could not be loaded", ERR_FATAL
);
240 * A wrapper for all the parsing functions and compiling functins
242 * @param str Unparsed template data
244 * @return str Parsed template data
246 function _parse($template)
248 $template = stripslashes($template);
249 $template = str_replace('"', '\"', $template);
250 $template = $this->_parse_phrases($template);
251 $template = $this->_parse_conditionals($template);
256 * Prepares language and locale information inside templates
258 * @param str Template data to be processed
260 * @return str Language-ready template data
262 function _parse_phrases($template)
264 $tag_start = '<lang ';
265 $tag_start_end = '">';
266 $tag_end = '</lang>';
268 $location_start = -1;
271 // Process the empty phrase objects -- do this now so we don't have to worry about it when we're parsing later
272 $template = preg_replace('#\{lang\.(.*?)\}#i', '" . ' . $this->langcall
. '(\'$1\') . "', $template);
276 // Find the start language object tag
277 $location_start = strpos($template, $tag_start, $location_end +
1);
278 if ($location_start === false
)
284 $location_end = strpos($template, $tag_end, $location_end +
strlen($tag_end));
285 if ($location_end === false
)
290 // Extract the language object
291 $phrase_bunch = substr($template, $location_start, ($location_end +
strlen($tag_end)) - $location_start);
293 // Find the close to the opening <lang>
294 $close_of_open = strpos($phrase_bunch, $tag_start_end);
295 if ($close_of_open === false
)
300 // Extract the opening tag so it can be parsed
301 $init_tag = substr($phrase_bunch, 0, ($close_of_open +
strlen($tag_start_end)));
302 $init_tag = str_replace($tag_start, '', $init_tag);
303 $init_tag = substr($init_tag, 0, strlen($init_tag) - 1);
305 // Get the args out of the tag
306 $args = preg_split('#([0-9].*?)=#', $init_tag);
307 foreach ($args AS $arg)
309 if ($arg AND $arg != ' ')
312 $arg = substr($arg, 2);
313 $arg = substr($arg, 0, strlen($arg) - 2);
318 // Just get the phrase name
319 $phrase_name = preg_replace('#<lang(.*?)>(.*?)</lang>#i', '$2', $phrase_bunch);
321 // Wrap the parsed data into the build function
322 $function_wrap = '" . ' . $this->langconst
. '(\'' . $phrase_name . '\', "' . implode('", "', $arglist) . '") . "';
324 // Replace the fully-parsed string back into the template
325 $template = substr_replace($template, $function_wrap, $location_start, $location_end +
strlen($tag_end) - $location_start);
334 * Parser for in-line template conditionals
336 * @param str Template data awaiting processing
338 * @return str Parsed template data
340 function _parse_conditionals($template)
343 $location_start = -1;
347 $tag_start = '<if condition=\\"';
348 $tag_start_end = '\\">';
349 $tag_else = '<else />';
354 // Finds the start tag: starts looking through the haystack one position
355 // after the old startpoint
356 $location_start = strpos($template, $tag_start, $location_start +
1);
357 if ($location_start === false
)
362 // Finds the end tag: starts looking through the haystack after the old end
364 $location_end = strpos($template, $tag_end, $location_end +
strlen($tag_end));
365 if ($location_end === false
)
370 // Split the template expression out of the template data
371 $expression = substr($template, $location_start, ($location_end +
strlen($tag_end)) - $location_start);
373 // If we have a nested expression, work it out by setting
374 // the search counters to be one and two after the current
376 if (substr_count($expression, $tag_end) > 1)
378 $location_start = $location_start +
1;
379 $location_end = $location_start +
2;
382 // Strip out the opening '<if expression="' part of the tag
383 $shell_removed = substr($expression, strlen($tag_start));
385 // Get the position of the end of the '<if epxression="' tag
386 $shell_end = strpos($shell_removed, $tag_start_end);
388 // Take the condition out of the mix
389 $expression_condition = stripslashes(substr($shell_removed, 0, $shell_end));
391 // Now that we've safely stored the condition, parse out the rest of the data
392 $shell_removed = substr($shell_removed, $shell_end +
strlen($tag_start_end));
394 // Find the end tag and then take out the iftrue data
395 $parsed = str_replace($tag_end, '', $shell_removed);
397 // Do we have an $tag_else
398 $location_else = strpos($parsed, $tag_else);
399 if ($location_else !== false
)
401 $data = explode($tag_else, $parsed);
404 if (count($data) > 2)
406 trigger_error('Multiple else statements encountered while parsing conditionals in template', ERR_ERROR
);
413 // Nope, reassign variables
420 // Put the condition and iftrue in the iff() function
421 $parsed_expression = '" . iff(' . stripslashes($expression_condition) . ',"' . $iftrue . '","' . $iffalse . '") . "';
423 // Parse the iff()'d expression back into the template data
424 $template = substr_replace($template, $parsed_expression, $location_start, $location_end +
strlen($tag_end) - $location_start);
427 // Repeat this process until it can't find any more
428 // expressions, then send back the parsed template data
434 /*=====================================================================*\
435 || ###################################################################
438 || ###################################################################
439 \*=====================================================================*/