1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\Role; |
6
|
|
|
use Apps\Model\Admin\User\FormInviteSend; |
7
|
|
|
use Apps\Model\Admin\User\FormUserDelete; |
8
|
|
|
use Apps\Model\Admin\User\FormUserGroupUpdate; |
9
|
|
|
use Apps\Model\Admin\User\FormUserSettings; |
10
|
|
|
use Apps\Model\Admin\User\FormUserUpdate; |
11
|
|
|
use Extend\Core\Arch\AdminAppController; |
12
|
|
|
use Apps\ActiveRecord\User as UserRecords; |
13
|
|
|
use Ffcms\Core\App; |
14
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
15
|
|
|
use Ffcms\Core\Helper\HTML\SimplePagination; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class User extends AdminAppController |
19
|
|
|
{ |
20
|
|
|
const ITEM_PER_PAGE = 10; |
21
|
|
|
|
22
|
|
|
// list users |
23
|
|
View Code Duplication |
public function actionIndex() |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
// init Active Record |
26
|
|
|
$query = new UserRecords(); |
27
|
|
|
|
28
|
|
|
// set current page and offset |
29
|
|
|
$page = (int)App::$Request->query->get('page'); |
30
|
|
|
$offset = $page * self::ITEM_PER_PAGE; |
31
|
|
|
|
32
|
|
|
// build pagination |
33
|
|
|
$pagination = new SimplePagination([ |
34
|
|
|
'url' => ['user/index'], |
35
|
|
|
'page' => $page, |
36
|
|
|
'step' => self::ITEM_PER_PAGE, |
37
|
|
|
'total' => $query->count() |
|
|
|
|
38
|
|
|
]); |
39
|
|
|
|
40
|
|
|
// build listing objects |
41
|
|
|
$records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get(); |
|
|
|
|
42
|
|
|
|
43
|
|
|
// display viewer |
44
|
|
|
$this->response = App::$View->render('index', [ |
45
|
|
|
'records' => $records, |
46
|
|
|
'pagination' => $pagination |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// edit user profiles |
51
|
|
View Code Duplication |
public function actionUpdate($id) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$user = UserRecords::findOrNew($id); |
54
|
|
|
|
55
|
|
|
// find user identify object |
56
|
|
|
//$user = App::$User->identity($id); |
|
|
|
|
57
|
|
|
// generate model data based on user object |
58
|
|
|
$model = new FormUserUpdate($user); |
|
|
|
|
59
|
|
|
|
60
|
|
|
// check is form is sended |
61
|
|
|
if ($model->send()) { |
62
|
|
|
if ($model->validate()) { // check validation |
63
|
|
|
$model->save(); |
64
|
|
|
App::$Session->getFlashBag()->add('success', __('Data was successful updated')); |
65
|
|
|
} else { |
66
|
|
|
App::$Session->getFlashBag()->add('error', __('Form validation is failed')); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// render viewer |
71
|
|
|
$this->response = App::$View->render('user_update', [ |
72
|
|
|
'model' => $model->export() |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Delete user data |
78
|
|
|
* @param $id |
79
|
|
|
* @throws NotFoundException |
80
|
|
|
*/ |
81
|
|
|
public function actionDelete($id) |
82
|
|
|
{ |
83
|
|
|
if ($id < !1 || !App::$User->isExist($id)) { |
84
|
|
|
throw new NotFoundException('User is not founded'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// get user object and load model |
88
|
|
|
$user = App::$User->identity($id); |
89
|
|
|
$model = new FormUserDelete($user); |
|
|
|
|
90
|
|
|
|
91
|
|
|
if ($model->send()) { |
92
|
|
|
$model->delete(); |
93
|
|
|
App::$Response->redirect('user/index'); |
94
|
|
|
} else { |
95
|
|
|
App::$Session->getFlashBag()->add('error', __('There is no way to revert this action! Be careful!')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// set view response |
99
|
|
|
$this->response = App::$View->render('user_delete', [ |
100
|
|
|
'model' => $model |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Show all role groups |
106
|
|
|
*/ |
107
|
|
|
public function actionGrouplist() |
108
|
|
|
{ |
109
|
|
|
// get all roles |
110
|
|
|
$roles = Role::getAll(); |
111
|
|
|
|
112
|
|
|
$this->response = App::$View->render('group_list', [ |
113
|
|
|
'records' => $roles |
114
|
|
|
]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Edit and add groups |
119
|
|
|
* @param $id |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
public function actionGroupUpdate($id) |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
// find role or create new object |
124
|
|
|
$role = Role::findOrNew($id); |
125
|
|
|
|
126
|
|
|
$model = new FormUserGroupUpdate($role); |
|
|
|
|
127
|
|
|
if ($model->send()) { // work with post request |
128
|
|
|
if ($model->validate()) { |
129
|
|
|
$model->save(); |
130
|
|
|
App::$Session->getFlashBag()->add('success', __('Data was successful updated')); |
131
|
|
|
} else { |
132
|
|
|
App::$Session->getFlashBag()->add('error', __('Form validation is failed')); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// render view |
137
|
|
|
$this->response = App::$View->render('group_update', [ |
138
|
|
|
'model' => $model |
139
|
|
|
]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* User identity settings |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
public function actionSettings() |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
// load model and pass property's as argument |
148
|
|
|
$model = new FormUserSettings($this->getConfigs()); |
149
|
|
|
|
150
|
|
|
if ($model->send()) { |
151
|
|
|
if ($model->validate()) { |
152
|
|
|
$this->setConfigs($model->getAllProperties()); |
153
|
|
|
App::$Response->redirect('user/index'); |
154
|
|
|
} else { |
155
|
|
|
App::$Session->getFlashBag()->add('error', __('Form validation is failed')); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// render view |
160
|
|
|
$this->response = App::$View->render('settings', [ |
161
|
|
|
'model' => $model->export() |
162
|
|
|
]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Send invite to users |
167
|
|
|
*/ |
168
|
|
View Code Duplication |
public function actionInvite() |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
// init model |
171
|
|
|
$model = new FormInviteSend(); |
172
|
|
|
|
173
|
|
|
if ($model->send()) { |
174
|
|
|
if ($model->validate()) { |
175
|
|
|
if ($model->make()) { |
176
|
|
|
App::$Session->getFlashBag()->add('success', __('Invite was successful send!')); |
177
|
|
|
} else { |
178
|
|
|
App::$Session->getFlashBag()->add('error', __('Mail server connection is failed!')); |
179
|
|
|
} |
180
|
|
|
} else { |
181
|
|
|
App::$Session->getFlashBag()->add('error', __('Form validation is failed')); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// render view |
186
|
|
|
$this->response = App::$View->render('invite', [ |
187
|
|
|
'model' => $model |
188
|
|
|
]); |
189
|
|
|
} |
190
|
|
|
} |
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.