/
EcommerceController.php
1853 lines (1567 loc) · 62.1 KB
/
EcommerceController.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
/**
* class EcommerceController
*
* @package frog
* @subpackage plugin.ecommerce
* @author Chris Earls <cgearls@gmail.com>
* @since Frog version 0.9.5
*/
// include models
include('models/Product.php');
include('models/ProductType.php');
include('models/ProductVendor.php');
include('models/ProductImage.php');
include('models/ProductVariant.php');
include('models/ProductFile.php');
include('models/ProductVideo.php');
include('models/Order.php');
include('models/Collection.php');
// helpers
use_helper('Pagination');
class EcommerceController extends PluginController {
public function __construct() {
$this->setLayout('backend');
$this->assignToLayout('sidebar', new View('../../plugins/ecommerce/views/sidebar'));
}
public function index() {
$this->dashboard();
}
/**
* Dashboard
*/
public function dashboard() {
global $__FROG_CONN__;
$sql = 'select l.message, l.created_on, u.name from ecommerce_log l left join user u on u.id = l.user_id order by created_on desc limit 20';
$stmt = $__FROG_CONN__->prepare($sql);
$stmt->execute();
$logs = $stmt->fetchAll();
$this->display('ecommerce/views/dashboard', array(
'logs' => $logs
));
}
function _insert_log($message)
{
$log_data = array("message"=>$message,"user_id"=>AuthUser::getId());
$record = Record::insert('ecommerce_log', $log_data);
}
/**
* Documentation
*/
public function documentation() {
$this->display('ecommerce/views/documentation');
}
/**
* Settings
*/
function settings() {
$this->display('ecommerce/views/settings');
}
/**
* Marketing / Promos
*/
function marketing() {
$this->display('ecommerce/views/marketing');
}
public function get_promo($code) {
global $__FROG_CONN__;
$sql = 'select discount, is_percent from ecommerce_promos where code = \''.mysql_escape_string($code).'\' and curdate() between start_date and end_date limit 0,1';
$stmt = $__FROG_CONN__->prepare($sql);
$stmt->execute();
$promo = $stmt->fetchObject();
return $promo;
}
/**
* Order
*/
public function order() {
$this->order_index();
}
public function order_index() {
$page = !empty($_GET['page']) ? $_GET['page']: 1;
$keywords = !empty($_GET['keywords']) ? $_GET['keywords']: '';
$per_page = 15;
$orders_total = Order::find(array(
'where' => 'first_name like \'%'.$keywords.'%\''
));
$orders = Order::find(array(
'limit' => $per_page,
'offset' => ($page-1)*($per_page),
'where' => 'first_name like \'%'.$keywords.'%\' or last_name like \'%'.$keywords.'%\''
));
$pagination = new Pagination(array(
'base_url' => '/'.ADMIN_DIR.'/plugin/ecommerce/order/?keywords='.$keywords.'&page=',
'total_rows' => count($orders_total),
'per_page' => $per_page,
'num_links' => 4,
'cur_page' => $page
));
$this->display('ecommerce/views/orders/index', array(
'orders' => $orders,
'pagination' => $pagination
));
}
public function order_show($id) {
$order = Record::findByIdFrom('Order', $id);
global $__FROG_CONN__;
$sql = 'select * from ecommerce_order_variant where order_id = '.$id.';';
$stmt = $__FROG_CONN__->prepare($sql);
$stmt->execute();
$variants = $stmt->fetchAll();
$this->display('ecommerce/views/orders/show', array(
'order' => $order,
'variants' => $variants
));
}
public function order_fulfilled($id) {
$order = Record::findByIdFrom('Order', $id);
$order_data = array("fulfilled"=>1);
$order->setFromData($order_data);
if ($order->save())
echo 'success';
}
public function order_not_fulfilled($id) {
$order = Record::findByIdFrom('Order', $id);
$order_data = array("fulfilled"=>0);
$order->setFromData($order_data);
if ($order->save())
echo 'success';
}
/**
* Checkout
*/
public function checkout() {
$output = '';
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$output = $this->_checkout_step_1();
}
else {
//valid helper
include'classes/valid.class.php';
$valid = new Valid();
//validation class
include'classes/validation.class.php';
$step = $_POST['step'];
if ($step == '1') {
//step 1 validation
$post = new Validation($_POST['order']);
$post->add_rules('first_name','required');
$post->add_rules('last_name','required');
$post->add_rules('company','required');
$post->add_rules('address','required');
$post->add_rules('city','required');
$post->add_rules('state','required');
$post->add_rules('country','required');
$post->add_rules('zip','required');
$post->add_rules('phone','required', array($valid, 'phone'));
$post->add_rules('email', 'required', array($valid, 'email'));
if (!isset($_POST['billing_is_shipping'])) {
$post->add_rules('ship_first_name','required');
$post->add_rules('ship_last_name','required');
$post->add_rules('ship_company','required');
$post->add_rules('ship_address','required');
$post->add_rules('ship_city','required');
$post->add_rules('ship_state','required');
$post->add_rules('ship_country','required');
$post->add_rules('ship_zip','required');
$post->add_rules('ship_phone','required', array($valid, 'phone'));
}
$post->pre_filter('trim');
//success, go to step 2
if ($post->validate()) {
//save order data
$_SESSION['order'] = $_POST['order'];
$output = $this->_checkout_step_2();
}
//errors, go back to step 1
else {
$errors = $post->errors();
$output = $this->_checkout_step_1($_POST,$errors);
}
}
elseif ($step == '2') {
//step 2 validation
$post = new Validation($_POST['order']);
$post->add_rules('cc_name','required');
$post->add_rules('cc_type','required');
$post->add_rules('cc_number','required', array($valid, 'credit_card'));
$post->add_rules('cc_cvv','required', 'length[3,4]', array($valid, 'digit'));
$post->add_rules('cc_exp_month','required');
$post->add_rules('cc_exp_year','required');
if(isset($_POST['order']['cc_exp_month']) && isset($_POST['order']['cc_exp_year']))
$post->add_callbacks('cc_exp_year', array($this,'_validate_cc_exp_date'));
$post->pre_filter('trim');
if ($post->validate()) {
$cart = new Cart('shopping_cart');
//order data array
$order_arr = array_merge($_SESSION['order'],$_POST['order']);
$full_cc_number = $order_arr['cc_number'];
$order_arr['cc_number'] = substr($order_arr['cc_number'], -4);
$order_arr['promo_discount'] = $cart->getDiscount($order_arr['promo_code']);
$order_arr['subtotal'] = $cart->getTotal();
$order_arr['tax'] = $cart->getTax();
//process payment
include'merchants/firstdata.class.php';
$merchant = new FirstData();
//billing info
$merchant->name = $order_arr['first_name'].' '.$order_arr['last_name'];
$merchant->company = $order_arr['company'];
$merchant->address = $order_arr['address'];
$merchant->address2 = $order_arr['address2'];
$merchant->city = $order_arr['city'];
$merchant->state = $order_arr['state'];
$merchant->country = $order_arr['country'];
$merchant->phone = $order_arr['phone'];
$merchant->fax = $order_arr['fax'];
$merchant->email = $order_arr['email'];
$merchant->zip = $order_arr['zip'];
//shipping info
$merchant->ship_name = $order_arr['ship_first_name'].' '.$order_arr['ship_last_name'];
$merchant->ship_address = $order_arr['ship_address'];
$merchant->ship_saddress2 = $order_arr['ship_address2'];
$merchant->ship_city = $order_arr['ship_city'];
$merchant->ship_state = $order_arr['ship_state'];
$merchant->ship_country = $order_arr['ship_country'];
$merchant->ship_zip = $order_arr['ship_zip'];
//payment info
$merchant->cc_number = $full_cc_number;
$merchant->cc_exp_month = $order_arr['cc_exp_month'];
$merchant->cc_exp_year = substr($order_arr['cc_exp_year'],-2);
$merchant->cc_cvv = $order_arr['cc_cvv'];
$merchant->subtotal = $order_arr['subtotal'];
$merchant->shipping = 0;
$merchant->tax = $order_arr['tax'];
$merchant->total = ($order_arr['subtotal']+$order_arr['tax'])-$order_arr['promo_discount'];
// set to GOOD for test or LIVE
$merchant->result = 'LIVE';
$merchant_success = false;
$result = $merchant->sale();
if ($result['r_approved'] == "APPROVED")
$merchant_success = true;
//merchant error
if (!$merchant_success) {
$errors = $post->errors();
$this->set_flash($result['r_error'],'error');
$output = $this->_checkout_step_2($_POST,$errors);
}
//merchant success
else {
//save order to database
$record = Record::insert('ecommerce_order', $order_arr);
$order_id = Record::lastInsertId();
//save order items to database
foreach ( $cart->getItems() as $variant_id=>$quantity ) :
//get variant data
$variant = Record::findByIdFrom('ProductVariant', $variant_id);
$variant->order_id = $order_id;
$variant->quantity = $quantity;
$variant_arr = (array)$variant;
//remove unneeded fields
unset($variant_arr['id']);
unset($variant_arr['created_on']);
unset($variant_arr['updated_on']);
unset($variant_arr['position']);
//insert
$record = Record::insert('ecommerce_order_variant', $variant_arr);
endforeach;
//save log
$this->_insert_log('Order <a href="'.get_url('plugin/ecommerce/order_show/'.$order_id).'">'.$order_id.'</a> was placed.');
//send emails to client and buyer
$this->_send_order_email('info@emedamerica.com',$order_id,$order_arr,$variant_arr);
$this->_send_order_email($order_arr['email'],$order_id,$order_arr,$variant_arr);
//success
$this->set_flash('Thank you for your order. You will receive a confirmation email shortly.','success');
//clear cart and order session
unset($_SESSION['order']);
unset($_SESSION['Cart']);
}
}
//errors, go back to step 2
else {
$errors = $post->errors();
$output = $this->_checkout_step_2($_POST,$errors);
}
}
}
return $output;
}
function set_flash($msg,$type) {
$output = '<div class="'.$type.'">'.$msg.'</div>';
$_SESSION['ecommerce_flash'] = $output;
}
function get_flash() {
return isset($_SESSION['ecommerce_flash']) ? $_SESSION['ecommerce_flash'] : null;
}
// checkout step 1 view
function _checkout_step_1($values=null,$errors=null) {
$cart = new Cart('shopping_cart');
$ship_checked = 'checked="checked"';
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !isset($values['billing_is_shipping']))
$ship_checked = '';
$output = '
<div id="checkout">
<div id="summary"><h3 class="price">$'.number_format($cart->getTotal(),2).'</h3></div>
<p id="step">step 1 of 2</p>
<form action="" method="post">
<input type="hidden" name="step" value="1" />
<h3>Billing Information</h3>
<table border="0">
<tr>
<td class="label"><label for="first_name">First Name:</label></td>
<td class="field"><input type="text" name="order[first_name]" size="30" value="'.htmlentities($values['order']['first_name']).'" maxlength="50" /> '.$this->_field_error($errors,'first_name').'</td>
</tr>
<tr>
<td class="label"><label for="last_name">Last Name:</label></td>
<td class="field"><input type="text" name="order[last_name]" size="30" value="'.htmlentities($values['order']['last_name']).'" maxlength="50" /> '.$this->_field_error($errors,'last_name').'</td>
</tr>
<tr>
<td class="label"><label for="company">Company:</label></td>
<td class="field"><input type="text" name="order[company]" size="30" value="'.htmlentities($values['order']['company']).'" maxlength="50" /> '.$this->_field_error($errors,'company').'</td>
</tr>
<tr>
<td class="label"><label for="address">Address:</label></td>
<td class="field"><input type="text" name="order[address]" size="30" value="'.htmlentities($values['order']['address']).'" maxlength="50" /> '.$this->_field_error($errors,'address').'</td>
</tr>
<tr>
<td class="label"><label for="address2">Address 2:</label></td>
<td class="field"><input type="text" name="order[address2]" size="30" value="'.htmlentities($values['order']['address2']).'" maxlength="50" /> '.$this->_field_error($errors,'address2').'</td>
</tr>
<tr>
<td class="label"><label for="city">City:</label></td>
<td class="field"><input type="text" name="order[city]" size="30" value="'.htmlentities($values['order']['city']).'" maxlength="50" /> '.$this->_field_error($errors,'city').'</td>
</tr>
<tr>
<td class="label"><label for="state">State:</label></td>
<td class="field"><select name="order[state]">'.str_replace('value="'.$values['order']['state'].'"','value="'.$values['order']['state'].'" selected="selected"',$this->_state_select()).'</select> '.$this->_field_error($errors,'state').'</td>
</tr>
<tr>
<td class="label"><label for="country">Country:</label></td>
<td class="field"><input type="text" name="order[country]" size="30" value="'.htmlentities($values['order']['country']).'" maxlength="50" /> '.$this->_field_error($errors,'country').'</td>
</tr>
<tr>
<td class="label"><label for="zip">Zip:</label></td>
<td class="field"><input type="text" name="order[zip]" size="30" value="'.htmlentities($values['order']['zip']).'" maxlength="50" /> '.$this->_field_error($errors,'zip').'</td>
</tr>
<tr>
<td class="label"><label for="email">Email:</label></td>
<td class="field"><input type="text" name="order[email]" size="30" value="'.htmlentities($values['order']['email']).'" maxlength="50" /> '.$this->_field_error($errors,'email').'</td>
</tr>
<tr>
<td class="label"><label for="phone">Phone:</label></td>
<td class="field"><input type="text" name="order[phone]" size="30" value="'.htmlentities($values['order']['phone']).'" maxlength="50" /> (XXX-XXX-XXXX) '.$this->_field_error($errors,'phone').'</td>
</tr>
<tr>
<td class="label"><label for="fax">Fax:</label></td>
<td class="field"><input type="text" name="order[fax]" size="30" value="'.htmlentities($values['order']['fax']).'" maxlength="50" /> '.$this->_field_error($errors,'fax').'</td>
</tr>
</table>
<p>
<input type="checkbox" '.$ship_checked.' name="billing_is_shipping" id="shipping-toggle" value="1" />
Ship items to the above billing address
</p>
<h3>Shipping Information</h3>
<p id="shipping-same" class="notice">Items will be shipped to your billing address.</p>
<div id="shipping_form" style="display: none;">
<table border="0" id="checkout">
<tr>
<td class="label"><label for="ship_first_name">First Name:</label></td>
<td class="field"><input type="text" name="order[ship_first_name]" size="30" value="'.htmlentities($values['order']['ship_first_name']).'" maxlength="50" /> '.$this->_field_error($errors,'ship_first_name').'</td>
</tr>
<tr>
<td class="label"><label for="ship_last_name">Last Name:</label></td>
<td class="field"><input type="text" name="order[ship_last_name]" size="30" value="'.htmlentities($values['order']['ship_last_name']).'" maxlength="50" /> '.$this->_field_error($errors,'ship_last_name').'</td>
</tr>
<tr>
<td class="label"><label for="ship_company">Company:</label></td>
<td class="field"><input type="text" name="order[ship_company]" size="30" value="'.htmlentities($values['order']['ship_company']).'" maxlength="50" /> '.$this->_field_error($errors,'ship_company').'</td>
</tr>
<tr>
<td class="label"><label for="ship_address">Address:</label></td>
<td class="field"><input type="text" name="order[ship_address]" size="30" value="'.htmlentities($values['order']['ship_address']).'" maxlength="50" /> '.$this->_field_error($errors,'ship_address').'</td>
</tr>
<tr>
<td class="label"><label for="ship_address2">Address 2:</label></td>
<td class="field"><input type="text" name="order[ship_address2]" size="30" value="'.htmlentities($values['order']['ship_address2']).'" maxlength="50" /> '.$this->_field_error($errors,'ship_address2').'</td>
</tr>
<tr>
<td class="label"><label for="ship_city">City:</label></td>
<td class="field"><input type="text" name="order[ship_city]" size="30" value="'.htmlentities($values['order']['ship_city']).'" maxlength="50" /> '.$this->_field_error($errors,'ship_city').'</td>
</tr>
<tr>
<td class="label"><label for="ship_state">State:</label></td>
<td class="field"><select name="order[ship_state]">'.str_replace('value="'.$values['order']['ship_state'].'"','value="'.$values['order']['state'].'" selected="selected"',$this->_state_select()).'</select> '.$this->_field_error($errors,'ship_state').'</td>
</tr>
<tr>
<td class="label"><label for="ship_country">Country:</label></td>
<td class="field"><input type="text" name="order[ship_country]" size="30" value="'.htmlentities($values['order']['ship_country']).'" maxlength="50" /> '.$this->_field_error($errors,'ship_country').'</td>
</tr>
<tr>
<td class="label"><label for="ship_zip">Zip:</label></td>
<td class="field"><input type="text" name="order[ship_zip]" size="30" value="'.htmlentities($values['order']['ship_zip']).'" maxlength="50" /> '.$this->_field_error($errors,'ship_zip').'</td>
</tr>
<tr>
<td class="label"><label for="ship_phone">Phone:</label></td>
<td class="field"><input type="text" name="order[ship_phone]" size="30" value="'.htmlentities($values['order']['ship_phone']).'" maxlength="50" /> (XXX-XXX-XXXX) '.$this->_field_error($errors,'ship_phone').'</td>
</tr>
</table>
</div>
<div id="promo">
<table border="0" id="checkout">
<tr>
<td class="label"><label for="promo_code">Promo Code:</label></td>
<td class="field"><input type="text" name="order[promo_code]" size="25" value="'.htmlentities($values['order']['promo_code']).'" maxlength="25" /> '.$this->_field_error($errors,'promo_code').'</td>
</tr>
</table>
</div>
<hr />
<p>
<input class="button" type="submit" value="Continue to next step" /> or <a href="/products/types">continue shopping</a>
</p>
</form>
</div>
';
return $output;
}
//checkout step 2 view
function _checkout_step_2($values=null,$errors=null) {
$cart = new Cart('shopping_cart');
//get promo discount
$discount_text = '';
if (isset($_SESSION['order']['promo_code'])) {
$discount = $cart->getDiscount($_SESSION['order']['promo_code']);
if($discount > 0)
$discount_text = '...including -$'.number_format($discount,2).' discount.';
}
else
$discount = 0;
//get total
$total = number_format(($cart->getTotal()+$cart->getTax())-$discount,2);
//get taxes
$taxes = number_format($cart->getTax(),2);
$output = '
<div id="checkout">
<div id="summary"><h3 class="price">$'.$total.'</h3>
<i>...including FREE shipping.<br />
...including $'.$taxes.' taxes.<br />
'.$discount_text.'</i></div>
<p id="step">step 2 of 2</p>
<form action="" method="post">
<input type="hidden" name="step" value="2" />
<h3>Shipping Information</h3>
<p>Shipping is <strong>FREE</strong></p>
<h3>Payment Information</h3>
<table border="0" id="checkout">
<tr>
<td class="label" nowrap="1"><label for="cc_name">Name:</label></td>
<td class="field"><input type="text" name="order[cc_name]" size="30" value="'.htmlentities($values['order']['cc_name']).'" /> '.$this->_field_error($errors,'cc_name').'</td>
</tr>
<tr>
<td class="label" nowrap="1"><label for="cc_type">Card Type:</label></td>
<td class="field">
<select name="order[cc_type]">
<option value="">-- Select Card --</option>
'.str_replace('value="'.$values['order']['cc_type'].'"','value="'.$values['order']['cc_type'].'" selected="selected"',$this->_cc_type_options()).'
</select> '.$this->_field_error($errors,'cc_type').'
</td>
</tr>
<tr>
<td class="label" nowrap="1"><label for="cc_number">Card Number:</label></td>
<td class="field"><input type="text" name="order[cc_number]" size="30" value="'.htmlentities($values['order']['cc_number']).'" /> '.$this->_field_error($errors,'cc_number').'</td>
</tr>
<tr>
<td class="label" nowrap="1"><label for="cc_cvv">CVV:</label></td>
<td class="field"><input type="text" name="order[cc_cvv]" size="4" value="'.htmlentities($values['order']['cc_cvv']).'" /> '.$this->_field_error($errors,'cc_cvv').' <a href="javascript:void(0);" onclick="$(\'cvv\').toggle();">what is this?</a>
<div id="cvv" style="display: none;">
<p>For MasterCard or Visa, it\'s the last three digits in the signature area on the back of your card. For American Express, it\'s the four digits on the front of the card.
</div>
</td>
</tr>
<tr>
<td class="label" nowrap="1"><label for="cc_exp_month">Expiration Date:</label></td>
<td class="field">
<select name="order[cc_exp_month]">
<option value="">-- Month --</option>
'.str_replace('value="'.$values['order']['cc_exp_month'].'"','value="'.$values['order']['cc_exp_month'].'" selected="selected"',$this->_cc_exp_month_options()).'
</select> '.$this->_field_error($errors,'cc_exp_month').'
<select name="order[cc_exp_year]">
<option value="">-- Year --</option>
'.str_replace('value="'.$values['order']['cc_exp_year'].'"','value="'.$values['order']['cc_exp_year'].'" selected="selected"',$this->_cc_exp_year_options()).'
</select> '.$this->_field_error($errors,'cc_exp_year').'
</td>
</tr>
</table>
<hr />
<p>
<input class="button" type="submit" value="Complete my purchase" /> or
<a href="/products/types">cancel, and continue shopping</a>
</p>
</form>
</div>
';
return $output;
}
function _send_order_email($to_email,$order_id,$order_arr,$variant_arr) {
require_once('Mail.php');
$subject = 'eMed America Online Order #'.$order_id;
$headers = "From: info@emedamerica.com\r\nReply-To: info@emedamerica.com";
$msg_line = "-------------------------------------------\r\n";
$msg = "Thank you for shopping with us! Your order details are below.\r\n\r\n";
//billing info
$msg .= $msg_line."Billing Information\r\n".$msg_line;
$msg .= $order_arr['first_name']." ".$order_arr['last_name']."\r\n";
$msg .= $order_arr['address']."\r\n";
if ($order_arr['address2'])
$msg .= $order_arr['address2']."\r\n";
$msg .= $order_arr['city'].", ".$order_arr['state']." ".$order_arr['zip']."\r\n";
$msg .= "Phone: ".$order_arr['phone']."\r\n";
if ($order_arr['fax'])
$msg .= "Fax: ".$order_arr['fax']."\r\n";
//shipping info
if ($order_arr['ship_first_name']) {
$msg .= $msg_line."Shipping Information\r\n".$msg_line;
$msg .= $order_arr['ship_first_name']." ".$order_arr['ship_last_name']."\r\n";
$msg .= $order_arr['ship_address']."\r\n";
if ($order_arr['ship_address2'])
$msg .= $order_arr['ship_address2']."\r\n";
$msg .= $order_arr['ship_city'].", ".$order_arr['ship_state']." ".$order_arr['ship_zip']."\r\n";
$msg .= "Phone: ".$order_arr['ship_phone']."\r\n";
}
//payment info
$subtotal = $order_arr['subtotal'];
$shipping = 0;
$tax = $order_arr['tax'];
$discount = $order_arr['promo_discount'];
$total = ($subtotal + $shipping + $tax) - $discount;
$msg .= "\r\n".$msg_line."Payment Information\r\n".$msg_line;
$msg .= "Card Name: ".$order_arr['cc_name']."\r\n";
$msg .= "Card Type: ".$order_arr['cc_type']."\r\n";
$msg .= "Card Number: ".$order_arr['cc_number']."\r\n";
$msg .= "Card Expiration: ".$order_arr['cc_exp_month']."/".$order_arr['cc_exp_year']."\r\n\r\n";
$msg .= "Subtotal: $".number_format($subtotal,2)."\r\n";
$msg .= "Shipping: $".number_format($shipping,2)."\r\n";
$msg .= "Tax: $".number_format($tax,2)."\r\n";
if ($order_arr['promo_discount']) {
$msg .= "Discount: $".number_format($discount,2)."\r\n";
}
$msg .= $msg_line."Total: $".number_format($total,2)."\r\n".$msg_line;
//item information
$msg .= "\r\n".$msg_line."Item Information\r\n".$msg_line;
global $__FROG_CONN__;
$sql = 'select * from ecommerce_order_variant where order_id = '.$order_id.';';
$stmt = $__FROG_CONN__->prepare($sql);
$stmt->execute();
$variants = $stmt->fetchAll();
if ($variants) {
foreach ($variants as $variant) :
$product = Product::findById($variant['product_id']);
$msg .= $product->title.' ('.$variant['title'].') - $'.$variant['price'].' x '.$variant['quantity'].' = $'.number_format($variant['price']*$variant['quantity'],2)."\r\n";
endforeach;
}
$mail = Mail::factory('smtp', array('host'=>'mail.myhost.com', 'port'=>25, 'auth'=>'login', 'username'=>'username', 'password'=>'password'));
$headers = array('To'=>$to_email, 'From'=>'from@email.com', 'Subject'=>$subject, 'Reply-To'=>'replyto@email.com', 'Return-Path'=>'return@email.com');
if ($mail->send($to_email, $headers, $msg))
return true;
else
return false;
}
function _validate_cc_exp_date(Validation $array, $field) {
if ((int)$_POST['order']['cc_exp_month'] < date('m') && (int)$_POST['order']['cc_exp_year'] <= date('Y')) {
$array->add_error($field, 'expired_card');
}
}
function _field_error($errors,$field_name) {
if(isset($errors[$field_name]))
return '<span class="field_error">'.$this->_error_message($errors[$field_name]).'</span>';
else
return null;
}
function _error_message($error) {
$messages = array
(
'required' => 'Field cannot be blank.',
'alpha' => 'Only alphabetic characters are allowed.',
'phone' => 'Enter a valid phone number.',
'email' => 'Enter a valid email address.',
'length' => 'Input isn\'t right length.',
'credit_card' => 'Enter a valid number.',
'digit' => 'Enter numeric characters only.',
'expired_card' => 'Your card has expired.'
);
return $messages[$error];
}
function _state_select() {
$output = '
<option value="">-- Select State --</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>';
return $output;
}
function _cc_type_options() {
$output = '
<option value="Visa">Visa</option>
<option value="MasterCard">MasterCard</option>
<option value="Amex">American Express</option>
<option value="Discover">Discover</option>
';
return $output;
}
function _cc_exp_month_options() {
$output ='
<option value="01" >January</option>
<option value="02" >February</option>
<option value="03" >March</option>
<option value="04" >April</option>
<option value="05" >May</option>
<option value="06" >June</option>
<option value="07" >July</option>
<option value="08" >August</option>
<option value="09" >September</option>
<option value="10" >October</option>
<option value="11" >November</option>
<option value="12" >December</option>
';
return $output;
}
function _cc_exp_year_options() {
$output = '';
for ($year = date('Y'); $year <= date('Y')+6; $year++)
$output .= '<option value="'.$year.'">'.$year.'</option>';
return $output;
}
/**
* Product
*/
public function product() {
$this->product_index();
}
public function product_index() {
$page = !empty($_GET['page']) ? $_GET['page']: 1;
$keywords = !empty($_GET['keywords']) ? $_GET['keywords']: '';
$per_page = 15;
$products_total = Product::find(array(
'where' => 'title like \'%'.$keywords.'%\''
));
$products = Product::find(array(
'limit' => $per_page,
'offset' => ($page-1)*($per_page),
'where' => 'title like \'%'.$keywords.'%\''
));
$pagination = new Pagination(array(
'base_url' => URL_PUBLIC.'/'.ADMIN_DIR.'/plugin/ecommerce/product/?keywords='.$keywords.'&page=',
'total_rows' => count($products_total),
'per_page' => $per_page,
'num_links' => 4,
'cur_page' => $page
));
$this->display('ecommerce/views/products/index', array(
'products' => $products,
'pagination' => $pagination
));
}
// TODO: set product parent page id in a setting set vendor and type parent id's dynamically
public function product_create()
{
if (get_request_method() == 'POST')
{
//get new type id if a new one was created
if ($_POST['product_type']['title']) {
//save type
$type_id = $this->_product_save(null,'product_type','ProductType');
$_POST['product']['type_id'] = $type_id;
//add new type page
$page_data = array("is_protected"=>1,"parent_id"=>87,"title"=>$_POST['product_type']['title'],"slug"=>$_POST['product_type']['slug'],"breadcrumb"=>$_POST['product_type']['title']);
$page = new Page($page_data);
$page->save();
}
//get new vendor id if a new one was created
if ($_POST['product_vendor']['title']) {
//save vendor
$vendor_id = $this->_product_save(null,'product_vendor','ProductVendor');
$_POST['product']['vendor_id'] = $vendor_id;
//add new vendor page
$page_data = array("is_protected"=>1,"parent_id"=>86,"title"=>$_POST['product_vendor']['title'],"slug"=>$_POST['product_vendor']['slug'],"breadcrumb"=>$_POST['product_vendor']['title']);
$page = new Page($page_data);
$page->save();
}
//create new page
//TODO: set product parent page id in a setting
$page_data = array("is_protected"=>1,"parent_id"=>9,"title"=>$_POST['product']['title'],"slug"=>$_POST['product']['slug'],"breadcrumb"=>$_POST['product']['title']);
$page = new Page($page_data);
if ($page->save()) {
//get page id
$_POST['product']['page_id'] = $page->id;
}
//save product
$product_id = $this->_product_save(null,'product','Product');
//save product variant
$_POST['product_variant']['product_id'] = $product_id;
$this->_product_save(null,'product_variant','Product');
//save images
if (isset($_SESSION['product_images'])) {
$this->_images_save($product_id,$_SESSION['product_images']);
unset($_SESSION['product_images']);
}
//add log entry
$this->_insert_log('New product: <a href="'.get_url('plugin/ecommerce/product_update/'.$product_id).'">'.$_POST['product']['title'].'</a>');
redirect(get_url('plugin/ecommerce/product'));
}
//pass types and vendors for select boxes
$types = ProductType::findAll();
$vendors = ProductVendor::findAll();
$this->display('ecommerce/views/products/create', array(
'action' => 'create',
'types' => $types,
'vendors' => $vendors
));
}
public function product_update($id=null)
{
if (is_null($id))
redirect(get_url('plugin/ecommerce'));
if ( ! $product = Product::findById($id))
{
Flash::set('error', __('Product not found!'));
redirect(get_url('plugin/ecommerce'));
}
if (get_request_method() == 'POST') {
//get new type id if a new one was created
if ($_POST['product_type']['title']) {
//save type
$type_id = $this->_product_save(null,'product_type','ProductType');
$_POST['product']['type_id'] = $type_id;
//add new type page
$page_data = array("is_protected"=>1,"parent_id"=>87,"title"=>$_POST['product_type']['title'],"slug"=>$_POST['product_type']['slug'],"breadcrumb"=>$_POST['product_type']['title']);
$page = new Page($page_data);
$page->save();
}
//get new vendor id if a new one was created
if ($_POST['product_vendor']['title']) {
//save vendor
$vendor_id = $this->_product_save(null,'product_vendor','ProductVendor');
$_POST['product']['vendor_id'] = $vendor_id;
//add new vendor page
$page_data = array("is_protected"=>1,"parent_id"=>86,"title"=>$_POST['product_vendor']['title'],"slug"=>$_POST['product_vendor']['slug'],"breadcrumb"=>$_POST['product_vendor']['title']);
$page = new Page($page_data);
$page->save();
}
//save product
$product_id = $this->_product_save($id,'product','Product');
//insert log
$this->_insert_log('Product <a href="'.get_url('plugin/ecommerce/product_update/'.$product_id).'">'.$_POST['product']['title'].'</a> was updated.');
//save images
if (isset($_SESSION['product_images']))
$this->_images_save($product_id,$_SESSION['product_images']);
//save product page
$product = Product::findById($id);
if ($product) {
$page = Record::findByIdFrom('Page', $product->page_id);
$page_data = array("is_protected"=>1,"title"=>$_POST['product']['title'],"slug"=>$_POST['product']['slug'],"breadcrumb"=>$_POST['product']['title'],"created_on_time"=>null,"published_on_time"=>null);
$page->setFromData($page_data);
$page->save();
}
redirect(get_url('plugin/ecommerce/product'));
}
$types = ProductType::findAll();
$vendors = ProductVendor::findAll();
$images = Record::findAllFrom('ProductImage', 'product_id=? order by position', array($id));
$variants = Record::findAllFrom('ProductVariant', 'product_id=? order by position', array($id));
$files = Record::findAllFrom('ProductFile', 'product_id=? order by position', array($id));
$videos = Record::findAllFrom('ProductVideo', 'product_id=? order by position', array($id));
$this->display('ecommerce/views/products/update', array(
'action' => 'update',
'product' => $product,
'types' => $types,
'vendors' => $vendors,
'images' => $images,
'variants' => $variants,
'files' => $files,
'videos' => $videos
));
}
public function product_delete($id)
{
if ($product = Record::findByIdFrom('Product', $id))
{
$product_title = $product->title;
//delete page for product
if ($page = Record::findByIdFrom('Page', $product->page_id))
$page->delete();
//delete variants
Record::deleteWhere('ecommerce_product_variant', 'product_id=?', array($id));
//delete images
Record::deleteWhere('ecommerce_product_image', 'product_id=?', array($id));
//delete files
Record::deleteWhere('ecommerce_product_file', 'product_id=?', array($id));
//delete videos
Record::deleteWhere('ecommerce_product_video', 'product_id=?', array($id));
if ($product->delete()) {
//add log entry
$this->_insert_log('Product \''.$product_title.'\' was deleted.');
Flash::set('success', __('Product has been deleted!'));
}
else
Flash::set('error', __('Product has not been deleted!'));
}
else
Flash::set('error', __('Product not found!'));
redirect(get_url('plugin/ecommerce/product'));