1 |
joko |
1.1 |
<? |
2 |
|
|
// ------------------------------------------------------------------------- |
3 |
|
|
// $Id$ |
4 |
|
|
// ------------------------------------------------------------------------- |
5 |
|
|
// $Log$ |
6 |
|
|
// ------------------------------------------------------------------------- |
7 |
|
|
|
8 |
|
|
class Site_Template { |
9 |
|
|
|
10 |
|
|
var $meta; |
11 |
|
|
|
12 |
|
|
function start() { |
13 |
|
|
//print "START<br>"; |
14 |
|
|
} |
15 |
|
|
|
16 |
|
|
function _register_langtext_safe() { |
17 |
|
|
if (!$this->meta[registered][lt]) { |
18 |
|
|
$this->meta[registered][lt] = 1; |
19 |
|
|
$this->_register_langtext(); |
20 |
|
|
} |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
function _register_langtext() { |
24 |
|
|
//print "REGISTER<br>"; |
25 |
|
|
// register 'lt' (LangText) template provider |
26 |
|
|
$this->site->smarty->register_resource( |
27 |
|
|
"lt", |
28 |
|
|
array( |
29 |
|
|
"db_get_template", |
30 |
|
|
"db_get_timestamp" |
31 |
|
|
) |
32 |
|
|
); |
33 |
|
|
/* |
34 |
|
|
array( |
35 |
|
|
"db_get_template", |
36 |
|
|
"db_get_timestamp", |
37 |
|
|
"db_get_secure", |
38 |
|
|
"db_get_trusted" |
39 |
|
|
) |
40 |
|
|
*/ |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
function display($identifier, $options) { |
44 |
|
|
//print "DISPLAY<br>"; |
45 |
|
|
$this->_build($identifier, $options); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
function get($identifier, $interpolate = array()) { |
49 |
|
|
//print "GET<br>"; |
50 |
|
|
// TODO: review... what about the cache-key here? use volatile by default or not? |
51 |
|
|
// for now: per default to be _very_ safe.... ;) |
52 |
|
|
$options = array( |
53 |
|
|
'interpolate' => $interpolate, |
54 |
|
|
'return' => 1, |
55 |
|
|
'cache_key' => 'volatile' |
56 |
|
|
); |
57 |
|
|
// 'cache_key' => 'volatile' |
58 |
|
|
return $this->_build($identifier, $options); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
function _build($identifier, $options) { |
62 |
|
|
|
63 |
|
|
//print "BUILD identifier $identifier<br>"; |
64 |
|
|
//print "options:<br>" . Dumper($options) . "<br>"; |
65 |
|
|
|
66 |
|
|
$this->_register_langtext_safe(); |
67 |
|
|
|
68 |
|
|
// TODO: just do '$this->site->smarty->assign($arguments[vars]);' ??? |
69 |
|
|
/* |
70 |
|
|
foreach ($arguments[vars] as $name => $value) { |
71 |
|
|
$this->site->smarty->assign($name, $value); |
72 |
|
|
} |
73 |
|
|
*/ |
74 |
|
|
|
75 |
|
|
// template variable interpolation |
76 |
|
|
if ($options['interpolate']) { |
77 |
|
|
//print Dumper($options['interpolate']); |
78 |
|
|
$this->site->smarty->assign($options['interpolate']); |
79 |
|
|
/* |
80 |
|
|
foreach ($options['interpolate'] as $key => $val) { |
81 |
|
|
print "key: $key - val $val<br>"; |
82 |
|
|
$this->site->smarty->assign($key, $val); |
83 |
|
|
} |
84 |
|
|
*/ |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
//print "cached: " . $this->site->smarty->is_cached($identifier) . "<br>"; |
88 |
|
|
|
89 |
|
|
// TODO: refactor this! |
90 |
|
|
// $smarty_method = 'fetch|display' |
91 |
|
|
// $cache_key??? |
92 |
|
|
if ($options['return']) { |
93 |
|
|
|
94 |
|
|
//print "RETURN<br>"; |
95 |
|
|
|
96 |
|
|
// return template |
97 |
|
|
// use of cache-key per template |
98 |
|
|
if ($cache_key = $options['cache_key']) { |
99 |
|
|
//print "1: $cache_key<br>"; |
100 |
|
|
if ($cache_key == 'volatile') { |
101 |
|
|
//$this->site->smarty->clear_cache($identifier, $cache_key); |
102 |
|
|
//$this->site->smarty->clear_cache('referer.php'); |
103 |
|
|
} |
104 |
|
|
$result = $this->site->smarty->fetch($identifier, $cache_key); |
105 |
|
|
} else { |
106 |
|
|
//print "2<br>"; |
107 |
|
|
$result = $this->site->smarty->fetch($identifier); |
108 |
|
|
} |
109 |
|
|
//print "result: $result<br>"; |
110 |
|
|
|
111 |
|
|
} else { |
112 |
|
|
|
113 |
|
|
// display template |
114 |
|
|
// use of cache-key per template |
115 |
|
|
if ($cache_key = $options['cache_key']) { |
116 |
|
|
$result = $this->site->smarty->display($identifier, $cache_key); |
117 |
|
|
} else { |
118 |
|
|
$result = $this->site->smarty->display($identifier); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
return $result; |
124 |
|
|
|
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
|
130 |
|
|
function db_get_template($tpl_name, &$tpl_source, &$smarty_obj) |
131 |
|
|
{ |
132 |
|
|
//print "db_get_template: '$tpl_name', '$tpl_source', '$smarty_obj'<br>"; |
133 |
|
|
//return $this->site->lt->getlt($tpl_name); |
134 |
|
|
global $site; |
135 |
|
|
if ($template = $site->lt->getlt($tpl_name)) { |
136 |
|
|
//print "template: '$template'<br>"; |
137 |
|
|
$tpl_source = $template; |
138 |
|
|
return true; |
139 |
|
|
} |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj) |
143 |
|
|
{ |
144 |
|
|
$tpl_timestamp = time(); |
145 |
|
|
return true; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
|
149 |
|
|
/* |
150 |
|
|
|
151 |
|
|
// ---------------------------------------------------------------------------------- |
152 |
|
|
// from: http://smarty.php.net/manual/en/templates.from.elsewhere.html |
153 |
|
|
// ---------------------------------------------------------------------------------- |
154 |
|
|
|
155 |
|
|
|
156 |
|
|
// from PHP script |
157 |
|
|
|
158 |
|
|
// put these function somewhere in your application |
159 |
|
|
function db_get_template ($tpl_name, &tpl_source, &$smarty_obj) |
160 |
|
|
{ |
161 |
|
|
// do database call here to fetch your template, |
162 |
|
|
// populating $tpl_source |
163 |
|
|
$sql = new SQL; |
164 |
|
|
$sql->query("select tpl_source |
165 |
|
|
from my_table |
166 |
|
|
where tpl_name='$tpl_name'"); |
167 |
|
|
if ($sql->num_rows) { |
168 |
|
|
$tpl_source = $sql->record['tpl_source']; |
169 |
|
|
return true; |
170 |
|
|
} else { |
171 |
|
|
return false; |
172 |
|
|
} |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj) |
176 |
|
|
{ |
177 |
|
|
// do database call here to populate $tpl_timestamp. |
178 |
|
|
$sql = new SQL; |
179 |
|
|
$sql->query("select tpl_timestamp |
180 |
|
|
from my_table |
181 |
|
|
where tpl_name='$tpl_name'"); |
182 |
|
|
if ($sql->num_rows) { |
183 |
|
|
$tpl_timestamp = $sql->record['tpl_timestamp']; |
184 |
|
|
return true; |
185 |
|
|
} else { |
186 |
|
|
return false; |
187 |
|
|
} |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
function db_get_secure($tpl_name, &$smarty_obj) |
191 |
|
|
{ |
192 |
|
|
// assume all templates are secure |
193 |
|
|
return true; |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
function db_get_trusted($tpl_name, &$smarty_obj) |
197 |
|
|
{ |
198 |
|
|
// not used for templates |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
// register the resource name "db" |
202 |
|
|
$smarty->register_resource("db", array("db_get_template", |
203 |
|
|
"db_get_timestamp", |
204 |
|
|
"db_get_secure", |
205 |
|
|
"db_get_trusted")); |
206 |
|
|
|
207 |
|
|
// using resource from php script |
208 |
|
|
$smarty->display("db:index.tpl"); |
209 |
|
|
|
210 |
|
|
{* using resource from within Smarty template *} |
211 |
|
|
{include file="db:/extras/navigation.tpl"} |
212 |
|
|
|
213 |
|
|
// ---------------------------------------------------------------------------------- |
214 |
|
|
|
215 |
|
|
*/ |
216 |
|
|
|
217 |
|
|
|
218 |
|
|
?> |