-
Notifications
You must be signed in to change notification settings - Fork 2
/
mic_commerce.module
1089 lines (976 loc) · 34.5 KB
/
mic_commerce.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
/**
* @file
* Commerce customizations for MIC.
*/
/**
* Implements hook_menu().
*
* @return array
* The menu items.
*/
function mic_commerce_menu() {
$items = array();
$items['create/product-classes'] = array(
'title' => 'Product Class creation',
'page callback' => 'drupal_get_form',
'page arguments' => array('mic_commerce_classes_products_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_CALLBACK,
);
$items['create/product-classes/%node'] = array(
'title' => 'Product Class creation',
'page callback' => 'mic_commerce_class_product_save',
'page arguments' => array(2),
'access arguments' => array('administer site configuration'),
'type' => MENU_CALLBACK,
);
$items['addtocart/%dialog_js/%'] = array(
'page callback' => 'mic_commerce_cart_ajax_callback',
'page arguments' => array(1, 2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['admin/commerce/student-fee'] = array(
'title' => t('Student Fee'),
'description' => t('Configure the student fee settings.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('mic_commerce_admin_student_fee_form'),
'access arguments' => array('administer site configuration'),
'file' => 'includes/admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/commerce/force-delete-order'] = array(
'title' => t('Force Delete an Order'),
'description' => t('Force delete an order manually from commerce tables.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('mic_commerce_admin_order_delete_form'),
'access arguments' => array('administer site configuration'),
'file' => 'includes/admin.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_default_rules_configuration().
*/
function mic_commerce_default_rules_configuration() {
$rules = array();
// Add a reaction rule to send order e-mail upon checkout completion.
$rule = rules_reaction_rule();
$rule->label = t('Send an order notification e-mail');
$rule->active = TRUE;
$rule
->event('commerce_checkout_complete')
->action('mail', array(
'to:select' => 'commerce-order:mail',
'subject' => t('Order [commerce-order:order-number] at [site:name]'),
'message' => t("Thank you for participating with MIC. \r\nYour order number is [commerce-order:order-number].\r\n\r\n**Classes**\r\nPlease contact our offices at 847.905.1500 for further inquiries. \r\n\r\n**Event Tickets**\r\nTickets will be mailed to you if purchased more than 7 days in advance of the concert. \r\nOtherwise, your tickets will be available at the box office, located in the lobby of Nichols Concert Hall (open one hour before each concert). \r\nPlease bring your receipt with you.\r\n\r\n**Logging In**\r\nYou can view your order history with us at any time by logging into our website at: [site:login-url]\r\nYou can find the status of your current order at: [commerce-order:customer-url]."),
'from' => '',
));
// Adjust the weight so this rule executes after the order has been updated to
// the proper user account.
$rule->weight = 4;
$rules['mic_commerce_checkout_order_email'] = $rule;
return $rules;
}
/**
* Implements hook_form_BASE_FORM_ID_alter
*
* @param unknown_type $form
* @param unknown_type $form_state
* @param unknown_type $form_id
*/
function mic_commerce_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) {
//if event, set the event name as the event detail in the line item
if ($form_state['line_item']->type == 'product_tkt' && $form_state['submitted'] == FALSE) {
$node = menu_get_object();
$form['line_item_fields']['field_lineitem_tkt_eventname'][LANGUAGE_NONE][0]['value']['#default_value'] = $node->title;
$form['line_item_fields']['field_lineitem_tkt_eventname'][LANGUAGE_NONE][0]['value']['#type'] = 'hidden';
// Sort tickets by certain order.
uasort($form['product_id']['#options'], function($a, $b) {
static $order = array(
'Adult' => 0,
'Senior' => 1,
'Student' => 2,
'3PASS Adult' => 3,
'3PASS Senior' => 4,
);
return $order[$a] - $order[$b];
});
// Set default selection.
$form['product_id']['#default_value'] = 154;
// Set help text.
$form['product_id']['#prefix'] = '<div class="ticket-help">Select your ticket type. To purchase multiple ticket types, add to cart then select again.</div>';
}
if ($form_state['line_item']->type == 'product_class' && $form_state['submitted'] == FALSE) {
$product = commerce_product_load($form['product_id']['#value']);
$message = field_get_items('commerce_product', $product, 'field_class_student_msg');
$message = (is_array($message)) ? $message[0]['value'] : 'Enter your Student Name and Birthdate';
$title = 'Step 1. ' . $message;
$form['line_item_fields']['field_class_lineitem_detail'][LANGUAGE_NONE][0]['value']['#title'] = $title;
$form['submit']['#prefix'] = '<strong>' . t('Step 2.') . '</strong> ';
}
}
/**
* Implements hook_form_views_form_commerce_cart_form_default_alter()
*
* @param unknown_type $form
* @param unknown_type $form_state
* @param unknown_type $form_id
*/
function mic_commerce_form_views_form_commerce_cart_form_default_alter(&$form, &$form_state, $form_id) {
global $user;
if ($user->uid == 0) {
$redirect = mic_commerce_redirect_cart();
$create_account = l(t('create account'), 'user/register', array('query' => array('destination' => $redirect)));
$login = l(t('login'), 'user/login', array('query' => array('destination' => 'checkout')));
}
}
/**
* Implements hook_ctools_plugin_api().
*/
function mic_commerce_ctools_plugin_api() {
list($module, $api) = func_get_args();
if ($module == "field_group" && $api == "field_group") {
return array("version" => "1");
}
}
/**
* Implements hook_views_api().
*/
function mic_commerce_views_api() {
return array("version" => "3.0");
}
/**
* Implements hook_page_build()
*
* @param array $page
*/
function mic_commerce_page_build(&$page) {
global $theme;
switch ($theme) {
case 'mic_theme':
$page['header5']['cart'] = array(
'#markup' => mic_commerce_cart(),
);
break;
}
}
/**
* Implements hook_block_info()
*
* @return array
*/
function mic_commerce_block_info() {
$blocks['mic_commerce_cart'] = array(
'info' => t('MIC: Cart'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_view()
*
* @param string $delta
* @return array
*/
function mic_commerce_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'mic_commerce_cart':
$block['subject'] = '';
$block['content'] = mic_commerce_cart();
break;
}
return $block;
}
/**
* Callback to display custom cart block
*
* @return array
*/
function mic_commerce_cart() {
global $user;
$user->name = ($user->uid) ? $user->name : FALSE;
$num_items = mic_commerce_num_items_cart();
return theme('mic_commerce_cart', array('num_items' => $num_items, 'name' => $user->name));
}
/**
* Return number of items in user's cart
*
* @param unknown_type $user
* @return unknown
*/
function mic_commerce_num_items_cart() {
global $user;
if ($order = commerce_cart_order_load($user->uid)) {
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$num_items = commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types());
return $num_items;
}
return 0;
}
/**
* Implements hook_theme()
*
* @return array
*/
function mic_commerce_theme() {
return array(
'mic_commerce_cart' => array(
'variables' => array('num_items' => NULL, 'name' => NULL),
'file' => 'theme.inc',
'path' => drupal_get_path('module', 'mic_commerce') . '/theme',
),
);
}
/**
* Implements hook_commerce_product_type_info()
*
* @return array
*/
function mic_commerce_commerce_product_type_info() {
return array(
'product_ticket' => array(
'type' => 'product_ticket',
'name' => t('Ticket'),
'description' => 'A ticket product to relate to events',
'help' => '',
),
'product_class' => array(
'type' => 'product_class',
'name' => t('Class'),
'description' => 'A class product to relate to courses',
'help' => '',
),
);
}
/**
* Implements hook_commerce_line_item_type_info()
*
* @return array
*/
function mic_commerce_commerce_line_item_type_info() {
$line_item_types = array();
$line_item_types['product_class'] = array(
'name' => t('Product Class'),
'description' => t('Line item type for product class'),
'product' => TRUE,
'add_form_submit_value' => t('Add class'),
'base' => 'commerce_product_line_item',
);
$line_item_types['product_tkt'] = array(
'name' => t('Product Ticket'),
'description' => t('Line item type for product ticket'),
'product' => TRUE,
'add_form_submit_value' => t('Add ticket'),
'base' => 'commerce_product_line_item',
);
$line_item_types['mic_newstudent_fee'] = array(
'name' => t('MIC Registration Fee'),
'description' => t('A MIC fee for new students.'),
'product' => FALSE,
'add_form_submit_value' => t('Add fee'),
'callbacks' => array(
'configuration' => 'mic_commerce_newstudent_fee_configuration',
'title' => 'mic_commerce_newstudent_fee_title',
'add_form' => 'mic_commerce_newstudent_fee_add_form',
'add_form_submit' => 'mic_commerce_newstudent_fee_add_form_submit',
),
);
$line_item_types['product_pi'] = array(
'name' => t('Product Private Instruction'),
'description' => t('Line item type for product Private Instruction'),
'product' => TRUE,
'add_form_submit_value' => t('Add Details'),
'base' => 'commerce_product_line_item',
);
return $line_item_types;
}
function mic_commerce_newstudent_fee_configuration($line_item_type) {
$type = $line_item_type['type'];
}
function mic_commerce_newstudent_fee_title($line_item) {
return(t('Registration Fee'));
}
function mic_commerce_newstudent_fee_add_form($element, &$form_state) {
$form = array();
return $form;
}
/**
* Adds the selected product information to a line item added via a line item
* manager widget (on the admin order page).
*
* @param $line_item
* The newly created line item object.
* @param $element
* The array representing the widget form element.
* @param $form_state
* The present state of the form upon the latest submission.
* @param $form
* The actual form array.
*
* @return
* NULL if all is well or an error message if something goes wrong.
*/
function mic_commerce_newstudent_fee_add_form_submit(&$line_item, $element, &$form_state, $form) {
$line_item->line_item_label = t('Registration Fee');
// Wrap the line item and product to easily set field information.
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
// Provide a default price.
$amount = 2500;
$currency_code = 'USD';
$line_item->commerce_unit_price = array('und' => array(
'0' => array('amount' => $amount, 'currency_code' => $currency_code)
));
if (!is_null($line_item_wrapper->commerce_unit_price->value())) {
// Add the base price to the components array.
if (!commerce_price_component_load($line_item_wrapper->commerce_unit_price->value(), 'base_price')) {
$line_item_wrapper->commerce_unit_price->data = commerce_price_component_add(
$line_item_wrapper->commerce_unit_price->value(),
'base_price',
$line_item_wrapper->commerce_unit_price->value(),
TRUE
);
}
}
}
/**
* Utility function which creates a new line item populated with the
* a price of $5.00, etc..
*
* @param $order_id
* The ID of the order the line item belongs to (if available).
*
* @return
* The fully loaded line item..
*/
function mic_commerce_newstudent_fee_line_item_new($order_id = 0) {
$type = 'mic_newstudent_fee';
// Create the new line item.
$line_item = entity_create('commerce_line_item', array(
'type' => $type,
'order_id' => $order_id,
'quantity' => 1,
));
$amount = 2500;
$currency_code = 'USD';
$line_item->commerce_unit_price = array('und' => array(
'0' => array('amount' => $amount, 'currency_code' => $currency_code)
));
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
if (!is_null($line_item_wrapper->commerce_unit_price->value())) {
// Add the base price to the components array.
if (!commerce_price_component_load($line_item_wrapper->commerce_unit_price->value(), 'base_price')) {
$line_item_wrapper->commerce_unit_price->data = commerce_price_component_add(
$line_item_wrapper->commerce_unit_price->value(),
'base_price',
$line_item_wrapper->commerce_unit_price->value(),
TRUE
);
}
}
// Return the line item.
return $line_item;
}
/**
* Implements hook_commerce_order_presave
*
* @param unknown_type $order
*/
function mic_commerce_commerce_order_presave($order) {
// Student fee admin setting.
$student_fee = variable_get('mic_student_fee', 0);
// Find out if our order already has a mic_student_fee line item
// and if new student was checked.
$type_exists = $new_student = FALSE;
if (!empty($order->commerce_line_items['und'])) {
// Go through each line item.
foreach ($order->commerce_line_items['und'] as $delta => $line_item_entry) {
if ($line_item = commerce_line_item_load($line_item_entry['line_item_id'])) {
if (isset($line_item->field_class_lineitem_newstud[LANGUAGE_NONE][0]['value'])) {
// If student fee set to new only, check if current value is new only.
if ($student_fee) {
if ($line_item->field_class_lineitem_newstud[LANGUAGE_NONE][0]['value'] == 1) {
$new_student = TRUE;
}
}
// Everyone gets charged the fee.
else {
$new_student = TRUE;
}
}
// If we find our fee, set some values.
if ($line_item->type == 'mic_newstudent_fee') {
$type_exists = TRUE;
$line_item_fee_id = array(
'delta' => $delta,
'id' => $line_item->line_item_id
);
// Get out of the loop.
break;
}
}
}
}
// If our line item is not yet in the order and the order has an ID,
// create a line item to add to the order.
if (!$type_exists && $order->order_id > 0) {
if ($new_student) {
$line_item = mic_commerce_newstudent_fee_line_item_new($order->order_id);
commerce_line_item_save($line_item);
$order->commerce_line_items[LANGUAGE_NONE][] = array('line_item_id' => $line_item->line_item_id);
}
}
// If a new student was added, then removed and the fee remained, need to remove fee
// if a continuing student is added to same order later
else if ($type_exists && ($order->order_id > 0) && !$new_student && isset($line_item_fee_id)) {
commerce_line_item_delete($line_item_fee_id['id']);
unset($order->commerce_line_items[LANGUAGE_NONE][$line_item_fee_id['delta']]);
}
}
/**
* Implements hook_commerce_product_presave().
*/
function mic_commerce_commerce_product_presave($product) {
//generate nice autosku
if (empty($product->sku) && ($product->type == 'product_class')) {
module_load_include('inc', 'mic_commerce', 'includes/transfer');
$semester_string = mic_commerce_get_semester_string($product, 'commerce_product');
$instructor_string = mic_commerce_get_instructor_string($product, 'commerce_product');
$day = mic_commerce_get_field_value($product, 'field_day_of_week', 'commerce_product');
$location_string = mic_commerce_get_location_string($product, 'commerce_product');
$time = mic_commerce_get_field_value($product, 'field_time', 'commerce_product');
$sku[] = strtolower(str_replace(' ', '', $product->title));
$sku[] = $semester_string;
$sku[] = $instructor_string;
$sku[] = strtolower($day['value']);
$sku[] = $location_string;
$sku[] = $time['value'];
$sku = implode('-', $sku);
ctools_include('cleanstring');
$sku = ctools_cleanstring($sku);
//Borrowed from pathauto...
if (!commerce_product_validate_sku_unique($sku, $product->product_id)) {
$original_sku = $sku;
$i = 0;
do {
$sku = ctools_cleanstring($original_sku . '-' . $i);
$i++;
} while (!commerce_product_validate_sku_unique($sku, $product->product_id));
}
$product->sku = $sku;
}
}
/**
* Implementes hook_admin_paths()
*
* @return array
*/
function mic_commerce_admin_paths() {
$paths = array(
'create/product-classes' => TRUE,
);
return $paths;
}
/**
* Classes, product build start form
*
* @return array
*/
function mic_commerce_classes_products_form() {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Start'),
);
return $form;
}
/**
* Submit handler for classes, product build form
*
* @param array $form
* @param array $form_state
*/
function mic_commerce_classes_products_form_submit(&$form, &$form_state) {
$operations = array();
//get all classes; put in batch to create matching products
$classes = _mic_commerce_get_classes();
foreach ($classes as $class) {
$operations[] = array('mic_commerce_class_product_batch_create', array($class));
}
$batch = array(
'operations' => $operations,
'finished' => 'mic_commerce_class_product_batch_finished',
);
batch_set($batch);
}
/**
* Batch creation of products to classes relationship
*
* @param int $class
* @param unknown_type $context
*/
function mic_commerce_class_product_batch_create($class, &$context) {
$class_node = node_load($class->nid);
//save product
$product = mic_commerce_class_product_save($class_node);
$context['message'] = t('Saved product %sku', array('%sku' => $product->sku));
}
/**
* Callback when batch is finished
*
* @param unknown_type $success
* @param unknown_type $results
* @param unknown_type $operations
*/
function mic_commerce_class_product_batch_finished($success, $results, $operations) {
if ($success) {
drupal_set_message(t('Import successful.'));
}
else {
drupal_set_message(t('There were errors in the import.'));
}
}
/**
* Save a product, either new or existing
*
* @param object $class_node
* @return object
*/
function mic_commerce_class_product_save($class_node) {
module_load_include('inc', 'mic_commerce', 'includes/transfer');
$price = mic_commerce_get_price($class_node);
//save product
$product = commerce_product_new('product_class');
$product->title = $class_node->title;
$product->commerce_price[LANGUAGE_NONE][0] = $price;
$product->uid = 1;
$semester_string = mic_commerce_get_semester_string($class_node);
$instructor_string = mic_commerce_get_instructor_string($class_node);
$sku[] = strtolower(str_replace(' ', '', $class_node->title));
$sku[] = $semester_string;
$sku[] = $instructor_string;
$sku[] = $class_node->nid;
$sku = implode('-', $sku);
$product->sku = $sku;
//get other fields
foreach ($class_node as $field => $value) {
if (strpos($field, 'field_') === 0) {
$product->{$field} = $class_node->{$field};
}
else { continue; }
}
commerce_product_save($product);
return $product;
}
/**
* Create a simple add-to-cart form.
*
* @param $product_id
* The ID of the product to display.
* @param $quantity
* The default number to be added to the cart, defaults to 1.
* @param $show_quantity
* Boolean indicating whether or not to provide the quantity selector.
* @param $order_id
* The order ID to be used, defaults to 0.
*
* @return
* A formatted add-to-cart form.
*/
function mic_commerce_add_to_cart($product_id, $quantity = 1, $show_quantity = FALSE, $order_id = 0) {
// Load the product.
$product = commerce_product_load($product_id);
// Build the line item object.
$line_item = commerce_line_item_new($product->type, $order_id);
$line_item->data['context']['product_ids'] = array($product->product_id);
$line_item->quantity = $quantity;
// Get the form_id.
$form_id = commerce_cart_add_to_cart_form_id(array($product->product_id), $line_item->quantity, $show_quantity);
// Render the final output.
$render = drupal_get_form($form_id, $line_item);
return render($render);
}
/**
* Ajax callback to load add-to-cart display in dialog
*
* @param string $js
* @param int $product_id
*/
function mic_commerce_cart_ajax_callback($js, $product_id) {
if ($js) {
dialog_display(TRUE);
$content = mic_commerce_add_to_cart($product_id);
$output[] = dialog_command_display($content, array('title' => 'Add to Cart'));
ajax_deliver(array('#type' => 'ajax', '#commands' => $output));
}
else {
drupal_set_message('Unable to add item to your cart. Please contact us by phone.');
drupal_goto('cart');
}
}
/**
* Implements hook_form_alter
*
* @param array $form
* @param array $form_state
* @param string $form_id
*/
function mic_commerce_form_alter(&$form, &$form_state, $form_id) {
//dialog for add_to_cart; breaks if JS compression is turned on
//get tooltip working
if (strpos($form_id, 'commerce_cart_add_to_cart_form_') === 0) {
//only do ajax for class forms
if (isset($form['line_item_fields']['field_class_lineitem_detail']) && isset($form['line_item_fields']['field_class_lineitem_newstud'])) {
$form['submit']['#ajax'] = array(
'callback' => 'mic_commerce_ajax_callback_submit',
'wrapper' => 'dialog-form-wrapper',
);
//set node id for redirect
list($callback, $type, $product_id, $nid) = explode('/', current_path());
$form['nid'] = array(
'#type' => 'value',
'#value' => $nid,
);
$form['#process'][] = 'dialog_process_ajax_form';
}
//tooltip on line item form
if (isset($form['line_item_fields']) && ($form['line_item_fields']['#bundle'] == 'product_pi')) {
// Grab entity information from the form.
$entity_type = $form['line_item_fields']['#entity_type'];
$bundle = $form['line_item_fields']['#bundle'];
// Lookup field instance information for all of the entity's fields.
$instances = field_info_instances($entity_type, $bundle);
// Add tooltips to each of the form's fields where appropriate.
foreach ($instances as $field_name => $instance) {
if (isset($form['line_item_fields'][$field_name])) {
if (!empty($instance['settings']['field_tooltips_enabled']) && !empty($instance['settings']['field_tooltips_tooltip'])) {
// Translate field tooltip.
if (module_exists('i18n_field')) {
$instance['settings']['field_tooltips_tooltip']['value'] = i18n_field_translate_tooltip($instance, $instance['settings']['field_tooltips_tooltip']['value']);
}
// Add a class to the parent field form item. This will be used by
// the tooltip JS to target the child field form items.
$form['line_item_fields'][$field_name]['#attributes']['class'][] = 'field-tooltips';
// Find all field form items and add the sanitized tooltip text as a
// title attribute.
foreach($form['line_item_fields'][$field_name][LANGUAGE_NONE] as $key => &$field) {
if (is_numeric($key) && is_array($field)) {
if (isset($field['value'])) {
$field['value']['#attributes']['title'] = check_markup($instance['settings']['field_tooltips_tooltip']['value'], $instance['settings']['field_tooltips_tooltip']['format']);
}
else {
$field['#attributes']['title'] = check_markup($instance['settings']['field_tooltips_tooltip']['value'], $instance['settings']['field_tooltips_tooltip']['format']);
}
}
}
}
}
}
// Attach the tooltip library.
$form['#attached']['library'][] = array('field_tooltips', 'tools.tooltip');
$form['#attached']['js'][] = drupal_get_path('module', 'field_tooltips') . '/field_tooltips.js';
$form['#attached']['css'][] = drupal_get_path('module', 'field_tooltips') . '/field_tooltips.css';
}
}
//add filter options to orders overview on view
if ($form_id == 'commerce_order_ui_redirect_form') {
$options = array('' => '- All -');
if ($vocabulary = taxonomy_vocabulary_machine_name_load('campus')) {
if ($terms = taxonomy_get_tree($vocabulary->vid)) {
foreach ($terms as $term) {
$options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
}
}
}
$semester_options = array('' => '- All -');
if ($vocabulary = taxonomy_vocabulary_machine_name_load('semester')) {
if ($terms = taxonomy_get_tree($vocabulary->vid)) {
foreach ($terms as $term) {
$semester_options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
}
}
}
$default = drupal_get_query_parameters();
$form['location'] = array(
'#title' => t('Campus'),
'#type' => 'select',
'#options' => $options,
'#default_value' => $default['location'],
);
$form['semester'] = array(
'#title' => t('Semester'),
'#type' => 'select',
'#options' => $semester_options,
'#default_value' => $default['semester'],
);
//use different validate
$form['#validate'][0] = 'mic_commerce_admin_commerce_orders_filter_validate';
$form['#submit'][] = 'mic_commerce_admin_commerce_orders_filter_submit';
$form['submit']['#value'] = t('Filter');
}
//add checkout button to redirect to login for non logged-in users
if (strpos($form_id, 'views_form_commerce_cart_form_') === 0) {
$redirect = mic_commerce_redirect_cart();
$form['actions']['checkout_redirect'] = array(
'#markup' => '<span class="anonymous-checkout">' . l('Checkout', 'user/login', array('query' => array('destination' => $redirect))) . '</span>',
'#weight' => 5,
'#access' => !(user_access('access checkout')),
);
}
//generate auto-sku for class forms
if (($form_id == 'commerce_product_ui_product_form') && ($form_state['commerce_product']->type == 'product_class')) {
$form['sku']['#required'] = FALSE;
$form['sku']['#disabled'] = TRUE;
$form['sku']['#description'] = t('SKU will be automatically generated.');
}
//show log messages
if ($form_id == 'commerce_order_ui_order_form') {
$log = mic_commerce_order_log_history($form['#entity']->order_number);
$form['order_status']['history'] = array(
'#markup' => $log,
'#weight' => 99,
);
}
// Add message for login
if (($form_id == 'user_login') && drupal_anonymous_user()) {
$redirect = mic_commerce_redirect_cart();
$form['#prefix'] = '<div class="first-time-msg">' . t('<strong>First time users</strong> must <a href="!create">create a new account</a> to complete purchase.', array('!create' => '/user/register?destination=' . $redirect)) . '<br />Credit card information will not be stored on our site.</div>';
}
// Change button text for review form of cart
if ($form_id == 'commerce_checkout_form_review') {
$form['buttons']['continue']['#value'] = t('Process payment');
}
}
/**
* Submit ajax callback for dialogs
*
* @return unknown
*/
function mic_commerce_ajax_callback_submit($form, &$form_state) {
$commands = array();
$commands[] = dialog_command_dismiss();
if (isset($form_state['values']['nid'])) {
$commands[] = dialog_command_redirect('node/' . $form_state['values']['nid']);
}
else {
$commands[] = dialog_command_reload();
}
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Helper to get all class nodes
*
* @return array
*/
function _mic_commerce_get_classes() {
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'item_class')
->propertyCondition('status', 1)
->fieldCondition('field_sunset_date', 'value', time(), '>=')
->execute();
return reset($result);
}
/**
* Rules action: displays a the default translatable Add to Cart message.
*/
function mic_commerce_cart_rules_add_to_cart_message($product) {
drupal_set_message(t('%title added to your cart. Add <a href="!private-url">private instruction</a> for another student, <a href="!group-url">add group classes</a>, <em>or</em> <strong><a href="!cart-url">review your order</a></strong>.', array(
'%title' => $product->title,
'!cart-url' => url('cart'),
'!private-url' => url('private-registration'),
'!group-url' => url('class-finder', array('query' => array('semester' => 327))),
)));
}
/**
* Implements hook_views_query_alter
*
* @param unknown_type $view
* @param unknown_type $query
*/
function mic_commerce_views_query_alter(&$view, &$query) {
//add a tag to filter commerce_orders
if ($view->name == 'commerce_orders') {
$filters = drupal_get_query_parameters();
if (!empty($filters)) {
$query->add_tag('commerce_orders_filter');
}
}
}
/**
* Implements hook_query_TAG_alter
* Join line items, products for filtering of orders
*
* @param QueryAlterableInterface $query
*/
function mic_commerce_query_commerce_orders_filter_alter(QueryAlterableInterface $query) {
$filters = drupal_get_query_parameters();
$query
->leftJoin('commerce_line_item', 'cli', 'commerce_order.order_id = cli.order_id');
$query
->leftJoin('commerce_product', 'cp', 'cli.line_item_label = cp.sku');
if (isset($filters['location'])) {
$tid = check_plain($filters['location']);
$query
->leftJoin('field_data_field_location_reference', 'fdflr', 'fdflr.entity_id = cp.product_id');
$query
->condition('fdflr.field_location_reference_tid', array($tid));
}
if (isset($filters['semester'])) {
$tid = check_plain($filters['semester']);
$query
->leftJoin('field_data_field_semester_reference', 'fdfsr', 'fdfsr.entity_id = cp.product_id');
$query
->condition('fdfsr.field_semester_reference_tid', array($tid));
}
$query->distinct();
}
/**
* Implements hook_views_pre_render
*
* @param unknown_type $view
*/
function mic_commerce_views_pre_render(&$view) {
//change title based on filters; drupal_set_title doesn't seem to work
if ($view->name == 'commerce_orders' && $view->current_display == 'admin_page') {
//set page title
$filters = drupal_get_query_parameters();
$title = array();
if (isset($filters['location'])) {
$tid = check_plain($filters['location']);
$location = taxonomy_term_load($tid);
$title[] = $location->name;
}
if (isset($filters['semester'])) {
$tid = check_plain($filters['semester']);
$semester = taxonomy_term_load($tid);
$title[] = $semester->name;
}
$title = implode(', ', $title);
if ($title) {
$view->build_info['title'] = t('Orders at @title', array('@title' => $title));
}
}
}
/**
* Custom validate callback for orders filter
*
* @param unknown_type $form
* @param unknown_type $form_state
*/
function mic_commerce_admin_commerce_orders_filter_validate($form, &$form_state) {
$order = FALSE;
// Attempt to load the specified order.
if ($form_state['values']['identifier'] == 'order_number') {
$order = commerce_order_load_by_number($form_state['values']['order_identifier']);
}
elseif ($form_state['values']['identifier'] == 'order_id') {
$order = commerce_order_load($form_state['values']['order_identifier']);
}
// If the order could not be loaded by ID or number or the user does not have
// view access for the order, throw an error.
if ($order) {
form_set_value($form['order'], $order, $form_state);
}
}
/**
* Custom submit callback for orders filter
*
* @param unknown_type $form
* @param unknown_type $form_state
*/
function mic_commerce_admin_commerce_orders_filter_submit($form, &$form_state) {
$location = $form_state['values']['location'];
$semester = $form_state['values']['semester'];
$order = $form_state['values']['order'];
$current_filter = array();
if (!$order) {
if (!empty($location)) {
$location_term = taxonomy_term_load($location);
drupal_set_message(t('Filter: @location Campus', array('@location' => $location_term->name)));
$current_filter['location'] = $location;