source: trunk/phpgwapi/inc/class.Template.inc.php @ 2

Revision 2, 12.2 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /**************************************************************************\
3  * eGroupWare API - Template class                                          *
4  * (C) Copyright 1999-2000 NetUSE GmbH Kristian Koehntopp                   *
5  * ------------------------------------------------------------------------ *
6  * This is not part of eGroupWare, but is used by eGroupWare.               *
7  * http://www.egroupware.org/                                               *
8  * ------------------------------------------------------------------------ *
9  * This program is free software; you can redistribute it and/or modify it  *
10  * under the terms of the GNU Lesser General Public License as published    *
11  * by the Free Software Foundation; either version 2.1 of the License, or   *
12  * any later version.                                                       *
13  \**************************************************************************/
14
15
16        class Template
17        {
18                var $classname = 'Template';
19
20                /**
21                 * @var $rb_debug mixed False for no debug, string or array of strings with:
22                 *      - function-name like set_var to get eg. all assignments or
23                 *      - handle- / variable-names - if you are only interested in some variables ;-)
24                 */
25                var $debug = False;     // array('cat_list','cat_list_t');
26
27                /* $file[handle] = 'filename'; */
28                var $file = array();
29
30                /* relative filenames are relative to this pathname */
31                var $root = '';
32
33                /* $varkeys[key] = 'key'; $varvals[key] = 'value'; */
34                var $varkeys = array();
35                var $varvals = array();
36
37                /* 'remove'  => remove undefined variables
38                 * 'comment' => replace undefined variables with comments
39                 * 'keep'    => keep undefined variables
40                 */
41                var $unknowns = 'remove';
42
43                /* 'yes' => halt, 'report' => report error, continue, 'no' => ignore error quietly */
44                var $halt_on_error = 'yes';
45
46                /* last error message is retained here */
47                var $last_error = '';
48
49                // if true change all phpGroupWare into eGroupWare in set_var
50                var $egroupware_hack = True;
51
52                /***************************************************************************/
53                /* public: Constructor.
54                 * root:     template directory.
55                 * unknowns: how to handle unknown variables.
56                 */
57                function Template($root = '.', $unknowns = 'remove')
58                {
59                        $this->set_root($root);
60                        $this->set_unknowns($unknowns);
61                }
62
63                /* public: setroot(pathname $root)
64                 * root:   new template directory.
65                 */
66                function set_root($root)
67                {
68                        if ($this->debug && $this->check_debug('set_root'))
69                        {
70                                echo "<p>Template::set_root('$root')</p>\n";
71                        }
72                        if (!is_dir($root))
73                        {
74                                $this->halt("set_root: $root is not a directory.");
75                                return false;
76                        }
77                        $this->root = $root;
78                        return true;
79                }
80
81                /* public: set_unknowns(enum $unknowns)
82                 * unknowns: 'remove', 'comment', 'keep'
83                 *
84                 */
85                function set_unknowns($unknowns = 'keep')
86                {
87                        if ($this->debug && $this->check_debug('set_unknows'))
88                        {
89                                echo "<p>Template::set_unknows('$unknowns')</p>\n";
90                        }
91                        $this->unknowns = $unknowns;
92                }
93
94                /* public: set_file(array $filelist)
95                 * filelist: array of handle, filename pairs.
96                 *
97                 * public: set_file(string $handle, string $filename)
98                 * handle: handle for a filename,
99                 * filename: name of template file
100                 */
101                function set_file($handle, $filename = '')
102                {
103                        if ($this->debug && $this->check_debug('set_file',$handle,$filename))
104                        {
105                                echo "<p>Template::set_file('".print_r($handle,true)."','$filename')</p>\n";
106                        }
107                        if (!is_array($handle))
108                        {
109                                if ($filename == '')
110                                {
111                                        $this->halt("set_file: For handle $handle filename is empty.");
112                                        return false;
113                                }
114                                $this->file[$handle] = $this->filename($filename);
115                        }
116                        else
117                        {
118                                foreach($handle as $h => $f)
119                                {
120                                        $this->file[$h] = $this->filename($f);
121                                }
122                        }
123                }
124
125                /* public: set_block(string $parent, string $handle, string $name = '')
126                 * extract the template $handle from $parent,
127                 * place variable {$name} instead.
128                 */
129                function set_block($parent, $handle, $name = '')
130                {
131                        if ($this->debug && $this->check_debug('set_block',$parent,$handle,$name))
132                        {
133                                echo "<p>Template::set_block('$parent','$handle','$name')</p>\n";
134                        }
135                        if (!$this->loadfile($parent))
136                        {
137                                $this->halt("set_block: unable to load '$parent'.");
138                                return false;
139                        }
140                        if ($name == '')
141                        {
142                                $name = $handle;
143                        }
144                        $str = $this->get_var($parent);
145                        $qhandle = preg_quote($handle);
146                        $reg = "/<!--\\s+BEGIN $qhandle\\s+-->(.*)\n\\s*<!--\\s+END $qhandle\\s+-->/s";
147                        if (!preg_match($reg,$str,$match))
148                        {
149                                // unfortunaly some apps set non-existing blocks, therefor I have to disable this diagnostics again for now
150                                // $this->halt("set_block: unable to find block '$handle' in '$parent'.");
151                                // return False;
152                        }
153                        $this->set_var($handle,$match[1]);
154                        $this->set_var($parent,preg_replace($reg, '{' . "$name}",$str));
155                }
156
157                /* public: set_var(array $values)
158                 * values: array of variable name, value pairs.
159                 *
160                 * public: set_var(string $varname, string $value)
161                 * varname: name of a variable that is to be defined
162                 * value:   value of that variable
163                 */
164                function set_var($varname, $value = '')
165                {
166                        if (!is_array($varname))
167                        {
168                                if (empty($varname))
169                                {
170                                        return;
171                                }
172                                $varname = array(
173                                        $varname => $value
174                                );
175                        }
176                        foreach($varname as $k => $v)
177                        {
178                                if (!empty($k))
179                                {
180                                        if ($this->debug && $this->check_debug('set_var',$k))
181                                        {
182                                                echo "<p>Template::set_var('$k','$v')</p>\n";
183                                        }
184                                        $this->varkeys[$k] = $this->varname($k);
185                                        $this->varvals[$k] = $this->egroupware_hack ? str_replace(
186                                                array('phpGroupWare','www.phpgroupware.org'),
187                                                array('eGroupWare','www.eGroupWare.org'),$v
188                                        ) : $v;
189                                }
190                        }
191                }
192
193                /* public: subst(string $handle)
194                 * handle: handle of template where variables are to be substituted.
195                 */
196                function subst($handle)
197                {
198                        if ($this->debug && $this->check_debug('subst',$handle))
199                        {
200                                echo "<p>Template::subst('$handle')</p>\n";
201                        }
202                        if (!$this->loadfile($handle))
203                        {
204                                $this->halt("subst: unable to load $handle.");
205                                return false;
206                        }
207
208                        $str = $this->get_var($handle);
209                        foreach($this->varkeys as $k => $v)
210                        {
211                                $str = str_replace($v, $this->varvals[$k], $str);
212                        }
213                        return $str;
214                }
215
216                /* public: psubst(string $handle)
217                 * handle: handle of template where variables are to be substituted.
218                 */
219                function psubst($handle)
220                {
221                        print $this->subst($handle);
222                        return false;
223                }
224
225                /* public: parse(string $target, string $handle, boolean append)
226                 * target: handle of variable to generate
227                 * handle: handle of template to substitute
228                 * append: append to target handle
229                 */
230                function parse($target, $handle, $append = false)
231                {
232                        if (!is_array($handle))
233                        {
234                                $str = $this->subst($handle);
235                                if ($append)
236                                {
237                                        $this->set_var($target, $this->get_var($target) . $str);
238                                }
239                                else
240                                {
241                                        $this->set_var($target, $str);
242                                }
243                        }
244                        else
245                        {
246                                foreach($handle as $i => $h)
247                                {
248                                        $str = $this->subst($h);
249                                        $this->set_var($target, $str);
250                                }
251                        }
252                        return $str;
253                }
254
255                function pparse($target, $handle, $append = false)
256                {
257                        print $this->parse($target, $handle, $append);
258                        return false;
259                }
260
261                /* This is short for finish parse */
262                function fp($target, $handle, $append = False)
263                {
264                        return $this->finish($this->parse($target, $handle, $append));
265                }
266
267                /* This is a short cut for print finish parse */
268                function pfp($target, $handle, $append = False)
269                {
270                        echo $this->finish($this->parse($target, $handle, $append));
271                }
272
273                /* public: get_vars()
274                 */
275                function get_vars()
276                {
277                        foreach($this->varkeys as $k => $v)
278                        {
279                                $result[$k] = $this->varvals[$k];
280                        }
281                        return $result;
282                }
283
284                /* public: get_var(string varname)
285                 * varname: name of variable.
286                 *
287                 * public: get_var(array varname)
288                 * varname: array of variable names
289                 */
290                function get_var($varname)
291                {
292                        if (!is_array($varname))
293                        {
294                                return $this->varvals[$varname];
295                        }
296                        else
297                        {
298                                foreach($varname as $k => $v)
299                                {
300                                        $result[$k] = $this->varvals[$k];
301                                }
302                                return $result;
303                        }
304                }
305
306                /* public: get_undefined($handle)
307                 * handle: handle of a template.
308                 */
309                function get_undefined($handle)
310                {
311                        if (!$this->loadfile($handle))
312                        {
313                                $this->halt("get_undefined: unable to load $handle.");
314                                return false;
315                        }
316
317                        preg_match_all("/\{([^}]+)\}/", $this->get_var($handle), $m);
318                        $m = $m[1];
319                        if (!is_array($m))
320                        {
321                                return false;
322                        }
323                        foreach($m as $k => $v)
324                        {
325                                if (!isset($this->varkeys[$v]))
326                                {
327                                        $result[$v] = $v;
328                                }
329                        }
330
331                        if (count($result))
332                        {
333                                return $result;
334                        }
335                        else
336                        {
337                                return false;
338                        }
339                }
340
341                /* public: finish(string $str)
342                 * str: string to finish.
343                 */
344                function finish($str)
345                {
346                        switch ($this->unknowns)
347                        {
348                                case 'keep':
349                                        break;
350                                case 'remove':
351                                        $str = preg_replace('/{[^ \t\r\n}]+}/', '', $str);
352                                        break;
353                                case 'comment':
354                                        $str = preg_replace('/{([^ \t\r\n}]+)}/', "<!-- Template $handle: Variable \\1 undefined -->", $str);
355                                        break;
356                        }
357
358                        return $str;
359                }
360
361                /* public: p(string $varname)
362                 * varname: name of variable to print.
363                 */
364                function p($varname)
365                {
366                        print $this->finish($this->get_var($varname));
367                }
368
369                function get($varname)
370                {
371                        return $this->finish($this->get_var($varname));
372                }
373
374                /***************************************************************************/
375                /* private: filename($filename)
376                 * filename: name to be completed.
377                 */
378                function filename($filename,$root='',$time=1)
379                {
380                        if($root == '')
381                        {
382                                $root = $this->root;
383                        }
384                        if(substr($filename, 0, 1) != '/')
385                        {
386                                $new_filename = $root . '/' . $filename;
387                        }
388                        else
389                        {
390                                $new_filename = $filename;
391                        }
392
393                        if (!file_exists($new_filename))
394                        {
395                                if($time==2)
396                                {
397                                        $this->halt("filename: file $new_filename does not exist.");
398                                }
399                                else
400                                {
401//                                      $new_root = str_replace($GLOBALS['phpgw_info']['server']['template_set'],'default',$root);
402//                                      $new_root = substr($root, 0, strrpos($root, $GLOBALS['phpgw_info']['server']['template_set'])).'default';
403                                        $new_root = substr($root, 0, strlen($root) - strlen($GLOBALS['phpgw_info']['server']['template_set'])) . 'default';
404                                        $new_filename = $this->filename(str_replace($root.'/','',$new_filename),$new_root,2);
405                                }
406                        }
407                        return $new_filename;
408                }
409
410                /* private: varname($varname)
411                 * varname: name of a replacement variable to be protected.
412                 */
413                function varname($varname)
414                {
415                        return '{'.$varname.'}';
416                }
417
418                /* private: loadfile(string $handle)
419                 * handle:  load file defined by handle, if it is not loaded yet.
420                 */
421                function loadfile($handle)
422                {
423                        if ($this->debug && $this->check_debug('loadfile',$handle))
424                        {
425                                echo "<p>Template::loadfile('$handle') file=<pre>\n".print_r($this->file,True)."</pre>\n";
426                                echo "<p>backtrace: ".function_backtrace()."</p>\n";
427                        }
428                        if (isset($this->varkeys[$handle]) && !empty($this->varvals[$handle]))
429                        {
430                                return true;
431                        }
432                        if (!isset($this->file[$handle]))
433                        {
434                                if ($this->debug && $this->check_debug('loadfile',$handle))
435                                {
436                                        echo "varkeys =<pre>".print_r($this->varkeys,True)."</pre>varvals =<pre>".print_r($this->varvals,True)."</pre>\n";
437                                }
438                                $this->halt("loadfile: $handle is not a valid handle.");
439                                return false;
440                        }
441                        $filename = $this->file[$handle];
442
443                        $str = implode('', @file($filename));
444                        if (empty($str))
445                        {
446                                $this->halt("loadfile: While loading $handle, $filename does not exist or is empty.");
447                                return false;
448                        }
449
450                        $this->set_var($handle, $str);
451                        return true;
452                }
453
454                /***************************************************************************/
455                /* public: halt(string $msg)
456                 * msg:    error message to show.
457                 */
458                function halt($msg)
459                {
460                        $this->last_error = $msg;
461
462                        if ($this->halt_on_error != 'no')
463                        {
464                                $this->haltmsg($msg);
465                        }
466
467                        if ($this->halt_on_error == 'yes')
468                        {
469                                echo('<b>Halted.</b>');
470                        }
471
472                        $GLOBALS['phpgw']->common->phpgw_exit(True);
473                }
474
475                /* public, override: haltmsg($msg)
476                 * msg: error message to show.
477                 */
478                function haltmsg($msg)
479                {
480                        printf("<b>Template Error:</b> %s<br>\n", $msg);
481                        echo "<b>Backtrace</b>: ".function_backtrace(2)."<br>\n";
482                }
483
484                function check_debug($str)
485                {
486                        if (!$this->debug) return False;
487
488                        foreach(func_get_args() as $arg)
489                        {
490                                if (!is_array($this->debug) && $this->debug === $arg ||
491                                        (is_array($this->debug) && (@$this->debug[$arg] || in_array($arg,$this->debug,True))))
492                                {
493                                        return True;
494                                }
495                        }
496                        return False;
497                }
498        }
Note: See TracBrowser for help on using the repository browser.