Completed
Push — master ( 0c8fa9...ad3ab7 )
by Iurii
01:00
created

Settings::setDataModuleSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package Summernote
5
 * @author Iurii Makukh
6
 * @copyright Copyright (c) 2017, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\summernote\controllers;
11
12
use gplcart\core\models\Module as ModuleModel;
13
use gplcart\core\controllers\backend\Controller as BackendController;
14
15
/**
16
 * Handles incoming requests and outputs data related Summernote module settings
17
 */
18
class Settings extends BackendController
19
{
20
21
    /**
22
     * Module model instance
23
     * @var \gplcart\core\models\Module $module
24
     */
25
    protected $module;
26
27
    /**
28
     * @param ModuleModel $module
29
     */
30
    public function __construct(ModuleModel $module)
31
    {
32
        parent::__construct();
33
34
        $this->module = $module;
35
    }
36
37
    /**
38
     * Route page callback to display the module settings page
39
     */
40
    public function editSettings()
41
    {
42
        $this->setTitleEditSettings();
43
        $this->setBreadcrumbEditSettings();
44
45
        $this->setDataModuleSettings();
46
47
        $this->submitSettings();
48
        $this->outputEditSettings();
49
    }
50
51
    /**
52
     * Sets module settings
53
     */
54
    protected function setDataModuleSettings()
55
    {
56
        $settings = $this->config->getFromModule('summernote');
57
        $settings['selector'] = implode("\n", $settings['selector']);
58
        $settings['config'] = gplcart_json_encode($settings['config'], true);
59
60
        $this->setData('settings', $settings);
61
    }
62
63
    /**
64
     * Set title on the module settings page
65
     */
66
    protected function setTitleEditSettings()
67
    {
68
        $vars = array('%name' => $this->text('Summernote'));
69
        $title = $this->text('Edit %name settings', $vars);
70
        $this->setTitle($title);
71
    }
72
73
    /**
74
     * Set breadcrumbs on the module settings page
75
     */
76
    protected function setBreadcrumbEditSettings()
77
    {
78
        $breadcrumbs = array();
79
80
        $breadcrumbs[] = array(
81
            'text' => $this->text('Dashboard'),
82
            'url' => $this->url('admin')
83
        );
84
85
        $breadcrumbs[] = array(
86
            'text' => $this->text('Modules'),
87
            'url' => $this->url('admin/module/list')
88
        );
89
90
        $this->setBreadcrumbs($breadcrumbs);
91
    }
92
93
    /**
94
     * Saves the submitted settings
95
     */
96
    protected function submitSettings()
97
    {
98
        if ($this->isPosted('save') && $this->validateSettings()) {
99
            $this->updateSettings();
100
        }
101
    }
102
103
    /**
104
     * Validate submitted module settings
105
     */
106
    protected function validateSettings()
107
    {
108
        $this->setSubmitted('settings', null, false);
109
110
        $this->validateElement('selector', 'required');
111
        $this->validateElement('config', 'json_encoded');
112
113
        if ($this->hasErrors()) {
114
            return false;
115
        }
116
117
        $this->setSubmittedArray('selector');
118
        $this->setSubmitted('config', json_decode($this->getSubmitted('config'), true));
119
        return true;
120
    }
121
122
    /**
123
     * Update module settings
124
     */
125
    protected function updateSettings()
126
    {
127
        $this->controlAccess('module_edit');
128
        $this->module->setSettings('summernote', $this->getSubmitted());
129
        $this->redirect('', $this->text('Settings have been updated'), 'success');
130
    }
131
132
    /**
133
     * Render and output the module settings page
134
     */
135
    protected function outputEditSettings()
136
    {
137
        $this->output('summernote|settings');
138
    }
139
140
}
141