forked from doofinder/doofinder-prestashop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeed.php
More file actions
552 lines (476 loc) · 17.2 KB
/
Copy pathfeed.php
File metadata and controls
552 lines (476 loc) · 17.2 KB
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the Software License Agreement.
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* You must not modify, adapt or create derivative works of this source code
*
* @author Doofinder
* @copyright Doofinder
* @license GPLv3
*/
/**
* Accepted parameters:
*
* - limit: Max results in this request.
* - offset: Zero-based position to start getting results.
* - language: Language ISO code, like "es" or "en"
* - currency: Currency ISO code, like "EUR" or "GBP"
* - taxes: Boolean. Apply taxes to prices. Default true.
* - prices: Boolean. Display Prices. Default true.
*/
@set_time_limit(3600 * 2);
require_once(dirname(__FILE__) . '/../../config/config.inc.php');
require_once(dirname(__FILE__) . '/../../init.php');
$doofinder_hash = Configuration::get('DF_FEED_HASH');
$enable_hash = Configuration::get('DF_ENABLE_HASH', null);
$dfsec_hash = Tools::getValue('dfsec_hash');
if ($enable_hash) {
if (!empty($doofinder_hash) && $dfsec_hash != $doofinder_hash) {
header('HTTP/1.1 403 Forbidden', true, 403);
$msgError = 'Forbidden access.'
. ' Maybe security token missed.'
. ' Please check on your doofinder module'
. ' configuration page the new URL'
. ' for your feed';
exit($msgError);
}
}
require_once(dirname(__FILE__) . '/doofinder.php');
function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// lowercase
$text = Tools::strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
if (empty($text)) {
return 'n-a';
}
return $text;
}
/**
* @author camlafit <https://github.com/camlafit>
* Merge multidemensionnal array by value on each row
* https://stackoverflow.com/questions/7973915/php-merge-arrays-by-value
*/
function array_merge_by_id_product($array1 = array(), $array2 = array())
{
$sub_key = 'id_product';
$result = array();
$result_row = array();
if (empty($array1)) {
return $array2;
}
if (empty($array2)) {
return $array1;
}
foreach ($array1 as $item1) {
$result_row = array();
//Merge data
foreach ($array2 as $item2) {
if ($item1[$sub_key] == $item2[$sub_key]) {
$result_row = array_merge($item1, $item2);
break;
}
}
//If no array merged
if (empty($result_row)) {
$result_row = $item1;
}
$result[] = $result_row;
}
return $result;
}
$context = Context::getContext();
$shop = new Shop((int) $context->shop->id);
if (!$shop->id) {
die('NOT PROPERLY CONFIGURED');
}
// CONFIG
$lang = dfTools::getLanguageFromRequest();
$currency = dfTools::getCurrencyForLanguageFromRequest($lang);
$cfg_short_description = (dfTools::cfg(
$shop->id,
'DF_GS_DESCRIPTION_TYPE',
Doofinder::GS_SHORT_DESCRIPTION
) == Doofinder::GS_SHORT_DESCRIPTION);
$cfg_display_prices = dfTools::getBooleanFromRequest(
'prices',
(bool) dfTools::cfg($shop->id, 'DF_GS_DISPLAY_PRICES', Doofinder::YES)
);
$cfg_prices_w_taxes = dfTools::getBooleanFromRequest(
'taxes',
(bool) dfTools::cfg($shop->id, 'DF_GS_PRICES_USE_TAX', Doofinder::YES)
);
$cfg_display_stock_qty = dfTools::cfg($shop->id, 'DF_GS_STOCK');
$cfg_image_size = dfTools::cfg($shop->id, 'DF_GS_IMAGE_SIZE');
$cfg_mod_rewrite = dfTools::cfg($shop->id, 'PS_REWRITING_SETTINGS', Doofinder::YES);
$cfg_product_variations = (int) dfTools::cfg($shop->id, 'DF_SHOW_PRODUCT_VARIATIONS');
$cfg_product_features = dfTools::cfg($shop->id, 'DF_SHOW_PRODUCT_FEATURES');
$cfg_debug = dfTools::cfg($shop->id, 'DF_DEBUG');
$cfg_features_shown = explode(',', dfTools::cfg($shop->id, 'DF_FEATURES_SHOWN'));
$cfg_group_attributes_shown = explode(',', dfTools::cfg($shop->id, 'DF_GROUP_ATTRIBUTES_SHOWN'));
$limit_group_attributes = false;
if (isset($cfg_group_attributes_shown) &&
count($cfg_group_attributes_shown) > 0 &&
$cfg_group_attributes_shown[0] !== "") {
$group_attributes = AttributeGroup::getAttributesGroups($lang->id);
$group_attributes_slug = array();
foreach ($group_attributes as $g) {
if (in_array($g['id_attribute_group'], $cfg_group_attributes_shown)) {
$group_attributes_slug[] = slugify($g['name']);
}
}
$limit_group_attributes = true;
}
$debug = dfTools::getBooleanFromRequest('debug', false);
$limit = Tools::getValue('limit', false);
$offset = Tools::getValue('offset', false);
if ($debug) {
error_reporting(E_ALL);
ini_set('display_errors', 1);
}
if ($cfg_debug) {
error_log("Starting feed.\n", 3, dirname(__FILE__) . '/doofinder.log');
}
// OUTPUT
if (isset($_SERVER['HTTPS'])) {
header('Strict-Transport-Security: max-age=500');
}
header("Content-Type:text/plain; charset=utf-8");
// HEADER
$header = array('id');
if ($cfg_product_variations == 1) {
$header[] = 'item_group_id';
}
$header = array_merge($header, array('title', 'link', 'description', 'alternate_description',
'meta_keywords', 'meta_title', 'meta_description', 'image_link',
'categories', 'availability', 'brand', 'mpn', 'ean13', 'upc', 'reference',
'supplier_reference','extra_title_1', 'extra_title_2', 'tags'));
if (dfTools::versionGte('1.7.0.0')) {
$header = array_merge($header, array('isbn'));
}
if ($cfg_display_stock_qty) {
$header[] = 'stock_quantity';
}
if ($cfg_display_prices) {
$header[] = 'price';
$header[] = 'sale_price';
}
if ($cfg_product_variations == 1) {
$header[] = 'variation_reference';
$attribute_keys = dfTools::getAttributeKeysForShopAndLang($shop->id, $lang->id);
$alt_attribute_keys = array();
foreach ($attribute_keys as $key) {
$header_value = slugify($key);
if ($limit_group_attributes && !in_array($header_value, $group_attributes_slug)) {
continue;
}
$alt_attribute_keys[] = $key;
$header[] = $header_value;
}
$attribute_keys = $alt_attribute_keys;
}
if ($cfg_product_features) {
$all_feature_keys = dfTools::getFeatureKeysForShopAndLang($shop->id, $lang->id);
if (isset($cfg_features_shown) &&
count($cfg_features_shown) > 0 &&
$cfg_features_shown[0] !== "") {
$feature_keys = dfTools::getSelectedFeatures($all_feature_keys, $cfg_features_shown);
} else {
$feature_keys = $all_feature_keys;
}
$header[] = "attributes";
}
/**
* @author camlafit <https://github.com/camlafit>
* Extend doofinder feed
*
* To add an new header, module can do an array_merge on $extra_header
* To add an new data to a product, module must create a multidemensionnal array as this :
* array(
* index => array(
* 'id_product' => value,
* 'new_header_column_name' => 'value related to the new column'
* ),
* [...]
* )
* As each module can extend $extra_header and $extra_rows don't forget to merge them
*/
$extra_header = array();
$extra_rows = array();
Hook::exec('actionDoofinderExtendFeed', array(
'extra_header' => &$extra_header,
'extra_rows' => &$extra_rows,
'id_lang' => $lang->id,
'id_shop' => $shop->id,
'limit' => $limit,
'offset' => $offset,
));
if (!empty($extra_header)) {
$header = array_merge($header, $extra_header);
}
if (!$limit || ($offset !== false && (int)$offset === 0)) {
echo implode(TXT_SEPARATOR, $header) . PHP_EOL;
dfTools::flush();
}
// PRODUCTS
$rows = dfTools::getAvailableProductsForLanguage($lang->id, $shop->id, $limit, $offset);
if (!empty($extra_rows)) {
$rows = array_merge_by_id_product($rows, $extra_rows);
}
foreach ($rows as $row) {
if ((int)$row['id_product'] > 0) {
// ID, TITLE, LINK
if ($cfg_product_variations == 1 &&
isset($row['id_product_attribute']) &&
(int)$row['id_product_attribute'] > 0) {
// ID
echo "VAR-" . $row['id_product_attribute'] . TXT_SEPARATOR;
//ITEM-GROUP-ID
echo $row['id_product'] . TXT_SEPARATOR;
// TITLE
$product_title = dfTools::cleanString($row['name']);
echo $product_title . TXT_SEPARATOR;
echo dfTools::cleanURL(
$context->link->getProductLink(
(int)$row['id_product'],
$row['link_rewrite'],
$row['cat_link_rew'],
$row['ean13'],
$lang->id,
$shop->id,
(int)$row['id_product_attribute'],
$cfg_mod_rewrite,
false,
true
)
) . TXT_SEPARATOR;
} else {
$eanLink = $row['ean13'];
// ID
echo $row['id_product'] . TXT_SEPARATOR;
if ($cfg_product_variations == 1) {
//ITEM-GROUP-ID
echo "" . TXT_SEPARATOR;
}
// TITLE
$product_title = dfTools::cleanString($row['name']);
echo $product_title . TXT_SEPARATOR;
echo dfTools::cleanURL(
$context->link->getProductLink(
(int)$row['id_product'],
$row['link_rewrite'],
$row['cat_link_rew'],
$eanLink,
$lang->id,
$shop->id,
0,
$cfg_mod_rewrite
)
) . TXT_SEPARATOR;
}
// DESCRIPTION
echo dfTools::cleanString(
$row[($cfg_short_description ? 'description_short' : 'description')]
) . TXT_SEPARATOR;
// ALTERNATE DESCRIPTION
echo dfTools::cleanString(
$row[($cfg_short_description ? 'description' : 'description_short')]
) . TXT_SEPARATOR;
// META KEYWORDS
echo dfTools::cleanString($row['meta_keywords']) . TXT_SEPARATOR;
// META TITLE
echo dfTools::cleanString($row['meta_title']) . TXT_SEPARATOR;
// META DESCRIPTION
echo dfTools::cleanString($row['meta_description']) . TXT_SEPARATOR;
// IMAGE LINK
if ($cfg_product_variations == 1 && isset($row['id_product_attribute']) &&
(int)$row['id_product_attribute'] > 0) {
$cover = Product::getCover($row['id_product_attribute']);
$id_image = dfTools::getVariationImg(
$row['id_product'],
$row['id_product_attribute']
);
if (isset($id_image)) {
$image_link = dfTools::cleanURL(
dfTools::getImageLink(
$row['id_product_attribute'],
$id_image,
$row['link_rewrite'],
$cfg_image_size
)
);
} else {
$image_link = dfTools::cleanURL(
dfTools::getImageLink(
$row['id_product_attribute'],
$row['id_image'],
$row['link_rewrite'],
$cfg_image_size
)
);
}
// For variations with no specific pictures
if (strpos($image_link, "/-") > -1) {
$image_link = dfTools::cleanURL(
dfTools::getImageLink(
$row['id_product'],
$row['id_image'],
$row['link_rewrite'],
$cfg_image_size
)
);
}
echo $image_link . TXT_SEPARATOR;
} else {
echo dfTools::cleanURL(
dfTools::getImageLink(
$row['id_product'],
$row['id_image'],
$row['link_rewrite'],
$cfg_image_size
)
) . TXT_SEPARATOR;
}
// PRODUCT CATEGORIES
echo dfTools::getCategoriesForProductIdAndLanguage(
$row['id_product'],
$lang->id,
$shop->id
) . TXT_SEPARATOR;
// AVAILABILITY
$available = (int)$row['available_for_order'] > 0;
if ((int)dfTools::cfg($shop->id, 'PS_STOCK_MANAGEMENT')) {
$stock = StockAvailable::getQuantityAvailableByProduct(
$row['id_product'],
(isset($row['id_product_attribute'])?$row['id_product_attribute']:null),
$shop->id
);
$allow_oosp = Product::isAvailableWhenOutOfStock($row['out_of_stock']);
echo ($available && ($stock > 0 || $allow_oosp) ? 'in stock' : 'out of stock') . TXT_SEPARATOR;
} else {
echo ($available ? 'in stock' : 'out of stock') . TXT_SEPARATOR;
}
// BRAND
echo dfTools::cleanString($row['manufacturer']) . TXT_SEPARATOR;
// MPN
echo dfTools::cleanString($row['mpn']) . TXT_SEPARATOR;
//EAN13
echo dfTools::cleanString($row['ean13']) . TXT_SEPARATOR;
//UPC
echo dfTools::cleanString($row['upc']) . TXT_SEPARATOR;
//REFERENCE
echo dfTools::cleanString($row['reference']) . TXT_SEPARATOR;
//SUPPLIER_REFERENCE
echo dfTools::cleanString($row['supplier_reference']) . TXT_SEPARATOR;
// EXTRA_TITLE_1
echo dfTools::cleanReferences($product_title) . TXT_SEPARATOR;
// EXTRA_TITLE_2
echo dfTools::splitReferences($product_title) . TXT_SEPARATOR;
// TAGS
echo dfTools::cleanString($row['tags']);
//ISBN
if (dfTools::versionGte('1.7.0.0')) {
echo TXT_SEPARATOR;
echo dfTools::cleanString($row['isbn']);
}
//STOCK_QUANTITY
if ($cfg_display_stock_qty) {
echo TXT_SEPARATOR;
echo dfTools::cleanString($row['stock_quantity']);
}
// PRODUCT PRICE & ON SALE PRICE
if ($cfg_display_prices && $cfg_product_variations !== 1) {
echo TXT_SEPARATOR;
$product_price = Product::getPriceStatic(
$row['id_product'],
$cfg_prices_w_taxes,
null,
6,
null,
false,
false
);
$onsale_price = Product::getPriceStatic(
$row['id_product'],
$cfg_prices_w_taxes,
null,
6
);
if (!$row['show_price']) {
$product_price = false;
$onsale_price = false;
}
echo ($product_price ? Tools::convertPrice(
$product_price,
$currency
) : "") . TXT_SEPARATOR;
echo (($product_price && $onsale_price && $product_price != $onsale_price)
? Tools::convertPrice($onsale_price, $currency) : "");
} elseif ($cfg_display_prices && $cfg_product_variations == 1) {
echo TXT_SEPARATOR;
$product_price = Product::getPriceStatic(
$row['id_product'],
$cfg_prices_w_taxes,
$row['id_product_attribute'],
6,
null,
false,
false
);
$onsale_price = Product::getPriceStatic(
$row['id_product'],
$cfg_prices_w_taxes,
$row['id_product_attribute'],
6
);
if (!$row['show_price']) {
$product_price = false;
$onsale_price = false;
}
echo ($product_price ? Tools::convertPrice($product_price, $currency) : "") . TXT_SEPARATOR;
echo (($product_price && $onsale_price && $product_price != $onsale_price) ?
Tools::convertPrice($onsale_price, $currency) : "");
}
if ($cfg_product_variations == 1) {
echo TXT_SEPARATOR;
echo $row['variation_reference'];
$variation_attributes = dfTools::getAttributesForProductVariation(
$row['id_product_attribute'],
$lang->id,
$attribute_keys
);
foreach ($variation_attributes as $attribute) {
echo TXT_SEPARATOR . str_replace('/', '//', dfTools::cleanString($attribute));
}
}
if ($cfg_product_features) {
echo TXT_SEPARATOR;
foreach (dfTools::getFeaturesForProduct($row['id_product'], $lang->id, $feature_keys) as $key => $values) {
echo slugify($key) . "=";
foreach ($values as $index => $value) {
echo str_replace('/', '\/', dfTools::cleanString($value)) . "/";
}
}
}
/**
* @author camlafit <https://github.com/camlafit>
*/
foreach ($extra_header as $extra) {
echo TXT_SEPARATOR;
echo isset($row[$extra]) ? $row[$extra] : "";
}
echo PHP_EOL;
dfTools::flush();
}
}