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

Last change on this file since 2217 was 2217, checked in by rvelices, 16 years ago
  • works with PHP5
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 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 2217 2008-02-27 03:15:31Z rvelices $
8// | last update   : $Date: 2008-02-27 03:15:31 +0000 (Wed, 27 Feb 2008) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2217 $
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->template_dir = $root;
82    $this->smarty->register_function( 'lang', array('Template', 'fn_l10n') );
83
84    $this->smarty->assign_by_ref( 'pwg', new PwgTemplateAdapter() );
85    $this->smarty->register_modifier( 'translate', array('Template', 'mod_translate') );
86
87    include($root.'/theme/'.$theme.'/themeconf.inc.php');
88    $this->smarty->assign('themeconf', $themeconf);
89
90    $this->_old = & new TemplateOld($root, $theme);
91  }
92
93  /** DEPRECATED */
94  function get_themeconf($val)
95  {
96    $tc = $this->smarty->get_template_vars('themeconf');
97    return isset($tc[$val]) ? $tc[$val] : '';
98  }
99
100  /**
101   * Sets the template filename for handle.
102   */
103  function set_filename($handle, $filename)
104  {
105    return $this->set_filenames( array($handle=>$filename) );
106  }
107
108  /**
109   * Sets the template filenames for handles. $filename_array should be a
110   * hash of handle => filename pairs.
111   */
112  function set_filenames($filename_array)
113  {
114    if (!is_array($filename_array))
115    {
116      return false;
117    }
118
119    reset($filename_array);
120    while(list($handle, $filename) = each($filename_array))
121    {
122      if (is_null($filename))
123        unset( $this->files[$handle] );
124      else
125        $this->files[$handle] = $filename;
126    }
127    return true;
128  }
129
130  /**
131   * DEPRECATED - backward compatibility only; use assign
132   */
133  function assign_vars($vararray)
134  {
135    is_array( $vararray ) || die('assign_vars parameter not array');
136    $this->assign( $vararray );
137  }
138
139  /**
140   * DEPRECATED - backward compatibility only; use assign
141   */
142  function assign_var($varname, $varval)
143  {
144    !is_array( $varname ) || die('assign_var parameter name is array');
145    $this->assign( $varname, $varval );
146  }
147
148  /**
149   * Inserts the uncompiled code for $handle as the value of $varname in the
150   * root-level. This can be used to effectively include a template in the
151   * middle of another template.
152   * This is equivalent to assign($varname, $this->parse($handle, true))
153   */
154  function assign_var_from_handle($varname, $handle)
155    {
156      $this->assign($varname, $this->parse($handle, true));
157      return true;
158    }
159
160  /**
161   * DEPRECATED - backward compatibility only
162   */
163  function assign_block_vars($blockname, $vararray)
164  {
165    if (strstr($blockname, '.')!==false)
166    {
167      $blocks = explode('.', $blockname);
168      $blockcount = sizeof($blocks) - 1;
169      $root_var = & $this->smarty->get_template_vars();
170
171      $str = '$root_var';
172      for ($i = 0; $i < $blockcount; $i++)
173      {
174        $str .= '[\'' . $blocks[$i] . '\']';
175        eval('$lastiteration = isset('.$str.') ? sizeof('.$str.')-1:0;');
176        $str .= '[' . $lastiteration . ']';
177      }
178      $str .= '[\'' . $blocks[$blockcount] . '\'][] = $vararray;';
179      eval($str);
180    }
181    else
182      $this->smarty->append( $blockname, $vararray );
183
184    $this->_old->assign_block_vars($blockname, $vararray);
185  }
186
187  /**
188   * DEPRECATED - backward compatibility only
189   */
190  function merge_block_vars($blockname, $vararray)
191  {
192    if (strstr($blockname, '.')!==false)
193    {
194      $blocks = explode('.', $blockname);
195      $blockcount = count($blocks);
196      $root_var = & $this->smarty->get_template_vars();
197
198      $str = '$root_var';
199      for ($i = 0; $i < $blockcount; $i++)
200      {
201        $str .= '[\'' . $blocks[$i] . '\']';
202        eval('$lastiteration = isset('.$str.') ? sizeof('.$str.')-1:-1;');
203        if ($lastiteration==-1)
204        {
205          return false;
206        }
207        $str .= '[' . $lastiteration . ']';
208      }
209      $str = $str.'=array_merge('.$str.', $vararray);';
210      eval($str);
211    }
212    else
213      $this->smarty->append( $blockname, $vararray, true );
214
215    $this->_old->merge_block_vars($blockname, $vararray);
216    return true;
217  }
218
219  /**
220   * Load the file for the handle, eventually compile the file and run the compiled
221   * code. This will add the output to the results or return the result if $return
222   * is true.
223   */
224  function parse($handle, $return=false)
225  {
226    return $this->_pparse_or_parse($handle, $return, false);
227  }
228
229  /**
230   * Load the file for the handle, eventually compile the file and run the compiled
231   * code. This will print out the results of executing the template.
232   */
233  function pparse($handle)
234  {
235    $this->_pparse_or_parse($handle, false, true);
236  }
237
238
239  /*private*/ function _pparse_or_parse($handle, $return, $immediate_display)
240  {
241    if ( !isset($this->files[$handle]) )
242    {
243      die("Template->parse(): Couldn't load template file for handle $handle");
244    }
245
246    $is_new = true;
247    $params = array('resource_name' => $this->files[$handle], 'quiet'=>true, 'get_source'=>true);
248    if ( $this->smarty->_fetch_resource_info($params) )
249    {
250      if (!preg_match('~{(/(if|section|foreach))|\$[a-zA-Z_]+}~', @$params['source_content']) )
251        $is_new = false;
252    }
253
254    if ($is_new)
255    {
256      $this->smarty->assign( 'pwg_root', get_root_url() ); //deprecated
257      $this->smarty->assign( 'ROOT_URL', get_root_url() );
258      if ($immediate_display)
259        $this->p();
260      $v = $this->smarty->fetch($this->files[$handle], null, null, $immediate_display ? true : false);
261      if ($immediate_display)
262        return;
263    }
264    else
265    {
266      $this->_old->set_filename( $handle, $this->files[$handle] );
267      $v = $this->_old->parse($handle, true);
268    }
269    if ($return==false)
270    {
271      $this->output .= $v;
272    }
273    return $v;
274  }
275
276  /** flushes current output */
277  function p()
278  {
279    echo $this->output;
280    $this->output='';
281  }
282
283  /**
284   * Root-level variable concatenation. Appends a  string to an existing
285   * variable assignment with the same name.
286   */
287  function concat_var($tpl_var, $value)
288  {
289    $old_val = & $this->smarty->get_template_vars($tpl_var);
290    if ( isset($old_val) )
291    {
292      $old_val .= $value;
293      $this->_old->concat_var( $tpl_var, $value );
294    }
295    else
296    {
297      $this->assign($tpl_var, $value);
298    }
299  }
300
301  /** see smarty assign http://www.smarty.net/manual/en/api.assign.php */
302  function assign($tpl_var, $value = null)
303  {
304    $this->smarty->assign( $tpl_var, $value );
305
306    if ( is_array($tpl_var) )
307      $this->_old->assign_vars( $tpl_var );
308    else
309      $this->_old->assign_var( $tpl_var, $value );
310  }
311
312  /** see smarty append http://www.smarty.net/manual/en/api.append.php */
313  function append($tpl_var, $value=null, $merge=false)
314  {
315    $this->smarty->append( $tpl_var, $value, $merge );
316  }
317
318  /** see smarty append http://www.smarty.net/manual/en/api.clear_assign.php */
319  function clear_assign($tpl_var)
320  {
321    $this->smarty->clear_assign( $tpl_var );
322  }
323
324  /*static*/ function fn_l10n($params, &$smarty)
325  {
326    return l10n($params['t']);
327  }
328
329  /**
330   * translate variable modifiers - translates a text to the currently loaded
331   * language
332   */
333  /*static*/ function mod_translate($text)
334  {
335    return l10n($text);
336  }
337}
338
339/**
340 * This class contains basic functions that can be called directly from the
341 * templates in the form $pwg->l10n('edit')
342 */
343class PwgTemplateAdapter
344{
345  function l10n($text)
346  {
347    return l10n($text);
348  }
349
350  function l10n_dec($s, $p, $v)
351  {
352    return l10n_dec($s, $p, $v);
353  }
354
355  function sprintf()
356  {
357    $args = func_get_args();
358    return call_user_func_array('sprintf',  $args );
359  }
360}
361
362?>
Note: See TracBrowser for help on using the repository browser.