PageRenderTime 65ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/s3db3.5.10/s3dbcore/class.Template.inc.php

https://code.google.com/p/s3db/
PHP | 461 lines | 306 code | 41 blank | 114 comment | 34 complexity | 5e9a9238c436f0c2f93978ddcdccf6d1 MD5 | raw file
  1. <?php
  2. /**
  3. * Template class
  4. * @author Kristian Koehntopp
  5. * @author Dan Kuykendall
  6. * @copyright Copyright (C) 1999,2000 NetUSE GmbH Kristian Koehntopp
  7. * @copyright Portions Copyright (C) 2001-2004 Free Software Foundation, Inc. http://www.fsf.org/
  8. * @license http://www.fsf.org/licenses/lgpl.html GNU Lesser General Public License
  9. * @package phpgwapi
  10. * @subpackage gui
  11. * @version $Id: class.Template.inc.php,v 1.12.4.3 2004/02/10 13:51:17 ceb Exp $
  12. * @internal Based on phplib
  13. */
  14. /**
  15. * Template class
  16. *
  17. * @package phpgwapi
  18. * @subpackage gui
  19. */
  20. class Template
  21. {
  22. var $classname = 'Template';
  23. /* if set, echo assignments */
  24. var $debug = False;
  25. //var $debug = True;
  26. /* $file[handle] = 'filename'; */
  27. var $file = array();
  28. /* relative filenames are relative to this pathname */
  29. var $root = '';
  30. /* $varkeys[key] = 'key'; $varvals[key] = 'value'; */
  31. var $varkeys = array();
  32. var $varvals = array();
  33. /* 'remove' => remove undefined variables
  34. * 'comment' => replace undefined variables with comments
  35. * 'keep' => keep undefined variables
  36. */
  37. var $unknowns = 'remove';
  38. /* 'yes' => halt, 'report' => report error, continue, 'no' => ignore error quietly */
  39. var $halt_on_error = 'yes';
  40. /* last error message is retained here */
  41. var $last_error = '';
  42. /***************************************************************************/
  43. /* public: Constructor.
  44. * root: template directory.
  45. * unknowns: how to handle unknown variables.
  46. */
  47. function Template($root = '.', $unknowns = 'remove')
  48. {
  49. //echo $root;
  50. $this->set_root($root);
  51. $this->set_unknowns($unknowns);
  52. }
  53. /* public: setroot(pathname $root)
  54. * root: new template directory.
  55. */
  56. function set_root($root)
  57. {
  58. if (!is_dir($root))
  59. {
  60. $this->halt("set_root: $root is not a directory.");
  61. return false;
  62. }
  63. $this->root = $root;
  64. return true;
  65. }
  66. /* public: set_unknowns(enum $unknowns)
  67. * unknowns: 'remove', 'comment', 'keep'
  68. *
  69. */
  70. function set_unknowns($unknowns = 'keep')
  71. {
  72. $this->unknowns = $unknowns;
  73. }
  74. /* public: set_file(array $filelist)
  75. * filelist: array of handle, filename pairs.
  76. *
  77. * public: set_file(string $handle, string $filename)
  78. * handle: handle for a filename,
  79. * filename: name of template file
  80. */
  81. function set_file($handle, $filename = '')
  82. {
  83. if (!is_array($handle))
  84. {
  85. if ($filename == '')
  86. {
  87. $this->halt("set_file: For handle $handle filename is empty.");
  88. return false;
  89. }
  90. $this->file[$handle] = $this->filename($filename);
  91. }
  92. else
  93. {
  94. reset($handle);
  95. while(list($h, $f) = each($handle))
  96. {
  97. $this->file[$h] = $this->filename($f);
  98. }
  99. }
  100. }
  101. /* public: set_block(string $parent, string $handle, string $name = '')
  102. * extract the template $handle from $parent,
  103. * place variable {$name} instead.
  104. */
  105. function set_block($parent, $handle, $name = '')
  106. {
  107. if (!$this->loadfile($parent))
  108. {
  109. $this->halt("subst: unable to load $parent.");
  110. return false;
  111. }
  112. if ($name == '')
  113. {
  114. $name = $handle;
  115. }
  116. $str = $this->get_var($parent);
  117. $reg = "/<!--\s+BEGIN $handle\s+-->(.*)\n\s*<!--\s+END $handle\s+-->/sm";
  118. preg_match_all($reg, $str, $m);
  119. $str = preg_replace($reg, '{' . "$name}", $str);
  120. $this->set_var($handle, $m[1][0]);
  121. $this->set_var($parent, $str);
  122. }
  123. /* public: set_var(array $values)
  124. * values: array of variable name, value pairs.
  125. *
  126. * public: set_var(string $varname, string $value)
  127. * varname: name of a variable that is to be defined
  128. * value: value of that variable
  129. */
  130. function set_var($varname, $value = '')
  131. {
  132. if (!is_array($varname))
  133. {
  134. if (!empty($varname))
  135. {
  136. if ($this->debug)
  137. {
  138. print "scalar: set *$varname* to *$value*<br>\n";
  139. }
  140. $this->varkeys[$varname] = $this->varname($varname);
  141. $this->varvals[$varname] = $value;
  142. }
  143. }
  144. else
  145. {
  146. reset($varname);
  147. while(list($k, $v) = each($varname))
  148. {
  149. if (!empty($k))
  150. {
  151. if ($this->debug)
  152. {
  153. print "array: set *$k* to *$v*<br>\n";
  154. }
  155. $this->varkeys[$k] = $this->varname($k);
  156. $this->varvals[$k] = $v;
  157. }
  158. }
  159. }
  160. }
  161. /* public: subst(string $handle)
  162. * handle: handle of template where variables are to be substituted.
  163. */
  164. function subst($handle)
  165. {
  166. if (!$this->loadfile($handle))
  167. {
  168. $this->halt("subst: unable to load $handle.");
  169. return false;
  170. }
  171. $str = $this->get_var($handle);
  172. reset($this->varkeys);
  173. while (list($k, $v) = each($this->varkeys))
  174. {
  175. $str = str_replace($v, $this->varvals[$k], $str);
  176. }
  177. return $str;
  178. }
  179. /* public: psubst(string $handle)
  180. * handle: handle of template where variables are to be substituted.
  181. */
  182. function psubst($handle)
  183. {
  184. print $this->subst($handle);
  185. return false;
  186. }
  187. /* public: parse(string $target, string $handle, boolean append)
  188. * public: parse(string $target, array $handle, boolean append)
  189. * target: handle of variable to generate
  190. * handle: handle of template to substitute
  191. * append: append to target handle
  192. */
  193. function parse($target, $handle, $append = false)
  194. {
  195. if (!is_array($handle))
  196. {
  197. $str = $this->subst($handle);
  198. if ($append)
  199. {
  200. $this->set_var($target, $this->get_var($target) . $str);
  201. }
  202. else
  203. {
  204. $this->set_var($target, $str);
  205. }
  206. }
  207. else
  208. {
  209. reset($handle);
  210. while(list($i, $h) = each($handle))
  211. {
  212. $str = $this->subst($h);
  213. $this->set_var($target, $str);
  214. }
  215. }
  216. return $str;
  217. }
  218. function pparse($target, $handle, $append = false)
  219. {
  220. print $this->parse($target, $handle, $append);
  221. return false;
  222. }
  223. /* This is short for finish parse */
  224. function fp($target, $handle, $append = False)
  225. {
  226. return $this->finish($this->parse($target, $handle, $append));
  227. }
  228. /* This is a short cut for print finish parse */
  229. function pfp($target, $handle, $append = False)
  230. {
  231. echo $this->finish($this->parse($target, $handle, $append));
  232. }
  233. /* public: get_vars()
  234. */
  235. function get_vars()
  236. {
  237. reset($this->varkeys);
  238. while(list($k, $v) = each($this->varkeys))
  239. {
  240. $result[$k] = $this->varvals[$k];
  241. }
  242. return $result;
  243. }
  244. /* public: get_var(string varname)
  245. * varname: name of variable.
  246. *
  247. * public: get_var(array varname)
  248. * varname: array of variable names
  249. */
  250. function get_var($varname)
  251. {
  252. if (!is_array($varname))
  253. {
  254. return $this->varvals[$varname];
  255. }
  256. else
  257. {
  258. reset($varname);
  259. while(list($k, $v) = each($varname))
  260. {
  261. $result[$k] = $this->varvals[$k];
  262. }
  263. return $result;
  264. }
  265. }
  266. /* public: get_undefined($handle)
  267. * handle: handle of a template.
  268. */
  269. function get_undefined($handle)
  270. {
  271. if (!$this->loadfile($handle))
  272. {
  273. $this->halt("get_undefined: unable to load $handle.");
  274. return false;
  275. }
  276. preg_match_all("/\{([^}]+)\}/", $this->get_var($handle), $m);
  277. $m = $m[1];
  278. if (!is_array($m))
  279. {
  280. return false;
  281. }
  282. reset($m);
  283. while(list($k, $v) = each($m))
  284. {
  285. if (!isset($this->varkeys[$v]))
  286. {
  287. $result[$v] = $v;
  288. }
  289. }
  290. if (count($result))
  291. {
  292. return $result;
  293. }
  294. else
  295. {
  296. return false;
  297. }
  298. }
  299. /* public: finish(string $str)
  300. * str: string to finish.
  301. */
  302. function finish($str)
  303. {
  304. switch ($this->unknowns)
  305. {
  306. case 'keep':
  307. break;
  308. case 'remove':
  309. $str = preg_replace('/{[^ \t\r\n}]+}/', '', $str);
  310. break;
  311. case 'comment':
  312. $str = preg_replace('/{([^ \t\r\n}]+)}/', "<!-- Template $handle: Variable \\1 undefined -->", $str);
  313. break;
  314. }
  315. return $str;
  316. }
  317. /* public: p(string $varname)
  318. * varname: name of variable to print.
  319. */
  320. function p($varname)
  321. {
  322. print $this->finish($this->get_var($varname));
  323. }
  324. function get($varname)
  325. {
  326. return $this->finish($this->get_var($varname));
  327. }
  328. /***************************************************************************/
  329. /* private: filename($filename)
  330. * filename: name to be completed.
  331. */
  332. function filename($filename,$root='',$time=1)
  333. {
  334. if($root=='')
  335. {
  336. $root=$this->root;
  337. }
  338. if (substr($filename, 0, 1) != '/')
  339. {
  340. $new_filename = $root.'/'.$filename;
  341. }
  342. else
  343. {
  344. $new_filename = $filename;
  345. }
  346. if (!file_exists($new_filename))
  347. {
  348. if($time==2)
  349. {
  350. $this->halt("filename: file $new_filename does not exist.");
  351. }
  352. else
  353. {
  354. //$new_root = str_replace($GLOBALS['phpgw_info']['server']['template_set'],'default',$root);
  355. $new_root = $root;
  356. $new_filename = $this->filename(str_replace($root.'/','',$new_filename),$new_root,2);
  357. }
  358. }
  359. return $new_filename;
  360. }
  361. /* private: varname($varname)
  362. * varname: name of a replacement variable to be protected.
  363. */
  364. function varname($varname)
  365. {
  366. return '{'.$varname.'}';
  367. }
  368. /* private: loadfile(string $handle)
  369. * handle: load file defined by handle, if it is not loaded yet.
  370. */
  371. function loadfile($handle)
  372. {
  373. if (isset($this->varkeys[$handle]) and !empty($this->varvals[$handle]))
  374. {
  375. //echo "1";
  376. //echo $handle;
  377. return true;
  378. }
  379. if (!isset($this->file[$handle]))
  380. {
  381. //echo "2";
  382. //echo $handle;
  383. $this->halt("loadfile: $handle is not a valid handle.");
  384. return false;
  385. }
  386. $filename = $this->file[$handle];
  387. $str = implode('', @file($filename));
  388. if (empty($str))
  389. {
  390. $this->halt("loadfile: While loading $handle, $filename does not exist or is empty.");
  391. return false;
  392. }
  393. $this->set_var($handle, $str);
  394. return true;
  395. }
  396. /***************************************************************************/
  397. /* public: halt(string $msg)
  398. * msg: error message to show.
  399. */
  400. function halt($msg)
  401. {
  402. $this->last_error = $msg;
  403. if ($this->halt_on_error != 'no')
  404. {
  405. $this->haltmsg($msg);
  406. }
  407. if ($this->halt_on_error == 'yes')
  408. {
  409. echo('<b>Halted.</b>');
  410. }
  411. //$GLOBALS['sdi']->common->phpgw_exit(True);
  412. }
  413. /* public, override: haltmsg($msg)
  414. * msg: error message to show.
  415. */
  416. function haltmsg($msg)
  417. {
  418. printf("<b>Template Error:</b> %s<br>\n", $msg);
  419. }
  420. }