Settings::getLibraryModesSettings()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
/**
4
 * @package Code Mirror
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\codemirror\controllers;
11
12
use gplcart\core\controllers\backend\Controller;
13
14
/**
15
 * Handles incoming requests and outputs data related Code Mirror module settings
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('modes', $this->getLibraryModesSettings());
29
        $this->setData('themes', $this->getLibraryThemesSettings());
30
        $this->setData('settings', $this->module->getSettings('codemirror'));
31
32
        $this->submitSettings();
33
        $this->outputEditSettings();
34
    }
35
36
    /**
37
     * Returns an array of available CodeMirror syntax modes
38
     * @return array
39
     */
40
    protected function getLibraryModesSettings()
41
    {
42
        $pattern = __DIR__ . '/../vendor/codemirror/CodeMirror/mode/*';
43
44
        $modes = array();
45
        foreach (glob($pattern, GLOB_ONLYDIR) as $directory) {
46
            $modes[] = basename($directory, '');
47
        }
48
49
        return gplcart_array_split($modes, 6);
50
    }
51
52
    /**
53
     * Returns an array of available CodeMirror themes
54
     * @return array
55
     */
56
    protected function getLibraryThemesSettings()
57
    {
58
        $pattern = __DIR__ . '/../vendor/codemirror/CodeMirror/theme/*.css';
59
60
        $themes = array();
61
62
        foreach (glob($pattern) as $file) {
63
            $themes[] = pathinfo($file, PATHINFO_FILENAME);
64
        }
65
66
        return $themes;
67
    }
68
69
    /**
70
     * Set title on the module settings page
71
     */
72
    protected function setTitleEditSettings()
73
    {
74
        $title = $this->text('Edit %name settings', array('%name' => $this->text('Codemirror')));
75
        $this->setTitle($title);
76
    }
77
78
    /**
79
     * Set breadcrumbs on the module settings page
80
     */
81
    protected function setBreadcrumbEditSettings()
82
    {
83
        $breadcrumbs = array();
84
85
        $breadcrumbs[] = array(
86
            'text' => $this->text('Dashboard'),
87
            'url' => $this->url('admin')
88
        );
89
90
        $breadcrumbs[] = array(
91
            'text' => $this->text('Modules'),
92
            'url' => $this->url('admin/module/list')
93
        );
94
95
        $this->setBreadcrumbs($breadcrumbs);
96
    }
97
98
    /**
99
     * Saves the submitted settings
100
     */
101
    protected function submitSettings()
102
    {
103
        if ($this->isPosted('save') && $this->validateSettings()) {
104
            $this->updateSettings();
105
        }
106
    }
107
108
    /**
109
     * Validate submitted module settings
110
     */
111
    protected function validateSettings()
112
    {
113
        $this->setSubmitted('settings');
114
115
        return !$this->hasErrors();
116
    }
117
118
    /**
119
     * Update module settings
120
     */
121
    protected function updateSettings()
122
    {
123
        $this->controlAccess('module_edit');
124
        $this->module->setSettings('codemirror', $this->getSubmitted());
125
        $this->redirect('', $this->text('Settings have been updated'), 'success');
126
    }
127
128
    /**
129
     * Render and output the module settings page
130
     */
131
    protected function outputEditSettings()
132
    {
133
        $this->output('codemirror|settings');
134
    }
135
136
}
137