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 app\helpers\Util; |
9
|
|
|
use app\components\BaseController; |
10
|
|
|
use app\models\User; |
11
|
|
|
use app\models\UserProfile; |
12
|
|
|
use app\modules\admin\models\search\UserSearch; |
13
|
|
|
use app\modules\admin\models\forms\UserForm; |
14
|
|
|
|
15
|
|
|
class UsersController extends BaseController |
16
|
|
|
{ |
17
|
|
|
public function behaviors() |
18
|
|
|
{ |
19
|
|
|
return [ |
20
|
|
|
'verbs' => [ |
21
|
|
|
'class' => VerbFilter::className(), |
22
|
|
|
'actions' => [ |
23
|
|
|
'set-active' => ['post'], |
24
|
|
|
'set-block' => ['post'], |
25
|
|
|
'delete' => ['post'], |
26
|
|
|
'operations' => ['post'], |
27
|
|
|
'photo-upload' => ['post'], |
28
|
|
|
'autocomplete' => ['post'], |
29
|
|
|
], |
30
|
|
|
], |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function actions() |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
|
|
'operations' => [ |
38
|
|
|
'class' => 'app\modules\admin\controllers\common\OperationsAction', |
39
|
|
|
'modelName' => 'app\models\User', |
40
|
|
|
'operations' => [ |
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\controllers\common\UpdateAttributesAction', |
48
|
|
|
'modelName' => 'app\models\User', |
49
|
|
|
'attributes' => ['status' => User::STATUS_ACTIVE], |
50
|
|
|
], |
51
|
|
|
'set-block' => [ |
52
|
|
|
'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction', |
53
|
|
|
'modelName' => 'app\models\User', |
54
|
|
|
'attributes' => ['status' => User::STATUS_BLOCKED], |
55
|
|
|
], |
56
|
|
|
'delete' => [ |
57
|
|
|
'class' => 'app\modules\admin\controllers\common\DeleteAction', |
58
|
|
|
'modelName' => 'app\models\User', |
59
|
|
|
], |
60
|
|
|
'photo-upload' => [ |
61
|
|
|
'class' => 'rkit\filemanager\actions\UploadAction', |
62
|
|
|
'modelName' => 'app\models\UserProfile', |
63
|
|
|
'attribute' => 'photo', |
64
|
|
|
'inputName' => 'file', |
65
|
|
|
'type' => 'image', |
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 UserForm(); |
87
|
|
|
|
88
|
|
|
if ($id) { |
89
|
|
|
$model = $this->loadModel($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
|
|
|
$auth = Yii::$app->authManager; |
99
|
|
|
$auth->revokeAll($model->id); |
100
|
|
|
|
101
|
|
|
if (!empty($model->role)) { |
102
|
|
|
$role = $auth->getRole($model->role); |
103
|
|
|
if ($role) { |
104
|
|
|
$auth->assign($role, $model->id); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'Saved successfully')); |
109
|
|
|
if (Yii::$app->request->isAjax) { |
110
|
|
|
return $this->response(['redirect' => Url::toRoute(['edit', 'id' => $model->id])]); |
111
|
|
|
} |
112
|
|
|
} else { |
113
|
|
|
if (Yii::$app->request->isAjax) { |
114
|
|
|
return $this->response(Util::collectModelErrors($model)); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->render('edit', [ |
120
|
|
|
'model' => $model, |
121
|
|
|
'roles' => Yii::$app->authManager->getRoles() |
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function actionProfile($id) |
126
|
|
|
{ |
127
|
|
|
$model = $this->loadModel(new UserProfile(), $id); |
128
|
|
|
|
129
|
|
|
if (Yii::$app->request->isPost) { |
130
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
131
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'Saved successfully')); |
132
|
|
|
if (Yii::$app->request->isAjax) { |
133
|
|
|
return $this->response(['redirect' => Url::toRoute(['profile', 'id' => $model->user_id])]); |
134
|
|
|
} |
135
|
|
|
} else { |
136
|
|
|
if (Yii::$app->request->isAjax) { |
137
|
|
|
return $this->response(Util::collectModelErrors($model)); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this->render('profile', [ |
143
|
|
|
'model' => $model |
144
|
|
|
]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function actionAutocomplete() |
148
|
|
|
{ |
149
|
|
|
$result = []; |
150
|
|
|
if (($term = Yii::$app->request->post('term')) !== null) { |
151
|
|
|
$data = User::find() |
152
|
|
|
->like($term, 'username') |
153
|
|
|
->asArray() |
154
|
|
|
->limit(10) |
155
|
|
|
->all(); |
156
|
|
|
|
157
|
|
|
foreach ($data as $item) { |
158
|
|
|
$result[] = [ |
159
|
|
|
'text' => $item['username'], |
160
|
|
|
'id' => $item['id'] |
161
|
|
|
]; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
return $this->response($result); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|