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