root/include/class.template.php

Revision 211, 10.9 kB (checked in by Nafania, 3 years ago)

--

Line 
1 <?php
2 /** </span>
3 <span class="code-comment">*
4 * @package phpBB3
5 * @version $Id: template.php,v 1.111 2007/07/09 18:27:57 davidmj Exp $
6 * @copyright (c) 2005 phpBB Group, sections (c) 2001 ispi of Lincoln Inc
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
8 *
9 */
10
11 /**
12 */
13
14 /**
15 * Base Template class.
16 * @package phpBB3
17 */
18 class template
19 {</span>
20 <span class="code-keyword">    /** variable that holds all the data we'll be substituting into
21     * the compiled templates. Takes form:
22     * --> $this->_tpldata[block][iteration#][child][iteration#][child2][iteration#][variablename] == value
23     * if it's a root-level variable, it'll be like this:
24     * --> $this->_tpldata[.][0][varname] == value
25     */
26     var $_tpldata = array('.' => array(0 => array()));
27     var $_rootref;
28
29     // Root dir and hash of filenames for each template handle.
30     var $root = '';
31     var $cachepath = '';
32     var $files = array();
33     var $filename = array();
34
35     // this will hash handle names to the compiled/uncompiled code for that handle.
36     var $compiled_code = array();
37
38     /**
39     * Set template location
40     * @access public
41     */
42     function set_template($template_name)
43     {
44         global $root_path;
45
46                 if (file_exists($root_path . 'templates/' . $template_name))
47                 {
48                         $this->root $root_path . 'templates/' . $template_name;
49                         $this->cachepath $root_path . 'cache/tpl_' . $template_name . '_';
50         }
51         else
52         {
53             trigger_error('Template path could not be found: templates/' . $template_name, E_USER_ERROR);
54         }
55
56         $this->_rootref = &$this->_tpldata['.'][0];
57
58         return true;
59     }
60
61     /**
62     * Set custom template location (able to use directory outside of phpBB)
63     * @access public
64     */
65     function set_custom_template($template_path, $template_name)
66     {
67         global $root_path;
68
69         $this->root = $template_path;
70                 $this->cachepath $root_path . 'cache/tpl_' . $template_name . '_';
71
72         return true;
73     }
74
75     /**
76     * Sets the template filenames for handles. $filename_array
77     * should be a hash of handle => filename pairs.
78     * @access public
79     */
80     function set_filenames($filename_array)
81     {
82         if (!is_array($filename_array))
83         {
84             return false;
85         }
86
87         foreach ($filename_array as $handle => $filename)
88         {
89             if (empty($filename))
90             {
91                 trigger_error("template->set_filenames: Empty filename specified for $handle", E_USER_ERROR);
92             }
93
94             $this->filename[$handle] = $filename;
95             $this->files[$handle] = $this->root . '/' . $filename;
96         }
97
98         return true;
99     }
100
101     /**
102     * Destroy template data set
103     * @access public
104     */
105     function destroy()
106     {
107         $this->_tpldata = array('.' => array(0 => array()));
108     }
109
110     /**
111     * Reset/empty complete block
112     * @access public
113     */
114     function destroy_block_vars($blockname)
115     {
116         if (strpos($blockname, '.') !== false)
117         {
118             // Nested block.
119             $blocks = explode('.', $blockname);
120             $blockcount = sizeof($blocks) - 1;
121
122             $str = &$this->_tpldata;
123             for ($i = 0; $i < $blockcount; $i++)
124             {
125                 $str = &$str[$blocks[$i]];
126                 $str = &$str[sizeof($str) - 1];
127             }
128
129             unset($str[$blocks[$blockcount]]);
130         }
131         else
132         {
133             // Top-level block.
134             unset($this->_tpldata[$blockname]);
135         }
136
137         return true;
138     }
139
140     /**
141     * Display handle
142     * @access public
143     */
144     function display($handle, $include_once = true)
145     {
146             global $lang;
147
148         if ($filename = $this->_tpl_load($handle))
149         {
150             ($include_once) ? include_once($filename) : include($filename);
151         }
152         else
153         {
154             eval(' ?>' . $this->compiled_code[$handle] . '<?php ');
155         }
156
157         return true;
158     }
159
160     /**
161     * Display the handle and assign the output to a template variable or return the compiled result.
162     * @access public
163     */
164     function assign_display($handle, $template_var = '', $return_content = true, $include_once = false)
165     {
166         ob_start();
167         $this->display($handle, $include_once);
168         $contents = ob_get_clean();
169
170         if ($return_content)
171         {
172             return $contents;
173         }
174
175         $this->assign_var($template_var, $contents);
176
177         return true;
178     }
179
180     /**
181     * Load a compiled template if possible, if not, recompile it
182     * @access private
183     */
184     function _tpl_load(&$handle)
185     {
186         global $config, $lang;
187
188         $filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.php';
189
190         //$recompile = (($config['load_tplcompile'] && @filemtime($filename) < filemtime($this->files[$handle])) || !file_exists($filename) || @filesize($filename) === 0) ? true : false;
191         $recompile = ((@filemtime($filename) < filemtime($this->files[$handle])) || !file_exists($filename) || @filesize($filename) === 0) ? true : false;
192
193         // Recompile page if the original template is newer, otherwise load the compiled version
194         if (!$recompile)
195         {
196             return $filename;
197         }
198
199         global $db, $root_path;
200
201         if (!class_exists('template_compile'))
202         {
203             include($root_path . 'include/functions_template.php');
204         }
205
206         $compile = new template_compile($this);
207
208         // If we don't have a file assigned to this handle, die.
209         if (!isset($this->files[$handle]))
210         {
211             trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR);
212         }
213
214         // Just compile if no user object is present (happens within the installer)
215                 /*if (!$user)
216                 {
217                         $compile->_tpl_load_file($handle);
218                         return false;
219                 } */
220
221
222         $compile->_tpl_load_file($handle);
223         return false;
224     }
225
226     /**
227     * Assign key variable pairs from an array
228     * @access public
229     */
230     function assign_vars($vararray)
231     {
232         foreach ($vararray as $key => $val)
233         {
234             $this->_rootref[$key] = $val;
235         }
236
237         return true;
238     }
239
240     /**
241     * Assign a single variable to a single key
242     * @access public
243     */
244     function assign_var($varname, $varval)
245     {
246         $this->_rootref[$varname] = $varval;
247
248         return true;
249     }
250
251     /**
252     * Assign key variable pairs from an array to a specified block
253     * @access public
254     */
255     function assign_block_vars($blockname, $vararray)
256     {
257         if (strpos($blockname, '.') !== false)
258         {
259             // Nested block.
260             $blocks = explode('.', $blockname);
261             $blockcount = sizeof($blocks) - 1;
262
263             $str = &$this->_tpldata;
264             for ($i = 0; $i < $blockcount; $i++)
265             {
266                 $str = &$str[$blocks[$i]];
267                 $str = &$str[sizeof($str) - 1];
268             }
269
270             $s_row_count = isset($str[$blocks[$blockcount]]) ? sizeof($str[$blocks[$blockcount]]) : 0;
271             $vararray['S_ROW_COUNT'] = $s_row_count;
272
273             // Assign S_FIRST_ROW
274             if (!$s_row_count)
275             {
276                 $vararray['S_FIRST_ROW'] = true;
277             }
278
279             // Now the tricky part, we always assign S_LAST_ROW and remove the entry before
280             // This is much more clever than going through the complete template data on display (phew)
281             $vararray['S_LAST_ROW'] = true;
282             if ($s_row_count > 0)
283             {
284                 unset($str[$blocks[$blockcount]][($s_row_count - 1)]['S_LAST_ROW']);
285             }
286
287             // Now we add the block that we're actually assigning to.
288             // We're adding a new iteration to this block with the given
289             // variable assignments.
290             $str[$blocks[$blockcount]][] = $vararray;
291         }
292         else
293         {
294             // Top-level block.
295             $s_row_count = (isset($this->_tpldata[$blockname])) ? sizeof($this->_tpldata[$blockname]) : 0;
296             $vararray['S_ROW_COUNT'] = $s_row_count;
297
298             // Assign S_FIRST_ROW
299             if (!$s_row_count)
300             {
301                 $vararray['S_FIRST_ROW'] = true;
302             }
303
304             // We always assign S_LAST_ROW and remove the entry before
305             $vararray['S_LAST_ROW'] = true;
306             if ($s_row_count > 0)
307             {
308                 unset($this->_tpldata[$blockname][($s_row_count - 1)]['S_LAST_ROW']);
309             }
310            
311             // Add a new iteration to this block with the variable assignments we were given.
312             $this->_tpldata[$blockname][] = $vararray;
313         }
314
315         return true;
316     }
317
318     /**
319     * Change already assigned key variable pair (one-dimensional - single loop entry)
320     *
321     * An example of how to use this function:
322     * {@example alter_block_array.php}
323     *
324     * @param    string    $blockname    the blockname, for example 'loop'
325     * @param    array    $vararray    the var array to insert/add or merge
326     * @param    mixed    $key        Key to search for
327     *
328     * array: KEY => VALUE [the key/value pair to search for within the loop to determine the correct position]
329     *
330     * int: Position [the position to change or insert at directly given]
331     *
332     * If key is false the position is set to 0
333     * If key is true the position is set to the last entry
334     *
335     * @param    string    $mode        Mode to execute (valid modes are 'insert' and 'change')
336     *
337     *    If insert, the vararray is inserted at the given position (position counting from zero).
338     *    If change, the current block gets merged with the vararray (resulting in new key/value pairs be added and existing keys be replaced by the new value).
339     *
340     * Since counting begins by zero, inserting at the last position will result in this array: array(vararray, last positioned array)
341     * and inserting at position 1 will result in this array: array(first positioned array, vararray, following vars)
342     *
343     * @return bool false on error, true on success
344     * @access public
345     */
346     function alter_block_array($blockname, $vararray, $key = false, $mode = 'insert')
347     {
348         if (strpos($blockname, '.') !== false)
349         {
350             // Nested blocks are not supported
351             return false;
352         }
353        
354         // Change key to zero (change first position) if false and to last position if true
355         if ($key === false || $key === true)
356         {
357             $key = ($key === false) ? 0 : sizeof($this->_tpldata[$blockname]);
358         }
359
360         // Get correct position if array given
361         if (is_array($key))
362         {
363             // Search array to get correct position
364             list($search_key, $search_value) = @each($key);
365
366             $key = NULL;
367             foreach ($this->_tpldata[$blockname] as $i => $val_ary)
368             {
369                 if ($val_ary[$search_key] === $search_value)
370                 {
371                     $key = $i;
372                     break;
373                 }
374             }
375
376             // key/value pair not found
377             if ($key === NULL)
378             {
379                 return false;
380             }
381         }
382
383         // Insert Block
384         if ($mode == 'insert')
385         {
386             // Make sure we are not exceeding the last iteration
387             if ($key >= sizeof($this->_tpldata[$blockname]))
388             {
389                 $key = sizeof($this->_tpldata[$blockname]);
390                 unset($this->_tpldata[$blockname][($key - 1)]['S_LAST_ROW']);
391                 $vararray['S_LAST_ROW'] = true;
392             }
393             else if ($key === 0)
394             {
395                 unset($this->_tpldata[$blockname][0]['S_FIRST_ROW']);
396                 $vararray['S_FIRST_ROW'] = true;
397             }
398
399             // Re-position template blocks
400             for ($i = sizeof($this->_tpldata[$blockname]); $i > $key; $i--)
401             {
402                 $this->_tpldata[$blockname][$i] = $this->_tpldata[$blockname][$i-1];
403                 $this->_tpldata[$blockname][$i]['S_ROW_COUNT'] = $i;
404             }
405
406             // Insert vararray at given position
407             $vararray['S_ROW_COUNT'] = $key;
408             $this->_tpldata[$blockname][$key] = $vararray;
409
410             return true;
411         }
412
413         // Which block to change?
414         if ($mode == 'change')
415         {
416             if ($key == sizeof($this->_tpldata[$blockname]))
417             {
418                 $key--;
419             }
420
421             $this->_tpldata[$blockname][$key] = array_merge($this->_tpldata[$blockname][$key], $vararray);
422             return true;
423         }
424
425         return false;
426     }
427
428     /**
429     * Include a separate template
430     * @access private
431     */
432     function _tpl_include($filename, $include = true)
433     {
434         $handle = $filename;
435         $this->filename[$handle] = $filename;
436         $this->files[$handle] = $this->root . '/' . $filename;
437
438          $filename = $this->_tpl_load($handle);
439
440         if ($include)
441         {
442             global $lang;
443
444             if ($filename)
445             {
446                 include($filename);
447                 return;
448             }
449             eval(' ?>' . $this->compiled_code[$handle] . '<?php ');
450         }
451     }
452 }
453
454 ?>
455
Note: See TracBrowser for help on using the browser.