00001 <?php
00002 Load::models('cache');
00003
00004
00005
00006
00007
00008
00009
00010 class ViewBase implements IView, ICache {
00011
00012
00013
00014
00015
00016 protected $cached_content = null;
00017
00018
00019
00020
00021
00022 protected $vars = array();
00023
00024
00025
00026
00027
00028 protected $cache_id;
00029
00030
00031
00032
00033
00034 protected $template;
00035
00036 public function __construct($template, $cache_id = '') {
00037 $this->template = $template;
00038 $this->set_cache_id($cache_id);
00039 }
00040
00041
00042
00043
00044
00045
00046
00047
00048 public function create_child_view($template) {
00049 $view = ViewFactory::create_view(IViewFactory::MESSAGE, $template);
00050 $this->copy_to($view);
00051 return $view;
00052 }
00053
00054
00055
00056
00057
00058
00059
00060 public function assign($var, $value) {
00061 $this->vars[$var] = $value;
00062 }
00063
00064
00065
00066
00067
00068
00069 public function assign_array($vars) {
00070 $this->vars = array_merge($this->vars, $vars);
00071 }
00072
00073
00074
00075
00076
00077
00078
00079 public function retrieve($var) {
00080 return Arr::get_item($this->vars, $var, false);
00081 }
00082
00083
00084
00085
00086
00087
00088 public function retrieve_array() {
00089 return $this->vars;
00090 }
00091
00092
00093
00094
00095
00096
00097 public function copy_to($view) {
00098 $view->assign_array($this->vars);
00099 }
00100
00101
00102
00103
00104
00105
00106 public function get_cache_id() {
00107 return $this->cache_id;
00108 }
00109
00110
00111
00112
00113
00114
00115 public function set_cache_id($cacheid) {
00116 $this->cache_id = $cacheid;
00117 }
00118
00119
00120
00121
00122
00123
00124 protected function should_cache() {
00125 return !empty($this->cache_id);
00126 }
00127
00128
00129
00130
00131
00132
00133 public function is_cached() {
00134 return ($this->get_cache_object() !== false);
00135 }
00136
00137
00138
00139
00140
00141
00142 protected function get_cache_object() {
00143 if (is_null($this->cached_content)) {
00144 $this->cached_content = $this->should_cache() ? $this->read_cache($this->cache_id) : false;
00145 }
00146 return $this->cached_content;
00147 }
00148
00149
00150
00151
00152
00153
00154 protected function get_cache_lifetime() {
00155 return 2 * GyroDate::ONE_HOUR;
00156 }
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 public function render($policy = self::NONE) {
00187 $this->render_preprocess($policy);
00188
00189 $ret = '';
00190 $ret = $this->render_from_cache($policy);
00191
00192 if (empty($ret)) {
00193 $ret = $this->do_render($policy);
00194 }
00195 $this->update_cache($ret, $policy);
00196 $this->render_postprocess($ret, $policy);
00197
00198 if (Common::flag_is_set($policy, self::DISPLAY)) {
00199 print $ret;
00200 }
00201 return $ret;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210 protected function render_from_cache($policy) {
00211 $ret = '';
00212 if (!Common::flag_is_set($policy, self::NO_CACHE) && $this->is_cached()) {
00213 $ret = $this->do_render_cache($this->get_cache_object(), $policy);
00214 }
00215 return $ret;
00216 }
00217
00218
00219
00220
00221
00222
00223
00224 protected function update_cache(&$data, $policy) {
00225 if (!Common::flag_is_set($policy, self::NO_CACHE)
00226 && !$this->is_cached()
00227 && $this->should_cache())
00228 {
00229 $this->store_cache($this->cache_id, $data, $this->get_cache_lifetime(), $policy);
00230 }
00231 }
00232
00233
00234
00235
00236
00237
00238
00239 protected function do_render($policy) {
00240 $timer = new Timer();
00241
00242 $this->assign_default_vars($policy);
00243 $ret = '';
00244 $this->before_render($ret, $policy);
00245 $this->render_content($ret, $policy);
00246 $this->after_render($ret, $policy);
00247 return $ret;
00248 }
00249
00250
00251
00252
00253
00254
00255
00256 protected function render_preprocess($policy) {
00257 }
00258
00259
00260
00261
00262
00263
00264
00265
00266 protected function render_postprocess(&$rendered_content, $policy) {
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276 protected function before_render(&$rendered_content, $policy) {
00277 EventSource::Instance()->invoke_event('view_before_render', array('view' => $this, 'policy' => $policy), $rendered_content);
00278 }
00279
00280
00281
00282
00283
00284
00285
00286
00287 protected function after_render(&$rendered_content, $policy) {
00288 EventSource::Instance()->invoke_event('view_after_render', array('view' => $this, 'policy' => $policy), $rendered_content);
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298 protected function render_content(&$rendered_content, $policy) {
00299 $arr_engine = $this->split_template($this->template);
00300 $template_engine = $this->create_template_engine($arr_engine['engine']);
00301 $template_engine->assign_array($this->vars);
00302 $rendered_content = $template_engine->fetch($arr_engine['resource']);
00303 }
00304
00305
00306
00307
00308
00309
00310 protected function split_template($template) {
00311 $arr_ret = array(
00312 'engine' => Config::get_value(Config::DEFAULT_TEMPLATE_ENGINE),
00313 'resource' => $template
00314 );
00315 $pos_colon = is_string($template) ? strpos($template, '::') : false;
00316 if ($pos_colon !== false) {
00317
00318
00319 $arr_ret['engine'] = substr($template, 0, $pos_colon);
00320 $arr_ret['resource'] = substr($template, $pos_colon + 2);
00321 }
00322 return $arr_ret;
00323 }
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 protected function create_template_engine($engine) {
00337 Load::directories('view/templateengines/' . $engine);
00338 require_once dirname(__FILE__) . '/templatepathresolver.cls.php';
00339 $cls = 'TemplateEngine' . ucfirst($engine);
00340 return new $cls();
00341 }
00342
00343
00344
00345
00346
00347
00348
00349
00350 protected function store_cache($cache_key, $content, $lifetime, $policy) {
00351 Cache::store($cache_key, $content, $lifetime);
00352 }
00353
00354
00355
00356
00357
00358
00359
00360 protected function read_cache($cache_key) {
00361 return Cache::read($cache_key);
00362 }
00363
00364
00365
00366
00367
00368
00369
00370
00371 protected function do_render_cache($cache, $policy) {
00372 return ($cache) ? $cache->get_content_plain() : '';
00373 }
00374
00375
00376
00377
00378
00379
00380 protected function assign_default_vars($policy) {
00381 $this->assign('self', $this);
00382 $this->assign('appname', Config::get_value(Config::TITLE));
00383 $this->assign('baseurl', Config::get_value(Config::URL_BASEDIR));
00384 $this->assign('baseurl_ssl', Config::get_url(Config::URL_BASEURL_SAFE));
00385 $this->assign('baseurl_http', Config::get_url(Config::URL_BASEURL));
00386 $this->assign('url_self', Url::current()->clear_query()->build(Url::RELATIVE));
00387 $this->assign('applang', GyroLocale::get_language());
00388 $this->assign('appcharset', GyroLocale::get_charset());
00389
00390 $imageurl = Config::get_url(Config::URL_IMAGES);
00391 $this->assign('imageurl', $imageurl);
00392 if (strpos($imageurl, '//') === false) {
00393 $imageurl = Config::get_url(Config::URL_SERVER) . $imageurl;
00394 }
00395 $this->assign('imageurl_http', $imageurl);
00396 }
00397 }