1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Controller |
4
|
|
|
* Base request handler, All controllers should be a child this class |
5
|
|
|
* or one of the subclasses (e.g. AjaxController or ApiController). |
6
|
|
|
* |
7
|
|
|
* @package erdiko/core |
8
|
|
|
* @copyright 2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com |
9
|
|
|
* @author John Arroyo <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
namespace erdiko\core; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class Controller |
15
|
|
|
{ |
16
|
|
|
/** Response */ |
17
|
|
|
protected $_response; |
18
|
|
|
/** Webroot */ |
19
|
|
|
protected $_webroot; |
20
|
|
|
/** Theme name */ |
21
|
|
|
protected $_themeName = null; |
22
|
|
|
/** Request URI */ |
23
|
|
|
protected $_pathInfo = null; |
24
|
|
|
/** Title (body title) */ |
25
|
|
|
protected $_title = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructor |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
$this->_webroot = ERDIKO_ROOT; |
33
|
|
|
$this->_response = new \erdiko\core\Response; |
34
|
|
|
|
35
|
|
|
if ($this->_themeName != null) { |
36
|
|
|
$this->_response->setThemeName($this->_themeName); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Before action hook |
42
|
|
|
* Anything here gets called immediately BEFORE the Action method runs. |
43
|
|
|
*/ |
44
|
|
|
public function _before() |
45
|
|
|
{ |
46
|
|
|
// Set up the theme object |
47
|
|
|
$this->prepareTheme(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* After action hook |
52
|
|
|
* Anything here gets called immediately AFTER the Action method runs. |
53
|
|
|
*/ |
54
|
|
|
public function _after() |
55
|
|
|
{ |
56
|
|
|
// do something... |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get action |
61
|
|
|
* |
62
|
|
|
* @param mixed $var |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function get($var = null) |
66
|
|
|
{ |
67
|
|
|
if (!empty($var)) { |
68
|
|
|
// load action based off of naming conventions |
69
|
|
|
return $this->_autoaction($var, 'get'); |
70
|
|
|
|
71
|
|
|
} else { |
72
|
|
|
return $this->getIndex(); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Put action |
78
|
|
|
* |
79
|
|
|
* @param mixed $var |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
public function put($var = null) |
83
|
|
|
{ |
84
|
|
|
if (!empty($var)) { |
85
|
|
|
// load action based off of naming conventions |
86
|
|
|
return $this->_autoaction($var, 'put'); |
87
|
|
|
|
88
|
|
|
} else { |
89
|
|
|
return $this->getIndex(); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Post action |
95
|
|
|
* |
96
|
|
|
* @param mixed $var |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
public function post($var = null) |
100
|
|
|
{ |
101
|
|
|
if (!empty($var)) { |
102
|
|
|
// load action based off of naming conventions |
103
|
|
|
return $this->_autoaction($var, 'post'); |
104
|
|
|
|
105
|
|
|
} else { |
106
|
|
|
return $this->getIndex(); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Delete action |
112
|
|
|
* |
113
|
|
|
* @param mixed $var |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
|
public function delete($var = null) |
117
|
|
|
{ |
118
|
|
|
if (!empty($var)) { |
119
|
|
|
// load action based off of naming conventions |
120
|
|
|
return $this->_autoaction($var, 'delete'); |
121
|
|
|
|
122
|
|
|
} else { |
123
|
|
|
return $this->getIndex(); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set Path Info (Requested URI) |
129
|
|
|
*/ |
130
|
|
|
public function setPathInfo($pathInfo) |
131
|
|
|
{ |
132
|
|
|
$this->_pathInfo = $pathInfo; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Prepare theme |
137
|
|
|
*/ |
138
|
|
|
public function prepareTheme() |
139
|
|
|
{ |
140
|
|
|
$this->getResponse()->setTheme(new \erdiko\core\Theme($this->getThemeName())); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set the theme name in the response object |
145
|
|
|
* |
146
|
|
|
* @param string $name, the name/id of the theme |
|
|
|
|
147
|
|
|
*/ |
148
|
|
|
public function setThemeName($name) |
149
|
|
|
{ |
150
|
|
|
$this->getResponse()->setThemeName($name); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Set the theme name in both the response and the theme objects |
155
|
|
|
* |
156
|
|
|
* @param string $name, the name/id of the theme |
|
|
|
|
157
|
|
|
*/ |
158
|
|
|
public function setTheme($name) |
159
|
|
|
{ |
160
|
|
|
$this->getResponse()->setThemeName($name); |
161
|
|
|
$this->getResponse()->getTheme()->setName($name); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get the theme name |
166
|
|
|
* |
167
|
|
|
* @return string $name |
168
|
|
|
*/ |
169
|
|
|
public function getThemeName() |
170
|
|
|
{ |
171
|
|
|
return $this->getResponse()->getThemeName(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Set the theme template used to render the page |
176
|
|
|
* @param string $template |
177
|
|
|
*/ |
178
|
|
|
public function setThemeTemplate($template) |
179
|
|
|
{ |
180
|
|
|
$this->getResponse()->setThemeTemplate($template); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get Response object |
185
|
|
|
* |
186
|
|
|
* @return ResponseObject |
187
|
|
|
*/ |
188
|
|
|
public function getResponse() |
189
|
|
|
{ |
190
|
|
|
return $this->_response; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Send |
195
|
|
|
*/ |
196
|
|
|
final public function send() |
197
|
|
|
{ |
198
|
|
|
echo $this->getResponse()->render(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Set Response key/value data |
203
|
|
|
* formerly called, setResponseDataValue() |
204
|
|
|
* |
205
|
|
|
* @param string $key |
206
|
|
|
* @param mixed $value |
207
|
|
|
*/ |
208
|
|
|
public function setResponseKeyValue($key, $value) |
209
|
|
|
{ |
210
|
|
|
$this->getResponse()->setKeyValue($key, $value); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get Response data value |
215
|
|
|
* |
216
|
|
|
* @param string $key |
217
|
|
|
*/ |
218
|
|
|
public function getResponseKeyValue($key) |
219
|
|
|
{ |
220
|
|
|
return $this->getResponse()->getKeyValue($key); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Add page title text to current page |
225
|
|
|
* |
226
|
|
|
* @param string $title |
227
|
|
|
*/ |
228
|
|
|
public function setPageTitle($title) |
229
|
|
|
{ |
230
|
|
|
$this->getResponse()->getTheme()->setPageTitle($title); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Set page content title to be themed in the view |
235
|
|
|
* |
236
|
|
|
* @param string $title |
237
|
|
|
*/ |
238
|
|
|
public function setBodyTitle($title) |
239
|
|
|
{ |
240
|
|
|
$this->getResponse()->getTheme()->setBodyTitle($title); |
241
|
|
|
$this->_title = $title; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Get page content title to be themed in a layout or view |
246
|
|
|
* |
247
|
|
|
* @param string $title |
|
|
|
|
248
|
|
|
*/ |
249
|
|
|
public function getBodyTitle() |
250
|
|
|
{ |
251
|
|
|
return $this->getResponse()->getTheme()->getBodyTitle(); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Set both the title (header) and page title (body) at the same time |
256
|
|
|
* @param string $title |
257
|
|
|
*/ |
258
|
|
|
public function setTitle($title) |
259
|
|
|
{ |
260
|
|
|
$this->setBodyTitle($title); |
261
|
|
|
$this->setPageTitle($title); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Set both the title (header) and page title (body) at the same time |
266
|
|
|
* @param string $title |
|
|
|
|
267
|
|
|
*/ |
268
|
|
|
public function getTitle() |
269
|
|
|
{ |
270
|
|
|
return $this->_title; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Set the response content |
275
|
|
|
* |
276
|
|
|
* @param string $content |
277
|
|
|
*/ |
278
|
|
|
public function setContent($content) |
279
|
|
|
{ |
280
|
|
|
$this->getResponse()->setContent($content); |
|
|
|
|
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Add/append html text to the response content |
285
|
|
|
* |
286
|
|
|
* @param string @content |
287
|
|
|
*/ |
288
|
|
|
public function appendContent($content) |
289
|
|
|
{ |
290
|
|
|
$this->getResponse()->appendContent($content); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Autoaction |
295
|
|
|
* |
296
|
|
|
* @param string $var |
297
|
|
|
* @param string $httpMethod |
298
|
|
|
*/ |
299
|
|
|
protected function _autoaction($var, $httpMethod = 'get') |
300
|
|
|
{ |
301
|
|
|
$method = $this->urlToActionName($var, $httpMethod); |
302
|
|
|
if (method_exists($this, $method)) { |
303
|
|
|
return $this->$method(); |
304
|
|
|
} else { |
305
|
|
|
ToroHook::fire('404', array( |
306
|
|
|
"error" => "Controller ".get_class($this)." does not contain $method action", |
307
|
|
|
"path_info" => $this->_pathInfo )); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Call back for preg_replace in urlToActionName |
313
|
|
|
*/ |
314
|
|
|
private function _replaceActionName($parts) |
315
|
|
|
{ |
316
|
|
|
return strtoupper($parts[1]); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Modify the action name coming from the URL into proper action name |
321
|
|
|
* @param string $name: The raw controller action name |
|
|
|
|
322
|
|
|
* @return string |
323
|
|
|
*/ |
324
|
|
|
public function urlToActionName($name, $httpMethod) |
325
|
|
|
{ |
326
|
|
|
// convert to camelcase if there are dashes |
327
|
|
|
$function = preg_replace_callback("/\-(.)/", array($this, '_replaceActionName'), $name); |
328
|
|
|
|
329
|
|
|
return $httpMethod.ucfirst($function); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Load a view with the given data |
334
|
|
|
* |
335
|
|
|
* @param string $viewName |
336
|
|
|
* @param array $data |
337
|
|
|
* @return string $html, view contents |
338
|
|
|
*/ |
339
|
|
View Code Duplication |
public function getView($viewName, $data = null, $templateRootFolder = null) |
|
|
|
|
340
|
|
|
{ |
341
|
|
|
$view = new \erdiko\core\View($viewName, $data); |
342
|
|
|
|
343
|
|
|
if ($templateRootFolder != null) { |
344
|
|
|
$view->setTemplateRootFolder($templateRootFolder); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
return $view; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Add a view from the current theme with the given data |
352
|
|
|
* |
353
|
|
|
* @param mixed $view, can be a string (view name) or object (view object) |
|
|
|
|
354
|
|
|
* @param array $data |
355
|
|
|
* @return string $html, view contents |
356
|
|
|
*/ |
357
|
|
|
public function addView($view, $data = null) |
358
|
|
|
{ |
359
|
|
|
if(!is_object($view)) |
360
|
|
|
$view = new \erdiko\core\View($view, $data); |
361
|
|
|
if($data != null) |
362
|
|
|
$view->setData($data); |
363
|
|
|
|
364
|
|
|
$this->appendContent($view->toHtml()); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Load a layout with the given data |
369
|
|
|
* |
370
|
|
|
* @param string $layoutName |
371
|
|
|
* @param array $data |
372
|
|
|
* @return string $html, layout contents |
373
|
|
|
*/ |
374
|
|
|
public function getLayout($layoutName, $data = null, $templateRootFolder = null) |
375
|
|
|
{ |
376
|
|
|
$layout = new \erdiko\core\Layout($layoutName, $data, $this->getThemeName()); |
377
|
|
|
$layout->setTitle($this->getBodyTitle()); |
378
|
|
|
|
379
|
|
|
if ($templateRootFolder != null) { |
380
|
|
|
$layout->setViewRootFolder($templateRootFolder); |
381
|
|
|
$layout->setTemplateRootFolder($templateRootFolder); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
return $layout->toHtml(); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Redirect to another url |
389
|
|
|
* @param string $url |
390
|
|
|
*/ |
391
|
|
|
public function redirect($url) |
392
|
|
|
{ |
393
|
|
|
header("Location: $url"); |
394
|
|
|
exit; |
|
|
|
|
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Add Meta tags to the page |
399
|
|
|
* |
400
|
|
|
* @param string $name, html meta name (e.g. 'description' or 'keywords') |
|
|
|
|
401
|
|
|
* @param string $content |
402
|
|
|
*/ |
403
|
|
|
public function addMeta($name, $content) |
404
|
|
|
{ |
405
|
|
|
$this->getResponse()->getTheme()->addMeta($name, $content); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Set multiple meta fields at once |
410
|
|
|
* @param array $meta |
411
|
|
|
*/ |
412
|
|
|
public function setMeta($meta) |
413
|
|
|
{ |
414
|
|
|
foreach($meta as $name => $content) |
415
|
|
|
$this->getResponse()->getTheme()->addMeta($name, $content); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Add Css includes to the page |
420
|
|
|
* |
421
|
|
|
* @param string $name |
422
|
|
|
* @param string $file |
423
|
|
|
* @param int $order |
424
|
|
|
* @param int $active |
425
|
|
|
*/ |
426
|
|
|
public function addCss($name, $file, $order = 10, $active = 1) |
427
|
|
|
{ |
428
|
|
|
$this->getResponse() |
429
|
|
|
->getTheme() |
430
|
|
|
->addCss($name, $file, $order,$active); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Add Css includes to the page |
435
|
|
|
* |
436
|
|
|
* @param string $name |
437
|
|
|
* @param string $file |
438
|
|
|
* @param int $order |
439
|
|
|
* @param int $active |
440
|
|
|
*/ |
441
|
|
|
public function addJs($name, $file, $order = 10, $active = 1) |
442
|
|
|
{ |
443
|
|
|
$this->getResponse() |
444
|
|
|
->getTheme() |
445
|
|
|
->addJs($name, $file, $order,$active); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Magic method experiments |
450
|
|
|
*/ |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Get a value |
454
|
|
|
*/ |
455
|
|
|
public function __get($key) |
456
|
|
|
{ |
457
|
|
|
return $this->getResponseKeyValue($key); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* Set a value |
462
|
|
|
*/ |
463
|
|
|
public function __set($key, $value) |
464
|
|
|
{ |
465
|
|
|
return $this->setResponseKeyValue($key, $value); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* |
471
|
|
|
* |
472
|
|
|
* Code below is deprecated, do not use |
473
|
|
|
* @todo Should be deleted, refactored or moved! |
474
|
|
|
* |
475
|
|
|
* |
476
|
|
|
*/ |
477
|
|
|
|
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Add phpToJs variable to be set on the current page |
481
|
|
|
* |
482
|
|
|
* @param mixed $key |
483
|
|
|
* @param mixed $value |
484
|
|
|
*/ |
485
|
|
|
public function addPhpToJs($key, $value) |
486
|
|
|
{ |
487
|
|
|
if (is_bool($value)) { |
488
|
|
|
$value = $value ? "true" : "false"; |
489
|
|
|
} elseif (is_string($value)) { |
490
|
|
|
$value = "\"$value\""; |
491
|
|
|
} elseif (is_array($value)) { |
492
|
|
|
$value = json_encode($value); |
493
|
|
|
} elseif (is_object($value) && method_exists($value, "toArray")) { |
494
|
|
|
$value = json_encode($value->toArray()); |
495
|
|
|
} else { |
496
|
|
|
throw new \Exception("Can not translate a parameter from PHP to JS\n".print_r($value, true)); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
$this->_themeExtras['phpToJs'][$key] = $value; |
|
|
|
|
500
|
|
|
} |
501
|
|
|
} |
502
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.