This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
ShoppingCheckout.module
executable file
·1206 lines (1026 loc) · 34.8 KB
/
ShoppingCheckout.module
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
class ShoppingCheckout extends WireData implements Module, ConfigurableModule
{
public static function getModuleInfo()
{
return array(
'title' => 'Shopping Checkout',
'version' => 003,
'summary' => 'Handles checkout process, order saving etc. Main module for PW Shop',
'singular' => false,
'autoload' => false,
'requires' => array(
"ShoppingCart"
)
);
}
static public function getDefaultFields() {
$required = array(
'visible' => 1,
'required' => 1
);
$visible = array(
'visible' => 1,
'required' => 0
);
$hidden = array(
'visible' => 0,
'required' => 0
);
return array(
'firstname' => array(
'label' => __('First name'),
'defaults' => $required
),
'lastname' => array(
'label' => __('Last name'),
'defaults' => $required
),
'streetaddress' => array(
'label' => __('Street address'),
'defaults' => $required
),
'zip' => array(
'label' => __('Zip'),
'defaults' => $hidden
),
'city' => array(
'label' => __('City'),
'defaults' => $required
),
'country' => array(
'label' => __('Country'),
'defaults' => $hidden
),
'email' => array(
'label' => __('Email'),
'defaults' => $required
),
'phone' => array(
'label' => __('Phone number'),
'defaults' => $visible
),
'greetings' => array(
'label' => __('Details about my order'),
'defaults' => $visible,
'type' => 'textarea'
),
'custom1' => array(
'label' => __('Custom field #1'),
'defaults' => $hidden
),
'custom2' => array(
'label' => __('Custom field #2'),
'defaults' => $hidden
)
);
}
static public function getDefaultData() {
$defaultData = array(
'confirmationUrlSegment' => "confirmation",
'paymentUrlSegment' => "payment",
'completedUrlSegment' => "completed",
'customThankyou' => 0
);
foreach(self::getDefaultFields() as $key => $arr) {
if ($arr['defaults']['visible']) $defaultData[$key] = 1;
$reqKey = $key . 'Required';
if ($arr['defaults']['required']) $defaultData[$reqKey] = 1;
$defaultData['label'] = $arr['label'];
}
return $defaultData;
}
/**
* Populate the default config data
*
* ProcessWire will automatically overwrite it with anything the user has specifically configured.
* This is done in construct() rather than init() because ProcessWire populates config data after
* construct(), but before init().
*
*/
public function __construct() {
foreach(self::getDefaultData() as $key => $value) {
$this->$key = $value;
}
}
public function init()
{
// update from 001 to 002 check added missing sc_phone field
$info = self::getModuleInfo();
$moduleVersion = $info['version'];
if($moduleVersion > 1) {
if (!$this->fields->get('sc_phone')) {
$this->updateAddPhoneField();
}
}
}
public function ready()
{
}
/*
* Simple controller to handle checkout process. Actually 4 steps:
*
* 1. renderInformation()
* 2. renderConfirmation()
* 3. renderPayment()
* 4. renderCompleted()
*
*/
public function renderCheckout()
{
if(!$this->input->urlSegment1) {
$out = $this->renderInformation();
}
else if ($this->input->urlSegment1 == $this->confirmationUrlSegment) {
$out = $this->renderConfirmation();
}
else if ($this->input->urlSegment1 == $this->paymentUrlSegment) {
$out = $this->renderPayment();
}
else if ($this->input->urlSegment1 == $this->completedUrlSegment) {
$out = $this->renderCompleted();
}
return $out;
}
/*
* Returns form to ask customer information before proceeding to confirmation.
* After valid information redirects to confirmation url.
*
*/
public function renderInformation() {
// First we check if there already is unprocessed order with this user
$sid = $this->modules->get("ShoppingCart")->getSession();
$order = $this->pages->get("template=sc-order, sc_session={$sid}, status=unpublished, limit=1, sort=-created");
// If we have already an order, we either go to payment or remove it, depending if customer is already added new products
if ($order->id) {
// Customer has failed order, but has added new products to cart. Remove old one and continue with new
if ($this->modules->ShoppingCart->getNumberOfItems() > 0) {
$this->pages->delete($order, true);
} else {
$this->session->redirect("./{$this->paymentUrlSegment}/");
}
}
if ($this->modules->get('ShoppingCart')->getNumberOfItems() === 0) {
return "<p>" . $this->_("Your shopping cart is empty.") . "</p>";
}
if (!$this->input->post->submit) {
$this->validateInformation(false);
} else {
$orderArray = $this->validateInformation(true);
if($orderArray['valid']) {
$this->session->redirect("./{$this->confirmationUrlSegment}/");
}
}
$out = "<h2>" . $this->_("Please provide your shipping address and contact information.") . "</h2>";
$out .= "<form action='./' method='post'>";
$out .= $this->renderInformationFormItems();
$out .= $this->renderShippingOptions();
$out .= $this->renderPaymentMethods();
$out .= "</form>";
return $out;
}
public function renderCart() {
return $this->modules->ShoppingCart->renderCart();
}
/*
* Returns simple view to review order status before proceeding to payment.
*
*/
public function renderConfirmation() {
if ($this->input->post->submit) {
$this->validateInformation(true); // We still need to validate here
$this->createOrder();
$this->session->redirect("../{$this->paymentUrlSegment}/");
}
$paymentmethod = $this->modules->get($this->session->orderArray['paymentmethod']['value']);
$paymentmethodInfo = $paymentmethod->getModuleInfo();
if (isset($this->session->orderArray['shippingoption']['value'])) {
$shippingOption = $this->modules->get($this->session->orderArray['shippingoption']['value']);
} else {
$shippingOption = NULL;
}
$out = "<h2>" . $this->_('Approve details of your order') . "</h2>";
$out .= $this->modules->ShoppingCart->renderCart(true, $shippingOption);
$out .= "<form action='./' method='post'>";
if ($shippingOption) {
$out .= "<input type='hidden' name='shippingoption' value='". $this->session->orderArray['shippingoption']['value'] ."' />";
}
$out .= $this->renderInformationFormItems(true);
$out .= "<p class='paymentmethod required'><label for='paymentmethod'>" . $this->_("Payment method") . ":<span class='reqstar'>*</span></label><span>". $paymentmethod->title . "</span></p>";
$out .= "<input type='hidden' name='paymentmethod' value='". $this->session->orderArray['paymentmethod']['value'] ."' />";
$out .= "<p><input type='submit' class='submit payorder' name='submit' value='". $this->_("Place your order") . "' /></p>";
$out .= "</form>";
$out .= "<a href='../'>". $this->_("Go back to edit your information") ."</a>";
return $out;
}
/*
* Returns processPayment() method from chosen paymentMethod class.
*
*/
public function renderPayment() {
// Get latest unpublished order from current user
$sid = $this->modules->get("ShoppingCart")->getSession();
$order = $this->pages->get("template=sc-order, sc_session={$sid}, status=unpublished, limit=1, sort=-created");
if ($order->id) {
$paymentmethod = $this->modules->get("{$order->sc_paymentmethod}");
return $paymentmethod->processPayment($order);
} else {
return $this->_("You don't have any unpaid orders.");
}
}
/*
* Simple view after the payent. Renders information about whether the order
* was success or not
*
*/
public function renderCompleted() {
if (!$this->session->orderId) return $this->_("Payment failed, session lost");
$orderId = $this->session->orderId;
$order = $this->pages->get($orderId);
if(!$order->id)
throw new WireExpection("Payment error, order couldn't be found");
if ($order->is(Page::statusUnpublished)) {
$out = "<h2>" . $this->_("Payment cancelled or failed.") . "</h2>";
$out .= "<a href='../{$this->paymentUrlSegment}/'>". $this->_("Try making payment again") ." ($order->sc_price €)</a>";
return $out;
}
// Payment ok, order is done!
else {
$this->calculateNewStock($order);
if ($this->customThankyou) {
$this->session->redirect($this->pages->get($this->customThankyou)->url);
} else {
$this->session->remove('orderId');
$out = "<h2>" . $this->_("Thank you for ordering from us.") . "</h2>";
}
return $out;
}
}
public function renderShippingOptions() {
$out = "";
$shoppingCartModule = $this->modules->get("ShoppingCart");
$shippingModules = $this->modules->find('className^=Shipping');
if ($shippingModules->count() > 0) {
$out .= "<p class='required'><label for='shippingoption'>" . $this->_("Choose shipping:") . " <span class='reqstar'>*</span></label>";
$out .= "<select name='shippingoption'>";
foreach($shippingModules as $module) {
$cost = $module->calculateShippingCost();
$cost = $shoppingCartModule->renderPrice($cost);
if (isset($this->session->orderArray['shippingoption']['value'])) {
$selected = ($this->session->orderArray['shippingoption']['value'] == get_class($module)) ? "selected" : "";
} else {
$selected = '';
}
$out .= "<option $selected value='". get_class($module) ."'>".$module->title." <strong>(+$cost)</strong></option>";
}
$out .= "</select>";
$out .= "</p>";
} else {
$out = "";
}
return $out;
}
public function renderPaymentMethods() {
$out = "";
$paymentModules = $this->modules->find('className^=Payment');
if ($paymentModules->count() > 0) {
$out .= "<p class='required'><label for='paymentmethod'>" . $this->_("Choose payment method:") . " <span class='reqstar'>*</span></label>";
$out .= "<select name='paymentmethod'>";
foreach($paymentModules as $module) {
$info = $module->getModuleInfo();
$selected = ($this->session->orderArray['paymentmethod']['value'] == get_class($module)) ? "selected" : "";
$out .= "<option $selected value='". get_class($module) ."'>".$module->title."</option>";
}
$out .= "</select>";
$out .= "</p>";
$out .= "<p>";
$out .= "<input class='submit' type='submit' name='submit' value='" . $this->_("Proceed to review your order") . "' />";
$out .= "</p>";
} else {
$out .= "<p>No payment methods installed, you should have at least one</p>";
}
return $out;
}
public function calculateNewStock(Page $order) {
foreach($order->children as $orderitem) {
$product = $orderitem->sc_product;
if ($product->id) {
$product->setOutputFormatting(false);
$product->sc_qty = ($product->sc_qty - $orderitem->sc_qty); // TODO: We need to make sure that negative integers are allowed. Currently not..
$product->save();
}
}
}
/*
* Return markup for Information form items
*
* @param boolen $viewOnly false|true if you want to allow customer to edit
* information, then use without viewOnly.
*
* @return string markup
*
*/
public function renderInformationFormItems($viewOnly = false) {
$out = '';
$errors = false;
if (empty($this->session->orderArray['fields'])) {
return;
}
foreach($this->session->orderArray['fields'] as $key => $field) {
$reqKey = $key . 'Required';
if ($this->$key) {
$class = ($this->$reqKey) ? 'required' : '';
$class .= (isset($field['error'])) ? ' error' : '';
if (strlen($field['value']) > 0 || !$viewOnly) {
$out .= "<p class='$key $class'>";
$out .= "<label for='$key'>";
$out .= $field['label'] . ":";
if ($this->$reqKey) $out .= "<span class='reqstar'>*</span>";
$out .= "</label>";
}
if (isset($field['error'])) {
$out .= "<span class='errormsg'>". $field['error'] ."</span>";
$errors = true;
}
if (!$viewOnly) {
switch($field['type']) {
case "textarea":
$out .= "<textarea class='$class' name='$key'>". $field['value'] ."</textarea>";
break;
default:
$out .= "<input type='text' class='$class' name='$key' value='". $field['value'] ."' />";
}
$out .= "</p>";
}
else if (strlen($field['value']) > 0) {
$out .= "<span>". $field['value'] ."</span>";
$out .= "<input type='hidden' name='$key' value='". $field['value'] ."' />";
$out .= "</p>";
}
}
}
if($errors) $out = "<p class='error errormsg'>" . $this->_("The formular contains errors, please check your informations.") . "</p>" . $out;
return $out;
}
/*
* Returns order data as an array
*
* @param boolean $validate If you validate, it will fetch data from post
* and sanitize & validate that. If false, it will just give you orderArray
* with current (should be default or once validated)
*
* @return array keeps information about order information fields (validation
* errors, actual values etc)
*
*/
public function validateInformation($validate = true) {
$orderArray = Array();
$orderArray['valid'] = true;
$orderArray['paymentmethod']['value'] = $this->session->orderArray['paymentmethod']['value'];
$orderArray['paymentmethod']['label'] = $this->_("Payment method");
$fields = self::getDefaultFields();
foreach($fields as $key => $field) {
// If not visible field, don't add to array
if ($this->$key != 1) continue;
// Set default labels
$orderArray['fields'][$key]['label'] = $field['label'];
// Set the form item type and default to input
if (isset($field['type'])) $orderArray['fields'][$key]['type'] = $field['type'];
else $orderArray['fields'][$key]['type'] = 'text';
// Set the custom values for custom field labels (can be set from settings)
if ($key == 'custom1' || $key == 'custom2') {
$customKey = $key . 'CustomLabel';
$orderArray['fields'][$key]['label'] = $this->$customKey;
$customTextareaKey = $key . 'CustomTextarea';
if ($this->$customTextareaKey) $orderArray['fields'][$key]['type'] = 'textarea';
}
// Set empty value for every field
$orderArray['fields'][$key]['value'] = '';
// And if we have value saved on session, update it
if(isset($this->session->orderArray['fields'][$key])) {
$orderArray['fields'][$key]['value'] = $this->session->orderArray['fields'][$key]['value'];
}
}
if ($validate) {
foreach($orderArray['fields'] as $key => $field) {
$reqKey = $key . 'Required';
$field['value'] = $this->input->post->$key;
$orderArray['fields'][$key]['value'] = $this->sanitizer->text($field['value']);
if($key == 'email' && !empty($this->input->post->email)) {
if($this->sanitizer->email($field['value']) == '') {
$orderArray['valid'] = false;
$orderArray['fields'][$key]['error'] = $this->_("Email is not in right format.");
}
}
if ($this->$reqKey && $orderArray['fields'][$key]['value'] == '') {
$orderArray['valid'] = false;
$orderArray['fields'][$key]['error'] = $this->_("This is a required field");
}
}
if ($this->input->post->shippingoption) {
$orderArray['shippingoption']['value'] = $this->sanitizer->fieldName($this->input->post->shippingoption);
}
$orderArray['paymentmethod']['value'] = $this->sanitizer->fieldName($this->input->post->paymentmethod);
$this->session->set('orderArray', $orderArray);
}
if (!isset($this->session->orderArray['valid'])) $this->session->set('orderArray', $orderArray);
return $orderArray;
}
/*
* return true if success
*
* Saves order page and it children products to database and destroys session
* based shopping cart fron ShoppingCart table
*
*/
public function createOrder()
{
if ($this->session->orderArray['valid'] == FALSE) throw new WireException("Non-valid order got through.");
$fields = $this->session->orderArray['fields'];
if(isset($this->session->orderArray['shippingoption']['value'])) {
$fields['shippingoption']['value'] = $this->session->orderArray['shippingoption']['value'];
$shippingOption = $this->modules->get($this->session->orderArray['shippingoption']['value']);
} else {
$fields['shippingoption']['value'] = '';
$shippingOption = NULL;
}
$fields['paymentmethod']['value'] = $this->session->orderArray['paymentmethod']['value'];
$items = $this->modules->ShoppingCart->getCurrentCart();
$orderName = time();
foreach($fields as $field) {
$orderName .= $field;
}
$orderName = md5($orderName);
$order = new Page();
$order->template = $this->templates->get('sc-order');
$order->parent = $this->pages->get("template=admin,name=orders");
$order->title = $this->_("Order: ") . $fields['firstname']['value'] . ' ' . $fields['lastname']['value'];
$order->name = $orderName;
$order->sc_firstname = $fields['firstname']['value'];
$order->sc_lastname = $fields['lastname']['value'];
$order->email = $fields['email']['value'];
$order->sc_streetaddress = $fields['streetaddress']['value'];
$order->sc_city = $fields['city']['value'];
$order->sc_zip = $fields['zip']['value'];
$order->sc_country = $fields['country']['value'];
$order->sc_phone = $fields['phone']['value'];
$order->sc_greetings = $fields['greetings']['value'];
$order->sc_custom1 = $fields['custom1']['value'];
$order->sc_custom2 = $fields['custom2']['value'];
$order->sc_price = $this->modules->ShoppingCart->getTotalSumFromItems($items, $shippingOption);
$order->sc_customer = $this->user;
$order->sc_paymentmethod = $fields['paymentmethod']['value'];
$order->sc_shippingoption = $fields['shippingoption']['value'];
$order->sc_session = $this->modules->ShoppingCart->getSession();
$order->addStatus(Page::statusUnpublished);
// Default status will be first one there is on /shop/settings/statuses/
$admin = $this->pages->get($this->config->adminRootPageID);
$order->sc_status = $this->pages->get("/{$admin->name}/shop/settings/statuses/")->children("check_access=0")->first();
$order->save();
$this->session->set('orderId', $order->id);
foreach ($items as $item) {
$product = $this->pages->get($item->product_id);
// If the product is repeater, we assume it is a product variation. Se let's prepend the product title before variation
if (strpos($product->template->name, "repeater_") === 0) {
$parentProduct = $product->getForPage();
if ($parentProduct->id) $product->title = $parentProduct->title . ": " . $product->title;
}
$p = new Page();
$p->template = $this->templates->get('sc-order-item');
$p->parent = $order;
$p->title = $product->title;
// $p->sc_price = $product->sc_price;
$p->sc_price = $this->modules->ShoppingCart->getProductPrice($item);
$p->sc_qty = $item->qty;
$p->sc_product = $product;
$p->save();
}
if($shippingOption) {
$p = new Page();
$p->template = $this->templates->get('sc-order-item');
$p->parent = $order;
$p->title = $this->_("Shipping costs");
$p->sc_price = $shippingOption->calculateShippingCost();
$p->sc_qty = 1;
$p->save();
}
$this->session->remove('orderArray');
$this->db->query("DELETE FROM ShoppingCart WHERE session_id = '" . $this->modules->ShoppingCart->getSession() . "'");
return ($order->id) ? true : false;
}
static public function getModuleConfigInputfields(Array $data) {
// this is a container for fields, basically like a fieldset
$fields = new InputfieldWrapper();
// since this is a static function, we can't use $this->modules, so get them from the global wire() function
$modules = wire('modules');
$data = array_merge(self::getDefaultData(), $data);
$field = $modules->get("InputfieldPageListSelect");
$field->attr('name', 'customThankyou');
$field->attr('value', $data['customThankyou']);
$field->label = "Thankyou-page where user is redirected after succesful order";
$field->description = 'On that page, you can customize more personal thank you notice. You get order page id from $session->orderId and can display all the details you want from the order.';
$fields->add($field);
$urls = $modules->get("InputfieldFieldset");
$urls->label = "Url segments";
$urls->set('collapsed', Inputfield::collapsedYes);
$field = $modules->get("InputfieldText");
$field->attr('name', 'confirmationUrlSegment');
$field->attr('value', $data['confirmationUrlSegment']);
$field->label = "UrlSegment for confirmation step";
$field->description = "This is only shown in the url. Default: confirmation";
$urls->add($field);
//$fields->add($field);
$field = $modules->get("InputfieldText");
$field->attr('name', 'paymentUrlSegment');
$field->attr('value', $data['paymentUrlSegment']);
$field->label = "UrlSegment for payment step";
$field->description = "This is only shown in the url. Default: payment";
$urls->add($field);
//$fields->add($field);
$field = $modules->get("InputfieldText");
$field->attr('name', 'completedUrlSegment');
$field->attr('value', $data['completedUrlSegment']);
$field->label = "UrlSegment for completed step";
$field->description = "This is only shown in the url. Default: completed";
$urls->add($field);
//$fields->add($field);
$fields->add($urls);
$customer = $modules->get("InputfieldFieldset");
$customer->label = "Information asked from customers";
$customer->set('collapsed', Inputfield::collapsedYes);
// This creates visible / required settings for each information field
foreach(self::getDefaultFields() as $key => $field) {
$fs = $modules->get("InputfieldFieldset");
$fs->label = $field['label'];
//$fs->set('collapsed', Inputfield::collapsedBlank);
//
//if (isset($data[$key])) {
// $checked = $data[$key];
//} else {
// $checked = $informationFields[$key]['defaults']['visible'];
//}
//
$f = $modules->get("InputfieldCheckbox");
$f->name = $key;
$f->label = "Use ". $field['label'] ." field?";
$f->value = 1;
$f->attr('checked', empty($data[$key]) ? '' : 'checked');
$f->set('collapsed', Inputfield::collapsedBlank);
$fs->add($f);
$f = $modules->get("InputfieldCheckbox");
$name = $key . "Required";
$f->name = $name;
$f->label = "Is ". $field['label'] ." required?";
$f->value = 1;
$f->attr('checked', empty($data[$name]) ? '' : 'checked');
$f->set('collapsed', Inputfield::collapsedBlank);
$fs->add($f);
if ($key == 'custom1' || $key == 'custom2') {
$f = $modules->get("InputfieldText");
$name = $key . "CustomLabel";
if (empty($data[$name])) $data[$name] = "Custom label";
$f->name = $name;
$f->label = "Label for this field";
$f->attr('value', $data[$name]);
$f->set('collapsed', Inputfield::collapsedBlank);
$fs->add($f);
$f = $modules->get("InputfieldCheckbox");
$name = $key . "CustomTextarea";
$f->name = $name;
$f->label = "Use textarea instead of regular input?";
$f->value = 1;
$f->attr('checked', empty($data[$name]) ? '' : 'checked');
$f->set('collapsed', Inputfield::collapsedBlank);
$fs->add($f);
}
$customer->add($fs);
}
$fields->add($customer);
return $fields;
}
public function install() {
$admin = $this->templates->get("admin");
$list = $this->modules->get('ProcessList');
$shop = $this->pages->get("template=admin, name=shop, parent=$admin->id");
if(!$shop->id) {
$shop = new Page();
$shop->template = $admin;
$shop->parent = $this->pages->get($this->config->adminRootPageID);
$shop->title = 'Shop';
$shop->name = 'shop';
$shop->addStatus(Page::statusHidden);
//$shop->process = $list;
$shop->save();
}
if ($shop->id) {
$orders = new Page();
$orders->template = $this->templates->get("admin");
$orders->parent = $shop;
$orders->title = 'Orders';
//$orders->name = 'orders';
$orders->process = $this;
$orders->save();
}
//shop/settings/
$settings = $this->pages->get("template=admin, name=settings, parent=$shop->id");
if (!$settings->id) {
$settings = new Page();
$settings->template = $admin;
$settings->parent = $shop;
$settings->title = 'Shop settings';
$settings->name = 'settings';
$settings->save();
}
//shop/settings/statuses/
$statuses = $this->pages->get("template=admin, name=statuses, parent=$settings->id");
if (!$statuses->id) {
$statuses = new Page();
$statuses->template = $admin;
$statuses->parent = $settings;
$statuses->title = 'Order statuses';
$statuses->name = 'statuses';
$statuses->save();
}
//shop/settings/statuses/completed/
$p = $this->pages->get("template=admin, name=completed, parent=$statuses->id");
if(!$p->id) {
$p = new Page();
$p->template = $admin;
$p->parent = $statuses;
$p->title = 'Completed';
$p->name = 'completed';
$p->save();
} else {
$p = '';
}
//shop/settings/statuses/in-progress/
$p = $this->pages->get("template=admin, name=in-progress, parent=$statuses->id");
if(!$p->id) {
$p = new Page();
$p->template = $admin;
$p->parent = $statuses;
$p->title = 'In Progress';
$p->name = 'in-progress';
$p->save();
} else {
$p = '';
}
//shop/settings/statuses/cancelled/
$p = $this->pages->get("template=admin, name=cancelled, parent=$statuses->id");
if(!$p->id) {
$p = new Page();
$p->template = $admin;
$p->parent = $statuses;
$p->title = 'Cancelled';
$p->name = 'cancelled';
$p->save();
} else {
$p = '';
}
$oifg = $this->fieldgroups->get('sc-order-item');
if (!$oifg->id) {
$oifg = new Fieldgroup();
$oifg->name = 'sc-order-item';
$oifg->add($this->fields->get('title'));
$oifg->save();
}
$oi = $this->templates->get('sc-order-item');
if(!$oi->id) {
$oi = new Template();
$oi->name = 'sc-order-item';
$oi->fieldgroup = $oifg;
$oi->pageLabelField = 'title';
$oi->noChildren = 1;
$oi->flags = Template::flagSystem;
$oi->save();
}
$fg = $this->fieldgroups->get('sc-order-item');
if(!$fg->id) {
$fg = new Fieldgroup();
$fg->name = 'sc-order';
$fg->add($this->fields->get('title'));
$fg->save();
}
$t = $this->templates->get('sc-order');
if(!$t->id) {
$t = new Template();
$t->name = 'sc-order';
$t->fieldgroup = $fg;
$t->pageLabelField = 'title';
$t->parentTemplates = array($admin->id);
$t->childTemplates = array($oi->id);
$t->flags = Template::flagSystem;
$t->save();
}
$f = $this->fields->get('sc_price');
if (!$f) {
$f = new Field();
$f->type = $this->modules->get("FieldtypeFloat");
$f->name = 'sc_price';
$f->precision = 2;
$f->label = 'Price of the product';
$f->save();
}
$fg->add($f);
$fg->save();
$oifg->add($f);
$oifg->save();
$f = $this->fields->get('sc_qty');
if (!$f) {
$f = new Field();
$f->type = $this->modules->get("FieldtypeInteger");
$f->name = 'sc_qty';
$f->label = 'Quantity';
$f->save();
}
$oifg->add($f);
$oifg->save();
if (!$this->fields->get('sc_product')) {
$f = new Field();
$f->type = $this->modules->get("FieldtypePage");
$f->name = 'sc_product';
$f->label = 'Product';
$f->derefAsPage = FieldtypePage::derefAsPageOrNullPage;
$f->inputfield = 'InputfieldPageListSelect';
$f->flags = Field::flagSystem | Field::flagPermanent;
$f->save();
}
$oifg->add($f);
$oifg->save();
if (!$this->fields->get('sc_customer')) {
$f = new Field();
$f->type = $this->modules->get("FieldtypePage");
$f->name = 'sc_customer';
$f->label = 'Customer';
$f->inputfield = 'InputfieldPageListSelect';
$f->derefAsPage = FieldtypePage::derefAsPageOrFalse;
$f->parent_id = $this->config->usersPageID;
$f->template_id = $this->config->userTemplateID;
$f->flags = Field::flagSystem | Field::flagPermanent;
$f->save();
}
$fg->add($f);
$fg->save();
if (!$this->fields->get('sc_status')) {
$f = new Field();
$f->type = $this->modules->get("FieldtypePage");
$f->name = 'sc_status';
$f->label = 'Order Status';
$f->inputfield = 'InputfieldSelect';
$f->derefAsPage = FieldtypePage::derefAsPageOrFalse;
$f->parent_id = $statuses->id;
$f->flags = Field::flagSystem | Field::flagPermanent;
$f->save();
}
$fg->add($f);
$fg->save();
if (!$this->fields->get('sc_firstname')) {
$f = new Field();
$f->type = $this->modules->get("FieldtypeText");
$f->name = 'sc_firstname';
$f->label = 'First name';
$f->flags = Field::flagSystem | Field::flagPermanent;
$f->save();
}
$fg->add($f);
$fg->save();
if (!$this->fields->get('sc_lastname')) {
$f = new Field();
$f->type = $this->modules->get("FieldtypeText");
$f->name = 'sc_lastname';
$f->label = 'Last name';
$f->flags = Field::flagSystem | Field::flagPermanent;
$f->save();
}
$fg->add($f);
$fg->save();
if (!$this->fields->get('sc_streetaddress')) {
$f = new Field();
$f->type = $this->modules->get("FieldtypeText");
$f->name = 'sc_streetaddress';
$f->label = 'Street address';
$f->flags = Field::flagSystem | Field::flagPermanent;
$f->save();
}
$fg->add($f);
$fg->save();
if (!$this->fields->get('sc_zip')) {
$f = new Field();
$f->type = $this->modules->get("FieldtypeText");
$f->name = 'sc_zip';
$f->label = 'ZIP / Postal code';
$f->flags = Field::flagSystem | Field::flagPermanent;
$f->save();
}
$fg->add($f);
$fg->save();
if (!$this->fields->get('sc_city')) {
$f = new Field();
$f->type = $this->modules->get("FieldtypeText");
$f->name = 'sc_city';
$f->label = 'City';
$f->flags = Field::flagSystem | Field::flagPermanent;
$f->save();
}
$fg->add($f);
$fg->save();
if (!$this->fields->get('sc_country')) {
$f = new Field();
$f->type = $this->modules->get("FieldtypeText");
$f->name = 'sc_country';
$f->label = 'Country';
$f->flags = Field::flagSystem | Field::flagPermanent;
$f->save();
}
$fg->add($f);
$fg->save();
if (!$this->fields->get('sc_phone')) {
$f = new Field();
$f->type = $this->modules->get("FieldtypeText");
$f->name = 'sc_phone';
$f->label = 'Phone';
$f->flags = Field::flagSystem | Field::flagPermanent;
$f->save();
}
$fg->add($f);
$fg->save();
if (!$this->fields->get('sc_greetings')) {
$f = new Field();