Repository navigation
Expand file tree
/
Copy pathPvcView.php
More file actions
403 lines (361 loc) · 12.2 KB
/
Copy pathPvcView.php
File metadata and controls
403 lines (361 loc) · 12.2 KB
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
<?php
/**
* Pvc View Class V. 0.9.1
* Part of Pvc, a module for ProcessWire 2.5+
*
* by Oliver Wehn
* https://github.com/oliverwehn
*
* View class extends PW’s native TemplateFile class.
* Don’t modifiy.
*
*/
class PvcView extends TemplateFile {
protected $_controller = null;
/**
* Construct the view from template name
*
* @param PvcController $controller template-specific controller object
*
*/
public function __construct(PvcController $controller) {
$this->set('_controller', $controller);
$fuel = self::getAllFuel();
$this->set('wire', $fuel);
foreach($fuel as $key => $value) $this->set($key, $value);
$page = $this->page;
}
public function ___loadViewFile($action='index') {
$filename = $this->getViewFilename($action);
if(file_exists($filename)) {
$this->setFilename($filename);
return $filename;
}
else {
throw new Wire404Exception(sprintf($this->_('View file "%s" was not found.'), $filename));
}
}
public function ___loadLayoutFile() {
$filename = $this->getLayoutFilename();
if(file_exists($filename)) {
\TemplateFile::__construct($filename);
return $filename;
}
else {
return FALSE;
}
}
public function ___buildScope(Array $actionScope) {
$scope = array();
$properties = array(
$this->getArray(),
);
foreach($properties as $propSet) {
foreach($propSet as $k=>$v) {
if(array_key_exists($k, $scope)) {
$scope[$k] = null;
}
$scope[$k] = $this->$k;
unset($v);
}
}
foreach($actionScope as $k => $v) {
$scope[$k] = $v;
}
return $scope;
}
public function ___importScope(Array $scope) {
return $this->set('scope', $scope);
}
public function ___render($super=false) {
$controller = $this->get('controller');
$action = $controller->calledAction();
if($super) return parent::___render();
$renderer = $this->pvc->getRenderer();
if($actionScope = $action()) {
$actionName = $actionScope['action'];
if($this->loadViewFile($actionName)) {
// $scope = $this->buildScope();
$scope = $this->buildScope($actionScope);
$this->savedDir = getcwd();
chdir(dirname($this->get('filename')));
$out = "\n" . $renderer->render($this, $scope) . "\n";
$options = $this->get('options');
if(count($options['pageStack']) == 0) {
// $layoutName = $this->_controller->get('layout');
// if($layoutName != NULL) {
// $layout = $this->_initLayout($layoutName);
// if($layout->loadLayoutFile()) {
// $scope['outlet'] = $out;
// $layout->importScope($scope);
// $out = $layout->render();
// }
// }
if($layoutFilename = $this->loadLayoutFile()) {
$scope['outlet'] = $out;
$out = $renderer->render($this, $scope, $layoutFilename);
}
}
if($this->savedDir) chdir($this->savedDir);
return $out;
}
return false;
}
else {
throw new WireException(sprintf($this->_('Failed to perform action on controller "' . get_class($controller) . '".')));
}
}
public function get($key) {
switch($key) {
case 'fuel': {
return parent::get($key);
break;
}
case 'filename': {
return parent::get($key);
break;
}
default: {
$method = 'get' . PvcCore::camelcase($key);
if(method_exists($this, $method)) {
return $this->$method();
}
return parent::get($key);
}
}
}
public function set($key, $value) {
switch($key) {
case 'fuel': {
return parent::set($key, $value);
break;
}
default: {
$method = 'set' . PvcCore::camelcase($key);
if(method_exists($this, $method)) {
return $this->$method($value);
}
return parent::set($key, $value);
}
}
}
public function setGlobal($key, $value, $override = false) {
return parent::setGlobal($key, $value, $override);
}
public function getController() {
return $this->_controller;
}
public function setController(PvcController $controller) {
$this->_controller = $controller;
return $this;
}
public function setFilename($filename) {
$options = $this->get('options');
$options['filename'] = $filename;
return parent::setFilename($filename);
}
public function setOptions($options) {
if(array_key_exists('filename', $options)) {
if($options['filename'] === $this->page->template->filename) {
unset($options['filename']);
}
}
if(is_object($this->_controller)) {
$this->_controller->set('options', $options);
}
else {
throw new WireException('Tried to set options on controller that isn’t set up, yet.');
}
return $this;
}
public function getOptions() {
if(is_object($this->_controller)) {
return $this->_controller->get('options');
}
return false;
}
public function ___getViewFilename($action = null) {
if(!$action && $filename = parent::get('filename')) return $filename;
$filename = $this->pvc->paths->views;
$dir = PvcCore::sanitizeFilename(get_class($this));
$filename .= $dir . '/';
$filename .= PvcCore::getFilename($action, 'template');
return $filename;
}
public function ___getLayoutFilename($layout_name=NULL) {
$filename = $this->pvc->paths->layouts;
if(!$layout_name) $layout_name = $this->_controller->get('layout');
$filename .= PvcCore::getFilename($layout_name, 'template');
return $filename;
}
protected function _initLayout($layoutName) {
$class = PvcCore::getClassname($layoutName, 'layout');
if($class && !class_exists($class)) {
$classFile = PvcCore::getFilename($class, 'layout');
$classPath = $this->pvc->paths->layouts . $classFile;
// check if class file exists
if(file_exists($classPath)) {
// yes: include it
require_once($classPath);
}
// check again
if(!class_exists($class)) {
// fall back to creating class on demand
$base_class = PvcCore::getClassname('Pvc', 'layout');
$base_class::extend($class, '$controller');
}
}
// initiate class
$instance = new $class($this->_controller);
return $instance;
}
/*************************************************************************
* View helper definition
*************************************************************************/
public function getViewHelpers($scope) {
$helpers = array(
'snippet' => function($name) use ($scope) {
$renderer = $this->pvc->getRenderer();
$snippetPath = $this->pvc->paths->snippets . strtolower($name) . $this->pvc->ext('snippet');
if($renderer instanceof PvcRenderer) {
return $renderer->render($this, $scope, $snippetPath);
}
return sprintf($this->_('Snippet "%s" can’t be found.'), $snippetPath);
},
'embed' => function($page) use ($scope) {
return $this->_embed($page);
},
'scripts' => function($group) use ($scope) {
return $this->_assets($scope['assets'], 'scripts', $group);
},
'styles' => function($group) use ($scope) {
return $this->_assets($scope['assets'], 'styles', $group);
}
);
if(method_exists($this, 'customViewHelpers')) {
$customHelpers = $this->customViewHelpers($scope);
if(is_array($customHelpers)) {
$helpers = array_merge($customHelpers, $helpers);
}
}
return $helpers;
}
/*************************************************************************
* View helper logic
*************************************************************************/
protected function _embed($page, $options = array()) {
if(is_string($options)) $options = array('action' => $options);
if(is_numeric($page)) {
$page = $this->pages->get($page);
} elseif(is_string($page)) {
if(strpos($page, '=') > 0) {
$page = $this->pages->get($page);
}
elseif(preg_match('#^(\.?/)?[a-z0-9_\-/]+$#', $page, $match)) {
if($match[1] === './' || !$match[1]) {
$it = $this->page->path . ltrim($page, './');
}
else {
$it = $page;
}
$page = $this->pages->get("path={$it}");
if(!$page->id) {
$urlSegments = array();
$maxSegments = $this->config->maxUrlSegments;
if(is_null($maxSegments)) $maxSegments = 4; // default
$cnt = 0;
// if the page isn't found, then check if a page one path level before exists
// this loop allows for us to have both a urlSegment and a pageNum
while((!$page || !$page->id) && $cnt < $maxSegments) {
$it = rtrim($it, '/');
$pos = strrpos($it, '/')+1;
$urlSegment = substr($it, $pos);
$urlSegments[$cnt] = $urlSegment;
$it = substr($it, 0, $pos); // $it no longer includes the urlSegment
$page = $this->pages->get("path=$it, status<" . \Page::statusMax);
$cnt++;
}
$options['route'] = '/' . (count($urlSegments) > 0 ? implode('/', $urlSegments) . '/' : '');
$page = $this->pages->get($page->id);
}
}
else $page = $this->pages->get('name=' . $page);
}
if(!($page instanceof \Page)) return false;
return $page->render($options);
}
public function _assets($assetsArr, $type, $group) {
$type = rtrim($type, 's') . 's';
if(!($markup = $this->pvc->getConfigValue(sprintf('cfg%sMarkup', $this->pvc->camelcase($type))))) return false;
$assets = $this->_extractAssets($assetsArr, $type, $group);
$assetsMarkup = "";
foreach($assets as $path) {
$assetsMarkup .= sprintf($markup, $path);
}
return $assetsMarkup;
}
public function _snippet($snippet_name) {
$snippet_file = $this->pvc->paths->snippets . $snippet_name . $this->ext('snippets');
if(!file_exists($snippet_file)) return FALSE;
if(method_exists($this, '_process_snippet')) return $this->_process_snippet($snippet_file);
else return implode('', file($snippet_file));
}
public function _get_embed($page, $action=null, $vars=null) {
if(!($page instanceof Page)) {
if(!is_string($page)) return FALSE;
if($page = $this->pages->get($page)) return $this->_get_embed($page);
else return FALSE;
}
if($page->template->name == $this->page->template->name) throw new \WireException(sprintf($this->__("Embedding pages of same template into each other prohibited.")));
$page->set('embedded', TRUE)->set('embedded_into', $this->page);
if(is_string($action))
$page->set('force_action', $action);
if(is_array($vars)) {
foreach($vars as $var=>$val)
$page->set($var, $val);
}
return $page->render();
}
protected function _extractAssets(\WireArray $assets, $type, $group=NULL) {
$assetsArray = [];
if($assets->has($type)) {
$assetsOfType = $assets->get($type);
if($group === NULL) {
foreach($assetsOfType as $group => $assetItems) {
$assetsArray = array_merge($assetsArray, $this->_extractAssets($assets, $type, $group));
}
}
else {
if($assetsOfType->has($group)) {
$assetsGroup = $assetsOfType->get($group);
$all = $assetsGroup->getArray();
usort($all, array($this, '_sortByPriority'));
foreach($all as $asset) {
$assetsArray[] = $asset->get('path');
}
}
}
}
return $assetsArray;
}
private function _sortByPriority($a, $b) {
$ap = $a->get('priority');
$bp = $b->get('priority');
if($ap == $bp) return 0;
return $ap < $bp?-1:1;
}
/*************************************************************************
* General helpers
*************************************************************************/
public static function extend($className) {
$classCode = '
class '. $className . ' extends ' . get_called_class() . ' {
public function __constructor(PvcController $controller) {
parent::__constructor($controller);
}
}
';
eval($classCode);
return class_exists($className);
}
}