forked from avdg/ppi-framework-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.php
411 lines (354 loc) · 10.9 KB
/
View.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?php
/**
* @author Paul Dragoonis <dragoonis@php.net>
* @license http://opensource.org/licenses/mit-license.php MIT
* @link www.ppiframework.com
* @package View
*/
class PPI_View {
/**
* The variables to be rendered into the view file
*
* @var array
*/
protected $_viewParams = array();
/**
* The current set view theme
*
* @var null|string
*/
protected $_viewTheme = null;
/**
* The master template file
*
* @var null|string
*/
protected $_masterTemplateFile = null;
/**
* Default renderer, PHP helper
*
* @var string $_defaultRenderer
*/
private $_defaultRenderer = 'php';
/**
* The active renderer.
* This variable is used so we don't have to instantiate the renderer multuple times.
*
* @var null|object
*/
protected $_activeRenderer = null;
/**
* CSS Files to be rendered
*
* @var array
*/
protected $_cssFiles = array();
/**
* Javascript files to be rendered
*
* @var array
*/
protected $_jsFiles = array();
/**
* The constructor
*
* @todo - When this is instantiated, pass it an options array,
* @todo - Get the skeleton app to pass $config->layout->toArray()
* @param array $options The options
*/
public function __construct(array $options = array()) {
if (isset($options['view_theme'])) {
$this->_viewTheme = $options['view_theme'];
}
$this->_config = PPI_Helper::getConfig();
}
/**
* Load function called from controllers
*
* @todo Make this alias to $this->render()
* @todo look into making this dynamic name rather than 'smarty', 'twig', 'php'
* @param string $p_tplFile The template filename
* @param array $p_tplParams Optional user defined params
* @return void
*/
public function load($p_tplFile, array $p_tplParams = array()) {
$this->render($p_tplFile, $p_tplParams);
}
/**
* Add a var to the view params
*
* @param string $p_sKey
* @param mixed $p_mVal
* @return void
*/
public function set($p_sKey, $p_mVal) {
$this->_viewParams[$p_sKey] = $p_mVal;
}
/**
* Add a var to the view params by ref
*
* @param string $p_sKey
* @param mixed &$p_mVal
* @return void
*/
public function setByRef($p_sKey, &$p_mVal) {
$this->_viewParams[$p_sKey] = &$p_mVal;
}
/**
* Add multiple vars to the view params
*
* @param string $p_sKey
* @param array $p_mVal
* @return void
*/
public function setByArray($p_sKey, array $p_mVal) {
$this->_viewParams = array_merge($this->_viewParams, $p_mVal);
}
/**
* Alias for $this->load() but forcing the renderer to smarty
*
* @param string $p_tplFile The template filename
* @param array $p_tplParams Optional user defined params
* @return void
*/
public function loadSmarty($p_tplFile, array $p_tplParams = array()) {
$this->setupRenderer(new PPI_Helper_Template_Smarty(), $p_tplFile, $p_tplParams);
}
/**
* Override the current set theme
*
* @param string $p_sThemeName
* @return void
*/
public function theme($p_sThemeName) {
$this->_viewTheme = $p_sThemeName;
}
/**
* Get the currently set view theme
*
* @return string
*/
protected function getViewTheme() {
if (null === $this->_viewTheme) {
$this->_viewTheme = $this->_config->layout->view_theme;
}
return $this->_viewTheme;
}
/**
* Initialisation for the renderer, assignment of default values, boot up of the master template
*
* @param PPI_Interface_Template $oTpl Templating renderer. Instance of PPI_Interface_Template
* @param string $p_tplFile The template file to render
* @param array $p_tplParams Optional user defined parameres
* @return mixed
*/
public function setupRenderer(PPI_Interface_Template $oTpl, $p_tplFile, array $params = array(), array $options = array()) {
// Default View Values
foreach ($params as $key => $val) {
$oTpl->assign($key, $val);
}
$p_tplFile = PPI_Helper::checkExtension($p_tplFile, $oTpl->getTemplateExtension());
// View Directory Preparation By Theme
$sViewDir = $this->getViewDir();
// Get the default view vars that come when you load a view page.
$defaultViewVars = $this->getDefaultRenderValues(array(
'viewDir' => $sViewDir,
'actionFile' => $p_tplFile
));
foreach ($defaultViewVars as $varName => $viewVar) {
$oTpl->assign($varName, $viewVar);
}
/*
// Flash Messages
if(!isset($this->_config->layout->useMessageFlash) ||
($this->_config->layout->useMessageFlash && $this->_config->layout->useMessageFlash == true)) {
}
*/
// The Scenarios where we only render the individual template and not the 'master template'
$nativeAjax = isset($params['core']['is']['ajax']) && $params['core']['is']['ajax'];
$overrideAjax = isset($params['isAjax']) && $params['isAjax'];
$fullLayoutDisable = isset($options['fullLayout']) && $options['fullLayout'] === false;
$partialLayout = isset($options['partial']) && $options['partial'] === true;
if($nativeAjax || $overrideAjax || $fullLayoutDisable || $partialLayout) {
$template = $p_tplFile;
} else {
// Master template
$template = $this->_masterTemplateFile !== null ? $this->_masterTemplateFile : $oTpl->getDefaultMasterTemplate();
$template = PPI_Helper::checkExtension($template, $oTpl->getTemplateExtension());
}
// Are we loading a template from the cache?
if(isset($options['cache'], $options['cacheHandler']) && $options['cache']) {
if(!$options['cacheHandler'] instanceof PPI_Cache_Interface) {
throw new PPI_Exception('Unable to use cache handler, it does not implement PPI_Cache_Interface');
}
// If our template exists in the cache
if($this->cachedRenderExists($template, $options)) {
return $this->getCachedRender($template, $options);
}
// Generate our cachename
$cacheName = $this->createCachedRenderKey($template, $options);
ob_start();
$oTpl->render($template);
$content = ob_get_contents();
ob_end_clean();
$ttl = isset($options['cacheTTL']) ? $options['cacheTTL'] : 0;
$options['cacheHandler']->set($cacheName, $content, $ttl);
return $content;
}
// Lets render baby !!
$oTpl->render($template);
}
/**
* Get the path to the view file dir
*
* @return string
*/
public function getViewDir() {
return APPFOLDER . 'View/' . $this->getViewTheme() . '/';
}
/**
* Obtain the list of default view variables
*
* @todo review making var names not HNC prefixed.
* @param array $options
* @return array
*/
public function getDefaultRenderValues(array $options) {
$authData = PPI_Helper::getSession()->getAuthData();
$request = array('controller' => '', 'method' => '');
$registry = PPI_Helper::getRegistry();
// Sometimes a render is forced before the PPI_Dispatch object has finished instantiating
// For example if a 404 is thrown inside the routing/dispatch process then this scenario occurs.
if ($registry->exists('PPI_Dispatch')) {
$oDispatch = PPI_Helper::getDispatcher();
$request = array(
'controller' => $oDispatch->getControllerName(),
'method' => $oDispatch->getMethodName()
);
}
/*
if($registry->exists('PPI_Request')) {
$oRequest = $registry->get('PPI_Request');
}
*/
return array(
'isLoggedIn' => !empty($authData),
'config' => $this->_config,
'request' => $request,
'authData' => $authData,
'baseUrl' => $this->_config->system->base_url,
'fullUrl' => PPI_Helper::getFullUrl(),
'currUrl' => PPI_Helper::getCurrUrl(),
'viewDir' => $options['viewDir'],
'actionFile' => $options['actionFile'],
'responseCode' => PPI_Helper::getRegistry()->get('PPI_View::httpResponseCode', 200),
'authInfo' => $authData, // Do not use, just BC stuff
'aAuthInfo' => $authData, // Do not use, just BC stuff.
'bIsLoggedIn' => !empty($authData), // Do not use, just BC stuff
'oConfig' => $this->_config, // Do not use, just BC stuff
);
}
/**
* To get a view variable that is set to get rendered. (TBC)
*
* @param string $key The Key
* @return mixed
*/
public function get($key) {
if (isset($this->_viewParams[$key])) {
return $this->_viewParams[$key];
}
throw new PPI_Exception('Unable to find View Key: ' . $key);
}
/**
* Override the default template file, with optional include for the .php or .tpl extension
*
* @param string $p_sNewTemplateFile New Template Filename
* @return void
*/
public function setTemplateFile($p_sNewTemplateFile) {
$this->_masterTemplateFile = $p_sNewTemplateFile;
}
/**
* The internal render function, this is called by $this->load('template');
*
* @param string $template The template name to render
* @param array $params Optional Parameters
* @param array $options Optional Options
* @return void
*/
public function render($template, array $params = array(), array $options = array()) {
$sRenderer = empty($this->_config->layout->renderer) ? $this->_defaultRenderer : $this->_config->layout->renderer;
return $this->setupRenderer($this->getRenderer($sRenderer), $template, array_merge($params, $this->_viewParams), $options);
}
/**
* Get the active renderer
*
* @param string $rendererName The renderer name
* @return object
*/
protected function getRenderer($rendererName = '') {
if($this->_activeRenderer !== null) {
return $this->_activeRenderer;
}
switch ($rendererName) {
case 'smarty':
$this->_activeRenderer = new PPI_Helper_Template_Smarty();
break;
case 'twig':
$this->_activeRenderer = new PPI_Helper_Template_Twig();
break;
case 'php':
default:
$this->_activeRenderer = new PPI_Helper_Template_PHP();
break;
}
return $this->_activeRenderer;
}
/**
* Check if a template exists
*
* @param string $templateName The template Name
* @return bool
*/
public function templateExists($templateName) {
$sRenderer = empty($this->_config->layout->renderer) ? $this->_defaultRenderer : $this->_config->layout->renderer;
return $this->getRenderer($sRenderer)->templateExists($templateName);
}
/**
* Check if a cachedRender item exists in the cache
*
* @param string $template
* @param array $options
* @return boolean
*/
public function cachedRenderExists($template, array $options = array()) {
$cacheName = $this->createCachedRenderKey($template, $options);
return $options['cacheHandler']->exists($cacheName);
}
/**
* Get the cached render file from the cache
*
* @param $template
* @param array $options
* @return string
*/
public function getCachedRender($template, array $options = array()) {
$cacheName = $this->createCachedRenderKey($template, $options);
if($options['cacheHandler']->exists($cacheName)) {
return $options['cacheHandler']->get($cacheName);
}
return '';
}
/**
* Create a cache key for our cached template
*
* @param string $template
* @param array $options
* @return string
*/
public function createCachedRenderKey($template, array $options = array()) {
return (isset($options['cachePrefix']) ? $options['cachePrefix'] : '') . 'ppi_cached_template_'
. str_replace(array('\\', '/'), '_', $template);
}
}