Passed
Push — dev-master ( 289114...1a42ae )
by Vijay
34:35
created

Config::search()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 1
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
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\ControllerMapper;
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('is_root')) {
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('is_root')) {
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
94
        if (false == $f3->get('is_root')) {
95
            $this->notify(_('You do not have (root) permission!'), 'error');
96
            return $f3->reroute('@admin_config_list');
97
        }
98
99
        $uuid = $f3->get('REQUEST.uuid');
100
101
        $mapper = new Mappers\ConfigData;
102
        $mapper->load(['uuid = ?', $uuid]);
103
104
        if (null == $mapper->id) {
105
            $this->notify(_('The config item no longer exists!'), 'error');
106
            return $f3->reroute('@admin_config_list');
107
        }
108
109
        $oldMapper = clone($mapper);
110
        $mapper->erase();
111
        $this->notify('Config item deleted!', 'success');
112
        $this->audit([
113
            'event' => 'Config Deleted',
114
            'old' => $oldMapper->cast(),
115
        ]);
116
        return $f3->reroute('@admin_config_list');
117
    }
118
119
120
    /**
121
     *
122
     *
123
     * @param \Base $f3
124
     * @return void
125
     */
126
    public function edit(\Base $f3)
127
    {
128
        $this->redirectLoggedOutUser();
129
130
        if (false == $f3->get('is_root')) {
131
            $this->notify(_('You do not have (root) permission!'), 'error');
132
            return $f3->reroute('@admin');
133
        }
134
135
        $uuid = $f3->get('REQUEST.uuid');
136
        $mapper = new Mappers\ConfigData;
137
138
        $mapper->load(['uuid = ?', $uuid]);
139
140
        if (null == $mapper->id) {
141
            $this->notify(_('The entry no longer exists!'), 'error');
142
            return $f3->reroute('@admin_config_list');
143
        }
144
145
        $f3->set('breadcrumbs', [
146
            _('Admin') => 'admin',
147
            _('Config') => 'admin_config_list',
148
            _('Edit') => '',
149
        ]);
150
151
        $f3->set('form', $mapper->cast());
152
        echo \View::instance()->render($this->template_path . 'edit.phtml');
153
    }
154
155
156
    /**
157
     *
158
     *
159
     * @param \Base $f3
160
     * @return void
161
     */
162
    public function editPost(\Base $f3)
163
    {
164
        $this->csrf('@admin_config_list');
165
        $this->redirectLoggedOutUser();
166
167
        if (false == $f3->get('is_root')) {
168
            $this->notify(_('You do not have (root) permission!'), 'error');
169
            return $f3->reroute('@admin');
170
        }
171
172
        $view = $this->template_path . 'edit.phtml';
173
174
        $f3->set('breadcrumbs', [
175
            _('Admin') => 'admin',
176
            _('Config') => 'admin_config_list',
177
            _('Edit') => '',
178
        ]);
179
180
        // get current user details
181
        $uuid = $f3->get('REQUEST.uuid');
182
        $mapper = new Mappers\ConfigData;
183
184
        $mapper->load(['uuid = ?', $uuid]);
185
186
        if (null == $mapper->id) {
187
            $this->notify(_('The entry no longer exists!'), 'error');
188
            return $f3->reroute('@admin_config_list');
189
        }
190
191
        $oldMapper = clone $mapper;
192
193
        // only allow updating of these fields
194
        $data = $f3->get('REQUEST');
195
        $fields = [
196
            'value', 'options', 'description'
197
        ];
198
199
        // check input data has values set for the above fields
200
        foreach ($fields as $k => $field) {
201
            if (!array_key_exists($field, $data)) {
202
                $data[$field] = null;
203
            }
204
        }
205
        // then remove any input data fields that aren't in the above fields
206
        foreach ($data as $field => $v) {
207
            if (!in_array($field, $fields)) {
208
                unset($data[$field]);
209
            }
210
        }
211
212
        // type check for filtering and validation
213
        $fRules = 'trim|sanitize_string';
214
        switch ($mapper->type) {
215
            case 'text':
216
            case 'textarea':
217
                break;
218
219
            case 'html':
220
            case 'markdown':
221
            case 'ini':
222
            case 'yaml':
223
                // trust raw input!
224
                $data['value'] = $f3->get('REQUEST_UNCLEAN.value');
225
                $fRules = '';
226
                break;
227
228
            case 'json':
229
                $data['value'] = $f3->get('REQUEST_UNCLEAN.value');
230
                $fRules = 'valid_json_String';
0 ignored issues
show
Unused Code introduced by
$fRules is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
231
232
            case 'email':
233
                $fRules = 'sanitize_email';
234
                $vRules = 'valid_email';
235
                break;
236
237
            case 'url':
238
                $vRules = 'valid_url';
239
                break;
240
241
            case 'numeric':
242
            case 'whole_number':
243
            case 'integer':
244
            case 'boolean':
245
            case 'float':
246
                if ('float' == $mapper->type) {
247
                    $fRules .= 'sanitize_floats';
248
                } else {
249
                    $fRules = 'sanitize_numbers';
250
                }
251
                $vRules = $mapper->type;
252
                break;
253
254
            case 'date':
255
                $vRules = $mapper->type;
256
                break;
257
        }
258
259
        if (!empty($fRules)) {
260
            $this->filterRules(['value' => $fRules]);
261
        }
262
        if (!empty($vRules)) {
263
            $this->validationRules(['value' => $vRules]);
264
            $errors = $this->validate(false, ['value' => $data['value']]);
265
            if (true !== $errors) {
266
                $this->notify(['warning' => $this->validationErrors($errors)]);
267
                $f3->set('form', $mapper->cast());
268
                echo \View::instance()->render($this->template_path . 'edit.phtml');
269
                return;
270
            }
271
        }
272
273
        // update required fields to check from ones which changed
274
        // validate the entered data
275
        $data['uuid'] = $uuid;
276
        $mapper->copyfrom($data);
277
278
        // no change, do nothing
279
        if ($mapper->cast() === $oldMapper->cast()) {
280
            $this->notify(_('There was nothing to change!'), 'info');
281
            return $f3->reroute('@admin_config_list');
282
        }
283
284
        // reset usermapper and copy in valid data
285
        $mapper->load(['uuid = ?', $data['uuid']]);
286
        $mapper->copyfrom($data);
287
        if ($mapper->validateSave()) {
288
            $this->audit([
289
                'event' => 'Config Data Updated',
290
                'old' => $oldMapper->cast(),
291
                'new' => $mapper->cast()
292
            ]);
293
            $this->notify(_('The config data was updated!'), 'success');
294
        } else {
295
            $this->notify(_('Unable to update config data!'), 'error');
296
            $f3->set('form', $f3->get('REQUEST'));
297
            echo \View::instance()->render($view);
298
            return;
299
        }
300
301
        $f3->reroute('@admin_config_search' . '?search=' . $mapper->uuid);
302
    }
