2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2002-[#]year[#] Blue Static
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
23 * File System Template System
29 $this->load('template', null);
32 * File System Template System
34 * This framework merely replaces the template loading functions with
35 * file-system based ones. It has an optional caching system in which
36 * template data will remain stored in the database as long as the filesystem
37 * file is modified. To do this, pass a table name to setDatabaseCache() and make sure
38 * there's a DB module that has access to a table with this schema:
40 * CREATE TABLE template (filename VARCHAR (250) NOT NULL, template TEXT NOT NULL, timestamp INT NOT NULL);
43 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
48 class Template_FS
extends Template
51 * The path, from the path of the application, where templates are stored
55 var $templatedir = '';
58 * The extension all the template files have
62 var $extension = 'tpl';
65 * The database table name for the template cache
69 var $dbCacheTable = null;
71 // ###################################################################
75 function __construct(&$registry)
77 $this->registry
=& $registry;
80 // ###################################################################
84 function Template_FS(&$registry)
86 $this->__construct($registry);
89 // ###################################################################
91 * Sets the template directory
95 * @param string The template directory
97 function setTemplateDir($path)
99 $this->templatedir
= $this->registry
->fetch_sourcepath($path);
102 // ###################################################################
104 * Sets the file extension
108 * @param string File extension
110 function setExtension($ext)
112 $this->extension
= $ext;
115 // ###################################################################
117 * Sets the name of the table to access for the datbase cache
119 * @param string DB table name
121 function setDatabaseCache($table)
123 $this->dbCacheTable
= $table;
126 // ###################################################################
128 * Takes an array of template names, loads them, and then stores a
129 * parsed version for optimum speed.
133 * @param array List of template names to be cached
135 function cache($namearray)
137 if (sizeof($this->cache
) > 0)
139 trigger_error('You cannot cache templates more than once per initialization', E_USER_WARNING
);
144 if ($this->dbCacheTable
)
146 $db =& $this->registry
->modules
[ISSO_DB_LAYER
];
147 $cache = $db->query("SELECT * FROM {$this->dbCacheTable} WHERE filename IN ('" . implode("', '", $namearray) . "')");
148 while ($tpl = $db->fetch_array($cache))
150 $time = filemtime($this->registry
->apppath
. $this->templatedir
. $tpl['filename'] . '.' . $this->extension
);
151 $template = $tpl['template'];
152 if ($time > $tpl['timestamp'])
154 $template = $this->_parse($this->_load($tpl['filename']));
155 $db->query("UPDATE {$this->dbCacheTable} SET template = '" . $db->escape_string($template) . "', timestamp = " . TIMENOW
. " WHERE filename = '" . $tpl['filename'] . "'");
156 $tpl['template'] = $template;
158 $dbCache["$tpl[filename]"] = $template;
161 foreach ($namearray AS $name)
163 if (isset($dbCache["$name"]))
165 $template = $dbCache["$name"];
169 $template = $this->_parse($this->_load($name));
170 if ($this->dbCacheTable
)
172 $db->query("INSERT INTO {$this->dbCacheTable} (filename, template, timestamp) VALUES ('$name', '" . $db->escape_string($template) . "', " . TIMENOW
. ")");
175 $this->cache
["$name"] = $template;
176 $this->usage["$name"] = 0;
181 // ###################################################################
183 * Loads a template from the file system from the specified
184 * $templatedir with the file extension $extension
188 * @param string The name of the template call
190 function _load($name)
192 $path = $this->registry
->apppath
. $this->templatedir
. $name . '.' . $this->extension
;
195 if (($template = @file_get_contents($path)) !== false)
201 trigger_error("Could not load the template '$path'", E_USER_ERROR);
207 trigger_error("Could not load the template
'$path'", E_USER_ERROR);