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

Reports::add()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
eloc 19
nc 2
nop 2
1
<?php
2
3
namespace FFCMS\Controllers\Admin;
4
5
use FFMVC\Helpers;
6
use FFCMS\{Traits, Controllers, Models, Mappers};
7
8
/**
9
 * Admin Reports 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 Reports extends Admin
16
{
17
    /**
18
     * For admin listing and search results
19
     */
20
    use Traits\ControllerMapper;
21
22
    protected $template_path = 'cms/admin/reports/';
23
24
25
    /**
26
     *
27
     *
28
     * @param \Base $f3
29
     * @return void
30
     */
31
    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\Reports));
38
39
        $f3->set('breadcrumbs', [
40
            _('Admin') => 'admin',
41
            _('Reports') => 'admin_reports_list',
42
        ]);
43
44
        $f3->set('form', $f3->get('REQUEST'));
45
        echo \View::instance()->render($this->template_path . $view);
46
    }
47
48
49
    /**
50
     *
51
     *
52
     * @param \Base $f3
53
     * @return void
54
     */
55
    public function search(\Base $f3)
56
    {
57
        $view = strtolower(trim(strip_tags($f3->get('REQUEST.view'))));
58
        $view = empty($view) ? 'list.phtml' : $view . '.phtml';
59
        $f3->set('REQUEST.view', $view);
60
61
        $f3->set('results', $this->getSearchResults($f3, new Mappers\Reports));
62
63
        $f3->set('breadcrumbs', [
64
            _('Admin') => 'admin',
65
            _('Reports') => 'admin_reports_list',
66
            _('Search') => '',
67
        ]);
68
69
        $f3->set('form', $f3->get('REQUEST'));
70
        echo \View::instance()->render($this->template_path . $view);
71
    }
72
73
74
    /**
75
     *
76
     *
77
     * @param \Base $f3
78
     * @return void
79
     */
80
    public function edit(\Base $f3)
81
    {
82
        $this->redirectLoggedOutUser();
83
84
        if (false == $f3->get('is_root')) {
85
            $this->notify(_('You do not have (root) permission!'), 'error');
86
            return $f3->reroute('@admin');
87
        }
88
89
        $uuid = $f3->get('REQUEST.uuid');
90
91
        $mapper = new Mappers\Reports;
92
        $mapper->load(['uuid = ?', $uuid]);
93
94
        if (null == $mapper->id) {
95
            $this->notify(_('The entry no longer exists!'), 'error');
96
            return $f3->reroute('@admin_reports_lists');
97
        }
98
99
        $f3->set('breadcrumbs', [
100
            _('Admin') => 'admin',
101
            _('Users') => $this->url('@admin_reports_search', [
102
                'search' => $mapper->users_uuid,
103
                'search_fields' => 'uuid',
104
                'type' => 'exact',
105
                ]),
106
            _('Reports') => $this->url('@admin_reports_search', [
107
                'search' => $mapper->users_uuid,
108
                'search_fields' => 'users_uuid',
109
                'order' => 'key',
110
                'type' => 'exact',
111
                ]),
112
            _('Edit') => '',
113
        ]);
114
115
        $f3->set('form', $mapper->cast());
116
        echo \View::instance()->render($this->template_path . 'edit.phtml');
117
    }
118
119
120
    /**
121
     *
122
     *
123
     * @param \Base $f3
124
     * @return void
125
     */
