forked from e107inc/vstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvstore.class.php
4181 lines (3591 loc) · 142 KB
/
vstore.class.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
/**
* e107 website system
*
* Copyright (C) 2008-2013 e107 Inc (e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
* Vstore shopping cart plugin
*
* @author CaMerOn <[email protected]>
* @author Achim Ennenbach <[email protected]>
* @copyright 2019 e107inc
*/
e107::css('vstore', 'vstore.css');
e107::js('vstore', 'js/vstore.js');
require_once('vendor/autoload.php');
use Omnipay\Omnipay;
use DvK\Vat\Rates\Exceptions\Exception;
class vstore
{
protected $cartId = null;
protected $sc;
protected $perPage = 9;
protected $from = 0;
protected $categories = array(); // all categories;
protected $categorySEF = array();
protected $item = array(); // current item.
protected $captionBase = "Vstore";
protected $captionCategories = "Product Brands";
protected $captionOutOfStock = "Out of Stock";
protected $get = array();
protected $post = array();
protected $categoriesTotal = 0;
protected $action = array();
protected $pref = array();
protected $parentData = array();
protected $currency = 'USD';
protected $order = null;
private $html_invoice = null;
/**
* Array with the available currencies
* 'key' is the 3-char ISO 4217 code of the currency
* 'title' is the name of the currency
* 'symbol' is the currency sign (usually only 1 letter)
* 'glyph' If no symbol is available, use the 'glyph' key to define a fontawesome or glyphicon symbol
* Use only 'symbol' of 'glyph', not both!
*
* @var array
*/
protected static $currencies = array(
'USD' => array(
'title' => 'US Dollars',
'symbol' => '$'
),
'CAN' => array(
'title' => 'Canadian Dollars',
'symbol' => '$'
),
'EUR' => array(
'title' => 'Euros',
'symbol' => '€'
),
'GBP' => array(
'title' => 'GB Pounds',
'symbol' => '£'
),
'BTC' => array(
'title' => 'Bitcoin',
'glyph' => 'fa-btc'
),
);
/**
* Array with the available gateways and their corresponding icons
*
* @var array The available gateways
*/
protected static $gateways = array(
'paypal' => array('title' => 'Paypal', 'icon' => 'fa-paypal'),
'paypal_rest' => array('title' => 'Paypal', 'icon' => 'fa-paypal'),
'mollie' => array('title' => 'Mollie', 'icon' => 'fa-laptop'),
// 'amazon' => array('title'=> 'Amazon', 'icon'=>'fa-amazon'),
// 'coinbase' => array('title'=> 'Bitcoin', 'icon'=>'fa-btc'),
'bank_transfer' => array('title' => 'Bank Transfer', 'icon' => 'fa-bank'),
);
/**
* Array with the payment methods of Mollie
*
* 'key' The payment method prefixed with mollie_
* 'title' The name of the payment method
* 'icon' The icon of the payment methods (usually an svg in the images folder)
*
* All values are required when adding a new payment method!
*
* @var array The available payment methods of the Mollie gateway
*/
protected static $mollie_payment_methods = array(
'mollie_bancontact' => array(
'title' => 'Bancontact',
'icon' => 'images/bancontact.svg'
),
'mollie_banktransfer' => array(
'title' => 'SEPA Bank transfer',
'icon' => 'images/sepa.svg'
),
'mollie_belfius' => array(
'title' => 'Belfius Direct Net',
'icon' => 'images/belfius.svg'
),
// 'mollie_bitcoin' => array(
// 'title' => 'Bitcoin',
// 'icon' => 'bitcoin'
// ),
'mollie_creditcard' => array(
'title' => 'Creditcard',
'icon' => 'images/amex.svg'
),
'mollie_directdebit' => array(
'title' => 'SEPA Direct debit',
'icon' => 'images/sepa.svg'
),
'mollie_eps' => array(
'title' => 'EPS',
'icon' => 'images/eps.svg'
),
'mollie_giftcard' => array(
'title' => 'Gift Cards',
'icon' => 'images/giftcards.svg'
),
'mollie_giropay' => array(
'title' => 'Giropay',
'icon' => 'images/giropay.svg'
),
'mollie_ideal' => array(
'title' => 'iDeal',
'icon' => 'images/ideal.svg'
),
'mollie_inghomepay' => array(
'title' => 'Ing Homepay',
'icon' => 'images/inghomepay.svg'
),
'mollie_kbc' => array(
'title' => 'Kbc Payment Button',
'icon' => 'images/kbc.svg'
),
'mollie_klarnapaylater' => array(
'title' => 'Klarna Pay Later',
'icon' => 'images/klarnapaylater.svg'
),
'mollie_klarnasliceit' => array(
'title' => 'Klarna Slice it',
'icon' => 'images/klarnasliceit.svg'
),
'mollie_paypal' => array(
'title' => 'Paypal',
'icon' => 'images/paypal.svg'
),
'mollie_paysafecard' => array(
'title' => 'Paysafecard',
'icon' => 'images/paysafecard.svg'
),
// 'mollie_przelewy24' => array(
// 'title' => 'Przelewy24',
// 'icon' => 'images/klarnasliceit.svg'
// ),
'mollie_sofort' => array(
'title' => 'SOFORT Banking',
'icon' => 'images/sofort.svg'
),
);
protected static $status = array(
'N' => 'New',
'P' => 'Processing',
'H' => 'On Hold',
'C' => 'Completed',
'X' => 'Cancelled',
'R' => 'Refunded'
);
/**
* @var array Array with email types
*/
protected static $emailTypes = array(
'default' => 'Order confirmation',
'completed' => 'Order completed',
'cancelled' => 'Order cancelled',
'refunded' => 'Order refunded'
);
/**
* @var array Array with shipping fieldnames
*/
protected static $shippingFields = array(
'firstname',
'lastname',
'phone',
'company',
'address',
'city',
'state',
'zip',
'country',
'notes' // Shipping notes
);
/**
* @var array Array with customer fieldnames
*/
protected static $customerFields = array(
'title',
'firstname',
'lastname',
'company',
'vat_id',
'taxcode',
'address',
'city',
'state',
'zip',
'country',
'email',
'phone',
'fax',
'additional_fields',
// 'notes' // Customer notes are for internal use only
);
/**
* @var array Array with official tay classes
*/
protected static $official_tax_classes = array(
'none',
'reduced',
'reduced1',
'reduced2',
'super_reduced',
'standard',
'parking'
);
/**
* @var array This array keeps track of the item vars types during inventory checks
*/
protected static $itemVarsTypes = array();
public function __construct()
{
$sql = e107::getDb();
$this->cartId = $this->getCartId();
/** @var vstore_shortcodes sc */
$this->sc = e107::getScParser()->getScObject('vstore_shortcodes', 'vstore', false);
$this->get = $_GET;
$this->post = $_POST;
$this->pref = e107::pref('vstore');
$this->order = e107::getSingleton('vstore_order', e_PLUGIN . 'vstore/inc/vstore_order.class.php');
$this->currency = vartrue($this->pref['currency'], 'USD');
if (!empty($this->pref['caption']) && !empty($this->pref['caption'][e_LANGUAGE])) {
$this->captionBase = $this->pref['caption'][e_LANGUAGE];
}
if (!empty($this->pref['additional_fields'])) {
foreach ($this->pref['additional_fields'] as $k => $v) {
if (vartrue($v['active'], false)) {
static::$customerFields[] = 'add_field' . $k;
}
}
}
if (!empty($this->pref['caption_categories']) && !empty($this->pref['caption_categories'][e_LANGUAGE])) {
$this->captionCategories = $this->pref['caption_categories'][e_LANGUAGE];
//e107::getDebug()->log("caption: ".$this->captionCategories);
}
if (!empty($this->pref['caption_outofstock']) && !empty($this->pref['caption_outofstock'][e_LANGUAGE])) {
$this->captionOutOfStock = $this->pref['caption_outofstock'][e_LANGUAGE];
$this->sc->captionOutOfStock = $this->captionOutOfStock;
}
if (deftrue('e_DEBUG_VSTORE')) {
e107::getDebug()->log($this->pref);
e107::getDebug()->log("CartID:" . $this->cartId);
}
// get all category data.
$count = 0;
$query = 'SELECT * FROM #vstore_cat WHERE cat_class IN (' . USERCLASS_LIST . ') ';
if ($data = $sql->retrieve($query, true)) {
foreach ($data as $row) {
$id = $row['cat_id'];
$this->categories[$id] = $row;
$sef = vartrue($row['cat_sef'], '--undefined--');
$this->categorySEF[$sef] = $id;
if (empty($row['cat_parent'])) {
$count++;
}
}
}
$this->categoriesTotal = $count;
$active = array();
$tp = e107::getParser();
foreach (self::$gateways as $k => $icon) {
$key = $k . "_active";
if (!empty($this->pref[$key])) {
if (self::isMollie($k)) {
$paymentMethods = array_keys($this->pref['mollie_payment_methods']);
foreach ($paymentMethods as $method) {
$active[$method] = $this->getMolliePaymentMethodIcon($method);
}
} else {
$active[$k] = $this->getGatewayIcon($k);
}
// get gateway prefs.
foreach ($this->pref as $key => $v) {
if (strpos($key, $k) === 0) {
$newkey = substr($key, (strlen($k) + 1));
$this->pref[$k][$newkey] = $v;
}
}
}
}
if (deftrue('e_DEBUG_VSTORE') && getperms('0')) {
e107::getDebug()->log($this->pref);
}
$this->active = $active;
}
public function init()
{
// print_a($this->get);
if (!empty($this->get['catsef'])) {
$sef = $this->get['catsef'];
$this->get['cat'] = vartrue($this->categorySEF[$sef], 0);
}
// Check for ajax requests and process them first
$this->process_ajax();
// In case this is not an ajax request continue with processing
$this->process();
}
/**
* Get status string from key or (if key is empty) complete status array
*
* @param string $key
* @return array/string
*/
public static function getStatus($key = null)
{
if (!empty($key)) {
return self::$status[$key];
}
return self::$status;
}
/**
* Get email type string from key or (if key is empty) complete email type array
*
* @param string $key
* @return array/string
*/
public static function getEmailTypes($type = null)
{
if (!empty($type)) {
return self::$emailTypes[$type];
}
return self::$emailTypes;
}
/**
* Return the official tax classes array
*
* @return array
*/
public static function getTaxClasses()
{
return self::$official_tax_classes;
}
/**
* Return the shippingFields array
*
* @return array
*/
public static function getShippingFields()
{
return self::$shippingFields;
}
/**
* Return the customerFields array
*
* @return array
*/
public static function getCustomerFields()
{
return self::$customerFields;
}
public static function getCurrencies()
{
return self::$currencies;
}
public static function getCurrencyTitle($currency)
{
return vartrue(self::$currencies[$currency]['title'], '');
}
public static function getCurrencySymbol($currency, $size = '1x')
{
$size = vartrue($size, '1x');
if (isset(self::$currencies[$currency]['symbol'])) {
if (preg_match('/[0-9.]+x/', $size)) {
// convert '1x' sizes to '1em'
$size = floatval($size) . 'em';
}
return '<span style="font-size: ' . $size . ';">' . self::$currencies[$currency]['symbol'] . '</span>';
} elseif (isset(self::$currencies[$currency]['glyph'])) {
return e107::getParser()->toGlyph(self::$currencies[$currency]['glyph'], array('size' => $size));
}
return '';
}
/**
* Return the $mollie_payment_methods or a single entry
*
* @param string $method Name of the payment method or null
*
* @return array|string
*/
public static function getMolliePaymentMethods($method = null)
{
if (!empty($method)) {
return self::$mollie_payment_methods[$method];
}
return self::$mollie_payment_methods;
}
/**
* Return the icon for the given gateway
*
* @param string $type Payment method
* @param string $size 5x (1x, 2x, 3x, 4x, 5x)
*
* @return string Icon of the payment method
*/
public static function getMolliePaymentMethodIcon($type = '', $size = '5x')
{
$size = vartrue($size, '5x');
if (preg_match('^[0-9.]+x$', $size)) {
// convert '1x' sizes to '1em'
$size = floatval($size) . 'em';
}
$size = vartrue(intval($size), 1) . 'em';
$text = (!empty(self::$mollie_payment_methods[$type])
? e_PLUGIN_ABS . 'vstore/' . self::$mollie_payment_methods[$type]['icon']
: '');
return e107::getParser()->toImage($text, array(
'style' => "width: " . $size . "; height: " . $size . ";",
'class' => 'vstore-mollie-payment-icon img-circle'));
}
/**
* Return the title for the given gateway
*
* @param string $type Payment method
*
* @return string Title of the payment method
*/
public static function getMolliePaymentMethodTitle($type = '')
{
return !empty(self::$mollie_payment_methods[$type]) ? self::$mollie_payment_methods[$type]['title'] : '';
}
/**
* Return if gateway type is Mollie
*
* @param string $type
*
* @return bool true if is mollie gateway
*/
public static function isMollie($type = '')
{
return substr($type, 0, 6) == 'mollie';
}
/**
* Parse and return Mollie Error message
*
* @param $str JSON string
*
* @return string Error message
*/
private function getMollieErrorMessage($str)
{
if ($json = e107::unserialize($str)) {
if (vartrue($json['status'], '') == 'canceled') {
return 'You have canceled your payment, but your cart is still available for further reference.';
} elseif (vartrue($json['status'], '') == 'failed') {
return 'Your payment failed for some reason, but your cart is still available for further reference.';
} elseif (vartrue($json['detail'], '')) {
return $json['detail'];
}
return 'Generic error';
}
return $str;
}
/**
* Handle & process all ajax requests
*
* @return void
*/
private function process_ajax()
{
if (e_AJAX_REQUEST) {
$js = e107::getJshelper();
$js->_reset();
// Process only ajax requests
if ($this->get['add']) {
// Add item to cart
$itemid = $this->get['add'];
$itemvars = $this->get['itemvar'];
if (!$this->addToCart($itemid, $itemvars)) {
$msg = e107::getMessage()->render('vstore');
ob_clean();
$js->sendTextResponse($msg);
exit;
} else {
include_once 'e_sitelink.php';
$sl = new vstore_sitelink();
$msg = $sl->storeCart();
}
ob_clean();
$js->sendTextResponse('ok ' . $msg);
exit;
}
if (!empty($this->get['reset'])) {
// Reset cart
$this->resetCart();
include_once 'e_sitelink.php';
$sl = new vstore_sitelink();
$msg = $sl->storeCart();
ob_clean();
$js->sendTextResponse('ok ' . $msg);
exit;
}
if (!empty($this->get['refresh'])) {
// Refresh cart menu
include_once 'e_sitelink.php';
$sl = new vstore_sitelink();
$msg = $sl->storeCart();
ob_clean();
$js->sendTextResponse('ok ' . $msg);
exit;
}
// Order processing
if (isset($this->post['order']) && intval($this->post['id']) > 0 && ADMIN) {
$this->order->load($this->post['id']);
if ($this->post['order'] === 'refund') {
// Refund a payment, $order_refund contains the orderId, access only for Admins!
$status = 'R';
$result = $this->order->refundOrder();
} elseif ($this->post['order'] === 'complete') {
// Complete an order, $order_complete contains the orderId, access only for Admins!
$status = 'C';
$result = $this->order->setOrderStatus($status);
} elseif ($this->post['order'] === 'cancel') {
// Cancel an order, $order_cancel contains the orderId, access only for Admins!
$status = 'X';
$result = $this->order->setOrderStatus($status);
} elseif ($this->post['order'] === 'process') {
// Cancel an order, $order_cancel contains the orderId, access only for Admins!
$status = 'P';
$result = $this->order->setOrderStatus($status);
} elseif ($this->post['order'] === 'hold') {
// Hold an order, $order_cancel contains the orderId, access only for Admins!
$status = 'H';
$result = $this->order->setOrderStatus($status);
} else {
// In case that none of the above has handled the ajax request
// (which shouldn't happen) just exit
exit;
}
ob_clean();
// send out the results to the browser
// will be used in a javascript alert() box
if ($result === true) {
// all went well
$js->sendTextResponse(
EMESSLAN_TITLE_SUCCESS . "\n" .
e107::getParser()->lanVars(
'Order updated to "[x]"',
self::getStatus($status)
)
);
} else {
// some error occured
$js->sendTextResponse(
($this->order->getLastError()
? $this->order->getLastError()
: EMESSLAN_TITLE_ERROR . "\n" . e107::getParser()->lanVars(
'Order couldn\'t be updated to "[x]"',
self::getStatus($status)
)
)
);
}
}
// In case that none of the above has handled the ajax request
// (which shouldn't happen) just exit
exit;
}
}
/**
* Handle & process all non-ajax requests
*
* @return void
*/
private function process()
{
if (!empty($this->get['reset'])) {
$this->resetCart();
}
if ($this->post['mode'] == 'confirmed') {
$this->setMode($this->post['mode']);
if (empty($this->getGatewayType(true))) {
e107::getMessage()->addError('No payment method selected!', 'vstore');
return;
} elseif (empty($this->getCheckoutData())) {
e107::getMessage()->addError('No items to checkout!', 'vstore');
return;
} elseif (empty($this->getCustomerData(true))) {
e107::getMessage()->addError('No customer data set!', 'vstore');
return;
} elseif (empty($this->getShippingData(true))) {
e107::getMessage()->addError('No shipping data set!', 'vstore');
return;
} else {
if (!empty(trim($this->post['ship']['notes']))) {
// validate/filter order notes
$tmp = $this->getShippingData(true);
$tmp['notes'] = trim(strip_tags($this->post['ship']['notes']));
$this->setShippingData($tmp);
}
$this->processGateway('init');
return;
}
}
if ($this->get['mode'] == 'return') {
$this->processGateway('return');
return;
}
if (varset($this->post['cartQty'])) {
$this->updateCart('modify', $this->post['cartQty'], $this->post['cartVars']);
}
if (varset($this->post['cartRemove'])) {
$this->updateCart('remove', $this->post['cartRemove']);
}
if (!empty($this->get['add'])) {
if (!e_AJAX_REQUEST) {
$this->addToCart($this->get['add'], $this->get['itemvar']);
}
}
// Cancel order
if (isset($this->post['cancel_order']) && intval($this->post['cancel_order']) > 0 && USER) {
$check = e107::getDb()->retrieve(
'vstore_orders',
'*',
'order_id=' . intval($this->post['cancel_order']) . ' AND order_e107_user = ' . USERID
);
if ($check) {
$log = e107::unserialize($check['order_log']);
$log[] = array(
'datestamp' => time(),
'user_id' => USERID,
'user_name' => USERNAME,
'text' => 'Order cancelled by user'
);
$update = array(
'data' => array(
'order_status' => 'X',
'order_log' => e107::serialize($log, 'json')
),
'WHERE' => 'order_id=' . intval($this->post['cancel_order'])
);
e107::getDb()->update('vstore_orders', $update);
$this->emailCustomerOnStatusChange($check['order_id']);
e107::redirect(e107::url('vstore', 'dashboard', array('dash' => 'orders')));
exit;
}
}
// Save address(es)
if (isset($this->post['edit_address']) && intval($this->post['edit_address']) > 0 && USER) {
$check = e107::getDb()->retrieve('vstore_customer', '*', 'cust_e107_user = ' . USERID);
if ($check) {
$save = true;
if (intval($this->post['edit_address']) === 1 && !empty($this->post['cust']['firstname'])) {
// Billing address
$fields = $this->pref['additional_fields'];
$add = array();
foreach ($fields as $key => $value) {
if (isset($this->post['cust']['add_field' . $key])) {
$add['add_field' . $key] = array(
'caption' => strip_tags($value['caption'][e_LANGUAGE]),
'value' => ($value['type'] == 'text'
? $this->post['cust']['add_field' . $key]
: ($this->post['cust']['add_field' . $key] ? 'X' : '-'))
);
unset($this->post['cust']['add_field' . $key]);
}
}
$this->post['cust']['additional_fields'] = e107::serialize($add, 'json');
foreach ($this->getCustomerFields() as $k) {
if (isset($this->post['cust'][$k])) {
$update['data']['cust_' . $k] = $this->post['cust'][$k];
}
}
} elseif (intval($this->post['edit_address']) === 2 && !empty($this->post['ship']['firstname'])) {
// Shipping address
$data = array();
foreach ($this->getShippingFields() as $k) {
if (isset($this->post['ship'][$k])) {
$data[$k] = $this->post['ship'][$k];
}
}
$update['data']['cust_shipping'] = e107::serialize($data, 'json');
} else {
$save = false;
e107::getMessage()->addError('Something went wrong! Unable to save changes!', 'vstore', true);
}
if ($save) {
$update['WHERE'] = 'cust_e107_user = ' . USERID;
$result = e107::getDb()->update('vstore_customer', $update);
e107::getMessage()->addSuccess('Changes successfully saved!', 'vstore', true);
e107::redirect(e107::url('vstore', 'dashboard', array('dash' => 'addresses')));
exit;
}
}
}
}
/**
* Render a form in case the current user is not logged in
* for him to decide if he wants to buy as guest, create a
* new user account or to login with an existing user account
*
* @return string
*/
private function renderGuestForm()
{
$tp = e107::getParser();
$template = e107::getTemplate('vstore', 'vstore', 'customer');
$text = $tp->parseTemplate($template['guest'], true, $this->sc);
return $text;
}
/**
* Render customer (billing) information form
*
* @return string the form
*/
private function renderCustomerForm()
{
$frm = e107::getForm();
$tp = e107::getParser();
if (!isset($this->post['cust']['firstname'])) {
// load saved shipping data and assign to variables
$data = $this->getCustomerData();
$fields = $this->getCustomerFields();
$prefix = (isset($data['cust_firstname']) ? 'cust_' : '');
foreach ($fields as $field) {
if ($field != 'additional_fields') {
$this->post['cust'][$field] = varset($data[$prefix . $field], null);
}
}
}
$template = e107::getTemplate('vstore', 'vstore', 'customer');
/**
* Additional checkout fields
* Start
*/
$addFieldActive = 0;
foreach ($this->pref['additional_fields'] as $k => $v) {
// Check if additional fields are enabled
if (vartrue($v['active'], false)) {
$addFieldActive++;
}
}
if ($addFieldActive > 0) {
// If any additional fields are enabled
// add active fields to form
foreach ($this->pref['additional_fields'] as $k => $v) {
if (vartrue($v['active'], false)) {
$fieldid = 'add_field' . $k;
$fieldname = 'cust[' . $fieldid . ']';
if (isset($this->post['cust'][$fieldid])) {
$fieldvalue = $this->post['cust'][$fieldid];
} else {
$fieldvalue = $this->post['cust']['additional_fields']['value'][$fieldid];
}
if ($v['type'] == 'text') {
// Textboxes
$field = $frm->text(
$fieldname,
$fieldvalue,
100,
array(
'placeholder' => varset($v['placeholder'][e_LANGUAGE], ''),
'required' => ($v['required'] ? 1 : 0)
)
);
} elseif ($v['type'] == 'checkbox') {
// Checkboxes
$field = '<div class="form-control">' .
$frm->checkbox(
$fieldname,
1,
0,
array('required' => ($v['required'] ? 1 : 0))
);
if (vartrue($v['placeholder'])) {
$field .= ' <label for="' .
$frm->name2id($fieldname) . '-1" class="text-muted"> ' .
$tp->toHTML($v['placeholder'][e_LANGUAGE]) . '</label>';
}
$field .= '</div>';
}
$this->sc->addVars(array(
'fieldname' => $fieldname,
'fieldcaption' => $tp->toHTML(varset($v['caption'][e_LANGUAGE], 'Additional field ' . $k)),
'field' => $field,
'fieldcount' => $addFieldActive,
'fieldrequired' => $v['required']
));
$this->post['cust']['add'][$fieldid] = $tp->parseTemplate(
$template['additional']['item'],
true,
$this->sc
);
}
}
}
$this->sc->setVars($this->post);
$text = $tp->parseTemplate($template['header'], true, $this->sc);
/**
* Additional checkout fields
* End
*/
// if (!USER) {
// $text .= e107::getParser()->parseTemplate($template['guest'], true, $this->sc);
// }
return $text;
}
/**
* Render customer shipping information form
*
* @return string the form
*/
private function renderShippingForm()
{
$tp = e107::getParser();
if (!isset($this->post['ship']['firstname'])) {
$prefix = '';
// load saved shipping data and assign to variables
$data = $this->getShippingData();
if (empty($data) || empty($data['firstname'])) {
$data = $this->getCustomerData(true);
$prefix = isset($data['cust_firstname']) ? 'cust_' : '';
}
$fields = $this->getShippingFields();
foreach ($fields as $field) {
$this->post['ship'][$field] = varset($data[$prefix . $field], null);
}
}
$template = e107::getTemplate('vstore', 'vstore', 'shipping');
$this->sc->setVars($this->post);
$text = $tp->parseTemplate($template['header'], true, $this->sc);
return $text;
}
/**
* Render the confirm order page to review a summary of the order before confirming the order
*
* @return string
*/
private function renderConfirmOrder()
{
$cust = $this->getCustomerData(true);
$ship = $this->getShippingData(true);
$data = $this->prepareCheckoutData($this->getCheckoutData(), true);
$template = e107::getTemplate('vstore', 'vstore', 'orderconfirm');
$data['cust'] = $cust;
$data['ship'] = $ship;
$data['order_pay_gateway'] = $this->getGatewayType(true);
$this->sc->setVars($data);
$data['billing_address'] = e107::getParser()->parseTemplate($template['billing'], true, $this->sc);
if ($data['order_use_shipping'] == 1) {
$data['shipping_address'] = e107::getParser()->parseTemplate($template['shipping'], true, $this->sc);
}
$this->sc->setVars($data);
$text = e107::getParser()->parseTemplate($template['main'], true, $this->sc);
return $text;
}
private function getMode()
{
return vartrue($this->get['mode']);
}
private function setMode($mode)
{
$this->get['mode'] = $mode;
}
/**