forked from PrestaShop/advancedeucompliance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advancedeucompliance.php
1655 lines (1435 loc) · 81.6 KB
/
advancedeucompliance.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
/**
* 2007-2015 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2015 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_')) {
exit;
}
/* Include required entities */
include_once dirname(__FILE__) . '/entities/AeucCMSRoleEmailEntity.php';
include_once dirname(__FILE__) . '/entities/AeucEmailEntity.php';
class Advancedeucompliance extends Module
{
/* Class members */
protected $config_form = false;
protected $entity_manager;
protected $filesystem;
protected $emails;
private $missing_templates = array();
protected $_errors;
protected $_warnings;
/* Constants used for LEGAL/CMS Management */
const LEGAL_NO_ASSOC = 'NO_ASSOC';
const LEGAL_NOTICE = 'LEGAL_NOTICE';
const LEGAL_CONDITIONS = 'LEGAL_CONDITIONS';
const LEGAL_REVOCATION = 'LEGAL_REVOCATION';
const LEGAL_REVOCATION_FORM = 'LEGAL_REVOCATION_FORM';
const LEGAL_PRIVACY = 'LEGAL_PRIVACY';
const LEGAL_ENVIRONMENTAL = 'LEGAL_ENVIRONMENTAL';
const LEGAL_SHIP_PAY = 'LEGAL_SHIP_PAY';
const DEFAULT_PS_PRODUCT_WEIGHT_PRECISION = 2;
public function __construct(Core_Foundation_Database_EntityManager $entity_manager,
Core_Foundation_FileSystem_FileSystem $fs,
Core_Business_Email_EmailLister $email)
{
$this->name = 'advancedeucompliance';
$this->tab = 'administration';
$this->version = '2.0.0';
$this->author = 'PrestaShop';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
/* Register dependencies to module */
$this->entity_manager = $entity_manager;
$this->filesystem = $fs;
$this->emails = $email;
$this->displayName = $this->l('Advanced EU Compliance');
$this->description = $this->l('This module helps European merchants comply with applicable e-commerce laws.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall this module?');
/* Init errors var */
$this->_errors = array();
}
/**
* Don't forget to create update methods if needed:
* http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
*/
public function install()
{
$return = parent::install() &&
$this->loadTables() &&
$this->installHooks() &&
$this->registerModulesBackwardCompatHook() &&
$this->registerHook('header') &&
$this->registerHook('displayProductPriceBlock') &&
$this->registerHook('overrideTOSDisplay') &&
$this->registerHook('actionEmailAddAfterContent') &&
$this->registerHook('advancedPaymentOptions') &&
$this->registerHook('displayAfterShoppingCartBlock') &&
$this->registerHook('displayBeforeShoppingCartBlock') &&
$this->registerHook('displayCartTotalPriceLabel') &&
$this->createConfig();
$this->emptyTemplatesCache();
return (bool)$return;
}
public function isThemeCompliant()
{
$return = true;
foreach ($this->getRequiredThemeTemplate() as $required_tpl) {
if (!is_file(_PS_THEME_DIR_ . $required_tpl)) {
$this->missing_templates[] = $required_tpl;
$return = false;
}
}
return $return;
}
public function getRequiredThemeTemplate()
{
return array(
'order-address-advanced.tpl',
'order-carrier-advanced.tpl',
'order-carrier-opc-advanced.tpl',
'order-opc-advanced.tpl',
'order-opc-new-account-advanced.tpl',
'order-payment-advanced.tpl',
'shopping-cart-advanced.tpl'
);
}
public function uninstall()
{
return parent::uninstall() &&
$this->dropConfig() &&
$this->unloadTables();
}
public function disable($force_all = false)
{
$is_adv_api_disabled = (bool)Configuration::updateValue('PS_ADVANCED_PAYMENT_API', false);
$is_adv_api_disabled &= (bool)Configuration::updateValue('PS_ATCP_SHIPWRAP', false);
return parent::disable() && $is_adv_api_disabled;
}
public function registerModulesBackwardCompatHook()
{
$return = true;
$module_to_check = array(
'bankwire', 'cheque', 'paypal',
'adyen', 'hipay', 'cashondelivery', 'sofortbanking',
'pigmbhpaymill', 'ogone', 'moneybookers',
'syspay'
);
$display_payment_eu_hook_id = (int)Hook::getIdByName('displayPaymentEu');
$already_hooked_modules_ids = array_keys(Hook::getModulesFromHook($display_payment_eu_hook_id));
foreach ($module_to_check as $module_name) {
if (($module = Module::getInstanceByName($module_name)) !== false &&
Module::isInstalled($module_name) &&
$module->active &&
!in_array($module->id, $already_hooked_modules_ids) &&
!$module->isRegisteredInHook('displayPaymentEu') ) {
$return &= $module->registerHook('displayPaymentEu');
}
}
return $return;
}
public function installHooks()
{
$hooks = array(
'displayBeforeShoppingCartBlock' => array(
'name' => 'display before Shopping cart block',
'description' => 'Display content after Shopping Cart'
),
'displayAfterShoppingCartBlock' => array(
'name' => 'display after Shopping cart block',
'description' => 'Display content after Shopping Cart'
),
'displayPaymentEu' => array(
'name' => 'Display EU payment options (helper)',
'description' => 'Hook to display payment options'
)
);
$return = true;
foreach ($hooks as $hook_name => $hook) {
if (Hook::getIdByName($hook_name)) {
continue;
}
$new_hook = new Hook();
$new_hook->name = $hook_name;
$new_hook->title = $hook_name;
$new_hook->description = $hook['description'];
$new_hook->position = true;
$new_hook->live_edit = false;
if (!$new_hook->add()) {
$return &= false;
$this->_errors[] = $this->l('Could not install new hook', 'advancedeucompliance') . ': ' . $hook_name;
}
}
return $return;
}
public function createConfig()
{
$delivery_time_available_values = array();
$delivery_time_oos_values = array();
$shopping_cart_text_before = array();
$shopping_cart_text_after = array();
$langs_repository = $this->entity_manager->getRepository('Language');
$langs = $langs_repository->findAll();
foreach ($langs as $lang) {
$delivery_time_available_values[(int)$lang->id] = $this->l('Delivery: 1 to 3 weeks', 'advancedeucompliance');
$delivery_time_oos_values[(int)$lang->id] = $this->l('Delivery: 3 to 6 weeks', 'advancedeucompliance');
$shopping_cart_text_before[(int)$lang->id] = '';
$shopping_cart_text_after[(int)$lang->id] = '';
}
/* Base settings */
$this->processAeucFeatTellAFriend(true);
$this->processAeucFeatReorder(true);
$this->processAeucFeatAdvPaymentApi(false);
$this->processAeucLabelRevocationTOS(false);
$this->processAeucLabelRevocationVP(false);
$this->processAeucLabelSpecificPrice(true);
$this->processAeucLabelTaxIncExc(true);
$this->processAeucLabelShippingIncExc(false);
$this->processAeucLabelWeight(true);
$this->processAeucLabelCombinationFrom(true);
$is_theme_compliant = $this->isThemeCompliant();
$ps_weight_precision_installed = Configuration::get('PS_PRODUCT_WEIGHT_PRECISION') ?
(int)Configuration::get('PS_PRODUCT_WEIGHT_PRECISION') :
Advancedeucompliance::DEFAULT_PS_PRODUCT_WEIGHT_PRECISION;
return Configuration::updateValue('AEUC_FEAT_TELL_A_FRIEND', false) &&
Configuration::updateValue('AEUC_FEAT_ADV_PAYMENT_API', false) &&
Configuration::updateValue('AEUC_LABEL_DELIVERY_TIME_AVAILABLE', $delivery_time_available_values) &&
Configuration::updateValue('AEUC_LABEL_DELIVERY_TIME_OOS', $delivery_time_oos_values) &&
Configuration::updateValue('AEUC_LABEL_SPECIFIC_PRICE', true) &&
Configuration::updateValue('AEUC_LABEL_TAX_INC_EXC', true) &&
Configuration::updateValue('AEUC_LABEL_WEIGHT', true) &&
Configuration::updateValue('AEUC_LABEL_REVOCATION_TOS', false) &&
Configuration::updateValue('AEUC_LABEL_REVOCATION_VP', true) &&
Configuration::updateValue('AEUC_LABEL_SHIPPING_INC_EXC', false) &&
Configuration::updateValue('AEUC_LABEL_COMBINATION_FROM', true) &&
Configuration::updateValue('AEUC_SHOPPING_CART_TEXT_BEFORE', $shopping_cart_text_before) &&
Configuration::updateValue('AEUC_SHOPPING_CART_TEXT_AFTER', $shopping_cart_text_after) &&
Configuration::updateValue('AEUC_IS_THEME_COMPLIANT', (bool)$is_theme_compliant) &&
Configuration::updateValue('PS_PRODUCT_WEIGHT_PRECISION', (int)$ps_weight_precision_installed);
}
public function unloadTables()
{
$state = true;
$sql = require dirname(__FILE__) . '/install/sql_install.php';
foreach ($sql as $name => $v) {
$state &= Db::getInstance()->execute('DROP TABLE IF EXISTS ' . $name);
}
return $state;
}
public function loadTables()
{
$state = true;
// Create module's table
$sql = require dirname(__FILE__) . '/install/sql_install.php';
foreach ($sql as $s) {
$state &= Db::getInstance()->execute($s);
}
// Fillin CMS ROLE
$roles_array = $this->getCMSRoles();
$roles = array_keys($roles_array);
$cms_role_repository = $this->entity_manager->getRepository('CMSRole');
foreach ($roles as $role) {
if (!$cms_role_repository->findOneByName($role)) {
$cms_role = $cms_role_repository->getNewEntity();
$cms_role->id_cms = 0; // No assoc at this time
$cms_role->name = $role;
$state &= (bool)$cms_role->save();
}
}
$default_path_email = _PS_MAIL_DIR_ . 'en' . DIRECTORY_SEPARATOR;
// Fill-in aeuc_mail table
foreach ($this->emails->getAvailableMails($default_path_email) as $mail) {
$new_email = new AeucEmailEntity();
$new_email->filename = (string)$mail;
$new_email->display_name = $this->emails->getCleanedMailName($mail);
$new_email->save();
unset($new_email);
}
return $state;
}
public function dropConfig()
{
// Remove roles
$roles_array = $this->getCMSRoles();
$roles = array_keys($roles_array);
$cms_role_repository = $this->entity_manager->getRepository('CMSRole');
$cleaned = true;
foreach ($roles as $role) {
$cms_role_tmp = $cms_role_repository->findOneByName($role);
if ($cms_role_tmp) {
$cleaned &= $cms_role_tmp->delete();
}
}
return Configuration::deleteByName('AEUC_FEAT_TELL_A_FRIEND') &&
Configuration::deleteByName('AEUC_FEAT_ADV_PAYMENT_API') &&
Configuration::deleteByName('AEUC_LABEL_DELIVERY_TIME_AVAILABLE') &&
Configuration::deleteByName('AEUC_LABEL_DELIVERY_TIME_OOS') &&
Configuration::deleteByName('AEUC_LABEL_SPECIFIC_PRICE') &&
Configuration::deleteByName('AEUC_LABEL_TAX_INC_EXC') &&
Configuration::deleteByName('AEUC_LABEL_WEIGHT') &&
Configuration::deleteByName('AEUC_LABEL_REVOCATION_TOS') &&
Configuration::deleteByName('AEUC_LABEL_REVOCATION_VP') &&
Configuration::deleteByName('AEUC_LABEL_SHIPPING_INC_EXC') &&
Configuration::deleteByName('AEUC_LABEL_COMBINATION_FROM') &&
Configuration::deleteByName('AEUC_SHOPPING_CART_TEXT_BEFORE') &&
Configuration::deleteByName('AEUC_SHOPPING_CART_TEXT_AFTER') &&
Configuration::deleteByName('AEUC_IS_THEME_COMPLIANT') &&
Configuration::updateValue('PS_ADVANCED_PAYMENT_API', false) &&
Configuration::updateValue('PS_ATCP_SHIPWRAP', false);
}
/*
This method checks if cart has virtual products
It's better to add this method (as hasVirtualProduct) and add 'protected static $_hasVirtualProduct = array(); property
in Cart class in next version of prestashop.
*/
private function hasCartVirtualProduct(Cart $cart)
{
$products = $cart->getProducts();
if (!count($products)) {
return false;
}
foreach ($products as $product) {
if ($product['is_virtual']) {
return true;
}
}
return false;
}
public function hookDisplayCartTotalPriceLabel($param)
{
$smartyVars = array();
if ((bool)Configuration::get('AEUC_LABEL_TAX_INC_EXC') === true) {
$customer_default_group_id = (int)$this->context->customer->id_default_group;
$customer_default_group = new Group($customer_default_group_id);
if ((bool)Configuration::get('PS_TAX') === true &&
!(Validate::isLoadedObject($customer_default_group) && (bool)$customer_default_group->price_display_method === true)) {
$smartyVars['price']['tax_str_i18n'] = $this->l('Tax included', 'advancedeucompliance');
} else {
$smartyVars['price']['tax_str_i18n'] = $this->l('Tax excluded', 'advancedeucompliance');
}
}
if (isset($param['from'])) {
if ($param['from'] == 'shopping_cart') {
$smartyVars['css_class'] = 'aeuc_tax_label_shopping_cart';
}
if ($param['from'] == 'blockcart') {
$smartyVars['css_class'] = 'aeuc_tax_label_blockcart';
}
}
$this->context->smarty->assign(array('smartyVars' => $smartyVars));
return $this->display(__FILE__, 'displayCartTotalPriceLabel.tpl');
}
/* This hook is present to maintain backward compatibility */
public function hookAdvancedPaymentOptions($param)
{
$legacyOptions = Hook::exec('displayPaymentEU', array(), null, true);
$newOptions = array();
Media::addJsDef(array('aeuc_tos_err_str' => Tools::htmlentitiesUTF8($this->l('You must agree to our Terms of Service before going any further!',
'advancedeucompliance'))));
Media::addJsDef(array('aeuc_submit_err_str' => Tools::htmlentitiesUTF8($this->l('Something went wrong. If the problem persists, please contact us.',
'advancedeucompliance'))));
Media::addJsDef(array('aeuc_no_pay_err_str' => Tools::htmlentitiesUTF8($this->l('Select a payment option first.',
'advancedeucompliance'))));
Media::addJsDef(array('aeuc_virt_prod_err_str' => Tools::htmlentitiesUTF8($this->l('Please check "Revocation of virtual products" box first !',
'advancedeucompliance'))));
if ($legacyOptions) {
foreach ($legacyOptions as $module_name => $legacyOption) {
if (!$legacyOption) {
continue;
}
foreach (Core_Business_Payment_PaymentOption::convertLegacyOption($legacyOption) as $option) {
$option->setModuleName($module_name);
$to_be_cleaned = $option->getForm();
if ($to_be_cleaned) {
$cleaned = str_replace('@hiddenSubmit', '', $to_be_cleaned);
$option->setForm($cleaned);
}
$newOptions[] = $option;
}
}
return $newOptions;
}
return null;
}
public function hookActionEmailAddAfterContent($param)
{
if (!isset($param['template']) || !isset($param['template_html']) || !isset($param['template_txt'])) {
return;
}
$tpl_name = (string)$param['template'];
$tpl_name_exploded = explode('.', $tpl_name);
if (is_array($tpl_name_exploded)) {
$tpl_name = (string)$tpl_name_exploded[0];
}
$id_lang = (int)$param['id_lang'];
$mail_id = AeucEmailEntity::getMailIdFromTplFilename($tpl_name);
if (!isset($mail_id['id_mail'])) {
return;
}
$mail_id = (int)$mail_id['id_mail'];
$cms_role_ids = AeucCMSRoleEmailEntity::getCMSRoleIdsFromIdMail($mail_id);
if (!$cms_role_ids) {
return;
}
$tmp_cms_role_list = array();
foreach ($cms_role_ids as $cms_role_id) {
$tmp_cms_role_list[] = $cms_role_id['id_cms_role'];
}
$cms_role_repository = $this->entity_manager->getRepository('CMSRole');
$cms_roles = $cms_role_repository->findByIdCmsRole($tmp_cms_role_list);
if (!$cms_roles) {
return;
}
$cms_repo = $this->entity_manager->getRepository('CMS');
$cms_contents = array();
foreach ($cms_roles as $cms_role) {
$cms_page = $cms_repo->i10nFindOneById((int)$cms_role->id_cms, $id_lang, $this->context->shop->id);
if (!isset($cms_page->content)) {
continue;
}
$cms_contents[] = $cms_page->content;
$param['template_txt'] .= strip_tags($cms_page->content, true);
}
$this->context->smarty->assign(array('cms_contents' => $cms_contents));
$param['template_html'] .= $this->display(__FILE__, 'hook-email-wrapper.tpl');
}
public function hookHeader($param)
{
$css_required = array(
'index',
'product',
'order',
'order-opc',
'category',
'products-comparison',
);
if (isset($this->context->controller->php_self) && in_array($this->context->controller->php_self, $css_required)) {
$this->context->controller->addCSS($this->_path . 'views/css/aeuc_front.css', 'all');
}
}
public function hookOverrideTOSDisplay($param)
{
$has_tos_override_opt = (bool)Configuration::get('AEUC_LABEL_REVOCATION_TOS');
$cms_repository = $this->entity_manager->getRepository('CMS');
// Check first if LEGAL_REVOCATION CMS Role is set
$cms_role_repository = $this->entity_manager->getRepository('CMSRole');
$cms_page_associated = $cms_role_repository->findOneByName(Advancedeucompliance::LEGAL_REVOCATION);
// Check if cart has virtual product
$has_virtual_product = (bool)Configuration::get('AEUC_LABEL_REVOCATION_VP') && $this->hasCartVirtualProduct($this->context->cart);
Media::addJsDef(array('aeuc_has_virtual_products' => (bool)$has_virtual_product,
'aeuc_virt_prod_err_str' => Tools::htmlentitiesUTF8($this->l('Please check "Revocation of virtual products" box first !',
'advancedeucompliance'))));
if ($has_tos_override_opt || (bool)Configuration::get('AEUC_LABEL_REVOCATION_VP')) {
$this->context->controller->addJS($this->_path . 'views/js/fo_aeuc_tnc.js', true);
}
$checkedTos = false;
$link_conditions = '';
$link_revocations = '';
// Get IDs of CMS pages required
$cms_conditions_id = (int)Configuration::get('PS_CONDITIONS_CMS_ID');
$cms_revocation_id = (int)$cms_page_associated->id_cms;
// Get misc vars
$id_lang = (int)$this->context->language->id;
$id_shop = (int)$this->context->shop->id;
$is_ssl_enabled = (bool)Configuration::get('PS_SSL_ENABLED');
$checkedTos = $this->context->cart->checkedTos ? true : false;
// Get CMS OBJs
$cms_conditions = $cms_repository->i10nFindOneById($cms_conditions_id, $id_lang, $id_shop);
$link_conditions =
$this->context->link->getCMSLink($cms_conditions, $cms_conditions->link_rewrite, $is_ssl_enabled);
if (!strpos($link_conditions, '?')) {
$link_conditions .= '?content_only=1';
} else {
$link_conditions .= '&content_only=1';
}
if ($has_tos_override_opt === true) {
$cms_revocations = $cms_repository->i10nFindOneById($cms_revocation_id, $id_lang, $id_shop);
// Get links to revocation page
$link_revocations =
$this->context->link->getCMSLink($cms_revocations, $cms_revocations->link_rewrite, $is_ssl_enabled);
if (!strpos($link_revocations, '?')) {
$link_revocations .= '?content_only=1';
} else {
$link_revocations .= '&content_only=1';
}
}
$this->context->smarty->assign(array(
'has_tos_override_opt' => $has_tos_override_opt,
'checkedTOS' => $checkedTos,
'link_conditions' => $link_conditions,
'link_revocations' => $link_revocations,
'has_virtual_product' => $has_virtual_product
));
return $this->display(__FILE__, 'hookOverrideTOSDisplay.tpl');
}
public function hookDisplayBeforeShoppingCartBlock($params)
{
if ($this->context->controller instanceof OrderOpcController || property_exists($this->context->controller, 'step') && $this->context->controller->step == 3) {
$cart_text = Configuration::get('AEUC_SHOPPING_CART_TEXT_BEFORE', $this->context->language->id);
if ($cart_text) {
$this->context->smarty->assign('cart_text', $cart_text);
return $this->display(__FILE__, 'displayShoppingCartBeforeBlock.tpl');
}
}
}
public function hookDisplayAfterShoppingCartBlock($params)
{
$cart_text = Configuration::get('AEUC_SHOPPING_CART_TEXT_AFTER', Context::getContext()->language->id);
if ($cart_text && isset($params['colspan_total'])) {
$this->context->smarty->assign(array('cart_text' => $cart_text,
'colspan_total' => (int)$params['colspan_total']
));
return $this->display(__FILE__, 'displayShoppingCartAfterBlock.tpl');
}
}
public function hookDisplayProductPriceBlock($param)
{
if (!isset($param['product']) || !isset($param['type'])) {
return;
}
$product = $param['product'];
if (is_array($product)) {
$product_repository = $this->entity_manager->getRepository('Product');
$product = $product_repository->findOne((int)$product['id_product']);
}
if (!Validate::isLoadedObject($product)) {
return;
}
$smartyVars = array();
/* Handle Product Combinations label */
if ($param['type'] == 'before_price' && (bool)Configuration::get('AEUC_LABEL_COMBINATION_FROM') === true) {
if ($product->hasAttributes()) {
$need_display = false;
$combinations = $product->getAttributeCombinations($this->context->language->id);
if ($combinations && is_array($combinations)) {
foreach ($combinations as $combination) {
if ((float)$combination['price'] > 0) {
$need_display = true;
break;
}
}
unset($combinations);
if ($need_display) {
$smartyVars['before_price'] = array();
$smartyVars['before_price']['from_str_i18n'] = $this->l('From', 'advancedeucompliance');
return $this->dumpHookDisplayProductPriceBlock($smartyVars);
}
}
return;
}
}
/* Handle Specific Price label*/
if ($param['type'] == 'old_price' && (bool)Configuration::get('AEUC_LABEL_SPECIFIC_PRICE') === true) {
$smartyVars['old_price'] = array();
$smartyVars['old_price']['before_str_i18n'] = $this->l('Before', 'advancedeucompliance');
return $this->dumpHookDisplayProductPriceBlock($smartyVars);
}
/* Handle taxes Inc./Exc. and Shipping Inc./Exc.*/
if ($param['type'] == 'price') {
$smartyVars['price'] = array();
$need_shipping_label = true;
if ((bool)Configuration::get('AEUC_LABEL_TAX_INC_EXC') === true) {
$customer_default_group_id = (int)$this->context->customer->id_default_group;
$customer_default_group = new Group($customer_default_group_id);
if ((bool)Configuration::get('PS_TAX') === true &&
!(Validate::isLoadedObject($customer_default_group) && (bool)$customer_default_group->price_display_method === true)) {
$smartyVars['price']['tax_str_i18n'] = $this->l('Tax included', 'advancedeucompliance');
} else {
$smartyVars['price']['tax_str_i18n'] = $this->l('Tax excluded', 'advancedeucompliance');
}
if (isset($param['from']) && $param['from'] == 'blockcart') {
$smartyVars['price']['css_class'] = 'aeuc_tax_label_blockcart';
$need_shipping_label = false;
}
}
if ((bool)Configuration::get('AEUC_LABEL_SHIPPING_INC_EXC') === true && $need_shipping_label === true) {
if (!$product->is_virtual) {
$cms_role_repository = $this->entity_manager->getRepository('CMSRole');
$cms_repository = $this->entity_manager->getRepository('CMS');
$cms_page_associated = $cms_role_repository->findOneByName(Advancedeucompliance::LEGAL_SHIP_PAY);
if (isset($cms_page_associated->id_cms) && $cms_page_associated->id_cms != 0) {
$cms_ship_pay_id = (int)$cms_page_associated->id_cms;
$cms_revocations = $cms_repository->i10nFindOneById($cms_ship_pay_id, $this->context->language->id,
$this->context->shop->id);
$is_ssl_enabled = (bool)Configuration::get('PS_SSL_ENABLED');
$link_ship_pay = $this->context->link->getCMSLink($cms_revocations, $cms_revocations->link_rewrite, $is_ssl_enabled);
if (!strpos($link_ship_pay, '?')) {
$link_ship_pay .= '?content_only=1';
} else {
$link_ship_pay .= '&content_only=1';
}
$smartyVars['ship'] = array();
$smartyVars['ship']['link_ship_pay'] = $link_ship_pay;
$smartyVars['ship']['ship_str_i18n'] = $this->l('Shipping excluded', 'advancedeucompliance');
}
}
}
return $this->dumpHookDisplayProductPriceBlock($smartyVars);
}
/* Handles product's weight */
if ($param['type'] == 'weight' && (bool)Configuration::get('PS_DISPLAY_PRODUCT_WEIGHT') === true &&
isset($param['hook_origin']) && $param['hook_origin'] == 'product_sheet'
) {
if ((float)$product->weight) {
$smartyVars['weight'] = array();
$rounded_weight = round((float)$product->weight, Configuration::get('PS_PRODUCT_WEIGHT_PRECISION'));
$smartyVars['weight']['rounded_weight_str_i18n'] =
$rounded_weight . ' ' . Configuration::get('PS_WEIGHT_UNIT');
return $this->dumpHookDisplayProductPriceBlock($smartyVars);
}
}
/* Handle Estimated delivery time label */
if ($param['type'] == 'after_price') {
$context_id_lang = $this->context->language->id;
$is_product_available = (StockAvailable::getQuantityAvailableByProduct($product->id) >= 1 ? true : false);
$smartyVars['after_price'] = array();
if ($is_product_available) {
$contextualized_content =
Configuration::get('AEUC_LABEL_DELIVERY_TIME_AVAILABLE', (int)$context_id_lang);
$smartyVars['after_price']['delivery_str_i18n'] = $contextualized_content;
} else {
$contextualized_content = Configuration::get('AEUC_LABEL_DELIVERY_TIME_OOS', (int)$context_id_lang);
$smartyVars['after_price']['delivery_str_i18n'] = $contextualized_content;
}
return $this->dumpHookDisplayProductPriceBlock($smartyVars);
}
}
private function emptyTemplatesCache()
{
$this->_clearCache('product.tpl');
$this->_clearCache('product-list.tpl');
}
private function dumpHookDisplayProductPriceBlock(array $smartyVars)
{
$this->context->smarty->assign(array('smartyVars' => $smartyVars));
$this->context->controller->addJS($this->_path . 'views/js/fo_aeuc_tnc.js', true);
return $this->display(__FILE__, 'hookDisplayProductPriceBlock.tpl');
}
/**
* Load the configuration form
*/
public function getContent()
{
$theme_warning = null;
$this->refreshThemeStatus();
$success_band = $this->_postProcess();
if ((bool)Configuration::get('AEUC_IS_THEME_COMPLIANT') === false) {
$missing = '<ul>';
foreach ($this->missing_templates as $missing_tpl) {
$missing .= '<li>'.$missing_tpl.' '.$this->l('missing').'</li>';
}
$missing .= '</ul><br/>';
$discard_warning_link = $this->context->link->getAdminLink('AdminModules', false) .
'&configure='.$this->name.
'&tab_module='.$this->tab.
'&module_name='.$this->name.
'&discard_tpl_warn=1'.
'&token='.Tools::getAdminTokenLite('AdminModules');
$missing .= '<a href="'.$discard_warning_link.'" type="button">'.$this->l('Hide this, I know what I am doing.',
'advancedeucompliance').
'</a>';
$theme_warning = $this->displayWarning($this->l('It seems that your current theme is not compatible with this module, some mandatory templates are missing. It is possible some options may not work as expected.',
'advancedeucompliance').$missing);
}
$this->context->smarty->assign('module_dir', $this->_path);
$this->context->smarty->assign('errors', $this->_errors);
$this->context->controller->addCSS($this->_path . 'views/css/configure.css', 'all');
// Render all required form for each 'part'
$formLabelsManager = $this->renderFormLabelsManager();
$formFeaturesManager = $this->renderFormFeaturesManager();
$formLegalContentManager = $this->renderFormLegalContentManager();
$formEmailAttachmentsManager = $this->renderFormEmailAttachmentsManager();
return $theme_warning . $success_band . $formLabelsManager . $formFeaturesManager . $formLegalContentManager .
$formEmailAttachmentsManager;
}
/**
* Save form data.
*/
protected function _postProcess()
{
$has_processed_something = false;
$post_keys_switchable =
array_keys(array_merge($this->getConfigFormLabelsManagerValues(), $this->getConfigFormFeaturesManagerValues()));
$post_keys_complex = array('AEUC_legalContentManager',
'AEUC_emailAttachmentsManager',
'PS_PRODUCT_WEIGHT_PRECISION',
'discard_tpl_warn'
);
$i10n_inputs_received = array();
$received_values = Tools::getAllValues();
foreach (array_keys($received_values) as $key_received) {
/* Case its one of form with only switches in it */
if (in_array($key_received, $post_keys_switchable)) {
$is_option_active = Tools::getValue($key_received);
$key = Tools::strtolower($key_received);
$key = Tools::toCamelCase($key);
if (method_exists($this, 'process' . $key)) {
$this->{'process' . $key}($is_option_active);
$has_processed_something = true;
}
continue;
}
/* Case we are on more complex forms */
if (in_array($key_received, $post_keys_complex)) {
// Clean key
$key = Tools::strtolower($key_received);
$key = Tools::toCamelCase($key, true);
if (method_exists($this, 'process' . $key)) {
$this->{'process' . $key}();
$has_processed_something = true;
}
}
/* Case Multi-lang input */
if (strripos($key_received, 'AEUC_LABEL_DELIVERY_TIME_AVAILABLE') !== false) {
$exploded = explode('_', $key_received);
$count = count($exploded);
$id_lang = (int)$exploded[$count - 1];
$i10n_inputs_received['AEUC_LABEL_DELIVERY_TIME_AVAILABLE'][$id_lang] = $received_values[$key_received];
}
if (strripos($key_received, 'AEUC_LABEL_DELIVERY_TIME_OOS') !== false) {
$exploded = explode('_', $key_received);
$count = count($exploded);
$id_lang = (int)$exploded[$count - 1];
$i10n_inputs_received['AEUC_LABEL_DELIVERY_TIME_OOS'][$id_lang] = $received_values[$key_received];
}
if (strripos($key_received, 'AEUC_SHOPPING_CART_TEXT_BEFORE') !== false) {
$exploded = explode('_', $key_received);
$count = count($exploded);
$id_lang = (int)$exploded[$count - 1];
$i10n_inputs_received['AEUC_SHOPPING_CART_TEXT_BEFORE'][$id_lang] = $received_values[$key_received];
}
if (strripos($key_received, 'AEUC_SHOPPING_CART_TEXT_AFTER') !== false) {
$exploded = explode('_', $key_received);
$count = count($exploded);
$id_lang = (int)$exploded[$count - 1];
$i10n_inputs_received['AEUC_SHOPPING_CART_TEXT_AFTER'][$id_lang] = $received_values[$key_received];
}
}
if (count($i10n_inputs_received) > 0) {
$this->processAeucLabelDeliveryTime($i10n_inputs_received);
$this->processAeucShoppingCartText($i10n_inputs_received);
$has_processed_something = true;
}
if ($has_processed_something) {
$this->emptyTemplatesCache();
return (count($this->_errors) ? $this->displayError($this->_errors) : '') .
(count($this->_warnings) ? $this->displayWarning($this->_warnings) : '') .
$this->displayConfirmation($this->l('Settings saved successfully!', 'advancedeucompliance'));
} else {
return (count($this->_errors) ? $this->displayError($this->_errors) : '') .
(count($this->_warnings) ? $this->displayWarning($this->_warnings) : '') . '';
}
}
protected function processPsProductWeightPrecision($option_value)
{
$option_value = (int)$option_value;
/* Avoid negative values */
if ($option_value < 0) {
$option_value = 0;
}
Configuration::updateValue('PS_PRODUCT_WEIGHT_PRECISION', (int)$option_value);
}
protected function processAeucLabelDeliveryTime(array $i10n_inputs)
{
if (isset($i10n_inputs['AEUC_LABEL_DELIVERY_TIME_AVAILABLE'])) {
Configuration::updateValue('AEUC_LABEL_DELIVERY_TIME_AVAILABLE', $i10n_inputs['AEUC_LABEL_DELIVERY_TIME_AVAILABLE']);
}
if (isset($i10n_inputs['AEUC_LABEL_DELIVERY_TIME_OOS'])) {
Configuration::updateValue('AEUC_LABEL_DELIVERY_TIME_OOS', $i10n_inputs['AEUC_LABEL_DELIVERY_TIME_OOS']);
}
}
protected function processAeucShoppingCartText(array $i10n_inputs)
{
if (isset($i10n_inputs['AEUC_SHOPPING_CART_TEXT_BEFORE'])) {
Configuration::updateValue('AEUC_SHOPPING_CART_TEXT_BEFORE', $i10n_inputs['AEUC_SHOPPING_CART_TEXT_BEFORE']);
}
if (isset($i10n_inputs['AEUC_SHOPPING_CART_TEXT_AFTER'])) {
Configuration::updateValue('AEUC_SHOPPING_CART_TEXT_AFTER', $i10n_inputs['AEUC_SHOPPING_CART_TEXT_AFTER']);
}
}
protected function processAeucLabelCombinationFrom($is_option_active)
{
if ((bool)$is_option_active) {
Configuration::updateValue('AEUC_LABEL_COMBINATION_FROM', true);
} else {
Configuration::updateValue('AEUC_LABEL_COMBINATION_FROM', false);
}
}
protected function processAeucLabelSpecificPrice($is_option_active)
{
if ((bool)$is_option_active) {
Configuration::updateValue('AEUC_LABEL_SPECIFIC_PRICE', true);
} else {
Configuration::updateValue('AEUC_LABEL_SPECIFIC_PRICE', false);
}
}
protected function processAeucEmailAttachmentsManager()
{
$json_attach_assoc = Tools::jsonDecode(Tools::getValue('emails_attach_assoc'));
if (!$json_attach_assoc) {
return;
}
// Empty previous assoc to make new ones
AeucCMSRoleEmailEntity::truncate();
foreach ($json_attach_assoc as $assoc) {
$assoc_obj = new AeucCMSRoleEmailEntity();
$assoc_obj->id_mail = $assoc->id_mail;
$assoc_obj->id_cms_role = $assoc->id_cms_role;
if (!$assoc_obj->save()) {
$this->_errors[] = $this->l('Failed to associate CMS content with an email template.', 'advancedeucompliance');
}
}
}
protected function processAeucLabelRevocationTOS($is_option_active)
{
// Check first if LEGAL_REVOCATION CMS Role has been set before doing anything here
$cms_role_repository = $this->entity_manager->getRepository('CMSRole');
$cms_page_associated = $cms_role_repository->findOneByName(Advancedeucompliance::LEGAL_REVOCATION);
$cms_roles = $this->getCMSRoles();
if ((bool)$is_option_active) {
if (!$cms_page_associated instanceof CMSRole || (int)$cms_page_associated->id_cms == 0) {
$this->_errors[] =
sprintf($this->l('\'Revocation Terms within ToS\' label cannot be activated unless you associate "%s" role with a CMS Page.',
'advancedeucompliance'), (string)$cms_roles[Advancedeucompliance::LEGAL_REVOCATION]);
return;
}
Configuration::updateValue('AEUC_LABEL_REVOCATION_TOS', true);
} else {
Configuration::updateValue('AEUC_LABEL_REVOCATION_TOS', false);
}
}
protected function processAeucLabelRevocationVP($is_option_active)
{
if ((bool)$is_option_active) {
Configuration::updateValue('AEUC_LABEL_REVOCATION_VP', true);
} else {
Configuration::updateValue('AEUC_LABEL_REVOCATION_VP', false);
}
}
protected function processAeucLabelShippingIncExc($is_option_active)
{
// Check first if LEGAL_SHIP_PAY CMS Role has been set before doing anything here