Passed
Push — master ( f5ccad...bac51f )
by Mihail
05:44
created

User::actionDelete()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 32
Code Lines 18

Duplication

Lines 10
Ratio 31.25 %

Importance

Changes 4
Bugs 3 Features 0
Metric Value
cc 7
eloc 18
c 4
b 3
f 0
nc 7
nop 1
dl 10
loc 32
rs 6.7272
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)App::$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 App::$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
75
        // find user identify object
76
        //$user = App::$User->identity($id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
77
        // generate model data based on user object
78
        $model = new FormUserUpdate($user);
79
80
        // check is form is sended
81
        if ($model->send()) {
82
            if ($model->validate()) { // check validation
83
                $model->save();
84
                App::$Session->getFlashBag()->add('success', __('Data was successful updated'));
85
            } else {
86
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
87
            }
88
        }
89
90
        // render viewer
91
        return App::$View->render('user_update', [
92
            'model' => $model->filter()
93
        ]);
94
    }
95
96
    /**
97
     * Delete user row from database
98
     * @param int $id
99
     * @return string
100
     * @throws \Ffcms\Core\Exception\SyntaxException
101
     * @throws \Ffcms\Core\Exception\NativeException
102
     * @throws NotFoundException
103
     */
104
    public function actionDelete($id = null)
105
    {
106
        // check if id is passed or get data from GET as array ids
107 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...
108
            $ids = App::$Request->query->get('selected');
109
            if (Obj::isArray($ids) && Arr::onlyNumericValues($ids)) {
110
                $id = $ids;
111
            } else {
112
                throw new NotFoundException('Bad conditions');
113
            }
114
        } else {
115
            $id = [$id];
116
        }
117
118
        // initialize delete model
119
        $model = new FormUserDelete($id);
120
121
        if ($model->users === null) {
122
            throw new NotFoundException(__('Users are not found'));
123
        }
124
125
        if ($model->send()) {
126
            $model->delete();
127
            App::$Session->getFlashBag()->add('success', __('Users and them data are successful removed'));
128
            App::$Response->redirect('user/index');
129
        }
130
131
        // set view response
132
        return App::$View->render('user_delete', [
133
            'model' => $model
134
        ]);
135
    }
136
137
    /**
138
     * Show all role groups
139
     * @return string
140
     * @throws \Ffcms\Core\Exception\NativeException
141
     * @throws \Ffcms\Core\Exception\SyntaxException
142
     */
143
    public function actionGrouplist()
144
    {
145
        // get all roles
146
        $roles = Role::getAll();
147
148
        return App::$View->render('group_list', [
149
            'records' => $roles
150
        ]);
151
    }
152
153
    /**
154
     * Edit and add groups
155
     * @param int $id
156
     * @return string
157
     * @throws \Ffcms\Core\Exception\SyntaxException
158
     * @throws \Ffcms\Core\Exception\NativeException
159
     */
160 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...
161
    {
162
        // find role or create new object
163
        $role = Role::findOrNew($id);
164
165
        $model = new FormUserGroupUpdate($role);
166
        if ($model->send()) { // work with post request
167
            if ($model->validate()) {
168
                $model->save();
169
                App::$Session->getFlashBag()->add('success', __('Data was successful updated'));
170
            } else {
171
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
172
            }
173
        }
174
175
        // render view
176
        return App::$View->render('group_update', [
177
            'model' => $model->filter()
178
        ]);
179
    }
180
181
    /**
182
     * User identity settings
183
     * @return string
184
     * @throws \Ffcms\Core\Exception\SyntaxException
185
     * @throws \Ffcms\Core\Exception\NativeException
186
     */
187 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...
188
    {
189
        // load model and pass property's as argument
190
        $model = new FormUserSettings($this->getConfigs());
191
192
        if ($model->send()) {
193
            if ($model->validate()) {
194
                $this->setConfigs($model->getAllProperties());
195
                App::$Session->getFlashBag()->add('success', __('Settings is successful updated'));
196
                App::$Response->redirect('user/index');
197
            } else {
198
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
199
            }
200
        }
201
202
        // render view
203
        return App::$View->render('settings', [
204
            'model' => $model->filter()
205
        ]);
206
    }
207
208
    /**
209
     * Send invite to user by email
210
     * @return string
211
     * @throws \Ffcms\Core\Exception\SyntaxException
212
     * @throws \Ffcms\Core\Exception\NativeException
213
     */
214 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...
215
    {
216
        // init model
217
        $model = new FormInviteSend();
218
219
        if ($model->send()) {
220
            if ($model->validate()) {
221
                if ($model->make()) {
222
                    App::$Session->getFlashBag()->add('success', __('Invite was successful send!'));
223
                } else {
224
                    App::$Session->getFlashBag()->add('error', __('Mail server connection is failed!'));
225
                }
226
            } else {
227
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
228
            }
229
        }
230
231
        // render view
232
        return App::$View->render('invite', [
233
            'model' => $model->filter()
234
        ]);
235
    }
236
}