2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright ©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 (TemplateFs.php)
28 require_once('ISSO/Template.php');
29 require_once('ISSO/Functions.php');
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 ©2002 - [#]year[#], Blue Static
48 class BSTemplateFs
extends BSTemplate
51 * The path, from the path of the application, where templates are stored
54 private $templateDir = '';
57 * The extension all the template files have
60 private $extension = 'tpl';
63 * The database table name for the template cache
66 private $dbCacheTable = null;
68 // ###################################################################
70 * Constructor (overriding so we don't require the Db module)
72 public function __construct() {}
74 // ###################################################################
76 * Sets the template directory name
78 * @param string Template directory name
80 public function setTemplateDirectory($dir)
82 $this->templateDir
= BSFunctions
::FetchSourcePath($dir);
85 // ###################################################################
87 * Sets the file extension for the templates
89 * @param string File extension
91 public function setExtension($ext)
93 $this->extension
= $ext;
96 // ###################################################################
98 * Sets the name of the table to access for the datbase cache
100 * @param string DB table name
102 public function setDatabaseCache($table)
104 $this->dbCacheTable
= $table;
107 // ###################################################################
109 * Takes an array of template names, loads them, and then stores a
110 * parsed version for optimum speed.
112 * @param array List of template names to be cached
114 public function cache($namearray)
116 if (sizeof($this->cache
) > 0)
118 trigger_error('You cannot cache templates more than once per initialization');
123 if ($this->dbCacheTable
)
125 $db =& BSRegister
::GetType('Db');
126 $cache = $db->query("SELECT * FROM {$this->dbCacheTable} WHERE filename IN ('" . implode("', '", $namearray) . "')");
127 while ($tpl = $db->fetchArray($cache))
129 $time = filemtime(BSRegister
::GetAppPath() . $this->templateDir
. $tpl['filename'] . '.' . $this->extension
);
130 $template = $tpl['template'];
131 if ($time > $tpl['timestamp'])
133 $template = $this->_parseTemplate($this->_loadTemplate($tpl['filename']));
134 $db->query("UPDATE {$this->dbCacheTable} SET template = '" . $db->escapeString($template) . "', timestamp = " . TIMENOW
. " WHERE filename = '" . $tpl['filename'] . "'");
135 $tpl['template'] = $template;
137 $dbCache["$tpl[filename]"] = $template;
140 foreach ($namearray AS $name)
142 if ($this->dbCacheTable
)
144 if (isset($dbCache["$name"]))
146 $template = $dbCache["$name"];
150 $template = $this->_parseTemplate($this->_loadTemplate($name));
151 $db->query("INSERT INTO {$this->dbCacheTable} (filename, template, timestamp) VALUES ('$name', '" . $db->escapeString($template) . "', " . TIMENOW
. ")");
156 $template = $this->_parseTemplate($this->_loadTemplate($name));
159 $this->cache
["$name"] = $template;
160 $this->usage["$name"] = 0;
165 // ###################################################################
167 * Loads a template from the file system from the specified
168 * $templatedir with the file extension $extension
170 * @param string The name of the template call
172 protected function _loadTemplate($name)
174 $path = BSRegister
::GetAppPath() . $this->templateDir
. $name . '.' . $this->extension
;
177 if (($template = @file_get_contents($path)) !== false)
183 trigger_error("Could not load the template '$path'");
189 trigger_error("Could not load the template
'$path'");
195 /*=====================================================================*\
196 || ###################################################################
199 || ###################################################################
200 \*=====================================================================*/