1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* HiPanel core package |
5
|
|
|
* |
6
|
|
|
* @link https://hipanel.com/ |
7
|
|
|
* @package hipanel-core |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hipanel\base; |
13
|
|
|
|
14
|
|
|
use hipanel\components\Cache; |
15
|
|
|
use hipanel\components\Response; |
16
|
|
|
use hiqdev\hiart\ActiveRecord; |
17
|
|
|
use Yii; |
18
|
|
|
use yii\di\Instance; |
19
|
|
|
use yii\filters\AccessControl; |
20
|
|
|
use yii\helpers\Inflector; |
21
|
|
|
use yii\web\NotFoundHttpException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Site controller. |
25
|
|
|
*/ |
26
|
|
|
class Controller extends \yii\web\Controller |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Cache|array|string the cache object or the application component ID of the cache object. |
30
|
|
|
*/ |
31
|
|
|
protected $_cache = 'cache'; |
32
|
|
|
|
33
|
|
|
public function setCache($cache) |
34
|
|
|
{ |
35
|
|
|
$this->_cache = $cache; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getCache() |
39
|
|
|
{ |
40
|
|
|
if (!is_object($this->_cache)) { |
41
|
|
|
$this->_cache = Instance::ensure($this->_cache, Cache::class); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->_cache; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array internal actions. |
49
|
|
|
*/ |
50
|
|
|
protected $_internalActions; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function behaviors() |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
'access' => [ |
59
|
|
|
'class' => AccessControl::class, |
60
|
|
|
'only' => ['index'], |
61
|
|
|
'rules' => [ |
62
|
|
|
[ |
63
|
|
|
'actions' => ['index'], |
64
|
|
|
'allow' => true, |
65
|
|
|
'roles' => ['@'], |
66
|
|
|
], |
67
|
|
|
], |
68
|
|
|
], |
69
|
1 |
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $submodel the submodel that will be added to the ClassName |
|
|
|
|
74
|
|
|
* @return string Main Model class name |
75
|
|
|
*/ |
76
|
|
|
public static function modelClassName() |
77
|
|
|
{ |
78
|
|
|
$parts = explode('\\', static::class); |
79
|
|
|
$last = array_pop($parts); |
80
|
|
|
array_pop($parts); |
81
|
|
|
$parts[] = 'models'; |
82
|
|
|
$parts[] = substr($last, 0, -10); |
83
|
|
|
|
84
|
|
|
return implode('\\', $parts); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $config config to be used to create the [[Model]] |
89
|
|
|
* @return ActiveRecord |
90
|
|
|
*/ |
91
|
|
|
public static function newModel($config = [], $submodel = '') |
92
|
|
|
{ |
93
|
|
|
$config['class'] = static::modelClassName() . $submodel; |
94
|
|
|
return Yii::createObject($config); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param array $config config to be used to create the [[Model]] |
99
|
|
|
* @return ActiveRecord|SearchModelTrait Search Model object |
100
|
|
|
*/ |
101
|
|
|
public static function searchModel($config = []) |
102
|
|
|
{ |
103
|
|
|
return static::newModel($config, 'Search'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string main model's formName() |
108
|
|
|
*/ |
109
|
|
|
public static function formName() |
110
|
|
|
{ |
111
|
|
|
return static::newModel()->formName(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string search model's formName() |
116
|
|
|
*/ |
117
|
|
|
public static function searchFormName() |
118
|
|
|
{ |
119
|
|
|
return static::newModel()->formName() . 'Search'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $separator |
124
|
|
|
* @return string Main model's camel2id'ed formName() |
125
|
|
|
*/ |
126
|
|
|
public static function modelId($separator = '-') |
127
|
|
|
{ |
128
|
|
|
return Inflector::camel2id(static::formName(), $separator); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the module ID based on the namespace of the controller. |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
|
|
public static function moduleId() |
136
|
|
|
{ |
137
|
|
|
return explode('\\', get_called_class())[2]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public static function controllerId() |
141
|
|
|
{ |
142
|
|
|
return strtolower(substr(explode('\\', get_called_class())[4], 0, -10)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param int|array $condition scalar ID or array to be used for searching |
147
|
|
|
* @param array $config config to be used to create the [[Model]] |
148
|
|
|
* @throws NotFoundHttpException |
149
|
|
|
* @return array|ActiveRecord|null|static |
150
|
|
|
*/ |
151
|
|
|
public static function findModel($condition, $config = []) |
152
|
|
|
{ |
153
|
|
|
/* @noinspection PhpVoidFunctionResultUsedInspection */ |
154
|
|
|
$model = static::newModel($config)->findOne(is_array($condition) ? $condition : ['id' => $condition]); |
155
|
|
|
if ($model === null) { |
156
|
|
|
throw new NotFoundHttpException('The requested object not found.'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $model; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public static function findModels($condition, $config = []) |
163
|
|
|
{ |
164
|
|
|
$containsIntKeys = 0; |
165
|
|
|
if (is_array($condition)) { |
166
|
|
|
foreach (array_keys($condition) as $item) { |
167
|
|
|
if (is_numeric($item)) { |
168
|
|
|
$containsIntKeys = true; |
169
|
|
|
break; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (!is_array($condition) || $containsIntKeys) { |
175
|
|
|
$condition = ['id' => $condition]; |
176
|
|
|
} |
177
|
|
|
$models = static::searchModel($config)->search([static::searchFormName() => $condition], ['pagination' => false])->getModels(); |
178
|
|
|
if ($models === null) { |
179
|
|
|
throw new NotFoundHttpException('The requested object not found.'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $models; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public static function renderJson($data) |
186
|
|
|
{ |
187
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
188
|
|
|
|
189
|
|
|
return $data; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public static function renderJsonp($data) |
193
|
|
|
{ |
194
|
|
|
Yii::$app->response->format = Response::FORMAT_JSONP; |
195
|
|
|
|
196
|
|
|
return $data; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function actionIndex() |
200
|
|
|
{ |
201
|
|
|
return $this->render('index'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function setInternalAction($id, $action) |
205
|
|
|
{ |
206
|
|
|
$this->_internalActions[$id] = $action; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function hasInternalAction($id) |
210
|
|
|
{ |
211
|
|
|
return array_key_exists($id, $this->_internalActions); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function createAction($id) |
215
|
|
|
{ |
216
|
|
|
$config = $this->_internalActions[$id]; |
217
|
|
|
return $config ? Yii::createObject($config, [$id, $this]) : parent::createAction($id); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Prepares array for building url to action based on given action id and parameters. |
222
|
|
|
* |
223
|
|
|
* @param string $action action id |
224
|
|
|
* @param string|int|array $params ID of object to be action'ed or array of parameters |
225
|
|
|
* @return array array suitable for Url::to |
226
|
|
|
*/ |
227
|
|
|
public static function getActionUrl($action = 'index', $params = []) |
228
|
|
|
{ |
229
|
|
|
$params = is_array($params) ? $params : ['id' => $params]; |
230
|
|
|
return array_merge([implode('/', ['', static::moduleId(), static::controllerId(), $action])], $params); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Prepares array for building url to search with given filters. |
235
|
|
|
*/ |
236
|
|
|
public static function getSearchUrl(array $params = []) |
237
|
|
|
{ |
238
|
|
|
return static::getActionUrl('index', [static::searchFormName() => $params]); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.