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