Settings::setBreadcrumbEditSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package File manager
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2017, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\file_manager\controllers;
11
12
use gplcart\core\controllers\backend\Controller;
13
14
/**
15
 * Handles incoming requests and outputs data related to File manager 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('file_dir', gplcart_path_normalize(GC_DIR_FILE));
29
        $this->setData('settings', $this->module->getSettings('file_manager'));
30
31
        $this->submitSettings();
32
        $this->setDataAccessSettings();
33
        $this->setDataMultilineTextSettings('filesize_limit');
34
        $this->setDataMultilineTextSettings('extension_limit');
35
36
        $this->outputEditSettings();
37
    }
38
39
    /**
40
     * Prepare and set data for "access" field
41
     */
42
    protected function setDataAccessSettings()
43
    {
44
        $access = $this->getData('settings.access');
45
46
        if (is_array($access)) {
47
48
            $string = '';
49
50
            foreach ($access as $role_id => $patterns) {
51
                foreach ($patterns as $pattern) {
52
                    $string .= "$role_id $pattern" . PHP_EOL;
53
                }
54
            }
55
56
            $this->setData('settings.access', trim($string));
57
        }
58
    }
59
60
    /**
61
     * Prepare and set data for textarea fields
62
     * @param string $setting
63
     */
64
    protected function setDataMultilineTextSettings($setting)
65
    {
66
        $data = $this->getData("settings.$setting");
67
68
        if (is_array($data)) {
69
70
            $string = '';
71
72
            foreach ($data as $key => $value) {
73
                $list = implode(',', (array) $value);
74
                $string .= "$key $list" . PHP_EOL;
75
            }
76
77
            $this->setData("settings.$setting", trim($string));
78
        }
79
    }
80
81
    /**
82
     * Set title on the module settings page
83
     */
84
    protected function setTitleEditSettings()
85
    {
86
        $title = $this->text('Edit %name settings', array('%name' => $this->text('File manager')));
87
        $this->setTitle($title);
88
    }
89
90
    /**
91
     * Set breadcrumbs on the module settings page
92
     */
93
    protected function setBreadcrumbEditSettings()
94
    {
95
        $breadcrumbs = array();
96
97
        $breadcrumbs[] = array(
98
            'text' => $this->text('Dashboard'),
99
            'url' => $this->url('admin')
100
        );
101
102
        $breadcrumbs[] = array(
103
            'text' => $this->text('Modules'),
104
            'url' => $this->url('admin/module/list')
105
        );
106
107
        $this->setBreadcrumbs($breadcrumbs);
108
    }
109
110
    /**
111
     * Saves the submitted settings
112
     */
113
    protected function submitSettings()
114
    {
115
        if ($this->isPosted('save') && $this->validateSettings()) {
116
            $this->updateSettings();
117
        }
118
    }
119
120
    /**
121
     * Validate submitted module settings
122
     * @return bool
123
     */
124
    protected function validateSettings()
125
    {
126
        $this->setSubmitted('settings', null, false);
127
        $this->validateComponent('file_manager_settings');
128
129
        return !$this->hasErrors();
130
    }
131
132
    /**
133
     * Update module settings
134
     */
135
    protected function updateSettings()
136
    {
137
        $this->controlAccess('module_edit');
138
139
        $this->module->setSettings('file_manager', $this->getSubmitted());
140
        $this->redirect('', $this->text('Settings have been updated'), 'success');
141
    }
142
143
    /**
144
     * Render and output the module settings page
145
     */
146
    protected function outputEditSettings()
147
    {
148
        $this->output('file_manager|settings');
149
    }
150
151
}
152