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

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

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