126
    public function editPost(\Base $f3
127
    {
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '{', expecting ')'
Loading history...
128
        $this->csrf('@admin_reports_list');
129
        $this->redirectLoggedOutUser();
130
131
        if (false == $f3->get('is_root')) {
132
            $this->notify(_('You do not have (root) permission!'), 'error');
133
            return $f3->reroute('@admin');
134
        }
135
136
        $view = $this->template_path . 'edit.phtml';
137
138
        // get current user details
139
        $uuid = $f3->get('REQUEST.uuid');
140
141
        $mapper = new Mappers\Reports;
142
        $mapper->load(['uuid = ?', $uuid]);
143
144
        if (null == $mapper->id) {
145
            $this->notify(_('The entry no longer exists!'), 'error');
146
            return $f3->reroute('@admin_reports_list');
147
        }
148
149
        $f3->set('breadcrumbs', [
150
            _('Admin') => 'admin',
151
            _('Users') => $this->url('@admin_reports_search', [
152
                'search' => $mapper->users_uuid,
153
                'search_fields' => 'uuid',
154
                'type' => 'exact',
155
                ]),
156
            _('Reports') => $this->url('@admin_reports_search', [
157
                'search' => $mapper->users_uuid,
158
                'search_fields' => 'users_uuid',
159
                'order' => 'key',
160
                'type' => 'exact',
161
                ]),
162
            _('Edit') => '',
163
        ]);
164
165
        $oldMapper = clone $mapper;
166
167
        // only allow updating of these fields
168
        $data = $f3->get('REQUEST');
169
        $fields = [
170
            'users_uuid',
171
            'groups',
172
            'key',
173
            'name',
174
            'description',
175
            'query',
176
        ];
177
178
        // check input data has values set for the above fields
179
        foreach ($fields as $k => $field) {
180
            if (!array_key_exists($field, $data)) {
181
                $data[$field] = null;
182
            }
183
        }
184
        // then remove any input data fields that aren't in the above fields
185
        foreach ($data as $field => $v) {
186
            if (!in_array($field, $fields)) {
187
                unset($data[$field]);
188
            }
189
        }
190
191
        // update required fields to check from ones which changed
192
        // validate the entered data
193
        $data['uuid'] = $f3->get('REQUEST.uuid');
194
        $data['users_uuid'] = $f3->get('uuid');
195
        $mapper->copyfrom($data);
196
        $mapper->validationRequired($fields);
197
        $errors = $mapper->validate(false);
198
        if (is_array($errors)) {
199
            $this->notify(['warning' => $mapper->validationErrors($errors)]);
200
            $f3->set('form', $f3->get('REQUEST'));
201
            echo \View::instance()->render($view);
202
            return;
203
        }
204
205
        // no change, do nothing
206
        if ($mapper->cast() === $oldMapper->cast()) {
207
            $this->notify(_('There was nothing to change!'), 'info');
208
            return $f3->reroute('@admin_reports_list');
209
        }
210
211
        // reset usermapper and copy in valid data
212
        $mapper->load(['uuid = ?', $data['uuid']]);
213
        $mapper->copyfrom($data);
214
        if ($mapper->validateSave()) {
215
            $this->audit([
216
                'event' => 'Report Updated',
217
                'old' => $oldMapper->cast(),
218
                'new' => json_encode($mapper->cast(), JSON_PRETTY_PRINT)
219
            ]);
220
            $this->notify(_('The report data was updated!'), 'success');
221
        } else {
222
            $this->notify(_('Unable to update report data!'), 'error');
223
            $f3->set('form', $f3->get('REQUEST'));
224
            echo \View::instance()->render($view);
225
            return;
226
        }
227
228
        $f3->reroute('@admin_reports_search' . '?search=' . $mapper->uuid);
229
    }
230
231
232
    /**
233
     *
234
     *
235
     * @param \Base $f3
236
     * @return void
237
     */
238
    public function add(\Base $f3)
239
    {
240
        $this->redirectLoggedOutUser();
241
242
        if (false == $f3->get('is_root')) {
243
            $this->notify(_('You do not have (root) permission!'), 'error');
244
            return $f3->reroute('@admin');
245
        }
246
247
        $uuid = $f3->get('REQUEST.uuid');
248
249
        $mapper = new Mappers\Reports;
250
251
        $data = $mapper->cast();
252
        $data['uuid'] = $uuid;
253
254
        $f3->set('breadcrumbs', [
255
            _('Admin') => 'admin',
256
            _('Reports') => $this->url('@admin_reports_search', [
257
                'search' => $uuid,
258
                'search_fields' => 'uuid',
259
                'order' => 'key',
260
                'type' => 'exact',
261
                ]),
262
            _('Add') => '',
263
        ]);
264
265
        $f3->set('form', $data);
266
        echo \View::instance()->render($this->template_path . 'add.phtml');
267
    }
268
269
270
    /**
271
     *
272
     *
273
     * @param \Base $f3
274
     * @return void
275
     */
276
    public function addPost(\Base $f3)
277
    {
278
        $this->csrf('@admin_reports_list');
279
        $this->redirectLoggedOutUser();
280
281
        if (false == $f3->get('is_root')) {
282
            $this->notify(_('You do not have (root) permission!'), 'error');
283
            return $f3->reroute('@admin');
284
        }
285
286
        $view = $this->template_path . 'add.phtml';
287
288
        $uuid = $f3->get('REQUEST.uuid');
289
290
        $mapper = new Mappers\Reports;
291
292
        $f3->set('breadcrumbs', [
293
            _('Admin') => 'admin',
294
            _('Users') => $this->url('@admin_reports_search', [
295
                'search' => $uuid,
296
                'search_fields' => 'uuid',
297
                'type' => 'exact',
298
                ]),
299
            _('Reports') => $this->url('@admin_reports_search', [
300
                'search' => $uuid,
301
                'search_fields' => 'users_uuid',
302
                'order' => 'key',
303
                'type' => 'exact',
304
                ]),
305
            _('Add') => '',
306
        ]);
307
308
        $oldMapper = clone $mapper;
309
        $oldMapper->load(['users_uuid = ?', $uuid]);
310
311
        // only allow updating of these fields
312
        $data = $f3->get('REQUEST');
313
        $fields = [
314
            'groups',
315
            'key',
316
            'name',
317
            'description',
318
            'query',
319
        ];
320
321
        // check input data has values set for the above fields
322
        foreach ($fields as $k => $field) {
323
            if (!array_key_exists($field, $data) || empty($data[$field])) {
324
                $data[$field] = null;
325
            }
326
        }
327
        // then remove any input data fields that aren't in the above fields
328
        foreach ($data as $field => $v) {
329
            if (!in_array($field, $fields)) {
330
                unset($data[$field]);
331
            }
332
        }
333
334
        // update required fields to check from ones which changed
335
        // validate the entered data
336
        $data['users_uuid'] = $f3->get('uuid');
337
        $mapper->copyfrom($data);
338
        $mapper->validationRequired($fields);
339
        $errors = $mapper->validate(false);
340
        if (is_array($errors)) {
341
            $this->notify(['warning' => $mapper->validationErrors($errors)]);
342
            $f3->set('form', $f3->get('REQUEST'));
343
            echo \View::instance()->render($view);
344
            return;
345
        }
346
347
        // no change, do nothing
348
        if ($mapper->cast() === $oldMapper->cast()) {
349
            $this->notify(_('There was nothing to change!'), 'info');
350
            return $f3->reroute('@admin_reports_list');
351
        }
352
353
        // reset usermapper and copy in valid data
354
        $mapper->load(['uuid = ?', $mapper->uuid]);
355
        $mapper->copyfrom($data);
356
        if ($mapper->validateSave()) {
357
            $this->audit([
358
                'event' => 'Report Updated',
359
                'old' => $oldMapper->cast(),
360
                'new' => json_encode($mapper->cast(), JSON_PRETTY_PRINT)
361
            ]);
362
            $this->notify(_('The report data was updated!'), 'success');
363
        } else {
364
            $this->notify(_('Unable to update report data!'), 'error');
365
            $f3->set('form', $f3->get('REQUEST'));
366
            echo \View::instance()->render($view);
367
            return;
368
        }
369
370
        $f3->reroute('@admin_reports_search' . '?search=' . $mapper->uuid);
371
    }
372
373
374
    /**
375
     *
376
     *
377
     * @param \Base $f3
378
     * @return void
379
     */
380
    public function view(\Base $f3)
381
    {
382
        $this->redirectLoggedOutUser();
383
384
        $uuid = $f3->get('REQUEST.uuid');
385
        $view = strtolower(trim(strip_tags($f3->get('REQUEST.view'))));
386
387
        $mapper = new Mappers\Reports;
388
        $mapper->load(['uuid = ?', $uuid]);
389
390
        if (null == $mapper->id) {
391
            $this->notify(_('The entry no longer exists!'), 'error');
392
            return $f3->reroute('@admin_reports_lists');
393
        }
394
395
        $f3->set('breadcrumbs', [
396
            _('Admin') => 'admin',
397
            _('Users') => $this->url('@admin_reports_search', [
398
                'search' => $mapper->users_uuid,
399
                'search_fields' => 'uuid',
400
                'type' => 'exact',
401
                ]),
402
            _('Reports') => $this->url('@admin_reports_search', [
403
                'search' => $mapper->users_uuid,
404
                'search_fields' => 'users_uuid',
405
                'order' => 'key',
406
                'type' => 'exact',
407
                ]),
408
            _('View') => '',
409
        ]);
410
411
        $db = \Registry::get('db');
412
        $results = $db->exec($mapper->query);
413
        $f3->set('results', $results);
414
415
        if ('csv' !== $view) {
416
            $view = empty($view) ? 'view.phtml' : $view . '.phtml';
417
            $f3->set('REQUEST.view', $view);
418
            $f3->set('form', $mapper->cast());
419
            echo \View::instance()->render($this->template_path . $view);
420
        } else {
421
            // write the csv file
422
            $file = realpath($f3->get('TEMP')) . '/' . date('Y-m-d') . '-' . $mapper->key  .  '.csv';
423
            if (!empty($results) && count($results) > 0) {
424
                $fp = fopen($file, 'w');
425
                fputcsv($fp, array_keys($results[0]));
426
                foreach ($results as $k => $fields) {
427
                    $values = array_values($fields);
428
                    fputcsv($fp, $values);
429
                }
430
                fclose($fp);
431
            }
432
433
            header('Content-Description: File Transfer');
434
            header('Content-type: application/csv; charset=' . $f3->get('ENCODING'));
435
            header("Content-Disposition: attachment; filename=" . basename($file));
436
            header('Content-Length: ' . filesize($file));
437
            header('Expires: 0');
438
            header('Cache-Control: must-revalidate');
439
            header('Pragma: public');
440
            readfile($file);
441
        }
442
    }
443
444
445
    /**
446
     *
447
     *
448
     * @param \Base $f3
449
     * @return void
450
     */
451
    public function delete(\Base $f3)
452
    {
453
        $this->redirectLoggedOutUser();
454
455
        if (false == $f3->get('is_root')) {
456
            $this->notify(_('You do not have (root) permission!'), 'error');
457
            return $f3->reroute('@admin_reports_list');
458
        }
459
460
        $uuid = $f3->get('REQUEST.uuid');
461
462
        $mapper = new Mappers\Reports;
463
        $mapper->load(['uuid = ?', $uuid]);
464
465
        if (null == $mapper->id) {
466
            $this->notify(_('The report no longer exists!'), 'error');
467
            return $f3->reroute('@admin_reports_list');
468
        }
469
470
        $oldMapper = clone($mapper);
471
        $mapper->erase();
472
        $this->notify('Report deleted!', 'success');
473
        $this->audit([
474
            'event' => 'Report Deleted',
475
            'old' => $oldMapper->cast(),
476
        ]);
477
        $this->notify(_('Unable to update report data!'), 'error');
478
        return $f3->reroute('@admin_reports_list');
479
    }
480
481
482
}
483