1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Exporter |
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\export\controllers; |
11
|
|
|
|
12
|
|
|
use gplcart\core\controllers\backend\Controller; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Handles incoming requests and outputs data related to Exporter 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
|
|
|
$this->setData('settings', $this->module->getSettings('export')); |
28
|
|
|
|
29
|
|
|
$this->submitSettings(); |
30
|
|
|
$this->setDataEditSettings(); |
31
|
|
|
$this->outputEditSettings(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Prepare data before rendering |
36
|
|
|
*/ |
37
|
|
|
protected function setDataEditSettings() |
38
|
|
|
{ |
39
|
|
|
$header = $this->getData('settings.header'); |
40
|
|
|
|
41
|
|
|
if (is_array($header)) { |
42
|
|
|
|
43
|
|
|
$string = ''; |
44
|
|
|
foreach ($header as $key => $value) { |
45
|
|
|
$string .= "$key $value" . PHP_EOL; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->setData('settings.header', trim($string)); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set title on the module settings page |
54
|
|
|
*/ |
55
|
|
|
protected function setTitleEditSettings() |
56
|
|
|
{ |
57
|
|
|
$title = $this->text('Edit %name settings', array('%name' => $this->text('Exporter'))); |
58
|
|
|
$this->setTitle($title); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set breadcrumbs on the module settings page |
63
|
|
|
*/ |
64
|
|
|
protected function setBreadcrumbEditSettings() |
65
|
|
|
{ |
66
|
|
|
$breadcrumbs = array(); |
67
|
|
|
|
68
|
|
|
$breadcrumbs[] = array( |
69
|
|
|
'text' => $this->text('Dashboard'), |
70
|
|
|
'url' => $this->url('admin') |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$breadcrumbs[] = array( |
74
|
|
|
'text' => $this->text('Modules'), |
75
|
|
|
'url' => $this->url('admin/module/list') |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
$this->setBreadcrumbs($breadcrumbs); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Saves the submitted settings |
83
|
|
|
*/ |
84
|
|
|
protected function submitSettings() |
85
|
|
|
{ |
86
|
|
|
if ($this->isPosted('reset')) { |
87
|
|
|
$this->updateSettings(array()); |
88
|
|
|
} else if ($this->isPosted('save') && $this->validateSettings()) { |
89
|
|
|
$this->updateSettings($this->getSubmitted()); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Validate submitted module settings |
95
|
|
|
*/ |
96
|
|
|
protected function validateSettings() |
97
|
|
|
{ |
98
|
|
|
$this->setSubmitted('settings'); |
99
|
|
|
|
100
|
|
|
$this->validateElement('limit', 'numeric'); |
101
|
|
|
$this->validateElement('limit', 'required'); |
102
|
|
|
$this->validateElement('multiple', 'required'); |
103
|
|
|
$this->validateElement('delimiter', 'required'); |
104
|
|
|
|
105
|
|
|
$this->validateHeaderSettings(); |
106
|
|
|
|
107
|
|
|
return !$this->hasErrors(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Validate header mapping |
112
|
|
|
*/ |
113
|
|
|
protected function validateHeaderSettings() |
114
|
|
|
{ |
115
|
|
|
$errors = $header = array(); |
116
|
|
|
$lines = gplcart_string_explode_multiline($this->getSubmitted('header', '')); |
117
|
|
|
|
118
|
|
|
foreach ($lines as $pos => $line) { |
119
|
|
|
|
120
|
|
|
$pos++; |
121
|
|
|
$data = array_filter(array_map('trim', explode(' ', $line, 2))); |
122
|
|
|
|
123
|
|
|
if (count($data) != 2) { |
124
|
|
|
$errors[] = $pos; |
125
|
|
|
continue; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
list($key, $label) = $data; |
129
|
|
|
|
130
|
|
|
if (preg_match('/^[a-z_]+$/', $key) !== 1) { |
131
|
|
|
$errors[] = $pos; |
132
|
|
|
continue; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$header[$key] = $label; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (empty($errors)) { |
139
|
|
|
$this->setSubmitted('header', $header); |
140
|
|
|
} else { |
141
|
|
|
$vars = array('@num' => implode(',', $errors)); |
142
|
|
|
$this->setError('header', $this->text('Error on line @num', $vars)); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Update module settings |
148
|
|
|
* @param array $settings |
149
|
|
|
*/ |
150
|
|
|
protected function updateSettings(array $settings) |
151
|
|
|
{ |
152
|
|
|
$this->controlAccess('module_edit'); |
153
|
|
|
|
154
|
|
|
$this->module->setSettings('export', $settings); |
155
|
|
|
$this->redirect('', $this->text('Settings have been updated'), 'success'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Render and output the module settings page |
160
|
|
|
*/ |
161
|
|
|
protected function outputEditSettings() |
162
|
|
|
{ |
163
|
|
|
$this->output('export|settings'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
} |
167
|
|
|
|