| 1 | joko | 1.1 | <? | 
| 2 |  |  | //    ------------------------------------------------------------------------- | 
| 3 | joko | 1.4 | //    $Id: Template.php,v 1.3 2004/05/13 19:16:45 jonen Exp $ | 
| 4 | joko | 1.1 | //    ------------------------------------------------------------------------- | 
| 5 | joko | 1.2 | //    $Log: Template.php,v $ | 
| 6 | joko | 1.4 | //    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 | jonen | 1.3 | //    Revision 1.2  2002/12/19 10:33:49  joko | 
| 10 |  |  | //    + debugging and bugfixing | 
| 11 |  |  | // | 
| 12 | joko | 1.2 | //    Revision 1.1  2002/12/19 06:20:39  joko | 
| 13 |  |  | //    + initial check-in | 
| 14 |  |  | // | 
| 15 | joko | 1.1 | //    ------------------------------------------------------------------------- | 
| 16 | joko | 1.2 |  | 
| 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 | joko | 1.4 | ); | 
| 44 | joko | 1.2 | } | 
| 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 | joko | 1.1 | // TODO: just do '$this->site->smarty->assign($arguments[vars]);' ??? | 
| 72 | joko | 1.2 | /* | 
| 73 | joko | 1.1 | foreach ($arguments[vars] as $name => $value) { | 
| 74 |  |  | $this->site->smarty->assign($name, $value); | 
| 75 | joko | 1.2 | } | 
| 76 |  |  | */ | 
| 77 |  |  |  | 
| 78 |  |  | // template variable interpolation | 
| 79 |  |  | if ($options['interpolate']) { | 
| 80 |  |  | $this->site->smarty->assign($options['interpolate']); | 
| 81 |  |  | } | 
| 82 |  |  |  | 
| 83 | joko | 1.1 | //print "cached: " . $this->site->smarty->is_cached($identifier) . "<br>"; | 
| 84 | joko | 1.2 |  | 
| 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 | joko | 1.1 | // use of cache-key per template | 
| 94 |  |  | if ($cache_key = $options['cache_key']) { | 
| 95 | joko | 1.2 | //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 | joko | 1.1 | $result = $this->site->smarty->fetch($identifier, $cache_key); | 
| 101 |  |  | } else { | 
| 102 | joko | 1.2 | //print "2<br>"; | 
| 103 | joko | 1.1 | $result = $this->site->smarty->fetch($identifier); | 
| 104 | joko | 1.2 | } | 
| 105 |  |  | //print "result: $result<br>"; | 
| 106 |  |  |  | 
| 107 | joko | 1.1 | } else { | 
| 108 |  |  |  | 
| 109 | joko | 1.2 | // display template | 
| 110 | joko | 1.1 | // 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 | joko | 1.2 | } | 
| 116 |  |  |  | 
| 117 |  |  | } | 
| 118 |  |  |  | 
| 119 | joko | 1.1 | return $result; | 
| 120 | joko | 1.2 |  | 
| 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 | jonen | 1.3 | // 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 | joko | 1.2 | $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 | joko | 1.4 | 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 | joko | 1.2 |  | 
| 158 |  |  | /* | 
| 159 |  |  |  | 
| 160 |  |  | // ---------------------------------------------------------------------------------- | 
| 161 | joko | 1.4 | // 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 | joko | 1.2 | // ---------------------------------------------------------------------------------- | 
| 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 | joko | 1.1 | ?> |