This repository has been archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpitticatrovaprezzi.php
494 lines (433 loc) · 17.4 KB
/
pitticatrovaprezzi.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<?php
/**
* PrestaShop Module - pitticatrovaprezzi
*
* Copyright 2020-2022 Pittica S.r.l.
*
* @category Module
* @package Pittica/Trovaprezzi
* @author Lucio Benini <[email protected]>
* @copyright 2020-2022 Pittica S.r.l.
* @license http://opensource.org/licenses/LGPL-3.0 The GNU Lesser General Public License, version 3.0 ( LGPL-3.0 )
* @link https://github.com/pittica/prestashop-trovaprezzi
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require_once dirname(__FILE__) . '/classes/TrovaprezziOffer.php';
require_once dirname(__FILE__) . '/classes/Provider.php';
/**
* Trovaprezzi module class.
*
* @category Module
* @package Pittica/Trovaprezzi
* @author Lucio Benini <[email protected]>
* @license http://opensource.org/licenses/LGPL-3.0 The GNU Lesser General Public License, version 3.0 ( LGPL-3.0 )
* @link https://github.com/pittica/prestashop-trovaprezzi/blob/main/pitticatrovaprezzi.php
* @since 1.0.0
*/
class PitticaTrovaprezzi extends Module
{
/**
* {@inheritDoc}
*
* @since 1.0.0
*/
public function __construct()
{
$this->name = 'pitticatrovaprezzi';
$this->tab = 'front_office_features';
$this->version = '1.3.5';
$this->author = 'Pittica';
$this->need_instance = 1;
$this->bootstrap = 1;
parent::__construct();
$this->displayName = $this->l('TrovaPrezzi');
$this->description = $this->l('Creates an XML feed for TrovaPrezzi.it.');
$this->ps_versions_compliancy = array(
'min' => '1.7.7.0',
'max' => _PS_VERSION_
);
}
/**
* {@inheritDoc}
*
* @return boolean
* @since 1.0.0
*/
public function install()
{
include(dirname(__FILE__) . '/sql/install.php');
$carriers = Carrier::getCarriers((int) Configuration::get('PS_LANG_DEFAULT'), true);
reset($carriers);
Configuration::updateValue('PITTICA_TROVAPREZZI_CARRIER', !empty($carriers[0]['id_carrier']) ? (int) $carriers[0]['id_carrier'] : -1);
return parent::install() && $this->installTab() && $this->registerHook('displayFooterAfter');
}
/**
* {@inheritDoc}
*
* @return boolean
* @since 1.0.0
*/
public function uninstall()
{
include(dirname(__FILE__) . '/sql/uninstall.php');
return parent::uninstall() && $this->uninstallTab() && Configuration::deleteByName('PITTICA_TROVAPREZZI_CARRIER');
}
/**
* Registers the controller tab.
*
* @return boolean
* @since 1.0.0
*/
public function installTab()
{
$id = (int) Tab::getIdFromClassName('AdminTrovaprezzi');
if (!$id) {
$id = null;
}
$tab = new Tab($id);
$tab->active = 1;
$tab->class_name = 'AdminTrovaprezzi';
$tab->name = array();
foreach (Language::getLanguages() as $lang) {
$tab->name[$lang['id_lang']] = 'AdminTrovaprezzi';
}
$tab->module = $this->name;
return $tab->add();
}
/**
* Registers the controller tab.
*
* @return boolean
* @since 1.0.0
*/
public function uninstallTab()
{
$id = (int) Tab::getIdFromClassName('AdminTrovaprezzi');
if ($id) {
$tab = new Tab($id);
return $tab->delete();
}
return false;
}
/**
* {@inheritDoc}
*
* @return string
* @since 1.0.0
*/
public function getContent()
{
$output = '';
if (Tools::isSubmit('savepitticatrovaprezzi')) {
Configuration::updateValue('PITTICA_TROVAPREZZI_CARRIER', Tools::getValue('carrier'));
$output .= $this->displayConfirmation($this->l('Settings updated.'));
}
return $output . $this->renderForm();
}
/**
* Renders settings form.
*
* @return void
* @since 1.0.0
*/
protected function renderForm()
{
$lang = (int) Configuration::get('PS_LANG_DEFAULT');
$carriers = Carrier::getCarriers($lang, true);
$helper = new HelperForm();
$helper->module = $this;
$helper->name_controller = 'pitticatrovaprezzi';
$helper->identifier = $this->identifier;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
$helper->default_form_language = $lang;
$helper->allow_employee_form_lang = $lang;
$helper->title = $this->displayName;
$helper->submit_action = 'savepitticatrovaprezzi';
$check = $this->context->link->getAdminLink('AdminTrovaprezzi');
$helper->fields_value = array(
'generate' => $this->getGenerateFeedHtml(null),
'feed_trovaprezzi' => $this->getViewFeedHtml('trovaprezzi'),
'generate_trovaprezzi' => $this->getGenerateFeedHtml('trovaprezzi'),
'feed_google' => $this->getViewFeedHtml('google'),
'generate_google' => $this->getGenerateFeedHtml('google'),
'carrier' => Configuration::get('PITTICA_TROVAPREZZI_CARRIER'),
'check' => '<a href="' . $check . '">' . $this->l('Check non-compliant products.') . '</a>'
);
return $helper->generateForm(
array(
array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings')
),
'input' => array(
array(
'type' => 'select',
'label' => $this->l('Carrier:'),
'name' => 'carrier',
'options' => array(
'query' => $carriers,
'id' => 'id_carrier',
'name' => 'name'
)
),
array(
'type' => 'free',
'label' => $this->l('Check Products'),
'name' => 'check'
)
),
'submit' => array(
'title' => $this->l('Save')
)
)
),
array(
'form' => array(
'legend' => array(
'title' => $this->l('Feed Generator')
),
'input' => array(
array(
'type' => 'free',
'label' => $this->l('Generator URL'),
'name' => 'generate'
)
),
'submit' => array(
'title' => $this->l('Save')
)
)
),
array(
'form' => array(
'legend' => array(
'title' => $this->l('Trovaprezzi')
),
'input' => array(
array(
'type' => 'free',
'label' => $this->l('Feed URL'),
'name' => 'feed_trovaprezzi'
),
array(
'type' => 'free',
'label' => $this->l('Generator URL'),
'name' => 'generate_trovaprezzi'
)
),
'submit' => array(
'title' => $this->l('Save')
)
)
),
array(
'form' => array(
'legend' => array(
'title' => $this->l('Google')
),
'input' => array(
array(
'type' => 'free',
'label' => $this->l('Feed URL'),
'name' => 'feed_google'
),
array(
'type' => 'free',
'label' => $this->l('Generator URL'),
'name' => 'generate_google'
)
),
'submit' => array(
'title' => $this->l('Save')
)
)
)
)
);
}
/**
* Handles the displayFooterAfter hook.
*
* @param array $params Hook parameters.
*
* @return mixed
* @since 1.0.0
*/
public function hookDisplayFooterAfter($params)
{
return $this->display(__FILE__, 'displayFooterAfter.tpl');
}
/**
* Gets the file path of the XML feeds.
*
* @param string $file Filename.
* @param int $id_shop Shop ID.
*
* @return string
* @since 1.0.0
*/
public function getFilePath($file = 'trovaprezzi', $id_shop = null)
{
return _PS_MODULE_DIR_ . $this->name . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . ($id_shop === null ? Shop::getContextShopID() : (int) $id_shop) . '_' . $file . '.xml';
}
/**
* Generates the secuirty token.
*
* @return string
* @since 1.0.0
*/
public function getToken()
{
return Tools::hash(Configuration::get('PS_SHOP_DOMAIN'));
}
/**
* Updates the products data.
*
* @param int $id_shop Shop ID.
*
* @return void
* @since 1.0.0
*/
public function updateProducts($id_shop = null)
{
TrovaprezziOffer::truncate();
$lang = (int) Configuration::get('PS_LANG_DEFAULT');
$root = (int) Configuration::get('PS_ROOT_CATEGORY');
$home = (int) Configuration::get('PS_HOME_CATEGORY');
$currency = (int) Configuration::get('PS_CURRENCY_DEFAULT');
$group = (int) Configuration::get('PS_GUEST_GROUP');
$country = new Country((int) Configuration::get('PS_COUNTRY_DEFAULT'));
$products = Product::getProducts($lang, 0, 0, 'id_product', 'ASC', false, true);
$shops = $id_shop === null ? Shop::getShops(true, null, true) : array($id_shop);
foreach ($shops as $shop) {
$free = (float) Configuration::get('PS_SHIPPING_FREE_PRICE', null, null, $shop);
$carrier = (int) Configuration::get('PITTICA_TROVAPREZZI_CARRIER', null, null, $shop);
if ($free <= 0.0) {
$free = null;
}
foreach ($products as $p) {
$product = new Product((int) $p['id_product'], $lang, (int) $shop);
$attributes = $product->getAttributesResume($lang, ': ');
$categories = array();
$cat = '';
foreach (Product::getProductCategoriesFull($product->id, $lang) as $category) {
if ($category['id_category'] != $root && $category['id_category'] != $home) {
$categories[] = $category['name'];
}
if ($category['id_category'] == $product->id_category_default) {
$cat = $category['link_rewrite'];
}
}
if (!empty($attributes)) {
foreach ($attributes as $attribute) {
if ((int) $attribute['quantity'] > 0) {
$minimal = (!empty($attribute['minimal_quantity']) && $attribute['minimal_quantity'] > 0) ? (int) $attribute['minimal_quantity'] : 1;
$ean = empty($attribute['ean13']) ? $product->ean13 : $attribute['ean13'];
$offer = TrovaprezziOffer::fromProduct($product, $shop, $lang);
$offer->updatePrices($shop, $currency, $minimal, $country->id, $group, $product->id, (int) $attribute['id_product_attribute']);
$offer->id_product_attribute = (int) $attribute['id_product_attribute'];
$offer->name = (is_array($product->name) ? $product->name[$lang] : $product->name) . ' - ' . $attribute['attribute_designation'];
$offer->link = $this->context->link->getProductLink($product, null, $cat, null, null, $shop, (int) $attribute['id_product_attribute']);
$offer->stock = (int) $attribute['quantity'];
$offer->categories = implode(', ', $categories);
$offer->weight = (float) $attribute['weight'] + (float) $product->weight;
$offer->part_number = empty($attribute['reference']) ? $ean : $attribute['reference'];
$offer->ean_code = $ean;
$offer->updateShippingCost($shop, $currency, $carrier, $country, $lang, $minimal, $product, (int) $attribute['id_product_attribute'], $free);
$offer->populateImages($this->context, $product, $lang, $shop);
$offer->add();
}
}
} else {
if ((int) $product->quantity) {
$minimal = $product->minimal_quantity > 0 ? (int) $product->minimal_quantity : 1;
$offer = TrovaprezziOffer::fromProduct($product, $shop, $lang);
$offer->updatePrices($shop, $currency, $minimal, $country->id, $group, $product->id);
$offer->name = is_array($product->name) ? $product->name[$lang] : $product->name;
$offer->link = $this->context->link->getProductLink($product, null, $cat, null, null, $shop);
$offer->stock = (int) $product->quantity;
$offer->categories = implode(', ', $categories);
$offer->weight = (float) $product->weight;
$offer->part_number = empty($product->reference) ? $product->ean13 : $product->reference;
$offer->ean_code = $product->ean13;
$offer->updateShippingCost($shop, $currency, $carrier, $country, $lang, $minimal, $product, null, $free);
$offer->populateImages($this->context, $product, $lang, $shop);
$offer->add();
}
}
}
}
}
/**
* Generates the feeds.
*
* @param boolean $refresh A value indicating whether the data requires to be refreshed.
* @param Provider $provider Feed provider.
* @param int $id_shop Shop ID.
*
* @return boolean Returns "True" whether the XML file has been generated; otherwise, "False".
* @since 1.0.0
*/
public function generate($refresh = true, $provider = null, $id_shop = null)
{
if ($refresh) {
$this->updateProducts();
}
$providers = Provider::getProviders();
$shops = $id_shop === null ? Shop::getShops(true, null, true) : array($id_shop);
if ($provider && in_array($provider, $providers)) {
$providers = array($provider);
}
foreach ($providers as $p) {
$object = Provider::getProvider($p);
foreach ($shops as $shop) {
$object->generate($this->getFilePath($p, $shop), $shop);
}
}
return true;
}
/**
* Generates the HTML of the generator link in the configuration form.
*
* @param Provider $provider Feed provider.
*
* @return void
* @since 1.0.0
*/
protected function getGenerateFeedHtml($provider)
{
$url = $this->context->link->getModuleLink(
$this->name,
'generate',
array(
'provider' => $provider,
'token' => $this->getToken()
)
);
return $this->l('Use this link to generate the XML Feed:') . '<br/><a href="' . $url . '" target="_system">' . $url . '</a>';
}
/**
* Generates the HTML of the view link in the configuration form.
*
* @param Provider $provider Feed provider.
*
* @return void
* @since 1.0.0
*/
protected function getViewFeedHtml($provider)
{
$url = $this->context->link->getModuleLink(
$this->name,
'download',
array(
'provider' => $provider,
'token' => $this->getToken()
)
);
return $this->l('XML Feed URL:') . '<br/><a href="' . $url . '" target="_system">' . $url . '</a>';
}
}