ControllerUserUserPermission   F
last analyzed

Complexity

Total Complexity 68

Size/Duplication

Total Lines 454
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 250
c 0
b 0
f 0
dl 0
loc 454
rs 2.96
wmc 68

8 Methods

Rating   Name   Duplication   Size   Complexity  
A validateForm() 0 11 4
F getList() 0 145 17
A add() 0 31 6
A edit() 0 31 6
F getForm() 0 159 23
A validateDelete() 0 17 4
A index() 0 9 1
B delete() 0 33 7

How to fix   Complexity   

Complex Class

Complex classes like ControllerUserUserPermission often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ControllerUserUserPermission, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/* 	Divine CMS - Open source CMS for widespread use.
4
    Copyright (c) 2019 Mykola Burakov ([email protected])
5
6
    See SOURCE.txt for other and additional information.
7
8
    This file is part of Divine CMS.
9
10
    This program is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
15
    This program is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
    GNU General Public License for more details.
19
20
    You should have received a copy of the GNU General Public License
21
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23
class ControllerUserUserPermission extends \Divine\Engine\Core\Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    private $error = array();
26
27
    public function index()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
28
    {
29
        $this->load->language('user/user_group');
30
31
        $this->document->setTitle($this->language->get('heading_title'));
32
33
        $this->load->model('user/user_group');
34
35
        $this->getList();
36
    }
37
38
    public function add()
39
    {
40
        $this->load->language('user/user_group');
41
42
        $this->document->setTitle($this->language->get('heading_title'));
43
44
        $this->load->model('user/user_group');
45
46
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
47
            $this->model_user_user_group->addUserGroup($this->request->post);
48
49
            $this->session->data['success'] = $this->language->get('text_success');
50
51
            $url = '';
52
53
            if (isset($this->request->get['sort'])) {
54
                $url .= '&sort=' . $this->request->get['sort'];
55
            }
56
57
            if (isset($this->request->get['order'])) {
58
                $url .= '&order=' . $this->request->get['order'];
59
            }
60
61
            if (isset($this->request->get['page'])) {
62
                $url .= '&page=' . $this->request->get['page'];
63
            }
64
65
            $this->response->redirect($this->url->link('user/user_permission', 'token=' . $this->session->data['token'] . $url, true));
66
        }
67
68
        $this->getForm();
69
    }
70
71
    public function edit()
72
    {
73
        $this->load->language('user/user_group');
74
75
        $this->document->setTitle($this->language->get('heading_title'));
76
77
        $this->load->model('user/user_group');
78
79
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
80
            $this->model_user_user_group->editUserGroup($this->request->get['user_group_id'], $this->request->post);
81
82
            $this->session->data['success'] = $this->language->get('text_success');
83
84
            $url = '';
85
86
            if (isset($this->request->get['sort'])) {
87
                $url .= '&sort=' . $this->request->get['sort'];
88
            }
89
90
            if (isset($this->request->get['order'])) {
91
                $url .= '&order=' . $this->request->get['order'];
92
            }
93
94
            if (isset($this->request->get['page'])) {
95
                $url .= '&page=' . $this->request->get['page'];
96
            }
97
98
            $this->response->redirect($this->url->link('user/user_permission', 'token=' . $this->session->data['token'] . $url, true));
99
        }
100
101
        $this->getForm();
102
    }
103
104
    public function delete()
105
    {
106
        $this->load->language('user/user_group');
107
108
        $this->document->setTitle($this->language->get('heading_title'));
109
110
        $this->load->model('user/user_group');
111
112
        if (isset($this->request->post['selected']) && $this->validateDelete()) {
113
            foreach ($this->request->post['selected'] as $user_group_id) {
114
                $this->model_user_user_group->deleteUserGroup($user_group_id);
115
            }
116
117
            $this->session->data['success'] = $this->language->get('text_success');
118
119
            $url = '';
120
121
            if (isset($this->request->get['sort'])) {
122
                $url .= '&sort=' . $this->request->get['sort'];
123
            }
124
125
            if (isset($this->request->get['order'])) {
126
                $url .= '&order=' . $this->request->get['order'];
127
            }
128
129
            if (isset($this->request->get['page'])) {
130
                $url .= '&page=' . $this->request->get['page'];
131
            }
132
133
            $this->response->redirect($this->url->link('user/user_permission', 'token=' . $this->session->data['token'] . $url, true));
134
        }
135
136
        $this->getList();
137
    }
138
139
    protected function getList()
140
    {
141
        if (isset($this->request->get['sort'])) {
142
            $sort = $this->request->get['sort'];
143
        } else {
144
            $sort = 'name';
145
        }
146
147
        if (isset($this->request->get['order'])) {
148
            $order = $this->request->get['order'];
149
        } else {
150
            $order = 'ASC';
151
        }
152
153
        if (isset($this->request->get['page'])) {
154
            $page = $this->request->get['page'];
155
        } else {
156
            $page = 1;
157
        }
158
159
        $url = '';
160
161
        if (isset($this->request->get['sort'])) {
162
            $url .= '&sort=' . $this->request->get['sort'];
163
        }
164
165
        if (isset($this->request->get['order'])) {
166
            $url .= '&order=' . $this->request->get['order'];
167
        }
168
169
        if (isset($this->request->get['page'])) {
170
            $url .= '&page=' . $this->request->get['page'];
171
        }
172
173
        $data['breadcrumbs'] = array();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
174
175
        $data['breadcrumbs'][] = array(
176
            'text' => $this->language->get('text_home'),
177
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
178
        );
179
180
        $data['breadcrumbs'][] = array(
181
            'text' => $this->language->get('heading_title'),
182
            'href' => $this->url->link('user/user_permission', 'token=' . $this->session->data['token'] . $url, true)
183
        );
184
185
        $data['add'] = $this->url->link('user/user_permission/add', 'token=' . $this->session->data['token'] . $url, true);
186
        $data['delete'] = $this->url->link('user/user_permission/delete', 'token=' . $this->session->data['token'] . $url, true);
187
188
        $data['user_groups'] = array();
189
190
        $filter_data = array(
191
            'sort'  => $sort,
192
            'order' => $order,
193
            'start' => ($page - 1) * $this->config->get('config_limit_admin'),
194
            'limit' => $this->config->get('config_limit_admin')
195
        );
196
197
        $user_group_total = $this->model_user_user_group->getTotalUserGroups();
198
199
        $results = $this->model_user_user_group->getUserGroups($filter_data);
200
201
        foreach ($results as $result) {
202
            $data['user_groups'][] = array(
203
                'user_group_id' => $result['user_group_id'],
204
                'name'          => $result['name'],
205
                'edit'          => $this->url->link('user/user_permission/edit', 'token=' . $this->session->data['token'] . '&user_group_id=' . $result['user_group_id'] . $url, true)
206
            );
207
        }
208
209
        $data['heading_title'] = $this->language->get('heading_title');
210
211
        $data['text_list'] = $this->language->get('text_list');
212
        $data['text_no_results'] = $this->language->get('text_no_results');
213
        $data['text_confirm'] = $this->language->get('text_confirm');
214
215
        $data['column_name'] = $this->language->get('column_name');
216
        $data['column_action'] = $this->language->get('column_action');
217
218
        $data['button_add'] = $this->language->get('button_add');
219
        $data['button_edit'] = $this->language->get('button_edit');
220
        $data['button_delete'] = $this->language->get('button_delete');
221
222
        if (isset($this->error['warning'])) {
223
            $data['error_warning'] = $this->error['warning'];
224
        } else {
225
            $data['error_warning'] = '';
226
        }
227
228
        if (isset($this->session->data['success'])) {
229
            $data['success'] = $this->session->data['success'];
230
231
            unset($this->session->data['success']);
232
        } else {
233
            $data['success'] = '';
234
        }
235
236
        if (isset($this->request->post['selected'])) {
237
            $data['selected'] = (array)$this->request->post['selected'];
238
        } else {
239
            $data['selected'] = array();
240
        }
241
242
        $url = '';
243
244
        if ($order == 'ASC') {
245
            $url .= '&order=DESC';
246
        } else {
247
            $url .= '&order=ASC';
248
        }
249
250
        if (isset($this->request->get['page'])) {
251
            $url .= '&page=' . $this->request->get['page'];
252
        }
253
254
        $data['sort_name'] = $this->url->link('user/user_permission', 'token=' . $this->session->data['token'] . '&sort=name' . $url, true);
255
256
        $url = '';
257
258
        if (isset($this->request->get['sort'])) {
259
            $url .= '&sort=' . $this->request->get['sort'];
260
        }
261
262
        if (isset($this->request->get['order'])) {
263
            $url .= '&order=' . $this->request->get['order'];
264
        }
265
266
        $pagination = new \Divine\Engine\Library\Pagination();
267
        $pagination->total = $user_group_total;
268
        $pagination->page = $page;
269
        $pagination->limit = $this->config->get('config_limit_admin');
270
        $pagination->url = $this->url->link('user/user_permission', 'token=' . $this->session->data['token'] . $url . '&page={page}', true);
271
272
        $data['pagination'] = $pagination->render();
273
274
        $data['results'] = sprintf($this->language->get('text_pagination'), ($user_group_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($user_group_total - $this->config->get('config_limit_admin'))) ? $user_group_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $user_group_total, ceil($user_group_total / $this->config->get('config_limit_admin')));
275
276
        $data['sort'] = $sort;
277
        $data['order'] = $order;
278
279
        $data['header'] = $this->load->controller('common/header');
280
        $data['column'] = $this->load->controller('common/column_left');
281
        $data['footer'] = $this->load->controller('common/footer');
282
283
        $this->response->setOutput($this->load->view('user/user_group_list', $data));
284
    }
285
286
    protected function getForm()
287
    {
288
        $data['heading_title'] = $this->language->get('heading_title');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
289
290
        $data['text_form'] = !isset($this->request->get['user_group_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
291
        $data['text_select_all'] = $this->language->get('text_select_all');
292
        $data['text_unselect_all'] = $this->language->get('text_unselect_all');
293
294
        $data['entry_name'] = $this->language->get('entry_name');
295
        $data['entry_access'] = $this->language->get('entry_access');
296
        $data['entry_modify'] = $this->language->get('entry_modify');
297
298
        $data['button_save'] = $this->language->get('button_save');
299
        $data['button_cancel'] = $this->language->get('button_cancel');
300
301
        if (isset($this->error['warning'])) {
302
            $data['error_warning'] = $this->error['warning'];
303
        } else {
304
            $data['error_warning'] = '';
305
        }
306
307
        if (isset($this->error['name'])) {
308
            $data['error_name'] = $this->error['name'];
309
        } else {
310
            $data['error_name'] = '';
311
        }
312
313
        $url = '';
314
315
        if (isset($this->request->get['sort'])) {
316
            $url .= '&sort=' . $this->request->get['sort'];
317
        }
318
319
        if (isset($this->request->get['order'])) {
320
            $url .= '&order=' . $this->request->get['order'];
321
        }
322
323
        if (isset($this->request->get['page'])) {
324
            $url .= '&page=' . $this->request->get['page'];
325
        }
326
327
        $data['breadcrumbs'] = array();
328
329
        $data['breadcrumbs'][] = array(
330
            'text' => $this->language->get('text_home'),
331
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
332
        );
333
334
        $data['breadcrumbs'][] = array(
335
            'text' => $this->language->get('heading_title'),
336
            'href' => $this->url->link('user/user_permission', 'token=' . $this->session->data['token'] . $url, true)
337
        );
338
339
        if (!isset($this->request->get['user_group_id'])) {
340
            $data['action'] = $this->url->link('user/user_permission/add', 'token=' . $this->session->data['token'] . $url, true);
341
        } else {
342
            $data['action'] = $this->url->link('user/user_permission/edit', 'token=' . $this->session->data['token'] . '&user_group_id=' . $this->request->get['user_group_id'] . $url, true);
343
        }
344
345
        $data['cancel'] = $this->url->link('user/user_permission', 'token=' . $this->session->data['token'] . $url, true);
346
347
        if (isset($this->request->get['user_group_id']) && $this->request->server['REQUEST_METHOD'] != 'POST') {
348
            $user_group_info = $this->model_user_user_group->getUserGroup($this->request->get['user_group_id']);
349
        }
350
351
        if (isset($this->request->post['name'])) {
352
            $data['name'] = $this->request->post['name'];
353
        } elseif (!empty($user_group_info)) {
354
            $data['name'] = $user_group_info['name'];
355
        } else {
356
            $data['name'] = '';
357
        }
358
359
        $ignore = array(
360
            'common/dashboard',
361
            'common/startup',
362
            'common/login',
363
            'common/logout',
364
            'common/forgotten',
365
            'common/reset',
366
            'common/footer',
367
            'common/header',
368
            'error/not_found',
369
            'error/permission'
370
        );
371
372
        $data['permissions'] = array();
373
374
        $data['name_permission'] = array();
375
        $old_heading_title = '';
376
377
        $files = array();
378
379
        // Make path into an array
380
        $path = array(SR_APPLICATION . 'controller/*');
381
382
        // While the path array is still populated keep looping through
383
        while (count($path) != 0) {
384
            $next = array_shift($path);
385
386
            foreach (glob($next) as $file) {
387
                // If directory add to path array
388
                if (is_dir($file)) {
389
                    $path[] = $file . '/*';
390
                }
391
392
                // Add the file to the files to be deleted array
393
                if (is_file($file)) {
394
                    $files[] = $file;
395
                }
396
            }
397
        }
398
399
        // Sort the file array
400
        sort($files);
401
402
        foreach ($files as $file) {
403
            $controller = substr($file, strlen(SR_APPLICATION . 'controller/'));
404
405
            $permission = substr($controller, 0, strrpos($controller, '.'));
406
407
            if (!in_array($permission, $ignore)) {
408
                $data['permissions'][] = $permission;
409
410
                $this->load->language($permission);
411
412
                $heading_title = strip_tags($this->language->get('heading_title'));
413
414
                if ($heading_title == $old_heading_title) {
415
                    $heading_title = '';
416
                } else {
417
                    $old_heading_title = $heading_title;
418
                }
419
420
                $data['name_permissions'][$permission] = $heading_title;
421
            }
422
        }
423
424
        if (isset($this->request->post['permission']['access'])) {
425
            $data['access'] = $this->request->post['permission']['access'];
426
        } elseif (isset($user_group_info['permission']['access'])) {
427
            $data['access'] = $user_group_info['permission']['access'];
428
        } else {
429
            $data['access'] = array();
430
        }
431
432
        if (isset($this->request->post['permission']['modify'])) {
433
            $data['modify'] = $this->request->post['permission']['modify'];
434
        } elseif (isset($user_group_info['permission']['modify'])) {
435
            $data['modify'] = $user_group_info['permission']['modify'];
436
        } else {
437
            $data['modify'] = array();
438
        }
439
440
        $data['header'] = $this->load->controller('common/header');
441
        $data['column'] = $this->load->controller('common/column_left');
442
        $data['footer'] = $this->load->controller('common/footer');
443
444
        $this->response->setOutput($this->load->view('user/user_group_form', $data));
445
    }
446
447
    protected function validateForm()
448
    {
449
        if (!$this->user->hasPermission('modify', 'user/user_permission')) {
450
            $this->error['warning'] = $this->language->get('error_permission');
451
        }
452
453
        if ((\voku\helper\UTF8::strlen($this->request->post['name']) < 3) || (\voku\helper\UTF8::strlen($this->request->post['name']) > 64)) {
454
            $this->error['name'] = $this->language->get('error_name');
455
        }
456
457
        return !$this->error;
458
    }
459
460
    protected function validateDelete()
461
    {
462
        if (!$this->user->hasPermission('modify', 'user/user_permission')) {
463
            $this->error['warning'] = $this->language->get('error_permission');
464
        }
465
466
        $this->load->model('user/user');
467
468
        foreach ($this->request->post['selected'] as $user_group_id) {
469
            $user_total = $this->model_user_user->getTotalUsersByGroupId($user_group_id);
470
471
            if ($user_total) {
472
                $this->error['warning'] = sprintf($this->language->get('error_user'), $user_total);
473
            }
474
        }
475
476
        return !$this->error;
477
    }
478
}
479