Completed
Push — dev-master ( 86aae1...377172 )
by Vijay
03:33
created

Pages::addScripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 9.4285
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 Pages 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 Pages extends Admin
16
{
17
    /**
18
     * For admin listing and search results
19
     */
20
    use Traits\SearchController;
21
22
    protected $template_path = 'cms/admin/pages/';
23
24
    /**
25
     * Add default scripts for displaying templates
26
     *
27
     * @return void
28
     * @see app/config/default.ini
29
     */
30
    protected function addScripts()
31
    {
32
        // no scripts to add, override me and set css and js
33
        $this->setScripts([], ['showdown']);
34
    }
35
36
37
    /**
38
     *
39
     *
40
     * @param \Base $f3
41
     * @return void
42
     */
43 View Code Duplication
    public function listing(\Base $f3)
44
    {
45
        $view = strtolower(trim(strip_tags($f3->get('REQUEST.view'))));
46
        $view = empty($view) ? 'list.phtml' : $view . '.phtml';
47
        $f3->set('REQUEST.view', $view);
48
49
        $f3->set('results', $this->getListingResults($f3, new Mappers\Pages));
50
51
        $f3->set('breadcrumbs', [
52
            _('Admin') => 'admin',
53
            _('Pages') => 'admin_pages_list',
54
        ]);
55
56
        $f3->set('form', $f3->get('REQUEST'));
57
        echo \View::instance()->render($this->template_path . $view);
58
    }
59
60
61
    /**
62
     *
63
     *
64
     * @param \Base $f3
65
     * @return void
66
     */
67 View Code Duplication
    public function search(\Base $f3)
68
    {
69
        $view = strtolower(trim(strip_tags($f3->get('REQUEST.view'))));
70
        $view = empty($view) ? 'list.phtml' : $view . '.phtml';
71
        $f3->set('REQUEST.view', $view);
72
73
        $f3->set('results', $this->getSearchResults($f3, new Mappers\Pages));
74
75
        $f3->set('breadcrumbs', [
76
            _('Admin') => 'admin',
77
            _('Pages') => 'admin_pages_list',
78
            _('Search') => '',
79
        ]);
80
81
        $f3->set('form', $f3->get('REQUEST'));
82
        echo \View::instance()->render($this->template_path . $view);
83
    }
84
85
86
    /**
87
     *
88
     *
89
     * @param \Base $f3
90
     * @return void
91
     */
92
    public function edit(\Base $f3)
93
    {
94
        $this->redirectLoggedOutUser();
95
        $this->csrf();
96
97
        if (false == $f3->get('isRoot')) {
98
            $this->notify(_('You do not have (root) permission!'), 'error');
99
            return $f3->reroute('@admin');
100
        }
101
102
        $uuid = $f3->get('REQUEST.uuid');
103
104
        $mapper = new Mappers\Pages;
105
        $mapper->load(['uuid = ?', $uuid]);
106
107
        // fix date fields
108
        foreach (['published', 'expires', 'updated'] as $field) {
109
            if ('0000-00-00 00:00:00' == $mapper->$field) {
110
                $mapper->$field = null;
111
            }
112
        }
113
114
        if (null == $mapper->id) {
115
            $this->notify(_('The entry no longer exists!'), 'error');
116
            return $f3->reroute('@admin_pages_list');
117
        }
118
119
        $f3->set('breadcrumbs', [
120
            _('Admin') => 'admin',
121
            _('Users') => $this->url('@admin_pages_search', [
122
                'search' => $mapper->users_uuid,
123
                'search_fields' => 'uuid',
124
                'type' => 'exact',
125
                ]),
126
            _('Pages') => $this->url('@admin_pages_search', [
127
                'search' => $mapper->users_uuid,
128
                'search_fields' => 'users_uuid',
129
                'order' => 'key',
130
                'type' => 'exact',
131
                ]),
132
            _('Edit') => '',
133
        ]);
134
135
        $f3->set('form', $mapper->cast());
136
        echo \View::instance()->render($this->template_path . 'edit.phtml');
137
    }
138
139
140
    /**
141
     * Set default values in page data where not set already
142
     * @param \Base $f3
143
     * @param \FFCMS\Mappers\Pages $mapper
144
     * @return $data array
0 ignored issues
show
Documentation introduced by
The doc-type $data could not be parsed: Unknown type name "$data" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
145
     */
146
    protected function filterPageInput(\Base $f3, \FFCMS\Mappers\Pages &$mapper): array
147
    {
148
        // only allow updating of these fields
149
        $data = $f3->get('REQUEST');
150
151
        $fields = [
152
            'key',
153
            'author',
154
            'language',
155
            'status',
156
            'slug',
157
            'path',
158
            'keywords',
159
            'description',
160
            'title',
161
            'summary',
162
            'body',
163
            'scopes',
164
            'category',
165
            'tags',
166
            'metadata',
167
            'expires',
168
            'published',
169
            'robots',
170
        ];
171
172
        // check input data has values set for the above fields
173
        foreach ($fields as $k => $field) {
174
            if (!array_key_exists($field, $data) || empty($data[$field])) {
175
                $data[$field] = null;
176
            }
177
        }
178
        // then remove any input data fields that aren't in the above fields
179
        foreach ($data as $field => $v) {
180
            if (!in_array($field, $fields)) {
181
                unset($data[$field]);
182
            }
183
        }
184
185
        // http://php.net/manual/en/function.strtotime.php
186
        $data['expires'] = Helpers\Time::database(strtotime($data['expires']));
187
        $data['published'] = Helpers\Time::database(strtotime($data['published']));
188
189
        // update required fields to check from ones which changed
190
        // validate the entered data
191
        $data['users_uuid'] = $f3->get('uuid');
192
193
        // set default key
194
        if (empty($data['key'])) {
195
            $data['key'] = $data['title'];
196
        }
197
198
        // set default slug
199
        if (empty($data['slug'])) {
200
            $data['slug'] = $data['title'];
201
        }
202
203
        // url path
204
        if (empty($data['path'])) {
205
            $data['path'] = '/';
206
        }
207
208
        // publish status
209
        if (empty($data['status'])) {
210
            $data['status'] = 'draft';
211
        }
212
213
        // language
214
        if (empty($data['language'])) {
215
            $data['language'] = 'en';
216
        }
217
218
        // author
219
        if (empty($data['author'])) {
220
            $usersMapper = $f3->get('usersMapper');
221
            $data['author'] = $usersMapper->fullName();
222
        }
223
224
        // category
225
        if (empty($data['category'])) {
226
            $data['category'] = 'page';
227
        }
228
229
        // scope
230
        if (empty($data['scopes'])) {
231
            $data['scopes'] = 'public';
232
        }
233
234
        if (empty($data['scopes'])) {
235
            $data['scopes'] = 'public';
236
        }
237
238
        $data['robots'] = (int) !empty($data['robots']);
239
240
        $mapper->copyfrom($data);
241
        $mapper->copyfrom($mapper->filter()); // filter mapa data
242
        $mapper->validationRequired([
243
            'title',
244
            'summary',
245
            'body',
246
        ]);
247
248
        return $mapper->cast();
249
    }
250
251
252
    /**
253
     *
254
     *
255
     * @param \Base $f3
256
     * @return void
257
     */
258
    public function editPost(\Base $f3)
259
    {
260
        $this->csrf('@admin_pages_list');
261
        $this->redirectLoggedOutUser();
262
263
        if (false == $f3->get('isRoot')) {
264
            $this->notify(_('You do not have (root) permission!'), 'error');
265
            return $f3->reroute('@admin');
266
        }
267
268
        $view = $this->template_path . 'edit.phtml';
269
270
        // get current user details
271
        $uuid = $f3->get('REQUEST.uuid');
272
273
        $mapper = new Mappers\Pages;
274
        $mapper->load(['uuid = ?', $uuid]);
275
276
        if (null == $mapper->id) {
277
            $this->notify(_('The entry no longer exists!'), 'error');
278
            return $f3->reroute('@admin_pages_list');
279
        }
280
281
        $f3->set('breadcrumbs', [
282
            _('Admin') => 'admin',
283
            _('Users') => $this->url('@admin_pages_search', [
284
                'search' => $mapper->users_uuid,
285
                'search_fields' => 'uuid',
286
                'type' => 'exact',
287
                ]),
288
            _('Pages') => $this->url('@admin_pages_search', [
289
                'search' => $mapper->users_uuid,
290
                'search_fields' => 'users_uuid',
291
                'order' => 'key',
292
                'type' => 'exact',
293
                ]),
294
            _('Edit') => '',
295
        ]);
296
297
        $data = $this->filterPageInput($f3, $mapper);
298
        $errors = $mapper->validate(false);
299 View Code Duplication
        if (is_array($errors)) {
300
            $this->notify(['warning' => $mapper->validationErrors($errors)]);
301
            $f3->set('form', $f3->get('REQUEST'));
302
            echo \View::instance()->render($view);
303
            return;
304
        }
305
306
        // no change, do nothing
307
        if (!$mapper->changed()) {
308
            $this->notify(_('There was nothing to change!'), 'info');
309
            return $f3->reroute('@admin_pages_list');
310
        }
311
312
        // reset usermapper and copy in valid data
313
        $mapper->load(['uuid = ?', $data['uuid']]);
314
        $mapper->copyfrom($data);
315 View Code Duplication
        if ($mapper->save()) {
316
            $this->notify(_('The page data was updated!'), 'success');
317
        } else {
318
            $this->notify(_('Unable to update page data!'), 'error');
319
            $f3->set('form', $f3->get('REQUEST'));
320
            echo \View::instance()->render($view);
321
            return;
322
        }
323
324
        $f3->reroute('@admin_pages_search' . '?search=' . $mapper->uuid);
325
    }
326
327
328
    /**
329
     *
330
     *
331
     * @param \Base $f3
332
     * @return void
333
     */
334
    public function add(\Base $f3)
335
    {
336
        $this->redirectLoggedOutUser();
337
        $this->csrf();
338
339
        if (false == $f3->get('isRoot')) {
340
            $this->notify(_('You do not have (root) permission!'), 'error');
341
            return $f3->reroute('@admin');
342
        }
343
344
        $uuid = $f3->get('REQUEST.uuid');
345
346
        $f3->set('breadcrumbs', [
347
            _('Admin') => 'admin',
348
            _('Pages') => $this->url('@admin_pages_search', [
349
                'search' => $uuid,
350
                'search_fields' => 'uuid',
351
                'order' => 'key',
352
                'type' => 'exact',
353
                ]),
354
            _('Add') => '',
355
        ]);
356
357
        $mapper = new Mappers\Pages;
358
        $data = $mapper->cast();
0 ignored issues
show
Unused Code introduced by
$data 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...
359
        $data = $this->filterPageInput($f3, $mapper);
360
        $data['uuid'] = $uuid;
361
        $data['robots'] = 1;
362
363
        $f3->set('form', $data);
364
        echo \View::instance()->render($this->template_path . 'add.phtml');
365
    }
366
367
368
    /**
369
     *
370
     *
371
     * @param \Base $f3
372
     * @return void
373
     */
374
    public function addPost(\Base $f3)
375
    {
376
        $this->csrf('@admin_pages_list');
377
        $this->redirectLoggedOutUser();
378
379
        if (false == $f3->get('isRoot')) {
380
            $this->notify(_('You do not have (root) permission!'), 'error');
381
            return $f3->reroute('@admin');
382
        }
383
384
        $uuid = $f3->get('REQUEST.uuid');
385
386
        $f3->set('breadcrumbs', [
387
            _('Admin') => 'admin',
388
            _('Users') => $this->url('@admin_pages_search', [
389
                'search' => $uuid,
390
                'search_fields' => 'uuid',
391
                'type' => 'exact',
392
                ]),
393
            _('Pages') => $this->url('@admin_pages_search', [
394
                'search' => $uuid,
395
                'search_fields' => 'users_uuid',
396
                'order' => 'key',
397
                'type' => 'exact',
398
                ]),
399
            _('Add') => '',
400
        ]);
401
402
        $view = $this->template_path . 'add.phtml';
403
404
        $mapper = new Mappers\Pages;
405
        $data = $this->filterPageInput($f3, $mapper);
406
407
        $errors = $mapper->validate(false);
408 View Code Duplication
        if (is_array($errors)) {
409
            $this->notify(['warning' => $mapper->validationErrors($errors)]);
410
            $f3->set('form', $data);
411
            echo \View::instance()->render($view);
412
            return;
413
        }
414
415
        // no change, do nothing
416
        if (!$mapper->changed()) {
417
            $this->notify(_('There was nothing to change!'), 'info');
418
            return $f3->reroute('@admin_pages_list');
419
        }
420
421
        // reset usermapper and copy in valid data
422
        $mapper->load(['uuid = ?', $mapper->uuid]);
423
        $mapper->copyfrom($data);
424 View Code Duplication
        if ($mapper->save()) {
425
            $this->notify(_('The page data was updated!'), 'success');
426
        } else {
427
            $this->notify(_('Unable to update page data!'), 'error');
428
            $f3->set('form', $data);
429
            echo \View::instance()->render($view);
430
            return;
431
        }
432
433
        $f3->reroute('@admin_pages_search' . '?search=' . $mapper->uuid);
434
    }
435
436
437
    /**
438
     *
439
     *
440
     * @param \Base $f3
441
     * @return void
442
     */
443
    public function view(\Base $f3)
444
    {
445
        $this->redirectLoggedOutUser();
446
447
        $uuid = $f3->get('REQUEST.uuid');
448
        $view = strtolower(trim(strip_tags($f3->get('REQUEST.view'))));
449
450
        $mapper = new Mappers\Pages;
451
        $mapper->load(['uuid = ?', $uuid]);
452
453
        if (null == $mapper->id) {
454
            $this->notify(_('The entry no longer exists!'), 'error');
455
            return $f3->reroute('@admin_pages_list');
456
        }
457
458
        $f3->set('breadcrumbs', [
459
            _('Admin') => 'admin',
460
            _('Users') => $this->url('@admin_pages_search', [
461
                'search' => $mapper->users_uuid,
462
                'search_fields' => 'uuid',
463
                'type' => 'exact',
464
                ]),
465
            _('Pages') => $this->url('@admin_pages_search', [
466
                'search' => $mapper->users_uuid,
467
                'search_fields' => 'users_uuid',
468
                'order' => 'key',
469
                'type' => 'exact',
470
                ]),
471
            _('View') => '',
472
        ]);
473
474
        $db = \Registry::get('db');
475
        $results = $db->exec($mapper->query);
0 ignored issues
show
Documentation introduced by
The property $query is declared protected in DB\Cursor. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
476
        $f3->set('results', $results);
477
478
        $view = empty($view) ? 'view.phtml' : $view . '.phtml';
479
        $f3->set('REQUEST.view', $view);
480
        $f3->set('form', $mapper->cast());
481
        echo \View::instance()->render($this->template_path . $view);
482
    }
483
484
485
    /**
486
     *
487
     *
488
     * @param \Base $f3
489
     * @return void
490
     */
491 View Code Duplication
    public function delete(\Base $f3)
492
    {
493
        $this->redirectLoggedOutUser();
494
        $this->csrf();
495
496
        if (false == $f3->get('isRoot')) {
497
            $this->notify(_('You do not have (root) permission!'), 'error');
498
            return $f3->reroute('@admin_pages_list');
499
        }
500
501
        $uuid = $f3->get('REQUEST.uuid');
502
503
        $mapper = new Mappers\Pages;
504
        $mapper->load(['uuid = ?', $uuid]);
505
506
        if (null == $mapper->id) {
507
            $this->notify(_('The page no longer exists!'), 'error');
508
            return $f3->reroute('@admin_pages_list');
509
        }
510
511
        $mapper->erase();
512
        $this->notify('Page deleted!', 'success');
513
        $this->notify(_('Unable to update page data!'), 'error');
514
        return $f3->reroute('@admin_pages_list');
515
    }
516
517
518
}
519