1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\admin\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\filters\VerbFilter; |
7
|
|
|
use app\traits\ModelTrait; |
8
|
|
|
use app\models\entity\User; |
9
|
|
|
use app\models\entity\UserProfile; |
10
|
|
|
use app\modules\admin\models\forms\UserForm; |
11
|
|
|
use app\modules\admin\models\forms\UserProfileForm; |
12
|
|
|
use app\modules\admin\models\search\UserSearch; |
13
|
|
|
|
14
|
|
|
class UsersController extends \yii\web\Controller |
15
|
|
|
{ |
16
|
|
|
use ModelTrait; |
17
|
|
|
|
18
|
6 |
|
public function behaviors() |
19
|
|
|
{ |
20
|
|
|
return [ |
21
|
6 |
|
'verbs' => [ |
22
|
|
|
'class' => VerbFilter::class, |
23
|
|
|
'actions' => [ |
24
|
|
|
'set-active' => ['post'], |
25
|
|
|
'set-block' => ['post'], |
26
|
|
|
'delete' => ['post'], |
27
|
|
|
'batch' => ['post'], |
28
|
|
|
'photo-upload' => ['post'], |
29
|
|
|
], |
30
|
|
|
], |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
6 |
|
public function actions() |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
6 |
|
'batch' => [ |
38
|
|
|
'class' => 'app\modules\admin\actions\BatchAction', |
39
|
|
|
'modelClass' => User::class, |
40
|
|
|
'actions' => [ |
41
|
|
|
'delete' => [], |
42
|
|
|
'set-active' => ['status' => User::STATUS_ACTIVE], |
43
|
|
|
'set-block' => ['status' => User::STATUS_BLOCKED] |
44
|
|
|
] |
45
|
|
|
], |
46
|
|
|
'set-active' => [ |
47
|
|
|
'class' => 'app\modules\admin\actions\UpdateAttributesAction', |
48
|
|
|
'modelClass' => User::class, |
49
|
|
|
'attributes' => ['status' => User::STATUS_ACTIVE], |
50
|
|
|
], |
51
|
|
|
'set-block' => [ |
52
|
|
|
'class' => 'app\modules\admin\actions\UpdateAttributesAction', |
53
|
|
|
'modelClass' => User::class, |
54
|
|
|
'attributes' => ['status' => User::STATUS_BLOCKED], |
55
|
|
|
], |
56
|
|
|
'delete' => [ |
57
|
|
|
'class' => 'app\modules\admin\actions\DeleteAction', |
58
|
|
|
'modelClass' => User::class, |
59
|
|
|
], |
60
|
|
|
'photo-upload' => [ |
61
|
|
|
'class' => 'rkit\filemanager\actions\UploadAction', |
62
|
|
|
'modelClass' => UserProfile::class, |
63
|
|
|
'attribute' => 'photo', |
64
|
|
|
'inputName' => 'file', |
65
|
|
|
], |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
6 |
|
public function actionIndex() |
70
|
|
|
{ |
71
|
6 |
|
$userSearch = new UserSearch(); |
72
|
6 |
|
$dataProvider = $userSearch->search(Yii::$app->request->get()); |
73
|
6 |
|
$statuses = User::getStatuses(); |
74
|
|
|
|
75
|
6 |
|
return $this->render('index', [ |
76
|
6 |
|
'userSearch' => $userSearch, |
77
|
6 |
|
'dataProvider' => $dataProvider, |
78
|
6 |
|
'statuses' => $statuses, |
79
|
6 |
|
'roles' => Yii::$app->authManager->getRoles() |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
public function actionEdit($id = null) |
84
|
|
|
{ |
85
|
3 |
|
$model = new UserForm(); |
86
|
|
|
|
87
|
3 |
|
if ($id) { |
88
|
2 |
|
$model->setModel($this->findModel(new User, $id)); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
if (Yii::$app->request->isPost) { |
92
|
|
|
Yii::$app->response->format = 'json'; |
93
|
|
|
|
94
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
95
|
|
|
$model->save(); |
96
|
|
|
|
97
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app.msg', 'Saved successfully')); |
98
|
|
|
return $this->redirect(['edit', 'id' => $model->id]); |
99
|
|
|
} |
100
|
|
|
return $this->asJsonModelErrors($model); |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
return $this->render('edit', [ |
104
|
2 |
|
'model' => $model, |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
public function actionProfile($id) |
109
|
|
|
{ |
110
|
2 |
|
$model = new UserProfileForm(); |
111
|
2 |
|
$model->setModel($this->findModel(new UserProfile, $id)); |
|
|
|
|
112
|
|
|
|
113
|
1 |
|
if (Yii::$app->request->isPost) { |
114
|
|
|
Yii::$app->response->format = 'json'; |
115
|
|
|
|
116
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
117
|
|
|
$model->save(); |
118
|
|
|
|
119
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app.msg', 'Saved successfully')); |
120
|
|
|
return $this->redirect(['profile', 'id' => $model->user_id]); |
121
|
|
|
} |
122
|
|
|
return $this->asJsonModelErrors($model); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
return $this->render('profile', [ |
126
|
1 |
|
'model' => $model |
127
|
|
|
]); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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.