ControllerUserUser   F
last analyzed

Complexity

Total Complexity 101

Size/Duplication

Total Lines 533
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 310
c 0
b 0
f 0
dl 0
loc 533
rs 2
wmc 101

8 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 9 1
F getForm() 0 190 36
F validateForm() 0 57 23
A edit() 0 31 6
B delete() 0 33 7
A add() 0 31 6
A validateDelete() 0 13 4
F getList() 0 151 18

How to fix   Complexity   

Complex Class

Complex classes like ControllerUserUser 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 ControllerUserUser, 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 ControllerUserUser 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');
30
31
        $this->document->setTitle($this->language->get('heading_title'));
32
33
        $this->load->model('user/user');
34
35
        $this->getList();
36
    }
37
38
    public function add()
39
    {
40
        $this->load->language('user/user');
41
42
        $this->document->setTitle($this->language->get('heading_title'));
43
44
        $this->load->model('user/user');
45
46
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
47
            $this->model_user_user->addUser($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', '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');
74
75
        $this->document->setTitle($this->language->get('heading_title'));
76
77
        $this->load->model('user/user');
78
79
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
80
            $this->model_user_user->editUser($this->request->get['user_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', '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');
107
108
        $this->document->setTitle($this->language->get('heading_title'));
109
110
        $this->load->model('user/user');
111
112
        if (isset($this->request->post['selected']) && $this->validateDelete()) {
113
            foreach ($this->request->post['selected'] as $user_id) {
114
                $this->model_user_user->deleteUser($user_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', '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 = 'username';
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', 'token=' . $this->session->data['token'] . $url, true)
183
        );
184
185
        $data['add'] = $this->url->link('user/user/add', 'token=' . $this->session->data['token'] . $url, true);
186
        $data['delete'] = $this->url->link('user/user/delete', 'token=' . $this->session->data['token'] . $url, true);
187
188
        $data['users'] = 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_total = $this->model_user_user->getTotalUsers();
198
199
        $results = $this->model_user_user->getUsers($filter_data);
200
201
        foreach ($results as $result) {
202
            $data['users'][] = array(
203
                'user_id'    => $result['user_id'],
204
                'username'   => $result['username'],
205
                'status'     => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
206
                'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
207
                'edit'       => $this->url->link('user/user/edit', 'token=' . $this->session->data['token'] . '&user_id=' . $result['user_id'] . $url, true)
208
            );
209
        }
210
211
        $data['heading_title'] = $this->language->get('heading_title');
212
213
        $data['text_list'] = $this->language->get('text_list');
214
        $data['text_no_results'] = $this->language->get('text_no_results');
215
        $data['text_confirm'] = $this->language->get('text_confirm');
216
217
        $data['column_username'] = $this->language->get('column_username');
218
        $data['column_status'] = $this->language->get('column_status');
219
        $data['column_date_added'] = $this->language->get('column_date_added');
220
        $data['column_action'] = $this->language->get('column_action');
221
222
        $data['button_add'] = $this->language->get('button_add');
223
        $data['button_edit'] = $this->language->get('button_edit');
224
        $data['button_delete'] = $this->language->get('button_delete');
225
226
        if (isset($this->error['warning'])) {
227
            $data['error_warning'] = $this->error['warning'];
228
        } else {
229
            $data['error_warning'] = '';
230
        }
231
232
        if (isset($this->session->data['success'])) {
233
            $data['success'] = $this->session->data['success'];
234
235
            unset($this->session->data['success']);
236
        } else {
237
            $data['success'] = '';
238
        }
239
240
        if (isset($this->request->post['selected'])) {
241
            $data['selected'] = (array)$this->request->post['selected'];
242
        } else {
243
            $data['selected'] = array();
244
        }
245
246
        $url = '';
247
248
        if ($order == 'ASC') {
249
            $url .= '&order=DESC';
250
        } else {
251
            $url .= '&order=ASC';
252
        }
253
254
        if (isset($this->request->get['page'])) {
255
            $url .= '&page=' . $this->request->get['page'];
256
        }
257
258
        $data['sort_username'] = $this->url->link('user/user', 'token=' . $this->session->data['token'] . '&sort=username' . $url, true);
259
        $data['sort_status'] = $this->url->link('user/user', 'token=' . $this->session->data['token'] . '&sort=status' . $url, true);
260
        $data['sort_date_added'] = $this->url->link('user/user', 'token=' . $this->session->data['token'] . '&sort=date_added' . $url, true);
261
262
        $url = '';
263
264
        if (isset($this->request->get['sort'])) {
265
            $url .= '&sort=' . $this->request->get['sort'];
266
        }
267
268
        if (isset($this->request->get['order'])) {
269
            $url .= '&order=' . $this->request->get['order'];
270
        }
271
272
        $pagination = new \Divine\Engine\Library\Pagination();
273
        $pagination->total = $user_total;
274
        $pagination->page = $page;
275
        $pagination->limit = $this->config->get('config_limit_admin');
276
        $pagination->url = $this->url->link('user/user', 'token=' . $this->session->data['token'] . $url . '&page={page}', true);
277
278
        $data['pagination'] = $pagination->render();
279
280
        $data['results'] = sprintf($this->language->get('text_pagination'), ($user_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($user_total - $this->config->get('config_limit_admin'))) ? $user_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $user_total, ceil($user_total / $this->config->get('config_limit_admin')));
281
282
        $data['sort'] = $sort;
283
        $data['order'] = $order;
284
285
        $data['header'] = $this->load->controller('common/header');
286
        $data['column'] = $this->load->controller('common/column_left');
287
        $data['footer'] = $this->load->controller('common/footer');
288
289
        $this->response->setOutput($this->load->view('user/user_list', $data));
290
    }
291
292
    protected function getForm()
293
    {
294
        $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...
295
296
        $data['text_form'] = !isset($this->request->get['user_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
297
        $data['text_enabled'] = $this->language->get('text_enabled');
298
        $data['text_disabled'] = $this->language->get('text_disabled');
299
300
        $data['entry_username'] = $this->language->get('entry_username');
301
        $data['entry_user_group'] = $this->language->get('entry_user_group');
302
        $data['entry_password'] = $this->language->get('entry_password');
303
        $data['entry_confirm'] = $this->language->get('entry_confirm');
304
        $data['entry_firstname'] = $this->language->get('entry_firstname');
305
        $data['entry_lastname'] = $this->language->get('entry_lastname');
306
        $data['entry_email'] = $this->language->get('entry_email');
307
        $data['entry_image'] = $this->language->get('entry_image');
308
        $data['entry_status'] = $this->language->get('entry_status');
309
310
        $data['button_save'] = $this->language->get('button_save');
311
        $data['button_cancel'] = $this->language->get('button_cancel');
312
313
        if (isset($this->error['warning'])) {
314
            $data['error_warning'] = $this->error['warning'];
315
        } else {
316
            $data['error_warning'] = '';
317
        }
318
319
        if (isset($this->error['username'])) {
320
            $data['error_username'] = $this->error['username'];
321
        } else {
322
            $data['error_username'] = '';
323
        }
324
325
        if (isset($this->error['password'])) {
326
            $data['error_password'] = $this->error['password'];
327
        } else {
328
            $data['error_password'] = '';
329
        }
330
331
        if (isset($this->error['confirm'])) {
332
            $data['error_confirm'] = $this->error['confirm'];
333
        } else {
334
            $data['error_confirm'] = '';
335
        }
336
337
        if (isset($this->error['firstname'])) {
338
            $data['error_firstname'] = $this->error['firstname'];
339
        } else {
340
            $data['error_firstname'] = '';
341
        }
342
343
        if (isset($this->error['lastname'])) {
344
            $data['error_lastname'] = $this->error['lastname'];
345
        } else {
346
            $data['error_lastname'] = '';
347
        }
348
349
        if (isset($this->error['email'])) {
350
            $data['error_email'] = $this->error['email'];
351
        } else {
352
            $data['error_email'] = '';
353
        }
354
355
        $url = '';
356
357
        if (isset($this->request->get['sort'])) {
358
            $url .= '&sort=' . $this->request->get['sort'];
359
        }
360
361
        if (isset($this->request->get['order'])) {
362
            $url .= '&order=' . $this->request->get['order'];
363
        }
364
365
        if (isset($this->request->get['page'])) {
366
            $url .= '&page=' . $this->request->get['page'];
367
        }
368
369
        $data['breadcrumbs'] = array();
370
371
        $data['breadcrumbs'][] = array(
372
            'text' => $this->language->get('text_home'),
373
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
374
        );
375
376
        $data['breadcrumbs'][] = array(
377
            'text' => $this->language->get('heading_title'),
378
            'href' => $this->url->link('user/user', 'token=' . $this->session->data['token'] . $url, true)
379
        );
380
381
        if (!isset($this->request->get['user_id'])) {
382
            $data['action'] = $this->url->link('user/user/add', 'token=' . $this->session->data['token'] . $url, true);
383
        } else {
384
            $data['action'] = $this->url->link('user/user/edit', 'token=' . $this->session->data['token'] . '&user_id=' . $this->request->get['user_id'] . $url, true);
385
        }
386
387
        $data['cancel'] = $this->url->link('user/user', 'token=' . $this->session->data['token'] . $url, true);
388
389
        if (isset($this->request->get['user_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
390
            $user_info = $this->model_user_user->getUser($this->request->get['user_id']);
391
        }
392
393
        if (isset($this->request->post['username'])) {
394
            $data['username'] = $this->request->post['username'];
395
        } elseif (!empty($user_info)) {
396
            $data['username'] = $user_info['username'];
397
        } else {
398
            $data['username'] = '';
399
        }
400
401
        if (isset($this->request->post['user_group_id'])) {
402
            $data['user_group_id'] = $this->request->post['user_group_id'];
403
        } elseif (!empty($user_info)) {
404
            $data['user_group_id'] = $user_info['user_group_id'];
405
        } else {
406
            $data['user_group_id'] = '';
407
        }
408
409
        $this->load->model('user/user_group');
410
411
        $data['user_groups'] = $this->model_user_user_group->getUserGroups();
412
413
        if (isset($this->request->post['password'])) {
414
            $data['password'] = $this->request->post['password'];
415
        } else {
416
            $data['password'] = '';
417
        }
418
419
        if (isset($this->request->post['confirm'])) {
420
            $data['confirm'] = $this->request->post['confirm'];
421
        } else {
422
            $data['confirm'] = '';
423
        }
424
425
        if (isset($this->request->post['firstname'])) {
426
            $data['firstname'] = $this->request->post['firstname'];
427
        } elseif (!empty($user_info)) {
428
            $data['firstname'] = $user_info['firstname'];
429
        } else {
430
            $data['firstname'] = '';
431
        }
432
433
        if (isset($this->request->post['lastname'])) {
434
            $data['lastname'] = $this->request->post['lastname'];
435
        } elseif (!empty($user_info)) {
436
            $data['lastname'] = $user_info['lastname'];
437
        } else {
438
            $data['lastname'] = '';
439
        }
440
441
        if (isset($this->request->post['email'])) {
442
            $data['email'] = $this->request->post['email'];
443
        } elseif (!empty($user_info)) {
444
            $data['email'] = $user_info['email'];
445
        } else {
446
            $data['email'] = '';
447
        }
448
449
        if (isset($this->request->post['image'])) {
450
            $data['image'] = $this->request->post['image'];
451
        } elseif (!empty($user_info)) {
452
            $data['image'] = $user_info['image'];
453
        } else {
454
            $data['image'] = '';
455
        }
456
457
        
458
459
        if (isset($this->request->post['image']) && is_file($_SERVER['DOCUMENT_ROOT'] . '/public_html/assets/images/' . $this->request->post['image'])) {
460
            $data['thumb'] = '/public_html/assets/images/' . $this->request->post['image'];
461
        } elseif (!empty($user_info) && $user_info['image'] && is_file($_SERVER['DOCUMENT_ROOT'] . '/public_html/assets/images/' . $user_info['image'])) {
462
            $data['thumb'] = '/public_html/assets/images/' . $user_info['image'];
463
        } else {
464
            $data['thumb'] = '/public_html/assets/images/no_image.png';
465
        }
466
467
        $data['placeholder'] = '/public_html/assets/images/no_image.png';
468
469
        if (isset($this->request->post['status'])) {
470
            $data['status'] = $this->request->post['status'];
471
        } elseif (!empty($user_info)) {
472
            $data['status'] = $user_info['status'];
473
        } else {
474
            $data['status'] = 0;
475
        }
476
477
        $data['header'] = $this->load->controller('common/header');
478
        $data['column'] = $this->load->controller('common/column_left');
479
        $data['footer'] = $this->load->controller('common/footer');
480
481
        $this->response->setOutput($this->load->view('user/user_form', $data));
482
    }
483
484
    protected function validateForm()
485
    {
486
        if (!$this->user->hasPermission('modify', 'user/user')) {
487
            $this->error['warning'] = $this->language->get('error_permission');
488
        }
489
490
        if ((\voku\helper\UTF8::strlen($this->request->post['username']) < 3) || (\voku\helper\UTF8::strlen($this->request->post['username']) > 20)) {
491
            $this->error['username'] = $this->language->get('error_username');
492
        }
493
494
        $user_info = $this->model_user_user->getUserByUsername($this->request->post['username']);
495
496
        if (!isset($this->request->get['user_id'])) {
497
            if ($user_info) {
498
                $this->error['warning'] = $this->language->get('error_exists_username');
499
            }
500
        } else {
501
            if ($user_info && ($this->request->get['user_id'] != $user_info['user_id'])) {
502
                $this->error['warning'] = $this->language->get('error_exists_username');
503
            }
504
        }
505
506
        if ((\voku\helper\UTF8::strlen(trim($this->request->post['firstname'])) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['firstname'])) > 32)) {
507
            $this->error['firstname'] = $this->language->get('error_firstname');
508
        }
509
510
        if ((\voku\helper\UTF8::strlen(trim($this->request->post['lastname'])) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['lastname'])) > 32)) {
511
            $this->error['lastname'] = $this->language->get('error_lastname');
512
        }
513
514
        if ((\voku\helper\UTF8::strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
515
            $this->error['email'] = $this->language->get('error_email');
516
        }
517
518
        $user_info = $this->model_user_user->getUserByEmail($this->request->post['email']);
519
520
        if (!isset($this->request->get['user_id'])) {
521
            if ($user_info) {
522
                $this->error['warning'] = $this->language->get('error_exists_email');
523
            }
524
        } else {
525
            if ($user_info && ($this->request->get['user_id'] != $user_info['user_id'])) {
526
                $this->error['warning'] = $this->language->get('error_exists_email');
527
            }
528
        }
529
530
        if ($this->request->post['password'] || (!isset($this->request->get['user_id']))) {
531
            if ((\voku\helper\UTF8::strlen($this->request->post['password']) < 4) || (\voku\helper\UTF8::strlen($this->request->post['password']) > 20)) {
532
                $this->error['password'] = $this->language->get('error_password');
533
            }
534
535
            if ($this->request->post['password'] != $this->request->post['confirm']) {
536
                $this->error['confirm'] = $this->language->get('error_confirm');
537
            }
538
        }
539
540
        return !$this->error;
541
    }
542
543
    protected function validateDelete()
544
    {
545
        if (!$this->user->hasPermission('modify', 'user/user')) {
546
            $this->error['warning'] = $this->language->get('error_permission');
547
        }
548
549
        foreach ($this->request->post['selected'] as $user_id) {
550
            if ($this->user->getId() == $user_id) {
551
                $this->error['warning'] = $this->language->get('error_account');
552
            }
553
        }
554
555
        return !$this->error;
556
    }
557
}
558