Passed
Push — master ( cc7747...d2190f )
by Mihail
10:36
created

User::actionUpdate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 21
loc 21
rs 9.3142
c 0
b 0
f 0
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\AdminController;
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
use Ffcms\Core\Helper\Type\Arr;
17
use Ffcms\Core\Helper\Type\Obj;
18
19
20
/**
21
 * Class User. Admin controller of user application.
22
 * @package Apps\Controller\Admin
23
 */
24
class User extends AdminController
25
{
26
    const VERSION = 0.1;
27
    const ITEM_PER_PAGE = 10;
28
29
    public $type = 'app';
30
31
    /**
32
     * List all users as table
33
     * @return string
34
     * @throws \Ffcms\Core\Exception\NativeException
35
     * @throws \Ffcms\Core\Exception\SyntaxException
36
     */
37 View Code Duplication
    public function actionIndex()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
38
    {
39
        // init Active Record
40
        $query = new UserRecords();
41
42
        // set current page and offset
43
        $page = (int)$this->request->query->get('page', 0);
44
        $offset = $page * self::ITEM_PER_PAGE;
45
46
        // build pagination
47
        $pagination = new SimplePagination([
48
            'url' => ['user/index'],
49
            'page' => $page,
50
            'step' => self::ITEM_PER_PAGE,
51
            'total' => $query->count()
52
        ]);
53
54
        // build listing objects
55
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
56
57
        // display viewer
58
        return $this->view->render('index', [
59
            'records' => $records,
60
            'pagination' => $pagination
61
        ]);
62
    }
63
64
    /**
65
     * Edit user profile by id
66
     * @param int $id
67
     * @return string
68
     * @throws \Ffcms\Core\Exception\NativeException
69
     * @throws \Ffcms\Core\Exception\SyntaxException
70
     */
71 View Code Duplication
    public function actionUpdate($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
72
    {
73
        $user = UserRecords::findOrNew($id);
74
        // generate model data based on user object
75
        $model = new FormUserUpdate($user);
76
77
        // check is form is sended
78
        if ($model->send()) {
79
            if ($model->validate()) { // check validation
80
                $model->save();
81
                App::$Session->getFlashBag()->add('success', __('Data was successful updated'));
82
            } else {
83
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
84
            }
85
        }
86
87
        // render viewer
88
        return $this->view->render('user_update', [
89
            'model' => $model
90
        ]);
91
    }
92
93
    /**
94
     * Delete user row from database
95
     * @param int $id
96
     * @return string
97
     * @throws \Ffcms\Core\Exception\SyntaxException
98
     * @throws \Ffcms\Core\Exception\NativeException
99
     * @throws NotFoundException
100
     */
101
    public function actionDelete($id = null)
102
    {
103
        // check if id is passed or get data from GET as array ids
104 View Code Duplication
        if ($id === 0 || (int)$id < 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
105
            $ids = $this->request->query->get('selected');
106
            if (Obj::isArray($ids) && Arr::onlyNumericValues($ids)) {
107
                $id = $ids;
108
            } else {
109
                throw new NotFoundException('Bad conditions');
110
            }
111
        } else {
112
            $id = [$id];
113
        }
114
115
        // initialize delete model
116
        $model = new FormUserDelete($id);
117
118
        // check if users is found
119
        if ($model->users === null) {
120
            throw new NotFoundException(__('Users are not found'));
121
        }
122
123
        // check if delete is submited
124
        if ($model->send() && $model->validate()) {
125
            $model->delete();
126
            App::$Session->getFlashBag()->add('success', __('Users and them data are successful removed'));
127
            $this->response->redirect('user/index');
128
        }
129
130
        // set view response
131
        return $this->view->render('user_delete', [
132
            'model' => $model
133
        ]);
134
    }
135
136
    /**
137
     * Show all role groups
138
     * @return string
139
     * @throws \Ffcms\Core\Exception\NativeException
140
     * @throws \Ffcms\Core\Exception\SyntaxException
141
     */
142
    public function actionGrouplist()
143
    {
144
        // get all roles
145
        $roles = Role::all();
146
147
        return $this->view->render('group_list', [
148
            'records' => $roles
149
        ]);
150
    }
151
152
    /**
153
     * Edit and add groups
154
     * @param int $id
155
     * @return string
156
     * @throws \Ffcms\Core\Exception\SyntaxException
157
     * @throws \Ffcms\Core\Exception\NativeException
158
     */
159 View Code Duplication
    public function actionGroupUpdate($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
160
    {
161
        // find role or create new object
162
        $role = Role::findOrNew($id);
163
164
        $model = new FormUserGroupUpdate($role);
165
        if ($model->send()) { // work with post request
166
            if ($model->validate()) {
167
                $model->save();
168
                App::$Session->getFlashBag()->add('success', __('Data was successful updated'));
169
            } else {
170
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
171
            }
172
        }
173
174
        // render view
175
        return $this->view->render('group_update', [
176
            'model' => $model
177
        ]);
178
    }
179
180
    /**
181
     * User identity settings
182
     * @return string
183
     * @throws \Ffcms\Core\Exception\SyntaxException
184
     * @throws \Ffcms\Core\Exception\NativeException
185
     */
186 View Code Duplication
    public function actionSettings()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
187
    {
188
        // load model and pass property's as argument
189
        $model = new FormUserSettings($this->getConfigs());
190
191
        if ($model->send()) {
192
            if ($model->validate()) {
193
                $this->setConfigs($model->getAllProperties());
194
                App::$Session->getFlashBag()->add('success', __('Settings is successful updated'));
195
                $this->response->redirect('user/index');
196
            } else {
197
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
198
            }
199
        }
200
201
        // render view
202
        return $this->view->render('settings', [
203
            'model' => $model
204
        ]);
205
    }
206
207
    /**
208
     * Send invite to user by email
209
     * @return string
210
     * @throws \Ffcms\Core\Exception\SyntaxException
211
     * @throws \Ffcms\Core\Exception\NativeException
212
     */
213 View Code Duplication
    public function actionInvite()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
214
    {
215
        // init model
216
        $model = new FormInviteSend();
217
218
        if ($model->send()) {
219
            if ($model->validate()) {
220
                if ($model->make()) {
221
                    App::$Session->getFlashBag()->add('success', __('Invite was successful send!'));
222
                } else {
223
                    App::$Session->getFlashBag()->add('error', __('Mail server connection is failed!'));
224
                }
225
            } else {
226
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
227
            }
228
        }
229
230
        // render view
231
        return $this->view->render('invite', [
232
            'model' => $model
233
        ]);
234
    }
235
}