forked from soft-industry/ps-yme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyme.php
336 lines (296 loc) · 10.8 KB
/
yme.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
* 2017 Soft Industry
*
* @author Skorobogatko Alexei <a.skorobogatko@soft-industry.com>
* @copyright 2017 Soft-Industry
* @license http://opensource.org/licenses/afl-3.0.php
*/
/**
* DON'T USE "use" STATEMENTS IN THIS FILE!
* THIS LEADS TO FATAL ERROR.
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require_once dirname(__FILE__) . '/classes/autoload.php';
require_once dirname(__FILE__) . '/vendor/autoload.php';
/**
* Yandex Market
*
* Export products to Yandex Market.
*/
class Yme extends Module
{
/**
* Module contructor.
*/
public function __construct()
{
$this->name = 'yme';
$this->version = '0.1.0';
$this->author = 'Soft Industry';
$this->tab = 'export';
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Yandex Market');
$this->description = $this->l('Export products to Yandex Market');
$this->confirmUninstall = $this->l('Are you sure to uninstall module ?');
}
/**
* Module installation.
*/
public function install()
{
// Multishop.
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}
return parent::install() &&
$this->registerHook('offerElementBeforeValidate');
}
/**
* Module uninstallation.
*/
public function uninstall()
{
if (!parent::uninstall()) {
return false;
}
Configuration::deleteByName('YME_OFFER');
Configuration::deleteByName('YME_EXPORT');
Configuration::deleteByName('YME_EXPORT_FORMAT');
return true;
}
/**
* Process module configuration form.
*
* @return string
*/
public function getContent()
{
$status = ''; // Message of last operation.
$offer_types = \SI\YandexMarket\OfferType::getTypes();
$offer_features = $this->getOfferFeatures();
// Update offer types.
foreach ($offer_types as $offer_type => $offer_title) {
if (!Tools::isSubmit("SubmitOfferFeatures_{$offer_type}")) {
continue;
}
$offer = new \SI\YandexMarket\OfferType($offer_type);
foreach ($offer->getFeatures() as $feature => $feature_title) {
$value_key = "YME_OFFER_{$offer_type}_{$feature}";
$offer_features[$offer_type][$feature] = (int) Tools::getValue($value_key);
}
if (Configuration::updateValue('YME_OFFER', serialize($offer_features))) {
$status .= $this->displayConfirmation(
$this->l($offer_title) . ' ' . $this->l('has been updated.')
);
}
}
// Update common module settings.
if (Tools::isSubmit('SubmitSettings')) {
$result = false; // Status result.
$offer_type = Tools::getValue('YME_EXPORT');
if (isset($offer_types[$offer_type])) {
$result = Configuration::updateValue('YME_EXPORT', $offer_type);
}
$export_format = Tools::getValue('YME_EXPORT_FORMAT');
$render_types = \SI\YandexMarket\Renders\Factory::getTypes();
if (isset($render_types[$export_format])) {
$result = Configuration::updateValue('YME_EXPORT_FORMAT', $export_format);
}
$enabled = (bool) Tools::getValue('YME_ENABLE');
Configuration::updateValue('YME_ENABLE', $enabled);
if ($result) {
$status .= $this->displayConfirmation($this->l('Settings has been saved.'));
}
}
return $status . $this->displayForm();
}
/**
* Module configuration form.
*
* @return string
*/
public function displayForm()
{
$helper = new HelperForm();
$features = array_merge(array('-'), Feature::getFeatures($this->context->language->id));
$offer_types = \SI\YandexMarket\OfferType::getTypes();
$render_types = \SI\YandexMarket\Renders\Factory::getTypes();
$form = array();
// Main settings form.
$form[] = array(
'form' => array(
'legend' => array(
'title' => $this->l('Yandex Market Export settings'),
),
'input' => array(
array(
'type' => 'switch',
'label' => $this->l('Enable export'),
'name' => 'YME_ENABLE',
'is_bool' => true,
'values' => array(
array(
'id' => 'YME_ENABLE_YES',
'value' => 1,
'label' => $this->l('Enabled'),
),
array(
'id' => 'YME_ENABLE_NO',
'value' => 0,
'label' => $this->l('Disabled'),
),
),
),
array(
'type' => 'select',
'label' => $this->l('Offer type'),
'name' => 'YME_EXPORT',
'default_value' => \SI\YandexMarket\OfferType::SIMPLE,
'options' => array(
'query' => array_map(null, array_keys($offer_types), array_values($offer_types)),
'id' => 0,
'name' => 1,
),
),
array(
'type' => 'radio',
'label' => $this->l('Output format'),
'name' => 'YME_EXPORT_FORMAT',
'default_value' => \SI\YandexMarket\Renders\Factory::YML,
'values' => array_map(function ($type, $title) {
return array(
'id' => $type,
'value' => $type,
'label' => $title,
);
}, array_keys($render_types), array_values($render_types)),
),
),
'submit' => array(
'title' => $this->l('Save'),
'name' => 'SubmitSettings',
),
),
);
$helper->fields_value['YME_EXPORT'] = $this->getOfferType();
$helper->fields_value['YME_EXPORT_FORMAT'] = $this->getExportFormat();
$helper->fields_value['YME_ENABLE'] = $this->isExportEnabled();
// Offer element forms.
foreach ($offer_types as $type => $title) {
$offer = new \SI\YandexMarket\OfferType($type);
$offer_features = $offer->getFeatures();
if (empty($offer_features)) {
continue;
}
$form_type = array(
'form' => array(
'legend' => array(
'title' => $this->l(Tools::ucfirst($type)),
),
'input' => array(),
),
);
$inputs = &$form_type['form']['input'];
foreach ($offer_features as $name => $title) {
$input_name = "YME_OFFER_{$type}_{$name}";
$inputs[] = array(
'type' => 'select',
'label' => $this->l($title),
'name' => $input_name,
'default_value' => '',
'options' => array(
'query' => $features,
'id' => 'id_feature',
'name' => 'name',
),
);
$helper->fields_value[$input_name] = $this->getOfferFeatures($type, $name);
}
$form_type['form']['submit'] = array(
'title' => $this->l('Save'),
'name' => 'SubmitOfferFeatures_' . $type,
);
$form[] = $form_type;
}
return $helper->generateForm($form);
}
/**
* Returns list of features assigned to types.
*
* @param string $type Optional. Offer type name.
* @param string $prop Optional. Offer element property.
* @param boolean $translated Optional. Translate feature id to feature name.
* @return array|string
*/
public function getOfferFeatures($type = null, $prop = null, $translated = false)
{
$features = unserialize(Configuration::get('YME_OFFER'));
if ($features === false) {
return array();
}
/**
* Translate feature id to feature name.
*
* @param array|string $ids
*/
$translate = function ($ids) use ($translated) {
if (!$translated) {
return $ids;
}
$id_lang = $this->context->language->id;
if (is_array($ids)) {
return array_map(function ($id) use ($id_lang) {
$feature = new Feature($id);
return Validate::isLoadedObject($feature) ? $feature->name[$id_lang] : null;
}, $ids);
}
$feature = new Feature($ids);
return Validate::isLoadedObject($feature) ? $feature->name[$id_lang] : null;
};
if ($type === null) {
return $translate($features);
}
if (!isset($features[$type])) {
return array();
}
if ($prop !== null) {
return isset($features[$type][$prop]) ? $translate($features[$type][$prop]) : '';
}
return $translate($features[$type]);
}
/**
* Get configured offer type.
*
* @return string
*/
public function getOfferType()
{
$value = Configuration::get('YME_EXPORT');
return $value === false ? \SI\YandexMarket\OfferType::SIMPLE : $value;
}
/**
* Get configured export format.
*
* @return string
*/
public function getExportFormat()
{
$value = Configuration::get('YME_EXPORT_FORMAT');
return $value === false ? \SI\YandexMarket\Renders\Factory::YML : $value;
}
/**
* Is export enabled ?
*
* @return bool
*/
public function isExportEnabled()
{
return (bool) Configuration::get('YME_ENABLE');
}
}