-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDfTools.php
1542 lines (1362 loc) · 49.9 KB
/
DfTools.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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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
*/
namespace PrestaShop\Module\Doofinder\Src\Entity;
if (!defined('_PS_VERSION_')) {
exit;
}
class DfTools
{
const CATEGORY_SEPARATOR = ' %% ';
const CATEGORY_TREE_SEPARATOR = '>';
const TXT_SEPARATOR = '|';
// http://stackoverflow.com/questions/4224141/php-removing-invalid-utf-8-characters-in-xml-using-filter
const VALID_UTF8 = '/([\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF]
[\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F]
[\x80-\xBF]{2})|./x';
protected static $rootCategoryIds;
protected static $cachedCategoryPaths = [];
/**
* Hash password.
*
* @param string $passwd String to hash
*
* @return string Hashed password
*/
public static function encrypt($passwd)
{
return md5(_COOKIE_KEY_ . $passwd);
}
//
// Validation
//
public static function isBasicValue($v)
{
return $v && \Validate::isGenericName($v);
}
//
// SQL Tools
//
public static function prepareSQL($sql, $args = [])
{
$keys = ['_DB_PREFIX_'];
$values = [_DB_PREFIX_];
foreach ($args as $k => $v) {
$keys[] = $k;
$values[] = $v;
}
return str_replace($keys, $values, $sql);
}
public static function limitSQL($sql, $limit = false, $offset = false)
{
if (false !== $limit && is_numeric($limit)) {
$sql .= ' LIMIT ' . (int) $limit;
if (false !== $offset && is_numeric($offset)) {
$sql .= ' OFFSET ' . (int) $offset;
}
}
return $sql;
}
//
// SQL Queries
/**
* Returns an array of image size names to be used in a <select> box. Array (assoc) with the value of each key as value for it
*
* @return array
*/
public static function getAvailableImageSizes()
{
$sizes = [];
$tableName = 'home';
$tableName = (method_exists(get_class(new \ImageType()), 'getFormattedName')) ? \ImageType::getFormattedName($tableName) : $tableName . '_default';
$sql = "
SELECT
`name` AS DF_GS_IMAGE_SIZE,
`name`
FROM
`_DB_PREFIX_image_type`
WHERE
`products` = 1
ORDER BY
CASE
WHEN name = '" . $tableName . "' THEN '1'
END DESC;
";
$imageSizes = \Db::getInstance()->ExecuteS(self::prepareSQL($sql));
foreach ($imageSizes as $size) {
$sizes[$size['DF_GS_IMAGE_SIZE']] = $size;
}
return $sizes;
}
/**
* Returns an assoc. array. Keys are currency ISO codes. Values are currency
* names.
*
* @return array
*/
public static function getAvailableCurrencies()
{
$currencies = [];
$sql = '
SELECT
`iso_code`,
`name`
FROM
`_DB_PREFIX_currency`
WHERE
`active` = 1
ORDER BY `name`;
';
foreach (\Db::getInstance()->ExecuteS(self::prepareSQL($sql)) as $currency) {
$currencies[$currency['iso_code']] = $currency;
}
return $currencies;
}
/**
* 1.[5].0.13 | 1.5.[0].5 | 1.5.0.[1]
* 1.[6].0.6 | 1.5.[1].0 | 1.5.0.[5]
*
* @param [type] $minVersion [description]
*
* @return [type] [description]
*/
public static function versionGte($minVersion)
{
$version = explode('.', _PS_VERSION_);
$minVersion = explode('.', $minVersion);
foreach ($version as $index => $value) {
if ((int) $value > (int) $minVersion[$index]) {
return true;
} elseif ((int) $value < (int) $minVersion[$index]) {
return false;
}
}
return true;
}
public static function getSelectedFeatures($features, $selectedKeys)
{
/**
* Returns the features selected by user
*
* @param array features
*
* @return array of rows (assoc arrays)
*/
$selectedFeatures = [];
foreach ($features as $key => $value) {
if (in_array((string) $key, $selectedKeys, true)) {
$selectedFeatures[] = $value;
}
}
return $selectedFeatures;
}
/**
* Returns the features of a product
*
* @param int $idShop shop ID
* @param int $idLang language ID
*
* @return array of rows (assoc arrays)
*/
public static function getFeatureKeysForShopAndLang($idShop, $idLang)
{
$sql = '
SELECT fl.name, fl.id_feature
FROM
_DB_PREFIX_feature_shop fs
LEFT JOIN _DB_PREFIX_feature_lang fl
ON (fl.id_feature = fs.id_feature AND fl.id_lang = _ID_LANG_)
WHERE
fs.id_shop = _ID_SHOP_
';
$sql = self::prepareSQL($sql, [
'_ID_LANG_' => (int) pSQL($idLang),
'_ID_SHOP_' => (int) pSQL($idShop),
]);
$result = \Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
$names = [];
foreach ($result as $elem) {
$names[$elem['id_feature']] = $elem['name'];
}
return $names;
}
/**
* Returns the features of a product
*
* @param int $idShop shop ID
* @param int $idLang language ID
*
* @return array of rows (assoc arrays)
*/
public static function getAttributeKeysForShopAndLang($idShop, $idLang)
{
$sql = '
SELECT agl.name
FROM
_DB_PREFIX_attribute_group_shop ags
LEFT JOIN _DB_PREFIX_attribute_group_lang agl
ON (agl.id_attribute_group = ags.id_attribute_group AND agl.id_lang = _ID_LANG_)
WHERE
ags.id_shop = _ID_SHOP_
';
$sql = self::prepareSQL($sql, [
'_ID_LANG_' => (int) pSQL($idLang),
'_ID_SHOP_' => (int) pSQL($idShop),
]);
$result = \Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
$names = [];
foreach ($result as $elem) {
$names[] = $elem['name'];
}
return $names;
}
/**
* Returns the features of a product
*
* @param int $idProduct ID of the product
* @param int $idProductAttribute id of the Product attribute
*
* @return array of rows (assoc arrays)
*/
public static function getVariationImg($idProduct, $idProductAttribute)
{
$sql = '
SELECT i.id_image
from
(
select pa.id_product, pa.id_product_attribute,paic.id_attribute,min(i.position) as min_position
from _DB_PREFIX_product_attribute pa
inner join _DB_PREFIX_product_attribute_image pai
on pai.id_product_attribute = pa.id_product_attribute
inner join _DB_PREFIX_product_attribute_combination paic
on pai.id_product_attribute = paic.id_product_attribute
inner join _DB_PREFIX_image i
on pai.id_image = i.id_image
where pa.id_product = ' . (int) pSQL($idProduct) . '
and pa.id_product_attribute = ' . (int) pSQL($idProductAttribute) . '
group by pa.id_product, pa.id_product_attribute,paic.id_attribute
) as P
inner join _DB_PREFIX_image i
on i.id_product = P.id_product and i.position = P.min_position
';
$sql = self::prepareSQL($sql, []);
$result = \Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
if (isset($result[0])) {
return $result[0]['id_image'];
} else {
return '';
}
}
/**
* Returns the features of a product
*
* @param int $idProduct product ID
* @param int language ID
* @param array $featureKeys keys of the features associated to the product
*
* @return array of rows (assoc arrays)
*/
public static function getFeaturesForProduct($idProduct, $idLang, $featureKeys)
{
$sql = '
SELECT fl.name,
fvl.value
FROM
_DB_PREFIX_feature_product fp
LEFT JOIN _DB_PREFIX_feature_lang fl
ON (fl.id_feature = fp.id_feature AND fl.id_lang = _ID_LANG_)
LEFT JOIN _DB_PREFIX_feature_value_lang fvl
ON (fvl.id_feature_value = fp.id_feature_value AND fvl.id_lang = _ID_LANG_)
WHERE
fp.id_product = _ID_PRODUCT
';
$sql = self::prepareSQL($sql, [
'_ID_LANG_' => (int) pSQL($idLang),
'_ID_PRODUCT' => (int) pSQL($idProduct),
]);
$result = \Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
$features = [];
foreach ($result as $elem) {
if (in_array($elem['name'], $featureKeys, true)) {
$features[$elem['name']][] = $elem['value'];
}
}
return $features;
}
/**
* Returns the product variation attributes
*
* @param int $variationId product variation Attribute ID
* @param int $idLang language ID
* @param array $attributeKeys keys of the attributes associated to the product
*
* @return array of rows (assoc arrays)
*/
public static function getAttributesForProductVariation($variationId, $idLang, $attributeKeys)
{
if (is_numeric($variationId) && $variationId > 0) {
$sql = '
SELECT pc.id_product_attribute,
pal.name,
pagl.name AS group_name
FROM
_DB_PREFIX_product_attribute_combination pc
LEFT JOIN _DB_PREFIX_attribute pa
ON pc.id_attribute = pa.id_attribute
LEFT JOIN _DB_PREFIX_attribute_lang pal
ON (pc.id_attribute = pal.id_attribute AND pal.id_lang = _ID_LANG_)
LEFT JOIN _DB_PREFIX_attribute_group_lang pagl
ON (pagl.id_attribute_group = pa.id_attribute_group AND pagl.id_lang = _ID_LANG_)
WHERE
pc.id_product_attribute = _VARIATION_ID
';
$sql = self::prepareSQL($sql, [
'_ID_LANG_' => (int) pSQL($idLang),
'_VARIATION_ID' => (int) pSQL($variationId),
]);
$result = \Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
} else {
$result = [];
}
if (count($attributeKeys) > 0) {
$attributes = array_fill(0, count($attributeKeys), '');
} else {
$attributes = [];
}
foreach ($result as $elem) {
if (array_search($elem['group_name'], $attributeKeys) !== false) {
$attributes[array_search($elem['group_name'], $attributeKeys)] = $elem['name'];
}
}
return $attributes;
}
/**
* Returns the product combination attributes
*
* @param int product Attribute ID
* @param bool attribute groups IDs
* @param int language ID
*
* @return array of rows (assoc arrays)
*/
public static function getAttributesByCombination($variationId, $idLang, $attrLimit = false)
{
if (isset($variationId) && $variationId > 0) {
$sql = 'SELECT pc.id_product_attribute,
pal.name,
pagl.name AS group_name
FROM
_DB_PREFIX_product_attribute_combination pc
LEFT JOIN _DB_PREFIX_attribute pa
ON pc.id_attribute = pa.id_attribute
LEFT JOIN _DB_PREFIX_attribute_lang pal
ON (pc.id_attribute = pal.id_attribute AND pal.id_lang = _ID_LANG_)
LEFT JOIN _DB_PREFIX_attribute_group_lang pagl
ON (pagl.id_attribute_group = pa.id_attribute_group AND pagl.id_lang = _ID_LANG_)
WHERE
pc.id_product_attribute = _VARIATION_ID';
if ($attrLimit) {
$sql .= ' AND pa.id_attribute_group IN (' . pSQL($attrLimit) . ')';
}
$sql = self::prepareSQL($sql, [
'_ID_LANG_' => (int) pSQL($idLang),
'_VARIATION_ID' => (int) pSQL($variationId),
]);
return \Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
} else {
return [];
}
}
/**
* Get attributes name
*
* @param array attributes ids
* @param int Language ID
*
* @return array
*/
public static function getAttributesName($attributesIds, $idLang)
{
$sql = '
SELECT pag.id_attribute_group, pagl.name
FROM _DB_PREFIX_attribute_group pag
LEFT JOIN _DB_PREFIX_attribute_group_lang pagl ON pag.id_attribute_group = pagl.id_attribute_group AND pagl.id_lang = _ID_LANG_
WHERE pag.id_attribute_group IN (' . implode(',', $attributesIds) . ')
';
$sql = self::prepareSQL($sql, [
'_ID_LANG_' => (int) pSQL($idLang),
]);
return \Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
}
/**
* Returns the products available for a language
*
* @param int language ID
* @param int Optional. Default false. Number of products to get.
* @param int Optional. Default false. Offset to start the select from.
* @param string Optional. Fields to select.
* @param array Optional. Filter product ids.
*
* @return array of rows (assoc arrays)
*/
public static function getAvailableProductsForLanguage($idLang, $idShop, $limit = false, $offset = false, $ids = null)
{
$Shop = new \Shop($idShop);
$isbn = '';
$isbnPa = '';
if (self::versionGte('1.7.0.0')) {
$isbn = 'p.isbn,';
if (self::cfg($idShop, 'DF_SHOW_PRODUCT_VARIATIONS') == 1) {
$isbnPa = 'IF(isnull(pa.id_product), p.isbn , pa.isbn) AS isbn,';
}
}
$mpn = 'p.reference as mpn,';
$mpnPa = 'pa.reference AS variation_mpn,';
if (self::versionGte('1.7.7.0')) {
$mpn = 'p.mpn AS mpn,';
if (self::cfg($idShop, 'DF_SHOW_PRODUCT_VARIATIONS') == 1) {
$mpnPa = 'pa.mpn AS variation_mpn,';
}
}
$sql = '
SELECT
ps.id_product,
ps.show_price,
cl.name as main_category,
__ID_CATEGORY_DEFAULT_FIELD__,
m.name AS manufacturer,
' . $mpn . '
p.ean13 AS ean13,
' . $isbn . "
p.upc,
p.reference,
psp.product_supplier_reference AS supplier_reference,
pl.name,
pl.description,
pl.description_short,
pl.meta_title,
pl.meta_description,
GROUP_CONCAT(tag.name SEPARATOR ',') AS tags,
pl.link_rewrite,
cl.link_rewrite AS cat_link_rew,
im.id_image,
ps.available_for_order,
sa.out_of_stock,
sa.quantity as stock_quantity
FROM
_DB_PREFIX_product p
INNER JOIN _DB_PREFIX_product_shop ps
ON (p.id_product = ps.id_product AND ps.id_shop = _ID_SHOP_)
LEFT JOIN _DB_PREFIX_product_lang pl
ON (p.id_product = pl.id_product AND pl.id_shop = _ID_SHOP_ AND pl.id_lang = _ID_LANG_)
LEFT JOIN _DB_PREFIX_manufacturer m
ON (p.id_manufacturer = m.id_manufacturer)
LEFT JOIN _DB_PREFIX_category_lang cl
ON (__ID_CATEGORY_DEFAULT_FIELD__ = cl.id_category AND cl.id_shop = _ID_SHOP_ AND cl.id_lang = _ID_LANG_)
LEFT JOIN (_DB_PREFIX_image im INNER JOIN _DB_PREFIX_image_shop ims ON im.id_image = ims.id_image)
ON (p.id_product = im.id_product AND ims.id_shop = _ID_SHOP_ AND _IMS_COVER_)
LEFT JOIN (_DB_PREFIX_tag tag
INNER JOIN _DB_PREFIX_product_tag pt ON tag.id_tag = pt.id_tag AND tag.id_lang = _ID_LANG_)
ON (pt.id_product = p.id_product)
LEFT JOIN _DB_PREFIX_stock_available sa
ON (p.id_product = sa.id_product AND sa.id_product_attribute = 0
AND (sa.id_shop = _ID_SHOP_ OR
(sa.id_shop = 0 AND sa.id_shop_group = _ID_SHOPGROUP_)))
LEFT JOIN _DB_PREFIX_product_supplier psp
ON (p.id_product = psp.id_product AND psp.id_product_attribute = 0)
WHERE
__IS_ACTIVE__
__VISIBILITY__
__PRODUCT_IDS__
GROUP BY
p.id_product
ORDER BY
p.id_product
";
$sqlVariations = "
SELECT
ps.id_product,
ps.show_price,
pa.id_product_attribute,
pa.reference AS variation_reference,
psp.product_supplier_reference AS variation_supplier_reference,
$mpnPa
pa.ean13 AS variation_ean13,
pa.upc AS variation_upc,
pa_im.id_image AS variation_image_id,
cl.name as main_category,
__ID_CATEGORY_DEFAULT_FIELD__,
m.name AS manufacturer,
$mpn
p.ean13 AS ean13,
$isbnPa
p.upc AS upc,
p.reference AS reference,
p.supplier_reference AS supplier_reference,
0 AS df_group_leader,
pl.name,
pl.description,
pl.description_short,
pl.meta_title,
pl.meta_description,
GROUP_CONCAT(tag.name SEPARATOR ',') AS tags,
pl.link_rewrite,
cl.link_rewrite AS cat_link_rew,
im.id_image,
ps.available_for_order,
sa.out_of_stock,
sa.quantity as stock_quantity
FROM
_DB_PREFIX_product p
INNER JOIN _DB_PREFIX_product_shop ps
ON (p.id_product = ps.id_product AND ps.id_shop = _ID_SHOP_)
LEFT JOIN _DB_PREFIX_product_lang pl
ON (p.id_product = pl.id_product AND pl.id_shop = _ID_SHOP_ AND pl.id_lang = _ID_LANG_)
LEFT JOIN _DB_PREFIX_manufacturer m
ON (p.id_manufacturer = m.id_manufacturer)
LEFT JOIN _DB_PREFIX_category_lang cl
ON (__ID_CATEGORY_DEFAULT_FIELD__ = cl.id_category AND cl.id_shop = _ID_SHOP_ AND cl.id_lang = _ID_LANG_)
LEFT JOIN (_DB_PREFIX_image im INNER JOIN _DB_PREFIX_image_shop ims ON im.id_image = ims.id_image)
ON (p.id_product = im.id_product AND ims.id_shop = _ID_SHOP_ AND _IMS_COVER_)
LEFT OUTER JOIN _DB_PREFIX_product_attribute pa
ON (p.id_product = pa.id_product)
LEFT JOIN _DB_PREFIX_product_supplier psp
ON (p.id_product = psp.id_product AND pa.id_product_attribute = psp.id_product_attribute)
LEFT JOIN _DB_PREFIX_product_attribute_shop pas
ON (pas.id_product_attribute = pa.id_product_attribute AND pas.id_shop = _ID_SHOP_)
LEFT JOIN _DB_PREFIX_product_attribute_image pa_im
ON (pa_im.id_product_attribute = pa.id_product_attribute)
LEFT JOIN (_DB_PREFIX_tag tag
INNER JOIN _DB_PREFIX_product_tag pt ON tag.id_tag = pt.id_tag AND tag.id_lang = _ID_LANG_)
ON (pt.id_product = p.id_product)
LEFT JOIN _DB_PREFIX_stock_available sa
ON (p.id_product = sa.id_product
AND sa.id_product_attribute = IF(isnull(pa.id_product), 0, pa.id_product_attribute)
AND (sa.id_shop = _ID_SHOP_ OR
(sa.id_shop = 0 AND sa.id_shop_group = _ID_SHOPGROUP_)))
WHERE
__IS_ACTIVE__
__VISIBILITY__
__PRODUCT_IDS__
AND pa.id_product_attribute is not null
GROUP BY pa.id_product_attribute, p.id_product
UNION
SELECT
ps.id_product,
ps.show_price,
0,
null AS variation_reference,
psp.product_supplier_reference AS variation_supplier_reference,
null AS variation_mpn,
null AS variation_ean13,
null AS variation_upc,
null AS variation_image_id,
cl.name as main_category,
__ID_CATEGORY_DEFAULT_FIELD__ as id_category_default,
m.name AS manufacturer,
$mpn
p.ean13 AS ean13,
$isbn
p.upc,
p.reference,
p.supplier_reference,
1 AS df_group_leader,
pl.name,
pl.description,
pl.description_short,
pl.meta_title,
pl.meta_description,
GROUP_CONCAT(tag.name SEPARATOR ',') AS tags,
pl.link_rewrite,
cl.link_rewrite AS cat_link_rew,
im.id_image,
ps.available_for_order,
sa.out_of_stock,
sa.quantity as stock_quantity
FROM
_DB_PREFIX_product p
INNER JOIN _DB_PREFIX_product_shop ps
ON (p.id_product = ps.id_product AND ps.id_shop = _ID_SHOP_)
LEFT JOIN _DB_PREFIX_product_lang pl
ON (p.id_product = pl.id_product AND pl.id_shop = _ID_SHOP_ AND pl.id_lang = _ID_LANG_)
LEFT JOIN _DB_PREFIX_manufacturer m
ON (p.id_manufacturer = m.id_manufacturer)
LEFT JOIN _DB_PREFIX_category_lang cl
ON (__ID_CATEGORY_DEFAULT_FIELD__ = cl.id_category AND cl.id_shop = _ID_SHOP_ AND cl.id_lang = _ID_LANG_)
LEFT JOIN (_DB_PREFIX_image im INNER JOIN _DB_PREFIX_image_shop ims ON im.id_image = ims.id_image)
ON (p.id_product = im.id_product AND ims.id_shop = _ID_SHOP_ AND _IMS_COVER_)
LEFT JOIN (_DB_PREFIX_tag tag
INNER JOIN _DB_PREFIX_product_tag pt ON tag.id_tag = pt.id_tag AND tag.id_lang = _ID_LANG_)
ON (pt.id_product = p.id_product)
LEFT JOIN _DB_PREFIX_stock_available sa
ON (p.id_product = sa.id_product AND sa.id_product_attribute = 0
AND (sa.id_shop = _ID_SHOP_ OR
(sa.id_shop = 0 AND sa.id_shop_group = _ID_SHOPGROUP_)))
LEFT JOIN _DB_PREFIX_product_supplier psp
ON (p.id_product = psp.id_product AND psp.id_product_attribute = 0)
WHERE
__IS_ACTIVE__
__VISIBILITY__
__PRODUCT_IDS__
GROUP BY
p.id_product
ORDER BY
id_product
";
if (self::cfg($idShop, 'DF_SHOW_PRODUCT_VARIATIONS') == 1) {
$sql = $sqlVariations;
}
// MIN: 1.5.0.9
$idCategoryDefault = self::versionGte('1.5.0.9') ? 'ps.id_category_default' : 'p.id_category_default';
// MIN: 1.5.1.0
$imsCover = self::versionGte('1.5.1.0') ? 'ims.cover = 1' : 'im.cover = 1';
$isActive = self::versionGte('1.5.1.0') ? 'ps.active = 1' : 'p.active = 1';
if (self::versionGte('1.5.1.0')) {
$visibility = "AND ps.visibility IN ('search', 'both')";
} elseif (self::versionGte('1.5.0.9')) {
$visibility = "AND p.visibility IN ('search', 'both')";
} else {
$visibility = '';
}
if (is_array($ids) && count($ids)) {
$productIds = 'AND p.id_product IN (' . implode(',', $ids) . ')';
} else {
$productIds = '';
}
$sql = self::limitSQL($sql, $limit, $offset);
$sql = self::prepareSQL($sql, [
'_ID_LANG_' => (int) pSQL($idLang),
'_ID_SHOP_' => (int) pSQL($idShop),
'_ID_SHOPGROUP_' => (int) pSQL($Shop->id_shop_group),
'_IMS_COVER_' => (string) pSQL($imsCover),
'__ID_CATEGORY_DEFAULT_FIELD__' => (string) pSQL($idCategoryDefault),
'__IS_ACTIVE__' => (string) pSQL($isActive),
'__VISIBILITY__' => (string) pSQL($visibility),
'__PRODUCT_IDS__' => (string) pSQL($productIds),
]);
$sql = str_replace("\'", "'", $sql);
return \Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
}
/**
* Returns an array of "root" categories in Prestashop for a language.
* The results are cached in a protected, static variable.
*
* @return array
*/
public static function getRootCategoryIds($idLang)
{
if (null === self::$rootCategoryIds) {
self::$rootCategoryIds = [];
foreach (\Category::getRootCategories($idLang) as $category) {
self::$rootCategoryIds[] = $category['id_category'];
}
}
return self::$rootCategoryIds;
}
/**
* Returns the path to the first, no root ancestor category for the selected
* category ID in a language for the selected shop.
* Results are cached by category ID.
*
* @param int $idCategory Category ID
* @param int $idLang Language ID
* @param int $idShop Shop ID
* @param bool $full return full category path
*
* @return string
*/
public static function getCategoryPath($idCategory, $idLang, $idShop, $full = true)
{
if (isset(self::$cachedCategoryPaths[$idCategory])) {
return self::$cachedCategoryPaths[$idCategory];
}
$excludedIds = self::getRootCategoryIds($idLang);
$sql = '
SELECT
cl.name
FROM
_DB_PREFIX_category_lang cl INNER JOIN _DB_PREFIX_category parent
ON (parent.id_category = cl.id_category)
INNER JOIN _DB_PREFIX_category_shop cs ON (cs.id_category = parent.id_category),
_DB_PREFIX_category node
WHERE
node.nleft BETWEEN parent.nleft AND parent.nright
AND node.id_category = _ID_CATEGORY_
AND cl.id_shop = _ID_SHOP_
AND cl.id_lang = _ID_LANG_
AND cs.id_shop = _ID_SHOP_
AND parent.level_depth <> 0
AND parent.active = 1 ';
if (count($excludedIds) > 0 && '' !== (string) $excludedIds[0]) {
$sql .= 'AND parent.id_category NOT IN (_EXCLUDED_IDS_) ';
}
$sql .= 'ORDER BY
parent.nleft
;';
$sql = self::prepareSQL($sql, [
'_ID_CATEGORY_' => (int) pSQL($idCategory),
'_ID_SHOP_' => (int) pSQL($idShop),
'_ID_LANG_' => (int) pSQL($idLang),
'_EXCLUDED_IDS_' => (string) pSQL(implode(',', $excludedIds)),
]);
$sql = str_replace("\'", "'", $sql);
$path = [];
foreach (\Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql) as $row) {
$path[] = str_replace([self::CATEGORY_TREE_SEPARATOR, self::CATEGORY_SEPARATOR], '-', $row['name']);
}
if ($full) {
$path = implode(self::CATEGORY_TREE_SEPARATOR, $path);
} else {
$path = end($path);
}
$path = self::cleanString($path);
self::$cachedCategoryPaths[$idCategory] = $path;
return $path;
}
/**
* Returns a string with all the paths for categories for a product in a language
* for the selected shop. If $flat == false then returns them as an array.
*
* @param int $idProduct Product ID
* @param int $idLang Language ID
* @param int $idShop Shop ID
* @param bool $flat optional implode values
*
* @return string or array
*/
public static function getCategoriesForProductIdAndLanguage($idProduct, $idLang, $idShop, $flat = true)
{
$useMainCategory = (bool) self::cfg($idShop, 'DF_FEED_MAINCATEGORY_PATH', DoofinderConstants::YES);
$sql = '
SELECT DISTINCT
c.id_category,
c.id_parent,
c.level_depth,
c.nleft,
c.nright
FROM
_DB_PREFIX_category c
INNER JOIN _DB_PREFIX_category_product cp
ON (c.id_category = cp.id_category AND cp.id_product = _ID_PRODUCT_)
INNER JOIN _DB_PREFIX_category_shop cs
ON (c.id_category = cs.id_category AND cs.id_shop = _ID_SHOP_)
_MAIN_CATEGORY_INNER_
WHERE
c.active = 1
_MAIN_CATEGORY_WHERE_
ORDER BY
c.nleft DESC,
c.nright ASC;
';
$mainCategoryInner = '';
$mainCategoryWhere = '';
if ($useMainCategory) {
$mainInnerSql = 'INNER JOIN _DB_PREFIX_product_shop ps '
. 'ON (ps.id_product = _ID_PRODUCT_ AND ps.id_shop = _ID_SHOP_)';
$mainCategoryInner = self::prepareSQL(
$mainInnerSql,
['_ID_PRODUCT_' => (int) pSQL($idProduct), '_ID_SHOP_' => (int) pSQL($idShop)]
);
$mainCategoryWhere = 'AND ps.id_category_default = cp.id_category';
}
$sql = self::prepareSQL($sql, [
'_ID_PRODUCT_' => (int) pSQL($idProduct),
'_MAIN_CATEGORY_INNER_' => (string) pSQL($mainCategoryInner),
'_MAIN_CATEGORY_WHERE_' => (string) pSQL($mainCategoryWhere),
'_ID_SHOP_' => (int) pSQL($idShop),
]);
$sql = str_replace("\'", "'", $sql);
$categories = [];
$lastSaved = 0;
$idCategory0 = 0;
$nleft0 = 0;
$nright0 = 0;
$useFullPath = (bool) self::cfg($idShop, 'DF_FEED_FULL_PATH', DoofinderConstants::YES);
foreach (\Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql) as $i => $row) {
if (!$i) {
$idCategory0 = (int) $row['id_category'];
$nleft0 = (int) $row['nleft'];
$nright0 = (int) $row['nright'];
} else {
$idCategory1 = (int) $row['id_category'];
$nleft1 = (int) $row['nleft'];
$nright1 = (int) $row['nright'];
if ($nleft1 < $nleft0 && $nright1 > $nright0) {
// $idCategory1 is an ancestor of $idCategory0
} elseif ($nleft1 < $nleft0 && $nright1 > $nright0) {
// $idCategory1 is a child of $idCategory0 so be replace $idCategory0
$idCategory0 = $idCategory1;
$nleft0 = $nleft1;
$nright0 = $nright1;
} else {
// $idCategory1 is not a relative of $idCategory0 so we save
// $idCategory0 now and make $idCategory1 the current category.
$categories[] = self::getCategoryPath($idCategory0, $idLang, $idShop, $useFullPath);
$lastSaved = $idCategory0;
$idCategory0 = $idCategory1;
$nleft0 = $nleft1;
$nright0 = $nright1;
}
}
} // endforeach
if ($lastSaved != $idCategory0) {
// The last item in loop didn't trigger the $idCategory0 saving event.
$categories[] = self::getCategoryPath($idCategory0, $idLang, $idShop, $useFullPath);
}
return $flat ? implode(self::CATEGORY_SEPARATOR, $categories) : $categories;
}
/**
* Check if product has variants
*
* @param int $idProduct product ID
*
* @return int
*/
public static function hasAttributes($idProduct)
{
return \Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(
'
SELECT COUNT(*)
FROM `' . _DB_PREFIX_ . 'product_attribute` pa
' . \Shop::addSqlAssociation('product_attribute', 'pa') . '
WHERE pa.`id_product` = ' . (int) $idProduct
);
}
/**
* Check if product has attributes
*
* @param int $idProduct product ID
* @param string $attributeGroupsId attribute groups IDs
*
* @return array
*/
public static function hasProductAttributes($idProduct, $attributeGroupsId)
{
if (!$attributeGroupsId) {
return false;
}
$result = \Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(
'
SELECT a.id_attribute_group
FROM `' . _DB_PREFIX_ . 'product` p
LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa ON p.id_product = pa.id_product
LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute_combination` pac ON pa.id_product_attribute = pac.id_product_attribute
LEFT JOIN `' . _DB_PREFIX_ . 'attribute` a ON pac.id_attribute = a.id_attribute
WHERE p.id_product = ' . pSQL($idProduct) . ' AND id_attribute_group IN ( ' . pSQL($attributeGroupsId) . ')
GROUP BY a.id_attribute_group
'
);
return array_column($result, 'id_attribute_group');
}
public static function getCategories($idLang, $active = true)
{
$result = \Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(
'
SELECT c.id_category
FROM `' . _DB_PREFIX_ . 'category` c
' . \Shop::addSqlAssociation('category', 'c') . '
LEFT JOIN `' . _DB_PREFIX_ . 'category_lang` cl ON c.`id_category` = cl.`id_category`' . \Shop::addSqlRestrictionOnLang('cl') . '
WHERE id_parent != 0
' . ($idLang ? 'AND `id_lang` = ' . (int) $idLang : '') . '
' . ($active ? 'AND `active` = 1' : '') . '
' . (!$idLang ? 'GROUP BY c.id_category' : '')
);
return array_column($result, 'id_category');
}
public static function getCmsPages($idLang, $idShop, $active = true)
{
$result = \CMS::getCMSPages($idLang, null, $active, $idShop);
return array_column($result, 'id_cms');
}
//
// Text Tools
//
public static function truncateText($text, $length)
{
$l = (int) $length;
$c = trim(preg_replace('/\s+/', ' ', $text));
if (strlen($c) <= $l) {
return $c;
}