Config::editPost()   F
last analyzed

Complexity

Conditions 28
Paths 2648

Size

Total Lines 135
Code Lines 88

Duplication

Lines 27
Ratio 20 %

Importance

Changes 0
Metric Value
cc 28
eloc 88
c 0
b 0
f 0
nc 2648
nop 1
dl 27
loc 135
rs 2

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