1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\admin\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\web\NotFoundHttpException; |
7
|
|
|
use app\controllers\ControllerTrait; |
8
|
|
|
use app\models\entity\{User, UserProfile}; |
9
|
|
|
use app\modules\admin\models\forms\{UserForm, UserProfileForm}; |
10
|
|
|
use app\modules\admin\models\search\UserSearch; |
11
|
|
|
|
12
|
|
|
class UsersController extends \yii\web\Controller |
13
|
|
|
{ |
14
|
|
|
use ControllerTrait; |
15
|
|
|
|
16
|
|
|
public function behaviors() |
17
|
|
|
{ |
18
|
6 |
|
return [ |
19
|
|
|
'verbs' => [ |
20
|
|
|
'class' => 'yii\filters\VerbFilter', |
21
|
6 |
|
'actions' => [ |
22
|
|
|
'set-active' => ['post'], |
23
|
|
|
'set-block' => ['post'], |
24
|
|
|
'delete' => ['post'], |
25
|
|
|
'batch' => ['post'], |
26
|
|
|
'photo-upload' => ['post'], |
27
|
|
|
'autocomplete' => ['post'], |
28
|
|
|
], |
29
|
|
|
], |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function actions() |
34
|
6 |
|
{ |
35
|
|
|
return [ |
36
|
|
|
'batch' => [ |
37
|
6 |
|
'class' => 'app\modules\admin\actions\BatchAction', |
38
|
|
|
'modelClass' => User::class, |
39
|
|
|
'actions' => [ |
40
|
|
|
'delete' => [], |
41
|
|
|
'set-active' => ['status' => User::STATUS_ACTIVE], |
42
|
|
|
'set-block' => ['status' => User::STATUS_BLOCKED] |
43
|
|
|
] |
44
|
|
|
], |
45
|
|
|
'set-active' => [ |
46
|
|
|
'class' => 'app\modules\admin\actions\UpdateAttributesAction', |
47
|
|
|
'modelClass' => User::class, |
48
|
|
|
'attributes' => ['status' => User::STATUS_ACTIVE], |
49
|
|
|
], |
50
|
|
|
'set-block' => [ |
51
|
|
|
'class' => 'app\modules\admin\actions\UpdateAttributesAction', |
52
|
|
|
'modelClass' => User::class, |
53
|
|
|
'attributes' => ['status' => User::STATUS_BLOCKED], |
54
|
|
|
], |
55
|
|
|
'delete' => [ |
56
|
|
|
'class' => 'app\modules\admin\actions\DeleteAction', |
57
|
|
|
'modelClass' => User::class, |
58
|
|
|
], |
59
|
|
|
'photo-upload' => [ |
60
|
|
|
'class' => 'rkit\filemanager\actions\UploadAction', |
61
|
|
|
'modelClass' => UserProfile::class, |
62
|
|
|
'attribute' => 'photo', |
63
|
|
|
'inputName' => 'file', |
64
|
|
|
], |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function actionIndex() |
69
|
6 |
|
{ |
70
|
|
|
$search = new UserSearch(); |
71
|
6 |
|
$provider = $search->search(Yii::$app->request->get()); |
72
|
6 |
|
$statuses = User::getStatuses(); |
73
|
6 |
|
|
74
|
|
|
return $this->render('index', [ |
75
|
6 |
|
'search' => $search, |
76
|
6 |
|
'provider' => $provider, |
77
|
6 |
|
'statuses' => $statuses, |
78
|
6 |
|
'roles' => Yii::$app->authManager->getRoles() |
79
|
6 |
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function actionEdit($id = null) |
83
|
3 |
|
{ |
84
|
|
|
$model = new UserForm(); |
85
|
3 |
|
|
86
|
|
|
if ($id) { |
87
|
3 |
|
$model->setModel($this->findUser($id)); |
|
|
|
|
88
|
2 |
|
} |
89
|
|
|
|
90
|
|
|
if (Yii::$app->request->isPost) { |
91
|
2 |
|
Yii::$app->response->format = 'json'; |
92
|
|
|
|
93
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
94
|
|
|
$model->save(); |
95
|
|
|
|
96
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'Saved successfully')); |
97
|
|
|
return $this->redirect(['edit', 'id' => $model->id]); |
98
|
|
|
} |
99
|
|
|
return $this->asJsonModelErrors($model); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->render('edit', [ |
103
|
2 |
|
'model' => $model, |
104
|
2 |
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function actionProfile($id) |
108
|
2 |
|
{ |
109
|
|
|
$model = new UserProfileForm($this->findProfile($id)); |
|
|
|
|
110
|
2 |
|
|
111
|
2 |
|
if (Yii::$app->request->isPost) { |
112
|
|
|
Yii::$app->response->format = 'json'; |
113
|
1 |
|
|
114
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
115
|
|
|
$model->save(); |
116
|
|
|
|
117
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'Saved successfully')); |
118
|
|
|
return $this->redirect(['profile', 'id' => $model->user_id]); |
119
|
|
|
} |
120
|
|
|
return $this->asJsonModelErrors($model); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->render('profile', [ |
124
|
|
|
'model' => $model |
125
|
1 |
|
]); |
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Find the model. |
130
|
|
|
* If the model is not found, then 404 HTTP exception will be thrown. |
131
|
|
|
* |
132
|
|
|
* @param int $id |
133
|
|
|
* @return Model |
134
|
|
|
* @throws NotFoundHttpException |
135
|
|
|
*/ |
136
|
|
|
private function findUser($id): yii\base\Model |
137
|
|
|
{ |
138
|
|
|
$model = User::findOne($id); |
139
|
|
|
|
140
|
|
|
if ($model === null) { |
141
|
|
|
throw new NotFoundHttpException(Yii::t('app', 'Page not found')); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $model; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Find the model. |
149
|
|
|
* If the model is not found, then 404 HTTP exception will be thrown. |
150
|
|
|
* |
151
|
|
|
* @param int $id |
152
|
|
|
* @return Model |
153
|
|
|
* @throws NotFoundHttpException |
154
|
|
|
*/ |
155
|
|
|
private function findProfile($id): yii\base\Model |
156
|
|
|
{ |
157
|
|
|
$model = UserProfile::findOne($id); |
158
|
|
|
|
159
|
|
|
if ($model === null) { |
160
|
|
|
throw new NotFoundHttpException(Yii::t('app', 'Page not found')); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $model; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.