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 Currency
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 GNU General Public License 3.0
8
 */
9
10
namespace gplcart\modules\currency\controllers;
11
12
use gplcart\core\controllers\backend\Controller;
13
use gplcart\modules\currency\models\Currency;
14
15
/**
16
 * Handles incoming requests and outputs data related to Currency module settings
17
 */
18
class Settings extends Controller
19
{
20
21
    /**
22
     * Currency model instance
23
     * @var \gplcart\modules\currency\models\Currency $currency_model
24
     */
25
    protected $currency_model;
26
27
    /**
28
     * @param Currency $currency
29
     */
30
    public function __construct(Currency $currency)
31
    {
32
        parent::__construct();
33
34
        $this->currency_model = $currency;
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
        $this->setData('settings', $this->module->getSettings('currency'));
45
        $this->submitSettings();
46
        $this->outputEditSettings();
47
    }
48
49
    /**
50
     * Saves the submitted settings
51
     */
52
    protected function submitSettings()
53
    {
54
        if ($this->isPosted('save') && $this->validateSettings()) {
55
            $this->updateSettings();
56
        }
57
    }
58
59
    /**
60
     * Validates an array of submitted values
61
     * @return bool
62
     */
63
    protected function validateSettings()
64
    {
65
        $this->setSubmitted('settings');
66
        $this->setSubmittedBool('status');
67
68
        $this->validateElement('interval', 'integer');
69
        $this->validateElement('correction', 'numeric');
70
71
        $parts = array_map('trim', explode(',', $this->getSubmitted('derivation')));
72
73
        if (count($parts) != count(array_filter($parts, 'is_numeric')) || count($parts) != 4) {
74
            $this->setError('derivation', $this->text('Derivation must contain exactly 4 positive numbers separated by comma'));
75
        }
76
77
        if ($this->hasErrors()) {
78
            return false;
79
        }
80
81
        $this->setSubmitted('derivation', $parts);
82
        return true;
83
    }
84
85
    /**
86
     * Updates module settings
87
     */
88
    protected function updateSettings()
89
    {
90
        $this->controlAccess('module_edit');
91
        $this->module->setSettings('currency', $this->getSubmitted());
92
93
        if ($this->getSubmitted('update')) {
94
            $results = $this->updateRateSettings();
95
            if (empty($results)) {
96
                $severity = 'warning';
97
                $message = $this->text('Currencies have not been updated');
98
            } else {
99
                $severity = 'success';
100
                $vars = array('@list' => implode(',', array_keys($results)));
101
                $message = $this->text('Updated the following currencies: @list', $vars);
102
            }
103
104
            $this->setMessage($message, $severity, true);
105
        }
106
107
        $this->redirect('', $this->text('Settings have been updated'), 'success');
108
    }
109
110
    /**
111
     * Updates currency rates
112
     * @return array
113
     */
114
    protected function updateRateSettings()
115
    {
116
        $this->controlAccess('currency_edit');
117
118
        return $this->currency_model->update($this->getSubmitted());
119
    }
120
121
    /**
122
     * Set title on the module settings page
123
     */
124
    protected function setTitleEditSettings()
125
    {
126
        $title = $this->text('Edit %name settings', array('%name' => $this->text('Currency')));
127
        $this->setTitle($title);
128
    }
129
130
    /**
131
     * Set breadcrumbs on the module settings page
132
     */
133
    protected function setBreadcrumbEditSettings()
134
    {
135
        $breadcrumbs = array();
136
137
        $breadcrumbs[] = array(
138
            'text' => $this->text('Dashboard'),
139
            'url' => $this->url('admin')
140
        );
141
142
        $breadcrumbs[] = array(
143
            'text' => $this->text('Modules'),
144
            'url' => $this->url('admin/module/list')
145
        );
146
147
        $this->setBreadcrumbs($breadcrumbs);
148
    }
149
150
    /**
151
     * Render and output the module settings page
152
     */
153
    protected function outputEditSettings()
154
    {
155
        $this->output('currency|settings');
156
    }
157
158
}
159