|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\views\View; |
|
6
|
|
|
use Ubiquity\exceptions\RouterException; |
|
7
|
|
|
use Ubiquity\themes\ThemesManager; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Base class for controllers. |
|
11
|
|
|
* Ubiquity\controllers$Controller |
|
12
|
|
|
* This class is part of Ubiquity |
|
13
|
|
|
* |
|
14
|
|
|
* @author jcheron <[email protected]> |
|
15
|
|
|
* @version 1.0.6 |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class Controller { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The view |
|
22
|
|
|
* |
|
23
|
|
|
* @var View |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $view; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Default action |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract public function index(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Constructor |
|
34
|
|
|
* initialize $view variable |
|
35
|
|
|
*/ |
|
36
|
44 |
|
public function __construct() { |
|
37
|
44 |
|
$this->view = new View (); |
|
38
|
44 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Method called before each action |
|
42
|
|
|
* Can be override in derived class |
|
43
|
|
|
*/ |
|
44
|
17 |
|
public function initialize() { |
|
45
|
17 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Method called after each action |
|
49
|
|
|
* Can be override in derived class |
|
50
|
|
|
*/ |
|
51
|
31 |
|
public function finalize() { |
|
52
|
31 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Loads the view $viewName possibly passing the variables $pdata |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $viewName The name of the view to load |
|
58
|
|
|
* @param mixed $pData Variable or associative array to pass to the view |
|
59
|
|
|
* If a variable is passed, it will have the name **$data** in the view, |
|
60
|
|
|
* If an associative array is passed, the view retrieves variables from the table's key names |
|
61
|
|
|
* @param boolean $asString If true, the view is not displayed but returned as a string (usable in a variable) |
|
62
|
|
|
* @throws \Exception |
|
63
|
|
|
* @return string null or the view content if **$asString** parameter is true |
|
64
|
|
|
*/ |
|
65
|
26 |
|
public function loadView($viewName, $pData = NULL, $asString = false) { |
|
66
|
26 |
|
if (isset ( $pData )) { |
|
67
|
20 |
|
$this->view->setVars ( $pData ); |
|
68
|
|
|
} |
|
69
|
26 |
|
return $this->view->render ( $viewName, $asString ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Loads the default view (controllerName/actionName) possibly passing the variables $pdata. |
|
74
|
|
|
* (@activeTheme/controllerName/actionName if themes are activated) |
|
75
|
|
|
* |
|
76
|
|
|
* @param mixed $pData Variable or associative array to pass to the view |
|
77
|
|
|
* If a variable is passed, it will have the name **$data** in the view, |
|
78
|
|
|
* If an associative array is passed, the view retrieves variables from the table's key names |
|
79
|
|
|
* @param boolean $asString If true, the view is not displayed but returned as a string (usable in a variable) |
|
80
|
|
|
* @throws \Exception |
|
81
|
|
|
* @return string null or the view content if **$asString** parameter is true |
|
82
|
|
|
*/ |
|
83
|
3 |
|
public function loadDefaultView($pData = NULL, $asString = false) { |
|
84
|
3 |
|
return $this->loadView ( $this->getDefaultViewName (), $pData, $asString ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Returns the default view name for this controller/action i.e ControllerName/actionName.html for the action actionName in ControllerName |
|
89
|
|
|
* If there is an activeTheme **@activeTheme/ControllerName/actionName.html** |
|
90
|
|
|
* |
|
91
|
|
|
* @return string the default view name |
|
92
|
|
|
*/ |
|
93
|
3 |
|
public function getDefaultViewName() { |
|
94
|
3 |
|
$activeTheme = ThemesManager::getActiveTheme (); |
|
95
|
3 |
|
if ($activeTheme !== '') { |
|
96
|
3 |
|
return '@activeTheme/' . Startup::getControllerSimpleName () . "/" . Startup::getAction () . "." . Startup::getViewNameFileExtension (); |
|
97
|
|
|
} |
|
98
|
|
|
return Startup::getControllerSimpleName () . "/" . Startup::getAction () . "." . Startup::getViewNameFileExtension (); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns True if access to the controller is allowed |
|
103
|
|
|
* To be override in sub classes |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $action |
|
106
|
|
|
* @return boolean |
|
107
|
|
|
*/ |
|
108
|
39 |
|
public function isValid($action) { |
|
109
|
39 |
|
return true; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Called if isValid () returns false |
|
114
|
|
|
* To be override in sub classes |
|
115
|
|
|
*/ |
|
116
|
5 |
|
public function onInvalidControl() { |
|
117
|
5 |
|
if (! headers_sent ()) { |
|
118
|
5 |
|
\header ( 'HTTP/1.1 401 Unauthorized', true, 401 ); |
|
119
|
|
|
} |
|
120
|
5 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Loads the controller $controller and calls its $action method by passing the parameters $params |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $controller The Controller |
|
126
|
|
|
* @param string $action The action to call |
|
127
|
|
|
* @param mixed $params Parameters passed to the **$action** method |
|
128
|
|
|
* @param boolean $initialize If true, the controller's initialize method is called before $action |
|
129
|
|
|
* @param boolean $finalize If true, the controller's finalize method is called after $action |
|
130
|
|
|
* @throws \Exception |
|
131
|
|
|
*/ |
|
132
|
2 |
|
public function forward($controller, $action = "index", $params = array (), $initialize = false, $finalize = false) { |
|
133
|
2 |
|
$u = array ($controller,$action ); |
|
134
|
2 |
|
if (\is_array ( $params )) { |
|
135
|
2 |
|
$u = \array_merge ( $u, $params ); |
|
136
|
|
|
} else { |
|
137
|
|
|
$u = \array_merge ( $u, [ $params ] ); |
|
138
|
|
|
} |
|
139
|
2 |
|
Startup::runAction ( $u, $initialize, $finalize ); |
|
140
|
2 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Redirect to a route by its name |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $routeName The route name |
|
146
|
|
|
* @param array $parameters The parameters to pass to the route action |
|
147
|
|
|
* @param boolean $initialize Call the **initialize** method if true |
|
148
|
|
|
* @param boolean $finalize Call the **finalize** method if true |
|
149
|
|
|
* @throws RouterException |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function redirectToRoute($routeName, $parameters = [ ], $initialize = false, $finalize = false) { |
|
152
|
1 |
|
$infos = Router::getRouteInfoByName ( $routeName ); |
|
153
|
1 |
|
if ($infos !== false) { |
|
154
|
1 |
|
if (isset ( $infos ['controller'] )) { |
|
155
|
1 |
|
$this->forward ( $infos ['controller'], $infos ['action'] ?? 'index', $parameters, $initialize, $finalize ); |
|
156
|
|
|
} else { |
|
157
|
|
|
$method = \strtolower ( $_SERVER ['REQUEST_METHOD'] ); |
|
158
|
|
|
if (isset ( $infos [$method] )) { |
|
159
|
|
|
$infos = $infos [$method]; |
|
160
|
|
|
$this->forward ( $infos ['controller'], $infos ['action'] ?? 'index', $parameters, $initialize, $finalize ); |
|
161
|
|
|
} else { |
|
162
|
1 |
|
throw new RouterException ( "Route {$routeName} not found for method {$method}", 404 ); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
} else { |
|
166
|
|
|
throw new RouterException ( "Route {$routeName} not found", 404 ); |
|
167
|
|
|
} |
|
168
|
1 |
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Returns the associated view instance |
|
172
|
|
|
* |
|
173
|
|
|
* @return \Ubiquity\views\View |
|
174
|
|
|
*/ |
|
175
|
15 |
|
public function getView() { |
|
176
|
15 |
|
return $this->view; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|