/
Controller.php
144 lines (117 loc) · 3.33 KB
/
Controller.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
<?php
namespace Concrete\Core\Controller;
use Concrete\Core\Http\Request;
use Concrete\Core\Page\Theme\Theme as PageTheme;
use Concrete\Core\Support\Facade\Facade;
use Concrete\Core\View\View;
class Controller extends AbstractController
{
protected $view;
/**
* The path to the view file (update it with the setViewPath method).
*
* @var string
*/
protected $viewPath = '';
protected $theme;
protected $controllerActionPath;
protected $themeViewTemplate;
public function __construct()
{
parent::__construct();
$this->setViewPath((string) $this->viewPath);
}
public function setViewObject(View $view)
{
$this->view = $view;
}
public function setTheme($mixed)
{
if ($mixed instanceof PageTheme) {
$this->theme = $mixed->getThemeHandle();
} else {
$this->theme = $mixed;
}
}
public function getTheme()
{
return $this->theme;
}
public function setThemeViewTemplate($template)
{
$this->themeViewTemplate = $template;
}
/**
* Returns the wrapper file that holds the content of the view. Usually view.php.
*
* @return string
*/
public function getThemeViewTemplate()
{
if (isset($this->view)) {
$templateFromView = $this->view->getViewTemplateFile();
}
if (isset($this->themeViewTemplate) && $templateFromView == FILENAME_THEMES_VIEW) {
return $this->themeViewTemplate;
}
if (!isset($templateFromView)) {
$templateFromView = FILENAME_THEMES_VIEW;
}
return $templateFromView;
}
public function getControllerActionPath()
{
if (isset($this->controllerActionPath)) {
return $this->controllerActionPath;
}
$request = Request::getInstance();
return $request->getPathInfo();
}
/**
* Get the path to the view file.
*/
public function getViewPath(): string
{
return $this->viewPath;
}
public function flash($key, $value, $isHTML = false)
{
$session = Facade::getFacadeApplication()->make('session');
$session->getFlashBag()->add('page_message', [$key, $value, $isHTML]);
}
public function getViewObject()
{
if ($this->view) {
$this->view->setController($this);
$this->view->setViewTheme($this->getTheme());
return $this->view;
}
}
public function action()
{
$a = func_get_args();
array_unshift($a, $this->getControllerActionPath());
return call_user_func_array([$this->view, 'url'], $a);
}
/**
* Set the path to the view file.
*
* @return $this
*/
protected function setViewPath(string $viewPath): self
{
$this->viewPath = $viewPath;
if ($this->viewPath === '') {
$this->view = null;
} else {
$this->view = new View($this->viewPath);
$matches = null;
if (preg_match('/Concrete\\\Package\\\(.*)\\\Controller/i', get_class($this), $matches)) {
$pkgHandle = uncamelcase($matches[1]);
$this->view->setPackageHandle($pkgHandle);
}
$this->view->setController($this);
}
return $this;
}
}