Completed
Push — master ( 723e59...09550d )
by Iurii
01:03
created

Settings::validateSettings()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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