source: trunk/include/template.class.php @ 2286

Last change on this file since 2286 was 2286, checked in by rvelices, 16 years ago
  • admin/notification_by_mail goes smarty - THE LAST ONE :-) :-)
  • get rid of user_list warnings
  • some code reorganisation in template class + explode modifier
  • minor template and language changes in search.tpl and cat_modify.tpl
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2008 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: template.class.php 2286 2008-03-20 00:35:36Z rvelices $
8// | last update   : $Date: 2008-03-20 00:35:36 +0000 (Thu, 20 Mar 2008) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2286 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27
28require 'smarty/libs/Smarty.class.php';
29
30// migrate lang:XXX
31//    sed "s/{lang:\([^}]\+\)}/{\'\1\'|@translate}/g" my_template.tpl
32// migrate change root level vars {XXX}
33//    sed "s/{pwg_root}/{ROOT_URL}/g" my_template.tpl
34// migrate change root level vars {XXX}
35//    sed "s/{\([a-zA-Z_]\+\)}/{$\1}/g" my_template.tpl
36// migrate all
37//    cat my_template.tpl | sed "s/{lang:\([^}]\+\)}/{\'\1\'|@translate}/g" | sed "s/{pwg_root}/{ROOT_URL}/g" | sed "s/{\([a-zA-Z_]\+\)}/{$\1}/g"
38
39
40class Template {
41
42  var $smarty;
43
44  var $_old;
45
46  var $output = '';
47
48  // Hash of filenames for each template handle.
49  var $files = array();
50
51  function Template($root = ".", $theme= "")
52  {
53    global $conf;
54
55    $this->smarty = new Smarty;
56    $this->smarty->debugging = $conf['debug_template'];
57    //$this->smarty->force_compile = true;
58
59    if ( isset($conf['compiled_template_dir'] ) )
60    {
61      $compile_dir = $conf['compiled_template_dir'];
62    }
63    else
64    {
65      $compile_dir = $conf['local_data_dir'];
66      if ( !is_dir($compile_dir) )
67      {
68        mkdir( $compile_dir, 0777);
69        file_put_contents($compile_dir.'/index.htm', '');
70      }
71      $compile_dir .= '/templates_c';
72    }
73    if ( !is_dir($compile_dir) )
74    {
75      mkdir( $compile_dir, 0777 );
76      file_put_contents($compile_dir.'/index.htm', '');
77    }
78
79    $this->smarty->compile_dir = $compile_dir;
80
81    $this->smarty->assign_by_ref( 'pwg', new PwgTemplateAdapter() );
82    $this->smarty->register_modifier( 'translate', array('Template', 'mod_translate') );
83    $this->smarty->register_modifier( 'explode', array('Template', 'mod_explode') );
84
85    if ( !empty($theme) )
86    {
87      include($root.'/theme/'.$theme.'/themeconf.inc.php');
88      $this->smarty->assign('themeconf', $themeconf);
89    }
90
91    $this->_old = & new TemplateOld($root, $theme);
92
93    $this->set_template_dir($root);
94  }
95
96  /**
97   * Sets the template root directory for this Template object.
98   */
99  function set_template_dir($dir)
100  {
101    $this->_old->set_rootdir($dir);
102    $this->smarty->template_dir = $dir;
103
104    $real_dir = realpath($dir);
105    $compile_id = crc32( $real_dir===false ? $dir : $real_dir);
106    $this->smarty->compile_id = sprintf('%08X', $compile_id );
107  }
108
109  /**
110   * Gets the template root directory for this Template object.
111   */
112  function get_template_dir()
113  {
114    return $this->smarty->template_dir;
115  }
116
117  /**
118   * Deletes all compiled templates.
119   */
120  function delete_compiled_templates()
121  {
122      $save_compile_id = $this->smarty->compile_id;
123      $this->smarty->compile_id = null;
124      $this->smarty->clear_compiled_tpl();
125      $this->smarty->compile_id = $save_compile_id;
126      file_put_contents($this->smarty->compile_dir.'/index.htm', '');
127  }
128
129  function get_themeconf($val)
130  {
131    $tc = $this->smarty->get_template_vars('themeconf');
132    return isset($tc[$val]) ? $tc[$val] : '';
133  }
134
135  /**
136   * Sets the template filename for handle.
137   */
138  function set_filename($handle, $filename)
139  {
140    return $this->set_filenames( array($handle=>$filename) );
141  }
142
143  /**
144   * Sets the template filenames for handles. $filename_array should be a
145   * hash of handle => filename pairs.
146   */
147  function set_filenames($filename_array)
148  {
149    if (!is_array($filename_array))
150    {
151      return false;
152    }
153
154    reset($filename_array);
155    while(list($handle, $filename) = each($filename_array))
156    {
157      if (is_null($filename))
158        unset( $this->files[$handle] );
159      else
160        $this->files[$handle] = $filename;
161    }
162    return true;
163  }
164
165  /**
166   * DEPRECATED - backward compatibility only; use assign
167   */
168  function assign_vars($vararray)
169  {
170    is_array( $vararray ) || die('assign_vars parameter not array');
171    $this->assign( $vararray );
172  }
173
174  /**
175   * DEPRECATED - backward compatibility only; use assign
176   */
177  function assign_var($varname, $varval)
178  {
179    !is_array( $varname ) || die('assign_var parameter name is array');
180    $this->assign( $varname, $varval );
181  }
182
183  /**
184   * DEPRECATED - backward compatibility only
185   */
186  function assign_block_vars($blockname, $vararray)
187  {
188    if (strstr($blockname, '.')!==false)
189    {
190      $blocks = explode('.', $blockname);
191      $blockcount = sizeof($blocks) - 1;
192      $root_var = & $this->smarty->get_template_vars();
193
194      $str = '$root_var';
195      for ($i = 0; $i < $blockcount; $i++)
196      {
197        $str .= '[\'' . $blocks[$i] . '\']';
198        eval('$lastiteration = isset('.$str.') ? sizeof('.$str.')-1:0;');
199        $str .= '[' . $lastiteration . ']';
200      }
201      $str .= '[\'' . $blocks[$blockcount] . '\'][] = $vararray;';
202      eval($str);
203    }
204    else
205      $this->smarty->append( $blockname, $vararray );
206
207    $this->_old->assign_block_vars($blockname, $vararray);
208  }
209
210  /**
211   * DEPRECATED - backward compatibility only
212   */
213  function merge_block_vars($blockname, $vararray)
214  {
215    if (strstr($blockname, '.')!==false)
216    {
217      $blocks = explode('.', $blockname);
218      $blockcount = count($blocks);
219      $root_var = & $this->smarty->get_template_vars();
220
221      $str = '$root_var';
222      for ($i = 0; $i < $blockcount; $i++)
223      {
224        $str .= '[\'' . $blocks[$i] . '\']';
225        eval('$lastiteration = isset('.$str.') ? sizeof('.$str.')-1:-1;');
226        if ($lastiteration==-1)
227        {
228          return false;
229        }
230        $str .= '[' . $lastiteration . ']';
231      }
232      $str = $str.'=array_merge('.$str.', $vararray);';
233      eval($str);
234    }
235    else
236      $this->smarty->append( $blockname, $vararray, true );
237
238    $this->_old->merge_block_vars($blockname, $vararray);
239    return true;
240  }
241
242  /** see smarty assign http://www.smarty.net/manual/en/api.assign.php */
243  function assign($tpl_var, $value = null)
244  {
245    $this->smarty->assign( $tpl_var, $value );
246
247    if ( is_array($tpl_var) )
248      $this->_old->assign_vars( $tpl_var );
249    else
250      $this->_old->assign_var( $tpl_var, $value );
251  }
252 
253  /**
254   * Inserts the uncompiled code for $handle as the value of $varname in the
255   * root-level. This can be used to effectively include a template in the
256   * middle of another template.
257   * This is equivalent to assign($varname, $this->parse($handle, true))
258   */
259  function assign_var_from_handle($varname, $handle)
260  {
261    $this->assign($varname, $this->parse($handle, true));
262    return true;
263  }
264
265  /** see smarty append http://www.smarty.net/manual/en/api.append.php */
266  function append($tpl_var, $value=null, $merge=false)
267  {
268    $this->smarty->append( $tpl_var, $value, $merge );
269  }
270
271  /**
272   * Root-level variable concatenation. Appends a  string to an existing
273   * variable assignment with the same name.
274   */
275  function concat($tpl_var, $value)
276  {
277    $old_val = & $this->smarty->get_template_vars($tpl_var);
278    if ( isset($old_val) )
279    {
280      $old_val .= $value;
281      $this->_old->concat_var( $tpl_var, $value );
282    }
283    else
284    {
285      $this->assign($tpl_var, $value);
286    }
287  }
288
289  /** see smarty append http://www.smarty.net/manual/en/api.clear_assign.php */
290  function clear_assign($tpl_var)
291  {
292    $this->smarty->clear_assign( $tpl_var );
293  }
294
295  /** see smarty get_template_vars http://www.smarty.net/manual/en/api.get_template_vars.php */
296  function &get_template_vars($name=null)
297  {
298    return $this->smarty->get_template_vars( $name );
299  }
300
301
302  /**
303   * Load the file for the handle, eventually compile the file and run the compiled
304   * code. This will add the output to the results or return the result if $return
305   * is true.
306   */
307  function parse($handle, $return=false)
308  {
309    if ( !isset($this->files[$handle]) )
310    {
311      die("Template->parse(): Couldn't load template file for handle $handle");
312    }
313
314    $is_new = true;
315    $params = array('resource_name' => $this->files[$handle], 'quiet'=>true, 'get_source'=>true);
316    if ( $this->smarty->_fetch_resource_info($params) )
317    {
318      if (!preg_match('~{(/(if|section|foreach))|ldelim|\$[a-zA-Z_]+}~', @$params['source_content']) )
319        $is_new = false;
320    }
321
322    if ($is_new)
323    {
324      $this->smarty->assign( 'ROOT_URL', get_root_url() );
325      $this->smarty->assign( 'TAG_INPUT_ENABLED',
326        ((is_adviser()) ? 'disabled="disabled" onclick="return false;"' : ''));
327      $v = $this->smarty->fetch($this->files[$handle], null, null, false);
328    }
329    else
330    {
331      $this->_old->assign_vars(array('TAG_INPUT_ENABLED' =>
332        ((is_adviser()) ? 'disabled onclick="return false;"' : '')));
333      $this->_old->set_filename( $handle, $this->files[$handle] );
334      $v = $this->_old->parse($handle, true);
335    }
336    if ($return)
337    {
338      return $v;
339    }
340    $this->output .= $v;
341  }
342
343  /**
344   * Load the file for the handle, eventually compile the file and run the compiled
345   * code. This will print out the results of executing the template.
346   */
347  function pparse($handle)
348  {
349    $this->parse($handle, false);
350    echo $this->output;
351    $this->output='';
352
353  }
354
355
356  /** flushes the output */
357  function p()
358  {
359    $start = get_moment();
360
361    echo $this->output;
362    $this->output='';
363
364    if ($this->smarty->debugging)
365    {
366      global $t2;
367      $this->smarty->assign(
368        array(
369        'AAAA_DEBUG_OUTPUT_TIME__' => get_elapsed_time($start, get_moment()),
370        'AAAA_DEBUG_TOTAL_TIME__' => get_elapsed_time($t2, get_moment())
371        )
372        );
373      require_once(SMARTY_CORE_DIR . 'core.display_debug_console.php');
374      echo smarty_core_display_debug_console(null, $this->smarty);
375    }
376  }
377
378  /**
379   * translate variable modifier - translates a text to the currently loaded
380   * language
381   */
382  /*static*/ function mod_translate($text)
383  {
384    return l10n($text);
385  }
386
387  /**
388   * explode variable modifier - similar to php explode
389   * 'Yes;No'|@explode:';' -> array('Yes', 'No')   
390   */
391  /*static*/ function mod_explode($text, $delimiter=',')
392  {
393    return explode($delimiter, $text);
394  }
395}
396
397/**
398 * This class contains basic functions that can be called directly from the
399 * templates in the form $pwg->l10n('edit')
400 */
401class PwgTemplateAdapter
402{
403  function l10n($text)
404  {
405    return l10n($text);
406  }
407
408  function l10n_dec($s, $p, $v)
409  {
410    return l10n_dec($s, $p, $v);
411  }
412
413  function sprintf()
414  {
415    $args = func_get_args();
416    return call_user_func_array('sprintf',  $args );
417  }
418}
419
420?>
Note: See TracBrowser for help on using the repository browser.