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