-
Notifications
You must be signed in to change notification settings - Fork 0
/
findify.php
1262 lines (1123 loc) · 56.2 KB
/
findify.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
/**
* AM FINDIFY
*
* @author LATOUTFRANCAIS | Arnaud Merigeau <[email protected]> - https://www.arnaud-merigeau.fr
* @copyright Arnaud Merigeau 2020 - https://www.arnaud-merigeau.fr
* @license Commercial
*
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class Findify extends Module
{
public function __construct()
{
$this->name = 'findify';
$this->tab = 'front_office_features';
$this->version = '1.0.6';
$this->author = 'LATOUTFRANCAIS';
$this->need_instance = 0;
$this->context = Context::getContext();
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Findify | Search & Personalization');
$this->description = $this->l('Accelerate your sales by putting the right products in front of the right customers at the right time.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall this module ?');
}
public function install()
{
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}
return parent::install()
&& $this->registerHook('header')
&& $this->registerHook('displayFooterProduct')
&& $this->registerHook('displayShoppingCart')
&& Configuration::updateValue('amf_fname', null)
&& Configuration::updateValue('amf_lname', null)
&& Configuration::updateValue('amf_email', Configuration::get('PS_SHOP_EMAIL'))
&& Configuration::updateValue('amf_sendemail', '1')
&& Configuration::updateValue('amf_ag', null)
&& Configuration::updateValue('amf_fg', null);
}
public function uninstall()
{
return parent::uninstall()
&& Configuration::deleteByName('amf_fname')
&& Configuration::deleteByName('amf_lname')
&& Configuration::deleteByName('amf_email')
&& Configuration::deleteByName('amf_sendemail')
&& Configuration::deleteByName('amf_ag')
&& Configuration::deleteByName('amf_fg');
}
public function uninstallFiles()
{
define('MOD_DIR', dirname(__FILE__));
define('THEME_DIR', _THEME_DIR_);
$theme_path = realpath(MOD_DIR . '/../../../' . THEME_DIR);
// uninstall cart-detailed.tpl
$file1 = 'cart-detailed.tpl';
unlink($theme_path . '/templates/checkout/_partials/' . $file1);
rename(MOD_DIR . '/views/templates/backup/' . $file1, $theme_path . '/templates/checkout/_partials/' . $file1);
// uninstall order-confirmation-table.tpl
$file2 = 'order-confirmation-table.tpl';
unlink($theme_path . '/templates/checkout/_partials/' . $file2);
rename(MOD_DIR . '/views/templates/backup/' . $file2, $theme_path . '/templates/checkout/_partials/' . $file2);
}
public static function convertIsoArrayToIdArray($array)
{
if (!is_array($array) || empty($array)) {
return array();
}
$converted = array();
foreach (Language::getLanguages(false) as $language) {
$key = key_exists($language['iso_code'], $array) ? $language['iso_code'] : 'en';
$converted[$language['id_lang']] = $array[$key];
}
return $converted;
}
public function getContent()
{
$this->context->controller->addCSS(($this->_path).'views/css/select2.min.css', 'all');
$this->context->controller->addCSS(($this->_path).'views/css/admin.findify.css', 'all');
$this->context->controller->addJS($this->_path . 'views/js/select2.full.min.js');
$this->context->controller->addJS($this->_path . 'views/js/admin.findify.js');
define('MOD_DIR', dirname(__FILE__));
define('THEME_DIR', _THEME_DIR_);
define('THEME_PATH', realpath(MOD_DIR . '/../../../' . THEME_DIR));
define('BKP_DIR', '/views/templates/backup/');
define('INSTALL_DIR', '/views/templates/install/');
define('FILE1', 'cart-detailed.tpl');
define('FILE2', 'order-confirmation-table.tpl');
define('FILE3', 'product-list.tpl');
define('FILE4', 'category-product-list.tpl');
define('FILE5', 'category.tpl');
define('FILE6', 'layout-both-columns.tpl');
define('FILE7', 'layout-left-column.tpl');
$output = null;
if (Tools::isSubmit('submit' . $this->name)) {
$amf_fname = Tools::getValue('amf_fname');
$amf_lname = Tools::getValue('amf_lname');
$amf_email = Tools::getValue('amf_email');
$amf_sendemail = Tools::getValue('amf_sendemail');
$amf_ag = Tools::getValue('amf_ag');
$amf_fg = Tools::getValue('amf_fg');
if (!Validate::isCustomerName($amf_fname)) {
$output .= $this->displayError($this->l('First name is invalid.'));
} elseif (!Validate::isCustomerName($amf_lname)) {
$output .= $this->displayError($this->l('Last name is invalid.'));
} elseif (!Validate::isEmail($amf_email)) {
$output .= $this->displayError($this->l('Email address is invalid.'));
} elseif ($amf_fname == '') {
$output .= $this->displayError($this->l('First name is required.'));
} elseif ($amf_lname == '') {
$output .= $this->displayError($this->l('Last name is required.'));
} elseif ($amf_email == '') {
$output .= $this->displayError($this->l('Email address is required.'));
} else {
Configuration::updateValue('amf_fname', $amf_fname);
Configuration::updateValue('amf_lname', $amf_lname);
Configuration::updateValue('amf_email', $amf_email);
Configuration::updateValue('amf_sendemail', $amf_sendemail);
if (count($amf_ag) > 1) {
$amf_ag = implode(',', $amf_ag);
} else {
$amf_ag = $amf_ag[0];
}
Configuration::updateValue('amf_ag', $amf_ag);
if (count($amf_fg) > 1) {
$amf_fg = implode(',', $amf_fg);
} else {
$amf_fg = $amf_fg[0];
}
Configuration::updateValue('amf_fg', $amf_fg);
$languages = Language::getLanguages(false);
foreach ($languages as $lang) {
$amf_script = Tools::getValue('amf_script_'.$lang['id_lang']);
Configuration::updateValue('amf_script_'.$lang['id_lang'], $amf_script);
}
$output .= $this->displayConfirmation($this->l('Update successful !'));
}
} else if (Tools::isSubmit('submit_type')) {
$type = Tools::getValue('type');
$file = Tools::getValue('file');
$this->installFiles($type, $file);
}
return $output . $this->displayBranding() . $this->displayForm() . $this->displayCron() /*. $this->stayInTouch()*/;
}
public function installFiles($type, $file)
{
if($type && $file){
if($type == 1 || $type == 2){
rename(THEME_PATH . '/templates/checkout/_partials/' . $file, MOD_DIR . BKP_DIR . $file);
copy(MOD_DIR . INSTALL_DIR . $file, THEME_PATH . '/templates/checkout/_partials/' . $file);
}else if($type == 3 || $type == 4 || $type == 5){
if($type == 3 || $type == 5)
rename(THEME_PATH . '/templates/catalog/listing/' . $file, MOD_DIR . BKP_DIR . $file);
copy(MOD_DIR . INSTALL_DIR . $file, THEME_PATH . '/templates/catalog/listing/' . $file);
}else if($type == 6 || $type == 7){
rename(THEME_PATH . '/templates/layouts/' . $file, MOD_DIR . BKP_DIR . $file);
copy(MOD_DIR . INSTALL_DIR . $file, THEME_PATH . '/templates/layouts/' . $file);
}
}
}
public function displayForm()
{
$id_lang = $this->context->language->id;
$output = null;
// Check options selected
$output .= '
<style>
select[name*=amf_ag]{min-width:280px!important;}
select[name*=amf_fg]{min-width:280px!important;}
</style>
<script>
var values="'.Configuration::get('amf_ag').'";
$.each(values.split(","), function(i,e){
$("select[name*=amf_ag] option[value=\'" + e + "\']").prop("selected", true);
});
var values="'.Configuration::get('amf_fg').'";
$.each(values.split(","), function(i,e){
$("select[name*=amf_fg] option[value=\'" + e + "\']").prop("selected", true);
});
</script>
';
// Get attribute groups
$ag_list = array();
if (Configuration::get('PS_COMBINATION_FEATURE_ACTIVE')) {
$ag = new AttributeGroup();
$ag_list = array();
foreach ($ag->getAttributesGroups($id_lang) as $group) {
$ag_list[] = array(
'id' => (int)$group['id_attribute_group'],
'name' => $group['name']
);
}
}
// Get feature groups
$fg_list = array();
if (Configuration::get('PS_FEATURE_FEATURE_ACTIVE')) {
$fg = new Feature();
foreach ($fg->getFeatures($id_lang) as $group) {
$fg_list[] = array(
'id' => (int)$group['id_feature'],
'name' => $group['name']
);
}
}
// Form
$fields_form = array();
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Product feed options')
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('First Name').'*',
'name' => 'amf_fname',
'desc' => $this->l('Required.')
),
array(
'type' => 'text',
'label' => $this->l('Last Name').'*',
'name' => 'amf_lname',
'desc' => $this->l('Required.')
),
array(
'type' => 'text',
'label' => $this->l('Email').'*',
'name' => 'amf_email',
'desc' => $this->l('Required.').' '.$this->l('Email address sent to Findify with your product feed.')
),
array(
'type' => 'switch',
'label' => $this->l('Send email'),
'name' => 'amf_sendemail',
'is_bool' => true,
'desc' => $this->l('Send product feed by mail to [email protected] when generated.'),
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
)
),
array(
'type' => 'select',
'label' => $this->l('Attribute groups to export'),
'name' => 'amf_ag[]',
'desc' => $this->l('Type or click in the field to choose attribute groups to export.'),
'multiple' => true,
'class' => 'fixed-width-xxl select2',
'options' => array(
'query' => $ag_list,
'id' => 'id',
'name' => 'name'
)
),
array(
'type' => 'select',
'label' => $this->l('Feature groups to export'),
'name' => 'amf_fg[]',
'desc' => $this->l('Type or click in the field to choose feature groups to export.'),
'multiple' => true,
'class' => 'fixed-width-xxl select2',
'options' => array(
'query' => $fg_list,
'id' => 'id',
'name' => 'name'
)
),
array(
'type' => 'text',
'lang' => true,
'label' => $this->l('Script url').'*',
'name' => 'amf_script',
'desc' => $this->l('Required.').' '.$this->l('Script url given by Findify.'),
),
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default'
)
);
$helper = new HelperForm();
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
$helper->title = $this->displayName;
$helper->show_toolbar = true;
$helper->toolbar_scroll = true;
$helper->submit_action = 'submit' . $this->name;
$helper->toolbar_btn = array(
'save' =>
array(
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex . '&configure=' . $this->name . '&save' . $this->name .
'&token=' . Tools::getAdminTokenLite('AdminModules'),
),
'back' => array(
'href' => AdminController::$currentIndex . '&token=' . Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
)
);
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
return $helper->generateForm($fields_form) . $output;
}
public function getConfigFieldsValues()
{
$languages = Language::getLanguages(false);
$fields = array();
$fields['amf_fname'] = Tools::getValue('amf_fname', Configuration::get('amf_fname'));
$fields['amf_lname'] = Tools::getValue('amf_lname', Configuration::get('amf_lname'));
$fields['amf_email'] = Tools::getValue('amf_email', Configuration::get('amf_email'));
$fields['amf_sendemail'] = Tools::getValue('amf_sendemail', Configuration::get('amf_sendemail'));
$fields['amf_ag[]'] = Tools::getValue('amf_ag[]', Configuration::get('amf_ag[]'));
$fields['amf_fg[]'] = Tools::getValue('amf_fg[]', Configuration::get('amf_fg[]'));
foreach ($languages as $lang) {
$fields['amf_script'][$lang['id_lang']] = Tools::getValue('amf_script_'.$lang['id_lang'], Configuration::get('amf_script_'.$lang['id_lang']));
}
return $fields;
}
public function displayCron()
{
// prestashop version
if (version_compare(_PS_VERSION_, '1.7.0.0', '>=')) {
$store_url = $this->context->link->getBaseLink();
}else{
$store_url = Tools::getHttpHost(true).__PS_BASE_URI__;
}
// base url
$useSSL = (Configuration::get('PS_SSL_ENABLED') || Tools::usingSecureMode()) ? true : false;
// display logs
$logsURLs = array();
define('DIR_LOG', $_SERVER['DOCUMENT_ROOT']._MODULE_DIR_.'findify/log/');
$logs = scandir(DIR_LOG, 1);
if($logs){
foreach ($logs as $k => $log) {
if(strpos($log, 'amf-log') !== false){
$logsURLs[$k]['url'] = $store_url . 'modules/findify/log/' . $log;
$logsURLs[$k]['filename'] = $log;
}
}
}
// display feed
$amf_feed_url = array();
define('URL_EXPORT', $store_url.'modules/findify/export/');
define('DIR_EXPORT', $_SERVER['DOCUMENT_ROOT']._MODULE_DIR_.'findify/export/');
$feeds = scandir(DIR_EXPORT, 1);
if($feeds){
foreach ($feeds as $k => $feed) {
if(strpos($feed, 'findify-product-feed') !== false){
$amf_feed_url[] = URL_EXPORT.$feed;
}
}
}
// test files backup
if (file_exists(MOD_DIR . BKP_DIR . FILE1)) {
$type1 = 1;
} else {
$type1 = null;
}
if (file_exists(MOD_DIR . BKP_DIR . FILE2)) {
$type2 = 1;
} else {
$type2 = null;
}
if (file_exists(MOD_DIR . BKP_DIR . FILE3)) {
$type3 = 1;
} else {
$type3 = null;
}
if (file_exists(THEME_PATH . '/templates/catalog/listing/' . FILE4)) {
$type4 = 1;
} else {
$type4 = null;
}
if (file_exists(MOD_DIR . BKP_DIR . FILE5)) {
$type5 = 1;
} else {
$type5 = null;
}
if (file_exists(MOD_DIR . BKP_DIR . FILE6)) {
$type6 = 1;
} else {
$type6 = null;
}
if (file_exists(MOD_DIR . BKP_DIR . FILE7)) {
$type7 = 1;
} else {
$type7 = null;
}
$this->context->smarty->assign(array(
'amf_feed_url' => $amf_feed_url,
'amf_cron' => $store_url . 'modules/findify/cron.php?token=' . Tools::substr(Tools::encrypt('findify/cron'), 0, 10) . '&id_shop=' . $this->context->shop->id,
'prestashop_ssl' => Configuration::get('PS_SSL_ENABLED'),
'shop' => $this->context->shop,
'logs' => $logsURLs,
'action' => AdminController::$currentIndex.'&configure='.$this->name.'&integrate&token='.Tools::getAdminTokenLite('AdminModules'),
'type1' => $type1,
'type2' => $type2,
'type3' => $type3,
'type4' => $type4,
'type5' => $type5,
'type6' => $type6,
'type7' => $type7
));
return $this->display(__FILE__, 'views/templates/admin/configuration.tpl');
}
/**
* Delete old log files
* @param dir/max_age
* @return list
*/
public function deleteOlderThan($dir, $max_age)
{
$list = array();
$limit = time() - $max_age;
$dir = realpath($dir);
if (!is_dir($dir)) {
return;
}
$dh = opendir($dir);
if ($dh === false) {
return;
}
while (($file = readdir($dh)) !== false) {
$file = $dir . '/' . $file;
if (!is_file($file)) {
continue;
}
if (filemtime($file) < $limit) {
$list[] = $file;
unlink($file);
}
}
closedir($dh);
return $list;
}
/**
* Debug function
* @param var/array
* @return -
*/
public function debug($var)
{
echo '<pre>';
print_r($var);
echo '</pre>';
}
/**
* Debug and die function
* @param var/array
* @return -
*/
public function debugdie($var)
{
echo '<pre>';
print_r($var);
echo '</pre>';
die;
}
/**
* Get attribute group name for a given language.
* @param int $idLang Language id
* @return array Attribute group name
*/
public static function getAttributeGroupName($id_attribute_group, $id_lang)
{
if (!Combination::isFeatureActive()) {
return array();
}
return Db::getInstance()->executeS('
SELECT name
FROM ' . _DB_PREFIX_ . 'attribute_group_lang
WHERE id_attribute_group = ' . $id_attribute_group . ' AND id_lang = ' . $id_lang . '
');
}
/**
* Get feature group name for a given language.
* @param int $idLang Language id
* @return array Feature group name
*/
public static function getFeatureGroupName($id_feature, $id_lang)
{
if (!Feature::isFeatureActive()) {
return array();
}
return Db::getInstance()->executeS('
SELECT name
FROM ' . _DB_PREFIX_ . 'feature_lang
WHERE id_feature = ' . $id_feature . ' AND id_lang = ' . $id_lang . '
');
}
/**
* Clean string.
* @param $str
* @return $str
*/
public function cleanStr($str)
{
// Strip HTML Tags
$str = strip_tags($str);
// Clean up things like &
$str = html_entity_decode($str);
// Strip out any url-encoded stuff
$str = urldecode($str);
// Replace non-AlNum characters with space
$str = preg_replace('/[^A-Za-z0-9]/', ' ', $str);
// Replace Multiple spaces with single space
$str = preg_replace('/ +/', ' ', $str);
// Trim the string of leading/trailing space
$str = trim($str);
return $str;
}
/**
* Remove \n.
* @param $str
* @return $str
*/
public function removeNl($str)
{
// Replace new line with space
$str = trim(preg_replace('/\s\s+/', ' ', $str));
return $str;
}
/**
* Export product feed
* @param -
* @return -
*/
public function exportFeed($language)
{
// Vars
define('DEV', 1);
if (Configuration::get('PS_SSL_ENABLED')){
$protocol = 'https://';
} else {
$protocol = 'http://';
}
$sep = ',';
$log = fopen(LOG_FILE, 'a+') or die($this->l('Permission error on log dir'));
// delete old export file
unlink(DIR_EXPORT.'findify-product-feed-'.$language['iso_code'].'.csv');
// create new export file
$export = DIR_EXPORT.'findify-product-feed-'.$language['iso_code'].'.csv';
$shop_name = Configuration::get('PS_SHOP_NAME');
if (Configuration::get('amf_email') != ''){
$from = Configuration::get('amf_email');
$to = Configuration::get('amf_email');
}else{
$from = Configuration::get('PS_SHOP_EMAIL');
$to = Configuration::get('PS_SHOP_EMAIL');
}
if (version_compare(_PS_VERSION_, '1.7.0.0', '>=')) {
$store_url = $this->context->link->getBaseLink();
}else{
$store_url = Tools::getHttpHost(true).__PS_BASE_URI__;
}
$context = Context::getContext();
$id_lang = $language['id_lang'];
$iso = $language['iso_code'];
try {
if (file_exists($export))
unlink($export);
echo '-----------------------------------------------------<br>'.date("Y-m-d H:i:s").' '.$this->l('Product feed export starts : ').$iso.'<br>';
fputs($log, '-----------------------------------------------------'.PHP_EOL.date("Y-m-d H:i:s").' Product feed export starts : '.$iso.PHP_EOL);
// Get data
$db = Db::getInstance();
$products = $db->connect()->query('
SELECT *
FROM '._DB_PREFIX_.'product p
LEFT JOIN '._DB_PREFIX_.'product_lang AS pl ON p.id_product = pl.id_product
WHERE p.active = 1 AND pl.id_lang = '.$id_lang.'
GROUP BY p.id_product
');
if ( $fp = fopen($export, 'w+') ) {
// Utf-8
// fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
// Tip ! fields doc here : https://developers.findify.io/docs/feed-generation-manual-csv
$header = array(
'id',
'item_group_id',
'title',
'description',
'price',
'image_url',
'product_url',
'category',
'thumbnail_url',
'availability',
'created_at',
'sku',
'brand',
'seller',
'quantity',
'on_sale',
'new',
'name_ar',
'sticker_new_edition'
);
$h = count($header);
// Add currencies labels to export prices for each currency
$currencies = new Currency();
foreach ($currencies->getCurrencies() as $currency) {
$h++;
$currency_label = 'price_'.Tools::strtolower($currency['iso_code']);
array_push($header, $currency_label);
}
// Add currencies labels to export sale prices for each currency
$currencies = new Currency();
foreach ($currencies->getCurrencies() as $currency) {
$h++;
$currency_label = 'sale_price_'.Tools::strtolower($currency['iso_code']);
array_push($header, $currency_label);
}
// Add attribute groups labels if selected
$labels_ag = explode(',', Configuration::get('amf_ag'));
foreach ($labels_ag as $label_ag) {
$h++;
$attribute_group_label = $this->getAttributeGroupName($label_ag, 1); // labels in english only
$attribute_group_label = Tools::strtolower('attribute_'.$attribute_group_label[0]['name']);
$attribute_group_label = str_replace(' ', '_', $attribute_group_label);
array_push($header, $attribute_group_label);
}
// Add feature groups labels if selected
$labels_fg = explode(',', Configuration::get('amf_fg'));
foreach ($labels_fg as $label_fg) {
$h++;
$feature_label = $this->getFeatureGroupName($label_fg, 1); // labels in english only
$feature_label = Tools::strtolower('feature_'.$feature_label[0]['name']);
$feature_label = str_replace(' ', '_', $feature_label);
array_push($header, $feature_label);
}
if(DEV){
echo "<pre>HEADER<br>";
print_r($header);
echo "</pre><br><br>";
}
fputcsv($fp, $header, $sep);
fseek($fp, -1, SEEK_CUR);
// fwrite($fp, "\r\n");
}
if($products->rowCount() > 0){
while ($p = $products->fetchObject()) {
if ( $fp = fopen($export, 'a+') ) {
// UTF8
// fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
// Declare product class
$product = new Product((int)$p->id_product, false, $id_lang);
// --------------------------------------------------------------------
// If product get variants
// --------------------------------------------------------------------
if ($variants = $product->getProductAttributesIds($p->id_product)) {
foreach ($variants as $variant) {
// Declare produit if attribute
$v_id = $variant['id_product_attribute'];
// Get description
$p->description_short = $this->cleanStr($p->description_short);
// Get price tax incl
$v_price_tax_incl = $product->getPriceStatic($p->id_product, true, $v_id, 6, null, false, false);
// Get url
$link = new Link();
$p->product_url = $link->getProductLink($p->id_product, null, null, null, $id_lang);
// Get image url & thumbnail_url
if ($v_has_img = $product->getCombinationImageById($v_id, $id_lang)){
$v_image_url = $protocol . $link->getImageLink(isset($p->link_rewrite) ? $p->link_rewrite : $p->name, (int)$v_has_img['id_image'], 'findify-image');
$v_thumbnail_url = $protocol . $link->getImageLink(isset($p->link_rewrite) ? $p->link_rewrite : $p->name, (int)$v_has_img['id_image'], 'findify-thumbnail');
}else{
$product_img = $product->getCover($p->id_product);
$v_image_url = $protocol . $link->getImageLink(isset($p->link_rewrite) ? $p->link_rewrite : $p->name, (int)$product_img['id_image'], 'findify-image');
$v_thumbnail_url = $protocol . $link->getImageLink(isset($p->link_rewrite) ? $p->link_rewrite : $p->name, (int)$product_img['id_image'], 'findify-thumbnail');
}
// Get categories
$p->category = '';
$product_categories = $product->getProductCategoriesFull($p->id_product, $id_lang);
$c=0;
foreach ($product_categories as $product_category) {
if ($c > 0)
$p->category .= ' > ';
$p->category .= $product_category['name'];
$c++;
}
// Get availability
$p->available_quantity = $product->getQuantity($p->id_product, $v_id);
$p->availability = ($p->available_quantity < 1) ? $this->l('out of stock') : $this->l('in stock');
// Get manufacturer name
$manufacturer = new Manufacturer();
$p->brand = $manufacturer->getNameById($p->id_manufacturer);
// Get sale price
$p->sale_price = $product->getPriceStatic($p->id_product, true, $v_id, 6, null, false, true);
// Get date
$p->created_at = date(DATE_ISO8601, strtotime($p->date_add));
// On sale
$p->on_sale = $p->on_sale;
// New
$p->new = $product->isNew();
if(!$p->new)
$p->new = 0;
// Extra fields
$p->name_ar = $p->name_ar;
// Sticker new edition
// id_st_sticker_map = 7
$ids_concerned = Db::getInstance()->executeS('
SELECT id_products
FROM ' . _DB_PREFIX_ . 'st_sticker_map
WHERE id_st_sticker = 7
');
$ids_concerned = explode(',', $ids_concerned[0]['id_products']);
if(in_array($p->id_product, $ids_concerned)){
$p->sticker_new_edition = 1;
}else{
$p->sticker_new_edition = 0;
}
// Export data
$line = array(
$v_id,
$p->id_product,
$p->name,
$p->description_short,
$v_price_tax_incl,
$v_image_url,
$p->product_url,
$p->category,
$v_thumbnail_url,
$p->availability,
$p->created_at,
$p->reference,
$p->brand,
$p->brand,
$p->available_quantity,
$p->on_sale,
$p->new,
$p->name_ar,
$p->sticker_new_edition
);
$i = count($line);
// Export prices for each currency
$currencies = new Currency();
foreach ($currencies->getCurrencies() as $currency) {
$line[$i] = $v_price_tax_incl * $currency['conversion_rate'];
$i++;
}
// Export sale prices for each currency
$currencies = new Currency();
foreach ($currencies->getCurrencies() as $currency) {
$line[$i] = $p->sale_price * $currency['conversion_rate'];
$i++;
}
// Export attribute groups values
$amf_attr_groups = explode(',', Configuration::get('amf_ag'));
if($amf_attr_groups && !empty($amf_attr_groups)){
$attr_value = null;
foreach ($amf_attr_groups as $amf_attr_group) {
$product_attribute_groups = $product->getAttributesGroups($id_lang);
foreach ($product_attribute_groups as $pag) {
if ($pag['id_attribute_group'] == $amf_attr_group){
$attr_value = $pag['attribute_name'];
}
}
$line[$i] = $attr_value;
unset($attr_value);
$i++;
}
}
// Export feature values
$amf_features = explode(',', Configuration::get('amf_fg'));
if($amf_features && !empty($amf_features)){
$feat_value = null;
foreach ($amf_features as $amf_feature) {
$product_features_groups = $product->getFeaturesStatic((int)$p->id_product);
foreach ($product_features_groups as $pf) {
if ($pf['id_feature'] == $amf_feature && $feature_values = FeatureValue::getFeatureValueLang($pf['id_feature_value'])){
foreach ($feature_values as $feature_value) {
if($feature_value['id_lang'] == $id_lang){
$feat_value = $this->removeNl($feature_value['value']);
}
}
}
}
$line[$i] = $feat_value;
unset($feat_value);
$i++;
}
}
if(DEV){
echo "<pre>P ID : ".$p->id_product." - PA ID : ".$v_id."<br>";
print_r($line);
echo "</pre><br><br>";
}
fputcsv($fp, $line, $sep);
fseek($fp, -1, SEEK_CUR);
// fwrite($fp, "\r\n");
echo date("Y-m-d H:i:s").' '.$this->l('Export product ID').' '.$p->id_product.' - '.$this->l('Attribute ID').' '.$v_id.'<br>';
fputs($log, date("Y-m-d H:i:s").' '.$this->l('Export product ID').' '.$p->id_product.' - '.$this->l('Attribute ID').' '.$v_id.PHP_EOL);
}
// --------------------------------------------------------------------
// If simple product
// --------------------------------------------------------------------
} else {
// Get description
$p->description_short = $this->cleanStr($p->description_short);
// Get price tax incl
$p->price_tax_incl = $product->getPriceStatic($p->id_product, true, null, 6, null, false, false);
// Get url
$link = new Link();
$p->product_url = $link->getProductLink($p->id_product, null, null, null, $id_lang);
// Get image url & thumbnail_url
$product_img = $product->getCover($p->id_product);
$p->image_url = $protocol . $link->getImageLink(isset($p->link_rewrite) ? $p->link_rewrite : $p->name, (int)$product_img['id_image'], 'findify-image');
$p->thumbnail_url = $protocol . $link->getImageLink(isset($p->link_rewrite) ? $p->link_rewrite : $p->name, (int)$product_img['id_image'], 'findify-thumbnail');
// Get categories
$p->category = '';
$product_categories = $product->getProductCategoriesFull($p->id_product, $id_lang);
$c=0;
foreach ($product_categories as $product_category) {
if ($c > 0)
$p->category .= ' > ';
$p->category .= $product_category['name'];
$c++;
}
// Get availability
$p->available_quantity = $product->getQuantity($p->id_product);
$p->availability = ($p->available_quantity < 1) ? $this->l('out of stock') : $this->l('in stock');
// Get manufacturer name
$manufacturer = new Manufacturer();
$p->brand = $manufacturer->getNameById($p->id_manufacturer);
// Get sale price
$p->sale_price = $product->getPriceStatic($p->id_product, true, null, 6, null, false, true);
// Get date
$p->created_at = date(DATE_ISO8601, strtotime($p->date_add));
// On sale
$p->on_sale = $p->on_sale;
// New
$p->new = $product->isNew();
if(!$p->new)
$p->new = 0;
// Extra fields
$p->name_ar = $p->name_ar;
// Sticker new edition
// id_st_sticker_map = 7
$ids_concerned = Db::getInstance()->executeS('
SELECT id_products
FROM ' . _DB_PREFIX_ . 'st_sticker_map
WHERE id_st_sticker = 7
');
$ids_concerned = explode(',', $ids_concerned[0]['id_products']);
if(in_array($p->id_product, $ids_concerned)){
$p->sticker_new_edition = 1;
}else{
$p->sticker_new_edition = 0;
}
// Export data
$line = array(
$p->id_product,
$p->id_product,
$p->name,
$p->description_short,
$p->price_tax_incl,
$p->image_url,
$p->product_url,
$p->category,
$p->thumbnail_url,
$p->availability,
$p->created_at,
$p->reference,
$p->brand,
$p->brand,
$p->available_quantity,
$p->on_sale,
$p->new,
$p->name_ar,
$p->sticker_new_edition
);