Settings::outputEditSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package XSS filter
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0-or-later
8
 */
9
10
namespace gplcart\modules\xss\controllers;
11
12
use gplcart\core\controllers\backend\Controller;
13
14
/**
15
 * Handles incoming requests and outputs data related to XSS filter module
16
 */
17
class Settings extends Controller
18
{
19
20
    /**
21
     * Route page callback to display the module settings page
22
     */
23
    public function editSettings()
24
    {
25
        $this->setTitleEditSettings();
26
        $this->setBreadcrumbEditSettings();
27
28
        $this->setData('settings', $this->getModuleSettings('xss'));
29
30
        $this->submitSettings();
31
        $this->setDataEditSettings();
32
        $this->outputEditSettings();
33
    }
34
35
    /**
36
     * Prepare an set template variables
37
     */
38
    protected function setDataEditSettings()
39
    {
40
        foreach (array('settings.tags', 'settings.protocols') as $field) {
41
42
            $data = $this->getData($field);
43
44
            if (is_array($data)) {
45
                $this->setData($field, implode(',', $data));
46
            }
47
        }
48
    }
49
50
    /**
51
     * Set title on the module settings page
52
     */
53
    protected function setTitleEditSettings()
54
    {
55
        $title = $this->text('Edit %name settings', array('%name' => $this->text('XSS filter')));
56
        $this->setTitle($title);
57
    }
58
59
    /**
60
     * Set breadcrumbs on the module settings page
61
     */
62
    protected function setBreadcrumbEditSettings()
63
    {
64
        $breadcrumbs = array();
65
66
        $breadcrumbs[] = array(
67
            'text' => $this->text('Dashboard'),
68
            'url' => $this->url('admin')
69
        );
70
71
        $breadcrumbs[] = array(
72
            'text' => $this->text('Modules'),
73
            'url' => $this->url('admin/module/list')
74
        );
75
76
        $this->setBreadcrumbs($breadcrumbs);
77
    }
78
79
    /**
80
     * Saves the submitted settings
81
     */
82
    protected function submitSettings()
83
    {
84
        if ($this->isPosted('save') && $this->validateSettings()) {
85
            $this->updateSettings();
86
        }
87
    }
88
89
    /**
90
     * Validate submitted module settings
91
     * @return bool
92
     */
93
    protected function validateSettings()
94
    {
95
        $this->setSubmitted('settings');
96
97
        foreach (array('tags', 'protocols') as $field) {
98
            $string = str_replace(' ', '', $this->getSubmitted($field));
99
            $this->setSubmitted($field, array_filter(explode(',', $string)));
100
        }
101
102
        return !$this->hasErrors();
103
    }
104
105
    /**
106
     * Update module settings
107
     */
108
    protected function updateSettings()
109
    {
110
        $this->controlAccess('module_edit');
111
        $this->controlAccess('module_xss_edit');
112
113
        $this->module->setSettings('xss', $this->getSubmitted());
114
        $this->redirect('', $this->text('Settings have been updated'), 'success');
115
    }
116
117
    /**
118
     * Render and output the module settings page
119
     */
120
    protected function outputEditSettings()
121
    {
122
        $this->output('xss|settings');
123
    }
124
}
125