1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\admin\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\filters\VerbFilter; |
7
|
|
|
use yii\helpers\Url; |
8
|
|
|
use yii\web\Response; |
9
|
|
|
use app\traits\ModelTrait; |
10
|
|
|
use app\models\User; |
11
|
|
|
use app\models\UserProfile; |
12
|
|
|
use app\modules\admin\models\search\UserSearch; |
13
|
|
|
|
14
|
|
|
class UsersController extends \yii\web\Controller |
15
|
|
|
{ |
16
|
|
|
use ModelTrait; |
17
|
|
|
|
18
|
|
|
public function behaviors() |
19
|
|
|
{ |
20
|
|
|
return [ |
21
|
|
|
'verbs' => [ |
22
|
|
|
'class' => VerbFilter::class, |
23
|
|
|
'actions' => [ |
24
|
|
|
'set-active' => ['post'], |
25
|
|
|
'set-block' => ['post'], |
26
|
|
|
'delete' => ['post'], |
27
|
|
|
'operations' => ['post'], |
28
|
|
|
'photo-upload' => ['post'], |
29
|
|
|
'autocomplete' => ['post'], |
30
|
|
|
], |
31
|
|
|
], |
32
|
|
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function actions() |
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
|
|
'operations' => [ |
39
|
|
|
'class' => 'app\modules\admin\controllers\common\OperationsAction', |
40
|
|
|
'modelClass' => 'app\models\User', |
41
|
|
|
'operations' => [ |
42
|
|
|
'delete' => [], |
43
|
|
|
'set-active' => ['status' => User::STATUS_ACTIVE], |
44
|
|
|
'set-block' => ['status' => User::STATUS_BLOCKED] |
45
|
|
|
] |
46
|
|
|
], |
47
|
|
|
'set-active' => [ |
48
|
|
|
'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction', |
49
|
|
|
'modelClass' => 'app\models\User', |
50
|
|
|
'attributes' => ['status' => User::STATUS_ACTIVE], |
51
|
|
|
], |
52
|
|
|
'set-block' => [ |
53
|
|
|
'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction', |
54
|
|
|
'modelClass' => 'app\models\User', |
55
|
|
|
'attributes' => ['status' => User::STATUS_BLOCKED], |
56
|
|
|
], |
57
|
|
|
'delete' => [ |
58
|
|
|
'class' => 'app\modules\admin\controllers\common\DeleteAction', |
59
|
|
|
'modelClass' => 'app\models\User', |
60
|
|
|
], |
61
|
|
|
'photo-upload' => [ |
62
|
|
|
'class' => 'rkit\filemanager\actions\UploadAction', |
63
|
|
|
'modelClass' => 'app\models\UserProfile', |
64
|
|
|
'attribute' => 'photo', |
65
|
|
|
'inputName' => 'file', |
66
|
|
|
], |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function actionIndex() |
71
|
|
|
{ |
72
|
|
|
$userSearch = new UserSearch(); |
73
|
|
|
$dataProvider = $userSearch->search(Yii::$app->request->get()); |
74
|
|
|
$statuses = User::getStatuses(); |
75
|
|
|
|
76
|
|
|
return $this->render('index', [ |
77
|
|
|
'userSearch' => $userSearch, |
78
|
|
|
'dataProvider' => $dataProvider, |
79
|
|
|
'statuses' => $statuses, |
80
|
|
|
'roles' => Yii::$app->authManager->getRoles() |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function actionEdit($id = null) |
85
|
|
|
{ |
86
|
|
|
$model = new User(); |
87
|
|
|
|
88
|
|
|
if ($id) { |
89
|
|
|
$model = $this->findModel($model, $id); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (Yii::$app->request->isPost) { |
93
|
|
|
if ($model->isNewRecord) { |
94
|
|
|
$model->setConfirmed(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
98
|
|
|
$this->assignRole($model); |
99
|
|
|
|
100
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app.messages', 'Saved successfully')); |
101
|
|
|
$urlToModel = Url::toRoute(['edit', 'id' => $model->id]); |
102
|
|
|
if (Yii::$app->request->isAjax) { |
103
|
|
|
return $this->asJson(['redirect' => $urlToModel]); |
104
|
|
|
} |
105
|
|
|
return $this->redirect($urlToModel); |
106
|
|
|
} |
107
|
|
|
if (Yii::$app->request->isAjax) { |
108
|
|
|
return $this->asJson($this->collectErrors($model)); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->render('edit', [ |
113
|
|
|
'model' => $model, |
114
|
|
|
'roles' => Yii::$app->authManager->getRoles() |
115
|
|
|
]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function actionProfile($id) |
119
|
|
|
{ |
120
|
|
|
$model = $this->findModel(new UserProfile(), $id); |
121
|
|
|
|
122
|
|
|
if (Yii::$app->request->isPost) { |
123
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
124
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app.messages', 'Saved successfully')); |
125
|
|
|
$urlToModel = Url::toRoute(['profile', 'id' => $model->user_id]); |
126
|
|
|
if (Yii::$app->request->isAjax) { |
127
|
|
|
return $this->asJson(['redirect' => $urlToModel]); |
128
|
|
|
} |
129
|
|
|
return $this->redirect($urlToModel); |
130
|
|
|
} |
131
|
|
|
if (Yii::$app->request->isAjax) { |
132
|
|
|
return $this->asJson($this->collectErrors($model)); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $this->render('profile', [ |
137
|
|
|
'model' => $model |
138
|
|
|
]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function actionAutocomplete() |
142
|
|
|
{ |
143
|
|
|
$result = []; |
144
|
|
|
if (($term = Yii::$app->request->post('term')) !== null) { |
145
|
|
|
$data = User::find() |
146
|
|
|
->like('username', $term) |
147
|
|
|
->asArray() |
148
|
|
|
->limit(10) |
149
|
|
|
->all(); |
150
|
|
|
|
151
|
|
|
foreach ($data as $item) { |
152
|
|
|
$result[] = [ |
153
|
|
|
'text' => $item['username'], |
154
|
|
|
'id' => $item['id'] |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this->asJson($result); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
private function assignRole(\yii\db\ActiveRecord $model) |
163
|
|
|
{ |
164
|
|
|
$auth = Yii::$app->authManager; |
165
|
|
|
$auth->revokeAll($model->id); |
166
|
|
|
|
167
|
|
|
if (!empty($model->role)) { |
168
|
|
|
$role = $auth->getRole($model->role); |
169
|
|
|
if ($role) { |
170
|
|
|
$auth->assign($role, $model->id); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|