303
304
305
    /**
306
     *
307
     *
308
     * @param \Base $f3
309
     * @return void
310
     */
311
    public function add(\Base $f3)
312
    {
313
        $this->redirectLoggedOutUser();
314
315
        if (false == $f3->get('is_root')) {
316
            $this->notify(_('You do not have (root) permission!'), 'error');
317
            return $f3->reroute('@admin');
318
        }
319
320
        $users_uuid = $f3->get('REQUEST.users_uuid');
321
        $mapper = new Mappers\ConfigData;
322
323
324
        $data = $mapper->cast();
325
        $data['users_uuid'] = $users_uuid;
326
327
        $f3->set('breadcrumbs', [
328
            _('Admin') => 'admin',
329
            _('Config') => 'admin_config_list',
330
            _('Add') => '',
331
        ]);
332
333
        $f3->set('form', $data);
334
        echo \View::instance()->render($this->template_path . 'add.phtml');
335
    }
336
337
338
    /**
339
     *
340
     *
341
     * @param \Base $f3
342
     * @return void
343
     */
344
    public function addPost(\Base $f3)
345
    {
346
        $this->csrf('@admin_config_list');
347
        $this->redirectLoggedOutUser();
348
349
        if (false == $f3->get('is_root')) {
350
            $this->notify(_('You do not have (root) permission!'), 'error');
351
            return $f3->reroute('@admin');
352
        }
353
354
        $view = $this->template_path . 'add.phtml';
355
356
        $uuid = $f3->get('REQUEST.uuid');
357
        $mapper = new Mappers\ConfigData;
358
359
        $f3->set('breadcrumbs', [
360
            _('Admin') => 'admin',
361
            _('Config') => 'admin_config_list',
362
            _('Add') => '',
363
        ]);
364
365
        $oldMapper = clone $mapper;
366
        $oldMapper->load(['uuid = ?', $uuid]);
367
368
        // only allow updating of these fields
369
        $data = $f3->get('REQUEST');
370
        $fields = [
371
            'key', 'type', 'options', 'description'
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) || empty($data[$field])) {
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['uuid'] = $uuid;
390
        $mapper->copyfrom($data);
391
        $mapper->validationRequired([
392
            'key', 'type', 'description'
393
        ]);
394
        $errors = $mapper->validate(false);
395
        if (is_array($errors)) {
396
            $this->notify(['warning' => $mapper->validationErrors($errors)]);
397
            $f3->set('form', $f3->get('REQUEST'));
398
            echo \View::instance()->render($view);
399
            return;
400
        }
401
402
        // no change, do nothing
403
        if ($mapper->cast() === $oldMapper->cast()) {
404
            $this->notify(_('There was nothing to change!'), 'info');
405
            return $f3->reroute('@admin_config_list');
406
        }
407
408
        // reset usermapper and copy in valid data
409
        $mapper->load(['uuid = ?', $mapper->uuid]);
410
        $mapper->copyfrom($data);
411
        if ($mapper->validateSave()) {
412
            $this->audit([
413
                'event' => 'Config Data Updated',
414
                'old' => $oldMapper->cast(),
415
                'new' => json_encode($mapper->cast(), JSON_PRETTY_PRINT)
416
            ]);
417
            $this->notify(_('The config data was updated!'), 'success');
418
        } else {
419
            $this->notify(_('Unable to update config data!'), 'error');
420
            $f3->set('form', $f3->get('REQUEST'));
421
            echo \View::instance()->render($view);
422
            return;
423
        }
424
425
        $f3->reroute('@admin_config_edit' . '?uuid=' . $mapper->uuid);
426
    }
427
428
}
429