Settings::editSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package Compressor
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\compressor\controllers;
11
12
use gplcart\core\controllers\backend\Controller;
13
14
/**
15
 * Handles incoming requests and outputs data related to Compressor 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('compressor'));
29
30
        $this->submitSettings();
31
        $this->outputEditSettings();
32
    }
33
34
    /**
35
     * Set title on the module settings page
36
     */
37
    protected function setTitleEditSettings()
38
    {
39
        $title = $this->text('Edit %name settings', array('%name' => $this->text('Compressor')));
40
        $this->setTitle($title);
41
    }
42
43
    /**
44
     * Set breadcrumbs on the module settings page
45
     */
46
    protected function setBreadcrumbEditSettings()
47
    {
48
        $breadcrumbs = array();
49
50
        $breadcrumbs[] = array(
51
            'text' => $this->text('Dashboard'),
52
            'url' => $this->url('admin')
53
        );
54
55
        $breadcrumbs[] = array(
56
            'text' => $this->text('Modules'),
57
            'url' => $this->url('admin/module/list')
58
        );
59
60
        $this->setBreadcrumbs($breadcrumbs);
61
    }
62
63
    /**
64
     * Saves the submitted settings
65
     */
66
    protected function submitSettings()
67
    {
68
        if ($this->isPosted('clear_cache')) {
69
            $this->clearCacheSettings();
70
        } else if ($this->isPosted('save') && $this->validateSettings()) {
71
            $this->updateSettings();
72
        }
73
    }
74
75
    /**
76
     * Validate submitted module settings
77
     * @return bool
78
     */
79
    protected function validateSettings()
80
    {
81
        $this->setSubmitted('settings');
82
        $this->setSubmittedBool('status_js');
83
        $this->setSubmittedBool('status_css');
84
85
        return !$this->hasErrors();
86
    }
87
88
    /**
89
     * Update module settings
90
     */
91
    protected function updateSettings()
92
    {
93
        $this->controlAccess('module_edit');
94
        $this->module->setSettings('compressor', $this->getSubmitted());
95
        $this->redirect('', $this->text('Settings have been updated'), 'success');
96
    }
97
98
    /**
99
     * Deletes all aggregated assets
100
     */
101
    protected function clearCacheSettings()
102
    {
103
        gplcart_file_delete_recursive(GC_DIR_ASSET . '/compiled');
104
105
        $this->redirect('', $this->text('Cache has been deleted'), 'success');
106
    }
107
108
    /**
109
     * Render and output the module settings page
110
     */
111
    protected function outputEditSettings()
112
    {
113
        $this->output('compressor|settings');
114
    }
115
}
116