AbstractController.php
4.95 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
<?php
namespace Nextend\Framework\Controller;
use Exception;
use Nextend\Framework\Acl\Acl;
use Nextend\Framework\Application\AbstractApplication;
use Nextend\Framework\Application\AbstractApplicationType;
use Nextend\Framework\Asset\AssetManager;
use Nextend\Framework\Asset\Predefined;
use Nextend\Framework\Form\Form;
use Nextend\Framework\Notification\Notification;
use Nextend\Framework\Pattern\GetPathTrait;
use Nextend\Framework\Pattern\MVCHelperTrait;
use Nextend\Framework\Plugin;
use Nextend\Framework\Request\Request;
use Nextend\SmartSlider3\Application\ApplicationSmartSlider3;
abstract class AbstractController {
use GetPathTrait;
use MVCHelperTrait;
/**
* @var AbstractApplicationType
*/
protected $applicationType;
/** @var callback[] */
protected $externalActions = array();
/**
* AbstractController constructor.
*
* @param AbstractApplicationType $applicationType
*/
public function __construct($applicationType) {
//PluggableController\Nextend\SmartSlider3\Application\Admin\Slider\ControllerSlider
Plugin::doAction('PluggableController\\' . get_class($this), array($this));
$this->applicationType = $applicationType;
$this->setMVCHelper($this->applicationType);
AssetManager::getInstance();
$this->initialize();
}
/**
* @param $actionName
* @param callback $callable
*/
public function addExternalAction($actionName, $callable) {
$this->externalActions[$actionName] = $callable;
}
/**
* @return AbstractApplication
*/
public function getApplication() {
return $this->applicationType->getApplication();
}
/**
* @return AbstractApplicationType
*/
public function getApplicationType() {
return $this->applicationType;
}
public function getRouter() {
return $this->applicationType->getRouter();
}
/**
* @param $actionName
* @param array $args
*
* @throws Exception
*/
final public function doAction($actionName, $args = array()) {
$originalActionName = $actionName;
if (method_exists($this, 'action' . $actionName)) {
call_user_func_array(array(
$this,
'action' . $actionName
), $args);
} else if (isset($this->externalActions[$actionName]) && is_callable($this->externalActions[$actionName])) {
call_user_func_array($this->externalActions[$actionName], $args);
} else {
$actionName = $this->missingAction($this, $actionName);
if (method_exists($this, 'action' . $actionName)) {
call_user_func_array(array(
$this,
'action' . $actionName
), $args);
} else {
throw new Exception(sprintf('Missing action (%s) for controller (%s)', $originalActionName, static::class));
}
}
}
protected function missingAction($controllerName, $actionName) {
return 'index';
}
public function initialize() {
Predefined::frontend();
}
/**
* Check ACL permissions
*
* @param $action
*
* @return bool
*/
public function canDo($action) {
return Acl::canDo($action, $this);
}
public function redirect($url, $statusCode = 302, $terminate = true) {
Request::redirect($url, $statusCode, $terminate);
}
public function validatePermission($permission) {
if (!$this->canDo($permission)) {
Notification::error(n2_('You are not authorised to view this resource.'));
ApplicationSmartSlider3::getInstance()
->getApplicationTypeAdmin()
->process('sliders', 'index');
return false;
}
return true;
}
public function validateVariable($condition, $property) {
if (!$condition) {
Notification::error(sprintf(n2_('Missing parameter: %s'), $property));
ApplicationSmartSlider3::getInstance()
->getApplicationTypeAdmin()
->process('sliders', 'index');
return false;
}
return true;
}
public function validateDatabase($condition, $showError = true) {
if (!$condition) {
if ($showError) {
Notification::error(n2_('Database error'));
ApplicationSmartSlider3::getInstance()
->getApplicationTypeAdmin()
->process('sliders', 'index');
}
return false;
}
return true;
}
public function validateToken() {
if (!Form::checkToken()) {
Notification::error(n2_('Security token mismatch'));
return false;
}
return true;
}
}