-
Notifications
You must be signed in to change notification settings - Fork 0
/
nlicconnector.php
1107 lines (876 loc) · 43.8 KB
/
nlicconnector.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
/**
* @author Labs64 <[email protected]>
* @license Apache-2.0
* @link http://netlicensing.io
* @copyright 2016 Labs64 NetLicensing
*/
if (!defined('_PS_VERSION_')) exit;
require_once __DIR__ . '/vendor/autoload.php';
class NLicConnector extends Module
{
public $name = 'nlicconnector';
public $tab = 'licensing';
public $version = '1.0';
public $author = 'labs64';
public $need_instance = 1;
public $ps_versions_compliancy = array('min' => '1.5');
const MODULE_PATH = _PS_MODULE_DIR_ . 'nlicconnector';
const TABLE_PRODUCT = 'nlic_product';
const TABLE_CATEGORY = 'nlic_category';
const TABLE_ORDER = 'nlic_order';
protected static $_allowed_license_models = array('Subscription');
public function __construct()
{
$this->displayName = $this->l('NetLicensing Connector');
$this->description = $this->l('NetLicensing Connector is the best way to monetize digital content.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
//prefix for configuration
$this->_prefix = strtoupper($this->name);
//Active bootstrap
$this->bootstrap = true;
/*display error on module page*/
$username = Configuration::get($this->_prefix . '_USERNAME');
$password = Configuration::get($this->_prefix . '_PASSWORD');
if (empty($username) || empty($password)) $this->warning = $this->l('Authorization error. Check username and password.');
parent::__construct();
}
public function install()
{
if (!parent::install()
|| !$this->registerHook('actionProductDelete')
|| !$this->registerHook('actionCategoryDelete')
|| !$this->registerHook('actionOrderStatusPostUpdate')
|| !$this->registerHook('displayAdminOrder')
|| !$this->registerHook('displayOrderConfirmation')
) return false;
$this->_createTables();
$this->_setDefaultConfiguration();
return true;
}
public function uninstall()
{
$this->_deleteTables();
$this->_deleteConfiguration();
return parent::uninstall();
}
public function hookActionProductDelete($params)
{
if (!$this->active) return null;
$product = !empty($params['product']) ? $params['product'] : null;
if (!empty($product->id)) {
$this->includeModel('NLicProduct');
$nlic_product = new NLicProduct($product->id);
$nlic_product->delete();
}
}
public function hookActionCategoryDelete($params)
{
if (!$this->active) return null;
$category = !empty($params['category']) ? $params['category'] : null;
if (!empty($category->id) && !$category->hasMultishopEntries()) {
$this->includeModel('NLicCategory');
$nlic_category = new NLicCategory($category->id);
$nlic_category->delete();
}
}
public function hookActionOrderStatusPostUpdate($params)
{
print_r('asdasd');
if (!$this->active) return null;
$id_order = $params['id_order'];
$this->includeModel('NLicOrder');
$nlic_order = new NLicOrder($id_order);
$status = $params['newOrderStatus'];
$context = new \NetLicensing\Context();
$context->setUsername($this->_getUsername());
$context->setPassword($this->_getPassword());
// if status not paid and nlic_order exist, deactivate license
if (!$status->paid && $nlic_order->id) {
if ($nlic_order->deactivateLicenses($context)) {
$nlic_order->save();
}
}
//if status paid and nlic order exist activate license
if ($status->paid && $nlic_order->id) {
if ($nlic_order->activateLicenses($context)) {
$nlic_order->save();
}
}
//if status paid and nlic_order not exist,create license
if ($status->paid && !$nlic_order->id) {
if ($nlic_order->createLicenses($context)) {
$nlic_order->save();
//send email
$this->_sendOrderLicensesEmail($nlic_order);
}
}
}
public function hookDisplayAdminOrder($params)
{
$messages = '';
$errors = '';
$this->includeModel('NLicOrder');
$nlic_order = new NLicOrder($params['id_order']);
if ($nlic_order->data) {
$submit = Tools::isSubmit('submit_' . $this->name . '_order');
if ($submit) {
$context = new \NetLicensing\Context();
$context->setUsername($this->_getUsername());
$context->setPassword($this->_getPassword());
if (Tools::getValue('submit_' . $this->name . '_order') == 'send_email') {
if ($nlic_order->checkLicensesState($context)) {
$nlic_order->save();
//send email
if ($this->_sendOrderLicensesEmail($nlic_order)) {
$messages .= $this->displayConfirmation($this->l('Email send'));
} else {
$errors .= $this->displayError($this->l('Error sending mail'));
}
}
}
if (Tools::getValue('submit_' . $this->name . '_order') == 'check_state') {
if ($nlic_order->checkLicensesState($context)) {
$nlic_order->save();
$messages .= $this->displayConfirmation($this->l('Licenses status updated'));
} else {
$errors .= $this->displayError($this->l('Failed to check licenses'));
}
}
}
$licenses_count = 0;
$licenses_data = array();
foreach ($nlic_order->data as $data) {
$name = Product::getProductName($data['id_product']);
$image_url = '';
$image_cover = ImageCore::getCover($data['id_product']);
if ($image_cover) {
$image = new Image($image_cover['id_image']);
if ($image->getExistingImgPath()) {
$image_url = _PS_BASE_URL_ . _THEME_PROD_DIR_ . $image->getExistingImgPath() . "-cart_default.jpg";
}
}
$licenses_data[$data['id_product']] = array(
'name' => $name,
'image_url' => $image_url,
);
$licenses_data[$data['id_product']] += $data;
if (empty($data['error']) && !empty($data['licenses'])) $licenses_count += count($data['licenses']);
}
$this->context->controller->addCSS($this->_path . 'views/css/order-form.css', 'all');
$this->context->smarty->assign(array(
'mod' => $this->name,
'action' => $this->context->link->getAdminLink('AdminOrders', true) . '&id_order=' . $params['id_order'] . '&vieworder',
'data' => $licenses_data,
'count' => $licenses_count,
'send_email' => $this->_getSendEmail()
)
);
return $errors . $messages . $this->display(__FILE__, 'views/templates/admin/order/form.tpl');
}
}
public function hookDisplayOrderConfirmation($params)
{
/** @var $order Order*/
$order = isset($params['objOrder']) ? $params['objOrder'] : $params['order'];
//check if order paid
$order_state = new OrderState($order->getCurrentState());
if ($order_state->paid) {
$this->includeModel('NLicOrder');
$nlic_order = new NLicOrder($order->id);
if ($nlic_order->id) {
if (Tools::isSubmit('submit_' . $this->name . '_send_email')) {
$this->_sendOrderLicensesEmail($nlic_order);
}
$this->context->smarty->assign('mod', $this->name);
return $this->display(__FILE__, 'views/templates/front/order/confirmation.tpl');
}
}
}
public function getContent()
{
$this->registerHook('displayAdminOrder');
if (!$this->active) return $this->displayError(sprintf($this->l('%s module is disabled for this store.'), $this->displayName));
$errors = '';
$messages = '';
$submit = Tools::isSubmit('submit_' . $this->name);
if ($submit) {
$submit_values = Tools::getValue('submit_' . $this->name);
switch ($submit_values) {
case 'save':
$username = Tools::getValue($this->name . '_username');
$password = Tools::getValue($this->name . '_password');
if (empty($username)) {
$errors .= $this->displayError($this->l('"Username" field is mandatory'));
}
if (empty($password)) {
$errors .= $this->displayError($this->l('"Password" field is mandatory'));
}
if (!$errors) {
try {
$context = new \NetLicensing\Context();
$context->setUsername($username);
$context->setPassword($password);
\NetLicensing\LicenseTemplateService::getList($context);
//update settings
$this->_updateUsername($username);
$this->_updatePassword($password);
$this->_updateSendEmail(Tools::getValue($this->name . '_send_email', 0));
$messages .= $this->displayConfirmation($this->l('Configuration updated'));
} catch (\NetLicensing\NetLicensingException $e) {
if ($e->getCode() == '401') {
$errors .= $this->displayError($this->l('Authorization error. Check username and password.'));
}
}
}
break;
case 'update_form':
$form = $this->_getImportForm();
break;
}
}
$submit_import = Tools::isSubmit('submit_import_' . $this->name);
if ($submit_import) {
$username = $this->_getUsername();
$password = $this->_getPassword();
try {
//check authorization
$context = new \NetLicensing\Context();
$context->setUsername($username);
$context->setPassword($password);
$tmp_license_templates = \NetLicensing\LicenseTemplateService::getList($context);
$tmp_product_modules = \NetLicensing\ProductModuleService::getList($context);
$license_templates = [];
$product_modules = [];
foreach ($tmp_license_templates as $license_template) {
/**@var $license_template \NetLicensing\LicenseTemplate */
$license_templates[$license_template->getNumber()] = $license_template;
}
foreach ($tmp_product_modules as $product_module) {
/**@var $product_module \NetLicensing\ProductModule */
$product_modules[$product_module->getNumber()] = $product_module;
}
$currency_eur_id = Currency::getIdByIsoCode('EUR');
$currency = new Currency($currency_eur_id);
$sorted_products = $this->_sortProducts($license_templates, $product_modules, $currency);
//delete products
if (!empty($sorted_products['delete']) && Tools::getValue($this->name . '_delete')) {
$deleted_products = 0;
foreach ($sorted_products['delete'] as $nlic_product) {
if ($nlic_product instanceof NLicProduct) {
$product = new Product($nlic_product->id);
$product_delete_state = $product->delete();
if ($product_delete_state) $deleted_products++;
}
}
$messages .= $this->displayConfirmation(sprintf($this->l('%d product(s) deleted'), $deleted_products));
}
//update products
if (!empty($sorted_products['update']) && Tools::getValue($this->name . '_update')) {
$updated_products = 0;
foreach ($sorted_products['update'] as $nlic_product) {
if ($nlic_product instanceof NLicProduct) {
$license_template = $license_templates[$nlic_product->number];
if ($license_template instanceof \NetLicensing\LicenseTemplate) {
$product = new Product($nlic_product->id_product);
$product->name = array((int)Configuration::get('PS_LANG_DEFAULT') => $license_template->getName($license_template->getNumber()));
//if need update price
if (Tools::getValue($this->name . '_update') == 2 && !empty($currency->id) && !empty($currency->active)) {
$product->price = $license_template->getPrice(0) * $currency->conversion_rate;
}
if ($product->save()) $updated_products++;
}
}
}
$messages .= $this->displayConfirmation(sprintf($this->l('%d product(s) updated'), $updated_products));
}
//import products
if (!empty($sorted_products['import']) && Tools::getValue($this->name . '_import')) {
$imported_products = 0;
$this->includeModel('NLicProduct');
$this->includeModel('NLicCategory');
//get exist categories
$categories = NLicCategory::getCategoriesByNumbers(array_keys($product_modules), 'number');
//create products
/** @var $license_template \NetLicensing\LicenseTemplate */
foreach ($sorted_products['import'] as $license_template) {
/** @var $product_module \NetLicensing\ProductModule */
$product_module = $product_modules[$license_template->getProperty('productModuleNumber')];
$product_module_number = $product_module->getNumber();
/** @var $nlic_category NLicCategory */
$nlic_category = !empty($categories[$product_module_number]) ? $categories[$product_module_number] : new NLicCategory();
//create category
if (!$nlic_category->id_category) {
$category = new Category();
$category->name = array((int)Configuration::get('PS_LANG_DEFAULT') => $product_module->getName($product_module->getNumber()));
$category->id_parent = Configuration::get('PS_HOME_CATEGORY');
$category->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') => $this->_createLinkRewrite($product_module->getName() . $product_module->getNumber()));
$category->id_shop_default = $this->context->shop->id;
//set category only for one shop
$_POST['checkBoxShopAsso_category'] = array($this->context->shop->id => 1);
$category->add();
$nlic_category = new NLicCategory();
$nlic_category->id_category = $category->id;
$nlic_category->number = $product_module_number;
$nlic_category->save();
$categories[$product_module_number] = $nlic_category;
}
if ($nlic_category->id_category) {
$product = new Product();
$product->name = array((int)Configuration::get('PS_LANG_DEFAULT') => $license_template->getName($license_template->getNumber()));
$product->link_rewrite = array((int)(Configuration::get('PS_LANG_DEFAULT')) => $this->_createLinkRewrite($license_template->getName() . $license_template->getNumber()));
if (Tools::getValue($this->name . '_import') == 2 && !empty($currency->id) && !empty($currency->active)) {
$product->price = $license_template->getPrice(0) * $currency->conversion_rate;
}
$product->id_category_default = $nlic_category->id_category;
if ($product->add()) {
$this->_createProductImage($product->id, '/views/img/' . strtolower($product_module->getLicensingModel()) . '-' . strtolower($license_template->getLicenseType()) . '.jpg');
$product->addToCategories(array($nlic_category->id_category));
$nlic_product = new NLicProduct();
$nlic_product->id_product = $product->id;
$nlic_product->number = $license_template->getNumber();
$nlic_product->save();
$imported_products++;
}
}
}
$messages .= $this->displayConfirmation(sprintf($this->l('%d product(s) imported'), $imported_products));
}
} catch (\NetLicensing\NetLicensingException $e) {
$errors .= $this->displayError($this->l($e->getMessage()));
}
}
if (empty($form)) {
$form = $this->_getSettingsForm();
}
return $errors . $messages . $form . $this->_getSettingsPage();
}
protected function _sendOrderLicensesEmail(NLicOrder $nlic_order)
{
if (!$this->_getSendEmail() || empty($nlic_order->data)) return false;
$mail_type = Configuration::get('PS_MAIL_TYPE');
$template = 'order_licenses';
//create mails from templates if mails don`t exist
$mail_html_path = self::MODULE_PATH . '/mails/' . $this->context->language->iso_code . '/' . $template . '.html';
$mail_txt_path = self::MODULE_PATH . '/mails/' . $this->context->language->iso_code . '/' . $template . '.txt';
if (!file_exists($mail_html_path) || !file_exists($mail_txt_path)) {
//create dir
mkdir(self::MODULE_PATH . '/mails/' . $this->context->language->iso_code);
$mail_html_common_template_path = self::MODULE_PATH . '/mails/templates/' . $template . '.html';
$mail_txt_common_template_path = self::MODULE_PATH . '/mails/templates/' . $template . '.html';
//create mail html template
$mail_html = fopen($mail_html_path, 'w');
$mail_html_content = file_get_contents($mail_html_common_template_path);
fwrite($mail_html, $mail_html_content);
fclose($mail_html);
//create mail text template
$mail_txt = fopen($mail_txt_path, 'w');
$mail_txt_content = file_get_contents($mail_txt_common_template_path);
fwrite($mail_txt, $mail_txt_content);
fclose($mail_txt);
}
$licenses_data = array();
foreach ($nlic_order->data as $data) {
$name = Product::getProductName($data['id_product']);
$image_url = '';
$image_cover = ImageCore::getCover($data['id_product']);
if ($image_cover) {
$image = new Image($image_cover['id_image']);
if ($image->getExistingImgPath()) {
$image_url = _PS_BASE_URL_ . _THEME_PROD_DIR_ . $image->getExistingImgPath() . "-cart_default.jpg";
}
}
$licenses_data[$data['id_product']] = array(
'name' => $name,
'image_url' => $image_url,
);
$licenses_data[$data['id_product']] += $data;
}
if (!$licenses_data) return false;
$this->context->smarty->assign('data', $licenses_data);
if ($mail_type == Mail::TYPE_BOTH || $mail_type == Mail::TYPE_HTML) {
$vars = array('{licenses}' => $this->display(__FILE__, 'views/templates/admin/order/email-html-table.tpl'));
} else {
$vars = array('{licenses}' => $this->display(__FILE__, 'views/templates/admin/order/email-txt-table.tpl'));
}
if (empty($vars)) return false;
$customer = $nlic_order->getOrder()->getCustomer();
return Mail::Send($this->context->language->id, 'order_licenses', Mail::l('Licenses', $this->context->language->id), $vars, $customer->email, null, null, null, null, null, dirname(__FILE__) . '/mails/', false);
}
protected function _getSettingsForm()
{
//add css
$this->context->controller->addCSS($this->_path . 'views/css/settings-form.css', 'all');
$this->context->smarty->assign(array(
'mod' => $this->name,
'username' => $this->_getUsername(),
'password' => $this->_getPassword(),
'send_email' => $this->_getSendEmail(),
'action' => $this->context->link->getAdminLink('AdminModules', true) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name,
'authorization' => ($this->_getUsername() && $this->_getPassword())
));
return $this->display(__FILE__, 'views/templates/admin/settings/form.tpl');
}
protected function _getImportForm()
{
$errors = '';
$username = $this->_getUsername();
$password = $this->_getPassword();
try {
//check authorization
$context = new \NetLicensing\Context();
$context->setUsername($username);
$context->setPassword($password);
$tmp_license_templates = \NetLicensing\LicenseTemplateService::getList($context);
$tmp_product_modules = \NetLicensing\ProductModuleService::getList($context);
$license_templates = [];
$product_modules = [];
foreach ($tmp_license_templates as $license_template) {
/**@var $license_template \NetLicensing\LicenseTemplate */
$license_templates[$license_template->getNumber()] = $license_template;
}
foreach ($tmp_product_modules as $product_module) {
/**@var $product_module \NetLicensing\ProductModule */
$product_modules[$product_module->getNumber()] = $product_module;
}
$currency_eur_id = Currency::getIdByIsoCode('EUR');
$currency = new Currency($currency_eur_id);
$sorted_products = $this->_sortProducts($license_templates, $product_modules, $currency);
if (!$sorted_products) {
return $this->displayError($this->l('No products to update')) . $this->_getSettingsForm();
}
$description = array();
foreach ($sorted_products as $action => $products) {
$count = count($products);
$description[] = sprintf($this->l('Found %d product(s), for %s'), $count, $action);
}
if (empty($currency->id) || !$currency->active) {
$description[] = $this->l('Can not import prices, the euro currency is not found or not active.');
}
$inputs = array();
if (!empty($sorted_products['import'])) {
$values = array(
array(
'value' => 0,
'label' => $this->l('Skip')
),
array(
'value' => 1,
'label' => $this->l('Import only name')
)
);
if (!empty($currency->id) && !empty($currency->active)) {
$values[] = array(
'value' => 2,
'label' => $this->l('Import name and price'),
);
}
$inputs[] = array(
'type' => 'radio',
'label' => $this->l('Import new product(s)'),
'name' => $this->name . '_import',
'required' => true,
'is_bool' => true,
'hint' => $this->l('Choose which action need to apply to new products.'),
'values' => $values
);
}
if (!empty($sorted_products['update'])) {
$values = array(
array(
'value' => 0,
'label' => $this->l('Skip')
),
array(
'value' => 1,
'label' => $this->l('Import only name')
)
);
if (!empty($currency->id) && !empty($currency->active)) {
$values[] = array(
'value' => 2,
'label' => $this->l('Import name and price'),
);
}
$inputs[] = array(
'type' => 'radio',
'label' => $this->l('Update exist product(s)'),
'name' => $this->name . '_update',
'required' => true,
'is_bool' => true,
'hint' => $this->l('Choose which action need to apply to exist products.'),
'values' => $values
);
}
if (!empty($sorted_products['delete'])) {
$fields_form['form']['input'][] = array(
'type' => 'switch',
'label' => $this->l('Delete disconnected product(s)'),
'name' => $this->name . '_delete',
'required' => false,
'is_bool' => true,
'values' => array(
array(
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'value' => 0,
'label' => $this->l('Disabled')
)
),
);
}
$fields_form['form'] = array(
'legend' => array(
'title' => $this->l('Update products'),
'icon' => 'icon-download'
),
'description' => implode('</br>', $description),
'input' => $inputs,
'submit' => array(
'title' => $this->l('Update'),
'icon' => 'process-icon-download',
'name' => 'submit_import_' . $this->name,
)
);
//set default value
$field_values = array();
if (!empty($sorted_products['import'])) $field_values[$this->name . '_import'] = Tools::getValue($this->name . '_import', 1);
if (!empty($sorted_products['update'])) $field_values[$this->name . '_update'] = Tools::getValue($this->name . '_update', 0);
if (!empty($sorted_products['delete'])) $field_values[$this->name . '_delete'] = Tools::getValue($this->name . '_delete', 0);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->identifier = $this->identifier;
$helper->submit_action = 'submit_' . $this->name;
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $field_values,
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
return $helper->generateForm(array($fields_form));
} catch (\NetLicensing\NetLicensingException $e) {
$errors .= $this->displayError($this->l($e->getMessage()));
}
return implode('', $errors) . $this->_getSettingsForm();
}
protected function _getSettingsPage()
{
//add css
$this->context->controller->addCSS($this->_path . 'views/css/settings-form.css', 'all');
$this->context->smarty->assign('mod', $this->name);
return $this->display(__FILE__, 'views/templates/admin/settings/page.tpl');
}
protected function _sortProducts($license_templates, $product_modules, $currency)
{
//sort products
$sorted_products = array();
//check if license template are allowed
$allowed_license_templates = array();
$tmp_license_templates = $license_templates;
$tmp_product_modules = $product_modules;
$license_templates = [];
$product_modules = [];
foreach ($tmp_license_templates as $license_template) {
/**@var $license_template \NetLicensing\LicenseTemplate */
$license_templates[$license_template->getNumber()] = $license_template;
}
foreach ($tmp_product_modules as $product_module) {
/**@var $product_module \NetLicensing\ProductModule */
$product_modules[$product_module->getNumber()] = $product_module;
}
foreach ($license_templates as $license_template) {
/** @var $license_template \NetLicensing\LicenseTemplate */
if ($license_template->getHidden()) continue;
/** @var $product_module \NetLicensing\ProductModule */
$product_module = $product_modules[$license_template->getProperty('productModuleNumber')];
if (!in_array($product_module->getLicensingModel(), self::$_allowed_license_models)) continue;
$allowed_license_templates[$license_template->getNumber()] = $license_template;
}
if ($allowed_license_templates) {
$this->includeModel('NLicProduct');
$nlic_products = NLicProduct::getProducts();
$tmp_license_templates = $allowed_license_templates;
$allowed_lt_numbers = array_keys($allowed_license_templates);
//get product for delete and product for update
foreach ($nlic_products as $nlic_product) {
if ($nlic_product instanceof NLicProduct) {
if (!in_array($nlic_product->number, $allowed_lt_numbers)) {
$sorted_products['delete'][$nlic_product->number] = $nlic_product;
unset($tmp_license_templates[$nlic_product->number]);
} else {
$product = new Product($nlic_product->id_product);
$license_template = $allowed_license_templates[$nlic_product->number];
$price = (!empty($currency->id) && !empty($currency->active)) ? $product->price / $currency->conversion_rate : $license_template->getPrice();
if ($product->name[(int)Configuration::get('PS_LANG_DEFAULT')] != $license_template->getName() || $price != $license_template->getPrice()) {
$sorted_products['update'][$nlic_product->number] = $nlic_product;
}
unset($tmp_license_templates[$nlic_product->number]);
}
}
}
if ($tmp_license_templates) {
$sorted_products['import'] = $tmp_license_templates;
}
}
return $sorted_products;
}
protected function _createLinkRewrite($text)
{
// replace non letter or digits by -
$link_rewrite = preg_replace('~[^\pL\d]+~u', '-', $text);
// transliterate
$link_rewrite = iconv('utf-8', 'us-ascii//TRANSLIT', $link_rewrite);
// remove unwanted characters
$link_rewrite = preg_replace('~[^-\w]+~', '', $link_rewrite);
// trim
$link_rewrite = trim($link_rewrite, '-');
// remove duplicate -
$link_rewrite = preg_replace('~-+~', '-', $link_rewrite);
// lowercase
$link_rewrite = strtolower($link_rewrite);
return $link_rewrite;
}
protected function _getUsername()
{
return Configuration::get($this->_prefix . '_USERNAME');
}
protected function _getPassword($decrypt = TRUE)
{
$password = Configuration::get($this->_prefix . '_PASSWORD');
if ($decrypt && !empty($password)) {
$encrypt_key = Configuration::getGlobalValue($this->_prefix . '_ENCRYPTION_KEY');
$password = $this->_mc_decrypt($password, $encrypt_key);
}
return $password;
}
protected function _getSendEmail()
{
return Configuration::get($this->_prefix . '_SEND_EMAIL');
}
protected function _updateUsername($username)
{
Configuration::updateValue($this->_prefix . '_USERNAME', $username);
}
protected function _updatePassword($password)
{
$encrypt_key = Configuration::getGlobalValue($this->_prefix . '_ENCRYPTION_KEY');
Configuration::updateValue($this->_prefix . '_PASSWORD', $this->_mc_encrypt($password, $encrypt_key));
}
protected function _updateSendEmail($state)
{
Configuration::updateValue($this->_prefix . '_SEND_EMAIL', $state);
}
protected function _createTables()
{
$sql = 'CREATE TABLE IF NOT EXISTS ' . _DB_PREFIX_ . self::TABLE_PRODUCT . '(
id_product INT(10) NOT NULL,
id_shop INT(10) NOT NULL,
number TEXT NOT NULL,
PRIMARY KEY (id_product),
INDEX shop_product (id_product, id_shop)
) ENGINE=INNODB CHARSET=utf8 COLLATE=utf8_general_ci;';
if (!Db::getInstance()->Execute($sql)) return false;
$sql = 'CREATE TABLE IF NOT EXISTS ' . _DB_PREFIX_ . self::TABLE_CATEGORY . '(
id_category INT(10) NOT NULL,
number TEXT NOT NULL,
PRIMARY KEY (id_category)
) ENGINE=INNODB CHARSET=utf8 COLLATE=utf8_general_ci;';
if (!Db::getInstance()->Execute($sql)) return false;
$sql = 'CREATE TABLE IF NOT EXISTS ' . _DB_PREFIX_ . self::TABLE_ORDER . '(
id_order INT(10) NOT NULL,
id_shop INT(10) NOT NULL,
data LONGBLOB,
PRIMARY KEY (id_order),
INDEX order_shop (id_order,id_shop)
) ENGINE=INNODB CHARSET=utf8 COLLATE=utf8_general_ci;';
if (!Db::getInstance()->Execute($sql)) return false;
}
protected function _deleteTables()
{
Db::getInstance()->Execute("DROP TABLE " . _DB_PREFIX_ . self::TABLE_PRODUCT . ";");
Db::getInstance()->Execute("DROP TABLE " . _DB_PREFIX_ . self::TABLE_CATEGORY . ";");
Db::getInstance()->Execute("DROP TABLE " . _DB_PREFIX_ . self::TABLE_ORDER . ";");
}
protected function _setDefaultConfiguration()
{
Configuration::updateGlobalValue($this->_prefix . '_ENCRYPTION_KEY', hash('sha256', uniqid()));
}
protected function _deleteConfiguration()
{
Configuration::deleteByName($this->_prefix . '_USERNAME');
Configuration::deleteByName($this->_prefix . '_PASSWORD');
Configuration::deleteByName($this->_prefix . '_SEND_EMAIL');
Configuration::deleteByName($this->_prefix . '_ENCRYPTION_KEY');
}
protected function _createLink($path, $text = '', $attributes = array())
{
if ($path == 'AdminLink') $path = $this->context->link->getAdminLink('AdminModules', true) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
foreach ($attributes as $attribute => &$data) {
$data = implode(' ', (array)$data);
$data = $attribute . '="' . $data . '"';
}
$attributes = $attributes ? ' ' . implode(' ', $attributes) : '';
return '<a href="' . $path . '" ' . $attributes . '>' . $this->l($text) . '</a>';
}
public static function getAllowedLicenseModels()
{
return self::$_allowed_license_models;
}
public static function includeModel($name)
{
if (is_file(self::MODULE_PATH . '/classes/' . $name . '.php')) require_once self::MODULE_PATH . '/classes/' . $name . '.php';
}
protected function _mc_encrypt($encrypt, $key)
{
$encrypt = serialize($encrypt);
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM);
$key = pack('H*', $key);
$mac = hash_hmac('sha256', $encrypt, substr(bin2hex($key), -32));
$passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $encrypt . $mac, MCRYPT_MODE_CBC, $iv);
$encoded = base64_encode($passcrypt) . '|' . base64_encode($iv);
return $encoded;
}
protected function _mc_decrypt($decrypt, $key)
{
$decrypt = explode('|', $decrypt . '|');
$decoded = base64_decode($decrypt[0]);
$iv = base64_decode($decrypt[1]);
if (strlen($iv) !== mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)) {
return false;
}
$key = pack('H*', $key);
$decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_CBC, $iv));
$mac = substr($decrypted, -64);
$decrypted = substr($decrypted, 0, -64);
$calcmac = hash_hmac('sha256', $decrypted, substr(bin2hex($key), -32));
if ($calcmac !== $mac) {
return false;
}
$decrypted = unserialize($decrypted);
return $decrypted;
}
protected function _toCamelCase($str, $capitaliseFirstChar = false)
{
if ($capitaliseFirstChar) {
$str[0] = strtoupper($str[0]);
}
return preg_replace('/_([a-z])/e', "strtoupper('\\1')", $str);
}
protected function _fromCamelCase($str)
{
$str[0] = strtolower($str[0]);
return preg_replace('/([A-Z])/e', "'_' . strtolower('\\1')", $str);
}
protected function _createProductImage($id_product, $path)
{
$image_path = self::MODULE_PATH . '/' . $path;
if (is_file($image_path)) {
$image_url = _PS_BASE_URL_ . '/' . $this->_path . $path;
$shops = Shop::getShops(true, null, true);
$image = new Image();
$image->id_product = $id_product;
$image->cover = true;
if (($image->validateFields(false, true)) === true && ($image->validateFieldsLang(false, true)) === true && $image->save()) {
$image->associateTo($shops);
if (!self::copyImg($id_product, $image->id, $image_url, 'products', false)) {
$image->delete();