UsersData::editPost()   F
last analyzed

Complexity

Conditions 30
Paths 3404

Size

Total Lines 155
Code Lines 106

Duplication

Lines 33
Ratio 21.29 %

Importance

Changes 0
Metric Value
cc 30
eloc 106
nc 3404
nop 1
dl 33
loc 155
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace FFCMS\Controllers\Admin;
4
5
use FFMVC\Helpers;
6
use FFCMS\{Traits, Controllers, Models, Mappers};
7
8
/**
9
 * Admin Users Data CMS Controller Class.
10
 *
11
 * @author Vijay Mahrra <[email protected]>
12
 * @copyright 2016 Vijay Mahrra
13
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
14
 */
15
class UsersData extends Admin
16
{
17
    /**
18
     * For admin listing and search results
19
     */
20
    use Traits\SearchController;
21
22
    protected $template_path = 'cms/admin/usersdata/';
23
24
25
    /**
26
     *
27
     *
28
     * @param \Base $f3
29
     * @return void
30
     */
31 View Code Duplication
    public function listing(\Base $f3)
32
    {
33
        $view = strtolower(trim(strip_tags($f3->get('REQUEST.view'))));
34
        $view = empty($view) ? 'list.phtml' : $view . '.phtml';
35
        $f3->set('REQUEST.view', $view);
36
37
        $f3->set('results', $this->getListingResults($f3, new Mappers\UsersData));
38
39
        $f3->set('breadcrumbs', [
40
            _('Admin') => 'admin',
41
            _('Users') => 'admin_users_list',
42
            _('Data') => 'admin_usersdata_list',
43
        ]);
44
45
        $f3->set('form', $f3->get('REQUEST'));
46
        echo \View::instance()->render($this->template_path . $view);
47
    }
48
49
50
    /**
51
     *
52
     *
53
     * @param \Base $f3
54
     * @return void
55
     */
56
    public function search(\Base $f3)
57
    {
58
        $view = strtolower(trim(strip_tags($f3->get('REQUEST.view'))));
59
        $view = empty($view) ? 'list.phtml' : $view . '.phtml';
60
        $f3->set('REQUEST.view', $view);
61
62
        $f3->set('results', $this->getSearchResults($f3, new Mappers\UsersData));
63
64
        $f3->set('breadcrumbs', [
65
            _('Admin') => 'admin',
66
            _('Users') => 'admin_users_list',
67
            _('Data') => 'admin_usersdata_list',
68
            _('Search') => '',
69
        ]);
70
71
        $f3->set('form', $f3->get('REQUEST'));
72
        echo \View::instance()->render($this->template_path . $view);
73
    }
74
75
76
    /**
77
     *
78
     *
79
     * @param \Base $f3
80
     * @return void
81
     */
82
    public function edit(\Base $f3)
83
    {
84
        $this->redirectLoggedOutUser();
85
        $this->csrf();
86
87
        if (false == $f3->get('isRoot')) {
88
            $this->notify(_('You do not have (root) permission!'), 'error');
89
            return $f3->reroute('@admin');
90
        }
91
92
        $uuid = $f3->get('REQUEST.uuid');
93
        $usersModel = Models\Users::instance();
94
        $mapper = $usersModel->getDataMapper();
95
        $mapper->load(['uuid = ?', $uuid]);
96
97
        if (null == $mapper->id) {
98
            $this->notify(_('The entry no longer exists!'), 'error');
99
            return $f3->reroute('@admin_users_lists');
100
        }
101
102
        $f3->set('breadcrumbs', [
103
            _('Admin') => 'admin',
104
            _('Users') => $this->url('@admin_users_search', [
105
                'search' => $mapper->users_uuid,
106
                'search_fields' => 'uuid',
107
                'type' => 'exact',
108
                ]),
109
            _('Data') => $this->url('@admin_usersdata_search', [
110
                'search' => $mapper->users_uuid,
111
                'search_fields' => 'users_uuid',
112
                'order' => 'key',
113
                'type' => 'exact',
114
                ]),
115
            _('Edit') => '',
116
        ]);
117
118
        $f3->set('form', $mapper->cast());
119
        echo \View::instance()->render($this->template_path . 'edit.phtml');
120
    }
121
122
123
    /**
124
     *
125
     *
126
     * @param \Base $f3
127
     * @return void
128
     */
129
    public function editPost(\Base $f3)
130
    {
131
        $this->csrf('@admin_usersdata_list');
132
        $this->redirectLoggedOutUser();
133
134
        if (false == $f3->get('isRoot')) {
135
            $this->notify(_('You do not have (root) permission!'), 'error');
136
            return $f3->reroute('@admin');
137
        }
138
139
        $view = $this->template_path . 'edit.phtml';
140
141
        // get current user details
142
        $uuid = $f3->get('REQUEST.uuid');
143
        $usersModel = Models\Users::instance();
144
        $mapper = $usersModel->getDataMapper();
145
        $mapper->load(['uuid = ?', $uuid]);
146
147
        if (null == $mapper->id) {
148
            $this->notify(_('The entry no longer exists!'), 'error');
149
            return $f3->reroute('@admin_usersdata_list');
150
        }
151
152
        $f3->set('breadcrumbs', [
153
            _('Admin') => 'admin',
154
            _('Users') => $this->url('@admin_users_search', [
155
                'search' => $mapper->users_uuid,
156
                'search_fields' => 'uuid',
157
                'type' => 'exact',
158
                ]),
159
            _('Data') => $this->url('@admin_usersdata_search', [
160
                'search' => $mapper->users_uuid,
161
                'search_fields' => 'users_uuid',
162
                'order' => 'key',
163
                'type' => 'exact',
164
                ]),
165
            _('Edit') => '',
166
        ]);
167
168
        // only allow updating of these fields
169
        $data = $f3->get('REQUEST');
170
        $fields = [
171
            'value'
172
        ];
173
174
        // check input data has values set for the above fields
175
        foreach ($fields as $k => $field) {
176
            if (!array_key_exists($field, $data)) {
177
                $data[$field] = null;
178
            }
179
        }
180
        // then remove any input data fields that aren't in the above fields
181
        foreach ($data as $field => $v) {
182
            if (!array_key_exists($field, $data) || empty($data[$field])) {
183
                unset($data[$field]);
184
            }
185
        }
186
187
        // type check for filtering and validation
188
        $fRules = '';
189
        switch ($mapper->type) {
190
            case 'text':
191
            case 'textarea':
192
                $fRules = 'trim|sanitize_string';
193
                break;
194
195
            case 'html':
196
            case 'markdown':
197
            case 'ini':
198
            case 'yaml':
199
                // trust raw input!
200
                $data['value'] = $f3->get('REQUEST_UNCLEAN.value');
201
                break;
202
203
            case 'json':
204
                $data['value'] = $f3->get('REQUEST_UNCLEAN.value');
205
                break;
206
207
            case 'email':
208
                $fRules = 'sanitize_email';
209
                $vRules = 'valid_email';
210
                break;
211
212
            case 'url':
213
                $fRules = 'trim|sanitize_string';
214
                $vRules = 'valid_url';
215
                break;
216
217
            case 'numeric':
218
            case 'whole_number':
219
            case 'integer':
220
            case 'boolean':
221 View Code Duplication
            case 'float':
222
                $fRules = 'trim|sanitize_string';
223
                if ('float' == $mapper->type) {
224
                    $fRules .= '|sanitize_floats';
225
                } else {
226
                    $fRules = 'sanitize_numbers';
227
                }
228
                $vRules = $mapper->type;
229
                break;
230
231
            case 'date':
232
                $fRules = 'trim|sanitize_string';
233
                $vRules = $mapper->type;
234
                break;
235
        }
236
237
        if (!empty($fRules)) {
238
            $this->filterRules(['value' => $fRules]);
239
        }
240 View Code Duplication
        if (!empty($vRules)) {
241
            $this->validationRules(['value' => $vRules]);
242
            $errors = $this->validate(false, ['value' => $data['value']]);
243
            if (true !== $errors) {
244
                $this->notify(['warning' => $this->validationErrors($errors)]);
245
                $f3->set('form', $mapper->cast());
246
                echo \View::instance()->render($this->template_path . 'edit.phtml');
247
                return;
248
            }
249
        }
250
251
        // update required fields to check from ones which changed
252
        // validate the entered data
253
        $data['uuid'] = $uuid;
254
        $mapper->copyfrom($data);
255
        $mapper->validationRequired($fields);
256
        $errors = $mapper->validate(false);
257 View Code Duplication
        if (is_array($errors)) {
258
            $this->notify(['warning' => $mapper->validationErrors($errors)]);
259
            $f3->set('form', $f3->get('REQUEST'));
260
            echo \View::instance()->render($view);
261
            return;
262
        }
263
264
        // no change, do nothing
265
        if ($mapper->changed()) {
266
            $this->notify(_('There was nothing to change!'), 'info');
267
            return $f3->reroute('@admin_usersdata_list');
268
        }
269
270
        // reset usermapper and copy in valid data
271
        $mapper->load(['uuid = ?', $data['uuid']]);
272
        $mapper->copyfrom($data);
273 View Code Duplication
        if ($mapper->save()) {
274
            $this->notify(_('The account data was updated!'), 'success');
275
        } else {
276
            $this->notify(_('Unable to update account data!'), 'error');
277
            $f3->set('form', $f3->get('REQUEST'));
278
            echo \View::instance()->render($view);
279
            return;
280
        }
281
282
        $f3->reroute('@admin_usersdata_search' . '?search=' . $mapper->uuid);
283
    }
284
285
286
    /**
287
     *
288
     *
289
     * @param \Base $f3
290
     * @return void
291
     */
292
    public function add(\Base $f3)
293
    {
294
        $this->redirectLoggedOutUser();
295
        $this->csrf();
296
297
        if (false == $f3->get('isRoot')) {
298
            $this->notify(_('You do not have (root) permission!'), 'error');
299
            return $f3->reroute('@admin');
300
        }
301
302
        $users_uuid = $f3->get('REQUEST.users_uuid');
303
        $usersModel = Models\Users::instance();
304
        $mapper = $usersModel->getDataMapper();
305
306
        $data = $mapper->cast();
307
        $data['users_uuid'] = $users_uuid;
308
309
        $f3->set('breadcrumbs', [
310
            _('Admin') => 'admin',
311
            _('Users') => $this->url('@admin_users_search', [
312
                'search' => $users_uuid,
313
                'search_fields' => 'uuid',
314
                'type' => 'exact',
315
                ]),
316
            _('Data') => $this->url('@admin_usersdata_search', [
317
                'search' => $users_uuid,
318
                'search_fields' => 'users_uuid',
319
                'order' => 'key',
320
                'type' => 'exact',
321
                ]),
322
            _('Add') => '',
323
        ]);
324
325
        $f3->set('form', $data);
326
        echo \View::instance()->render($this->template_path . 'add.phtml');
327
    }
328
329
330
    /**
331
     *
332
     *
333
     * @param \Base $f3
334
     * @return void
335
     */
336
    public function addPost(\Base $f3)
337
    {
338
        $this->csrf('@admin_usersdata_list');
339
        $this->redirectLoggedOutUser();
340
341
        if (false == $f3->get('isRoot')) {
342
            $this->notify(_('You do not have (root) permission!'), 'error');
343
            return $f3->reroute('@admin');
344
        }
345
346
        $view = $this->template_path . 'add.phtml';
347
348
        $users_uuid = $f3->get('REQUEST.users_uuid');
349
        $usersModel = Models\Users::instance();
350
        $mapper = $usersModel->getDataMapper();
351
352
        $f3->set('breadcrumbs', [
353
            _('Admin') => 'admin',
354
            _('Users') => $this->url('@admin_users_search', [
355
                'search' => $users_uuid,
356
                'search_fields' => 'uuid',
357
                'type' => 'exact',
358
                ]),
359
            _('Data') => $this->url('@admin_usersdata_search', [
360
                'search' => $users_uuid,
361
                'search_fields' => 'users_uuid',
362
                'order' => 'key',
363
                'type' => 'exact',
364
                ]),
365
            _('Add') => '',
366
        ]);
367
368
        // only allow updating of these fields
369
        $data = $f3->get('REQUEST');
370
        $fields = [
371
            'users_uuid', 'key', 'value', 'type'
372
        ];
373
374
        // check input data has values set for the above fields
375
        foreach ($fields as $k => $field) {
376
            if (!array_key_exists($field, $data)) {
377
                $data[$field] = null;
378
            }
379
        }
380
        // then remove any input data fields that aren't in the above fields
381
        foreach ($data as $field => $v) {
382
            if (!in_array($field, $fields)) {
383
                unset($data[$field]);
384
            }
385
        }
386
387
        // update required fields to check from ones which changed
388
        // validate the entered data
389
        $data['users_uuid'] = $users_uuid;
390
        $mapper->copyfrom($data);
391
        $mapper->validationRequired($fields);
392
        $errors = $mapper->validate(false);
393 View Code Duplication
        if (is_array($errors)) {
394
            $this->notify(['warning' => $mapper->validationErrors($errors)]);
395
            $f3->set('form', $f3->get('REQUEST'));
396
            echo \View::instance()->render($view);
397
            return;
398
        }
399
400
        // no change, do nothing
401
        if (!$mapper->changed()) {
402
            $this->notify(_('There was nothing to change!'), 'info');
403
            return $f3->reroute('@admin_usersdata_list');
404
        }
405
406
        // reset usermapper and copy in valid data
407
        $mapper->load(['uuid = ?', $mapper->uuid]);
408
        $mapper->copyfrom($data);
409 View Code Duplication
        if ($mapper->save()) {
410
            $this->notify(_('The account data was updated!'), 'success');
411
        } else {
412
            $this->notify(_('Unable to update account data!'), 'error');
413
            $f3->set('form', $f3->get('REQUEST'));
414
            echo \View::instance()->render($view);
415
            return;
416
        }
417
418
        $f3->reroute('@admin_usersdata_edit' . '?uuid=' . $mapper->uuid);
419
    }
420
421
422
    /**
423
     *
424
     *
425
     * @param \Base $f3
426
     * @return void
427
     */
428
    public function delete(\Base $f3)
429
    {
430
        $this->redirectLoggedOutUser();
431
        $this->csrf();
432
433
        if (false == $f3->get('isRoot')) {
434
            $this->notify(_('You do not have (root) permission!'), 'error');
435
            return $f3->reroute('@admin_usersdata_list');
436
        }
437
438
        $uuid = $f3->get('REQUEST.uuid');
439
440
        $mapper = new Mappers\UsersData;
441
        $mapper->load(['uuid = ?', $uuid]);
442
443
        if (null == $mapper->id) {
444
            $this->notify(_('The data item no longer exists!'), 'error');
445
            return $f3->reroute('@admin_usersdata_list');
446
        }
447
448
        $mapper->erase();
449
        $this->notify('User data deleted!', 'success');
450
        return $f3->reroute('@admin_usersdata_list');
451
    }
452
453
}
454