-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwoolman_website.module
More file actions
1718 lines (1622 loc) · 60.4 KB
/
Copy pathwoolman_website.module
File metadata and controls
1718 lines (1622 loc) · 60.4 KB
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
// TODO: Remove for D7 version
define('DRUPAL_ROOT', getcwd());
// CiviCRM contact_id of Woolman
define('WOOLMAN', 243);
// Country ID of United States
define('USA', 1228);
/**
* Implements hook_menu.
*/
function woolman_website_menu() {
$items = array();
$items['staff/js/autocomplete'] = array(
'title' => 'Contacts',
'page callback' => 'woolman_website_autocomplete',
'access arguments' => array('access CiviCRM'),
'file' => 'woolman_website_utils.inc',
'type' => MENU_CALLBACK,
);
$items['staff/directory'] = array(
'title' => 'Woolman Directory',
'page callback' => 'drupal_get_form',
'page arguments' => array('woolman_directory'),
'access arguments' => array('access CiviCRM'),
'type' => MENU_CALLBACK,
'file' => 'woolman_directory.inc',
);
$items['phone'] = array(
'title' => 'Phonathon',
'page callback' => 'drupal_get_form',
'page arguments' => array('woolman_phonathon'),
'access arguments' => array('use phonathon form'),
'type' => MENU_CALLBACK,
'file' => 'woolman_phonathon.inc',
);
$items['staff/js/directory'] = array(
'page callback' => 'woolman_directory_reload',
'access arguments' => array('access CiviCRM'),
'type' => MENU_CALLBACK,
'file' => 'woolman_directory.inc',
);
$items['~semester/about/sending-schools'] = array(
'title' => 'Sending Schools',
'page callback' => 'woolman_sending_schools',
'access arguments' => array('access content'),
'file' => 'woolman_sending_schools.inc',
);
return $items;
}
/**
* Implements hook_theme.
*/
function woolman_website_theme() {
return array(
'multicolumn_options' => array(
'arguments' => array('element' => NULL),
'file' => 'woolman_utils.inc',
),
'woolman_sending_schools' => array(
'arguments' => array('schools' => NULL),
),
);
}
/**
* Implements hook_perm.
* Define the permissions this module uses.
*/
function woolman_website_perm() {
return array('use phonathon form');
}
/**
* hook_mail callback
* @param $key: Any value will send a simple message. Pass "create_activity" to log an activity in civicrm
*/
function woolman_website_mail($key, &$msg, $params) {
$msg['subject'] = $params['subject'];
$msg['body'][] = $params['body'];
// $msg['headers']['Errors-To'] = $msg['headers']['Return-Path'] = $msg['headers']['Sender'] = $msg['headers']['From'];
if (!empty($params['cc'])) {
$msg['headers']['Cc'] = $params['cc'];
}
if (!empty($params['bcc'])) {
$msg['headers']['Bcc'] = $params['bcc'];
}
// Create activity
if ($key==='create_activity') {
civicrm_initialize();
woolman_civicrm_api('activity', 'create', array(
'activity_type_id' => woolman_aval($params, 'activity_type_id', 3),
'source_contact_id' => !empty($params['from_cid']) ? $params['from_cid'] : woolman_user_cid(),
'target_contact_id' => is_array($params['to_cid']) ? $params['to_cid'] : array($params['to_cid']),
'subject' => $params['subject'],
'status_id' => 2,
'activity_date_time' => date('YmdHis'),
'details' => nl2br($params['body']),
));
}
}
/**
* Implements hook_form_alter.
*/
function woolman_website_form_alter(&$form, $form_state, $form_id) {
// Alter node edit forms
if (woolman_aval($form, '#id') == 'node-form') {
drupal_add_js(drupal_get_path('module', 'woolman_website') . '/node_form.js', 'module', 'footer');
// Inline images
if (isset($form['field_inline_images'])) {
$form['field_inline_images']['#prefix'] = '
<button id="show-field-inline-images" type="button" onclick="$(\'#field-inline-images-items\').show(400); $(\'#show-field-inline-images\').hide(); return false;">Add Images</button>' .
str_replace('<div', '<div style="display:none;"', $form['field_inline_images']['#prefix']);
$form['field_inline_images']['field_inline_images_add_more']['#value'] = "Add Another Image";
}
// Blog tweaks
if ($form_id == 'blog_entry_node_form') {
module_load_include('inc', 'woolman_website', 'woolman_blog');
_woolman_blog_form_alter($form, $form_state);
}
// Hide Event ID field
if ($form_id == 'event_node_form' && isset($form['field_civicrm_event_id'])) {
$form['field_civicrm_event_id']['#access'] = FALSE;
}
return;
}
// Change search button text in various views
$view_name = isset($form_state['view']) && isset($form_state['view']->name) ? $form_state['view']->name : '';
if ($view_name == 'blog' || $view_name == 'songbook') {
if (!empty($form['submit']['#value'])) {
$form['submit']['#value'] = t('Search');
}
}
elseif ($view_name == 'admin_content') {
if (!empty($form['submit']['#value'])) {
$form['submit']['#value'] = t('Filter');
}
}
// Admissions activity view
elseif ($view_name == 'admissions_activities' && !empty($form['mine'])) {
$form['mine']['#options'] = array(
'All' => 'Any outreach person',
1 => 'Only my trips'
);
}
// Student Projects
elseif ($view_name == 'student_projects' && !empty($form['semester'])) {
module_load_include('inc', 'woolman_website', 'woolman_website_utils');
$form['semester']['#type'] = 'select';
$form['semester']['#multiple'] = FALSE;
$form['semester']['#options'] = array('' => '-Any-') + woolman_semesters_list_as_integers();
unset($form['semester']['#size']);
// Add a fake form item to switch urls
$item = '
<div style="float:left;padding-right:1em;">
<label for="edit-project-type">Type</label>
<div class="form-item">
<select id="edit-project-type" class="form-select">
<option value="">-Any-</option>';
foreach(taxonomy_get_tree(19) as $term) {
$key = str_replace(' ', '-', strtolower($term->name));
if (arg(2) == $term->tid) {
$key .= '" selected="selected';
}
$item .= '<option value="/' . $key . '">' . $term->name . '</option>';
}
$item .= '
</select>
</div>
</div>';
$form = array('project_type_markup' => array('#value' => $item)) + $form;
drupal_add_js('jQuery(function($) {
$("#edit-project-type").change(function() {
var $form = $(this).parents("form");
var action = $form.attr("action").split("/projects");
$form.attr("action", action[0] + "/projects" + $(this).val());
});
});', 'inline');
}
// Customize text in comment form
if ($form_id == 'comment_form') {
if (!empty($form['name']['#default_value'])) {
$form['name']['#default_value'] = '';
}
$form['mail']['#description'] = t('For verification purposes only. We promise not to spam you.');
$form['homepage']['#title'] = t('Your Website or Blog');
$form['homepage']['#attributes']['onblur'] = "if (this.value == 'http://') {this.value = '';}";
$form['homepage']['#attributes']['onfocus'] = "if (this.value == '') {this.value = 'http://';}";
$form['homepage']['#description'] = t('OPTIONAL: If you want to include a link (i.e. to your facebook page), enter the url.');
}
// Custom search box
if ($form_id == 'search_theme_form') {
unset($form['search_theme_form']['#title']);
}
// Contact form -- set default category from url
if ($form_id == 'contact_mail_page') {
if (!empty($_GET['c']) && is_numeric($_GET['c'])) {
$form['cid']['#default_value'] = (int) $_GET['c'];
}
}
}
/**
* Implements hook_nodeapi.
*/
function woolman_website_nodeapi(&$node, $op, $teaser, $page) {
// Sync CiviCRM events to drupal event nodes and rsvp webform
if (isset($node->type) && $node->type == 'event') {
if ($node->field_event_type[0]['value'] == 'Woolman Semester' && $op == 'presave') {
civicrm_initialize();
$params = array(
'event_type_id' => 3,
'start_date' => date('YmdHis', strtotime($node->field_event_date[0]['value']) + $node->field_event_date[0]['offset']),
'end_date' => date('YmdHis', strtotime($node->field_event_date[0]['value2']) + $node->field_event_date[0]['offset2']),
'is_public' => 1,
'is_active' => 1,
'title' => $node->title,
'description' => $node->body,
);
if (!empty($node->field_civicrm_event_id[0]['value'])) {
$params['id'] = $node->field_civicrm_event_id[0]['value'];
}
else {
$node->body .= '<p><a href="/events/rsvp">Click Here to RSVP</a></p>';
}
$result = woolman_civicrm_api('event', 'create', $params);
$node->field_civicrm_event_id[0]['value'] = $result['id'];
woolman_website_cron(TRUE);
}
elseif (($op == 'presave' || $op == 'delete') && !empty($node->field_civicrm_event_id[0]['value'])) {
civicrm_initialize();
woolman_civicrm_api('event', 'delete', array('id' => $node->field_civicrm_event_id[0]['value']));
$node->field_civicrm_event_id[0]['value'] = NULL;
woolman_website_cron(TRUE);
}
}
// Respond to a new blog entry being created
if ($op == 'insert' && isset($node->type) && $node->type == 'blog_entry') {
module_load_include('inc', 'woolman_website', 'woolman_blog');
_woolman_blog_insert($node);
}
}
/**
* Implements hook_cron.
* Refresh events list in RSVP form once a day (or manually during event crud)
*/
function woolman_website_cron($force = FALSE) {
if ($force || date('G') < 2) {
$node = node_load(1394);
civicrm_initialize();
module_load_include('inc', 'webform_civicrm', 'webform_civicrm_utils');
$event_field = $node->webform['components'][98];
$events = webform_civicrm_field_options($event_field, 'component_insert', $node->webform_civicrm['data']);
if ($events != $event_field['extra']['items']) {
module_load_include('inc', 'webform', 'includes/webform.components');
$event_field['extra']['items'] = $events;
webform_component_update($event_field);
}
}
}
/**
* Implements hook_civicrm_buildForm.
*/
function woolman_website_civicrm_buildForm($formName, &$form) {
// Alter contribution forms
if ($formName == 'CRM_Contribute_Form_Contribution_Main'
&& !empty($form->_values['financial_type_id'])) {
// Limit options of donation Payment Forms
if ($form->_values['financial_type_id'] == 1) {
$allowed = array('annual fund', 'ws scholarship', 'campership', 'seeds of peace');
foreach ($form->_elements as &$e) {
// The following is commented out because it somehow interfered with form validation
// So now we are using a cheap css trick to hide the unwanted options
/*if (!empty($e->_attributes['name']) && $e->_attributes['name'] == 'custom_18') {
foreach ($e->_options as $oid => $o) {
if (!in_array(woolman_aval($o, 'attr:value'), $allowed)) {
unset($e->_options[$oid]);
}
}
}*/
if (!empty($e->_name) && $e->_name == 'preferred_communication_method') {
unset($e->_elements[0], $e->_elements[2], $e->_elements[3], $e->_elements[4], $e->_elements[5]);
}
}
}
// Change language of non-donation Payment Forms
else {
foreach ($form->_elements as &$e) {
if (!empty($e->_label) && $e->_label == 'Contribution Amount') {
$e->_label = 'Payment Amount';
}
elseif (!empty($e->_name) && $e->_name == 'is_recur') {
foreach ($e->_elements as &$f) {
if (!empty($f->_text)) {
$f->_text = str_replace(array('contribution', 'contribute'), array('payment', 'pay'), $f->_text);
}
}
}
}
}
}
}
/**
* Implements hook_civicrm_pre.
*/
function woolman_website_civicrm_pre($op, $type, $id, &$p) {
$access = user_access('access CiviCRM');
// Clean up user-entered data
if (($op == 'create' || $op == 'edit')
&& ($type == 'Individual' || $type == 'Organization' || $type == 'Address')) {
if ($type == 'Address') {
// Don't mess with shared addresses
if (!empty($p['master_id']) && is_numeric($p['master_id'])) {
return;
}
$cleanup = array('street_address', 'city');
// Move zip+4 to correct field
if (empty($p['postal_code_suffix']) && woolman_aval($p, 'country_id', USA) == USA
&& strpos(woolman_aval($p, 'postal_code', ''), '-')) {
list($p['postal_code'], $p['postal_code_suffix']) = explode('-', $p['postal_code'], 2);
}
}
else {
$cleanup = array('first_name', 'nick_name', 'last_name', 'middle_name', 'organization_name');
}
$sep = array('', ' ', "'", '-', '.');
foreach ($cleanup as $fid) {
if (!empty($p[$fid]) && is_string($par = &$p[$fid])) {
$par = trim($par);
if (($op == 'create' || !$access)
&& ($par == strtolower($par) || ($par == strtoupper($par) && !strpos($par, '.') && strlen($par) > 3))) {
// Iterate through string and capitalize words (an improved version of ucwords())
$parray = str_split(strtolower($par));
$par = $l = '';
foreach ($parray as $c) {
if (in_array($l, $sep)) {
if ($c == $l) {
// Strip excess punctuation
continue;
}
$c = strtoupper($c);
}
$par .= $l = $c;
}
}
}
}
if ($type == 'Individual' && (isset($p['last_name']) || isset($p['first_name']) || isset($p['nick_name']))) {
$first = strtolower(woolman_aval($p, 'first_name', ''));
$nick = strtolower(woolman_aval($p, 'nick_name', ''));
$last = strtolower(woolman_aval($p, 'last_name', ''));
if ($op == 'edit' && (arg(0) != 'civicrm' || !$access) && woolman_name('last', $id)) {
$existing_first = strtolower(woolman_name('first', $id));
$existing_last = strtolower(woolman_name('last', $id));
$existing_nick = strtolower(woolman_name('nick', $id));
// Send a warning to the site admin if the name gets totally changed
if (isset($p['last_name']) && $last != $existing_last
&& ($first != $existing_first || $nick != $existing_nick)) {
$old = woolman_name('display', $id);
$new = ucwords($first . ' ' . ($nick ? "($nick) " : '') . $last);
$args = $_GET;
unset($args['q']);
$link = url($_GET['q'], array('absolute' => TRUE, 'query' => $args));
$message = array(
'subject' => "Contact \"$old\" changed to \"$new\"",
'body' => "A contact's name has been altered, possibly in error. This is often caused by a user filling out a form which was intended for a specific person (i.e. while logged-in or via a hashed link), but instead erasing the name and entering a different person.\n\n" .
" Old name: $old\n" .
" New name: $new\n " .
woolman_contact_url($id) .
"\n\nThis change was made by " . woolman_name('full') . " (cid " . woolman_user_cid() . ")\n" .
"from IP " . ip_address() . "\n" .
"on " . date('M j Y, g:i a') . "\n" .
"on the page: \"" . drupal_get_title() . "\" $link\n\n" .
"-This message brought to you by the Woolman Website module.",
);
drupal_mail('woolman_website', 0, 'webmaster@woolman.org', language_default(), $message, 'info@woolman.org');
}
$existing_nick = $existing_nick == $existing_first ? '' : $existing_nick;
// Prevent accidental deletion of nick names
if ($existing_nick == $first && !empty($first) && empty($nick)) {
$p['nick_name'] = $p['first_name'];
$p['first_name'] = woolman_name('first', $id);
}
}
// Get rid of redundant nick names
if ($nick
&& ($first == $nick || $last == $nick || ($first . ' ' . $last) == $nick)) {
$p['nick_name'] = '';
}
}
}
}
/**
* Implements hook_civicrm_post.
* Ignore $object because this hook is often passed an incomplete object
* Instead we use $object to prevent recursion when updating spouse
*/
function woolman_website_civicrm_post($op, $type, $id, $object) {
// Build a "smart" name & greeting
if (($op == 'create' || $op == 'edit') && $type == 'Individual' && !empty($id)) {
$c = CRM_Core_DAO::executeQuery("
SELECT con.first_name, con.middle_name, con.last_name, con.nick_name, con.display_name, con.sort_name, con.postal_greeting_display, con.email_greeting_display, con.addressee_display, con.postal_greeting_id, con.email_greeting_id, con.addressee_id, s.label as suffix, spouse.first_name AS spouse_first, spouse.nick_name AS spouse_nick, spouse.last_name AS spouse_last, spouse.id AS spouse_id
FROM civicrm_contact con
LEFT JOIN civicrm_option_value s ON s.value = con.suffix_id AND s.option_group_id = (SELECT id FROM civicrm_option_group WHERE name = 'individual_suffix')
LEFT JOIN civicrm_contact spouse ON (spouse.id IN (SELECT contact_id_a FROM civicrm_relationship WHERE contact_id_b = con.id AND relationship_type_id = 2 AND is_active = 1 AND end_date IS NULL) OR spouse.id IN (SELECT contact_id_b FROM civicrm_relationship WHERE contact_id_a = con.id AND relationship_type_id = 2 AND is_active = 1 AND end_date IS NULL)) AND spouse.is_deceased = 0 AND spouse.is_deleted = 0
WHERE con.id = $id
LIMIT 0, 1");
$c->fetch();
if (strtolower($c->first_name) == strtolower($c->nick_name) && $c->nick_name) {
$samename = TRUE;
$c->nick_name = NULL;
}
else {
$samename = FALSE;
}
$display_name = $addressee_display = $c->first_name . ' ';
$postal_greeting_display = $email_greeting_display = ($c->nick_name ? $c->nick_name : $c->first_name);
$sort_name = $c->last_name . ', ' . $c->first_name;
if ($c->nick_name) {
$display_name .= '(' . $c->nick_name . ') ';
}
if ($c->middle_name) {
$middle = str_replace('.', ' ', $c->middle_name);
$middle = str_replace(' ', ' ', $middle);
$mi = '';
foreach (explode(' ', $middle) as $m) {
if ($m) {
$mi .= ($mi ? ' ' : '') . strtoupper($m[0]) . '.';
}
}
$sort_name .= ' ' . $mi;
if ($c->nick_name !== $c->middle_name) {
$display_name .= $mi . ' ';
}
}
$display_name .= $c->last_name;
if ($c->suffix) {
$display_name .= ' ' . $c->suffix;
}
if ($c->spouse_first && $c->spouse_last) {
$postal_greeting_display .= ' and ' . ($c->spouse_nick ? $c->spouse_nick : $c->spouse_first);
if ($c->spouse_last == $c->last_name) {
$addressee_display .= 'and ' . $c->spouse_first . ' ' . $c->last_name;
}
else {
$addressee_display .= $c->last_name . ' and ' . $c->spouse_first . ' ' . $c->spouse_last;
}
}
else {
$addressee_display .= $c->last_name;
if ($c->suffix) {
$addressee_display .= ' ' . $c->suffix;
}
}
// Use email if no name
if (!trim($display_name)) {
$postal_greeting_display = $email_greeting_display = 'friend';
$email = CRM_Core_DAO::singleValueQuery("SELECT email FROM civicrm_email WHERE is_primary = 1 AND contact_id = $id");
if ($email) {
$sort_name = $display_name = $addressee_display = $email;
}
}
if ($display_name !== $c->display_name || $sort_name !== $c->sort_name || $addressee_display !== $c->addressee_display || $postal_greeting_display !== $c->postal_greeting_display || !$c->addressee_id || !$c->postal_greeting_id || !$c->email_greeting_id || $samename) {
$q = "UPDATE civicrm_contact SET display_name = %1, sort_name = %2";
$args = array(
1 => array($display_name, 'String'),
2 => array($sort_name, 'String'),
);
$x = 3;
foreach (array('addressee', 'postal_greeting', 'email_greeting') as $n) {
if ((${$n . '_display'} !== $c->{$n . '_display'} && $c->{$n . '_id'} == 1) || !$c->{$n . '_id'}) {
$q .= ", {$n}_id = 1, {$n}_display = %$x";
$args[$x] = array(${$n . '_display'}, 'String');
++$x;
}
}
if ($samename) {
$q .= ", nick_name = NULL";
}
$q .= " WHERE id = %$x";
$args[$x] = array($id, 'Integer');
CRM_Core_DAO::executeQuery($q, $args);
}
// Clear cache if user changes own name
if ($id === woolman_aval($_SESSION, 'CiviCRM:userID')) {
unset($_SESSION['woolman_name']);
}
// Update spouse record, prevent recursion
if ($c->spouse_id && $object !== 'called_from_spouse') {
woolman_website_civicrm_post('edit', 'Individual', $c->spouse_id, 'called_from_spouse');
}
}
elseif ($type === 'Relationship' && isset($object->relationship_type_id) && $object->relationship_type_id == 2) {
// Also call the above code when creating/updating spouse relationship
woolman_website_civicrm_post('edit', 'Individual', $object->contact_id_a, 'called_from_spouse');
woolman_website_civicrm_post('edit', 'Individual', $object->contact_id_b, 'called_from_spouse');
}
}
/**
* Implements hook_civicrm_searchColumns.
*/
function woolman_website_civicrm_searchColumns($type, &$headers, &$rows, &$selector) {
switch (strtolower($type)) {
case 'contribution':
// Overwrite 'Premium' column with 'Campaign', and append account info to donations
foreach ($headers as &$h) {
if ($h['sort'] == 'product_name' || $h['name'] == 'Premium') {
unset($h['sort']);
$h['name'] = 'Campaign';
}
}
$acct = array();
foreach ($rows as $n => &$r) {
if ($r['contribution_type'] == 'Donation') {
$acct[$r['contribution_id']] = $n;
}
$r['product_name'] = $r['campaign'];
}
// Fetch account data from custom field
if ($acct) {
$sql = "
SELECT entity_id AS eid, account_18 AS acct, designation_60 AS des
FROM civicrm_value_contribution_accounts_10
WHERE account_18 <> '' AND entity_id IN (" . implode(',', array_keys($acct)) . ')';
$dao = &CRM_Core_DAO::executeQuery($sql);
while ($dao->fetch()) {
$d = $dao->des ? $dao->des : ucwords($dao->acct);
$rows[$acct[$dao->eid]]['contribution_type'] .= ' (<em>' . $d . '</em>)';
}
}
break;
}
}
/**
* Implements hook_civicrm_postProcess.
*
* @param string $formName
* @param CRM_Core_Form $form
*/
function woolman_website_civicrm_postProcess($formName, &$form) {
// Record contribution submissions by IP address for flood control, set to expire in 2 hours
if ($formName == 'CRM_Contribute_Form_Contribution_Confirm') {
$ip = ip_address();
$n = 1;
if (($c1 = cache_get('civicontribute_flood_1_' . $ip)) && time() < $c1->expire) {
++$n;
}
cache_set('civicontribute_flood_' . $n . '_' . $ip, 1, 'cache', strtotime('+2 hours'));
}
// Reload name when editing contact inline
if ($formName == 'CRM_Contact_Form_Inline_ContactInfo') {
$form->ajaxResponse['reloadBlocks'][] = '#crm-contactname-content';
}
}
/**
* Implements hook_civicrm_validate.
*/
function woolman_website_civicrm_validate($formName, &$fields, &$files, &$form) {
// Validate contribution pages for flood control
// If 2 entries from this IP address exist, reject the form submission
if ($formName == 'CRM_Contribute_Form_Contribution_Main') {
$ip = ip_address();
if ((($c1 = cache_get('civicontribute_flood_1_' . $ip)) && ($c2 = cache_get('civicontribute_flood_2_' . $ip)))
&& time() < $c1->expire && time() < $c2->expire) {
watchdog('civicontribute_flood', 'Prevented contribution form submission from ip: ' . $ip);
return array('qfKey' => 'Sorry, for security reasons we do not allow more than two credit card transactions at a time. If you feel you have received this message in error, please contact us at 530-273-3183.');
}
}
return TRUE;
}
/**
* Implements hook_civicrm_dupeQuery.
*/
function woolman_website_civicrm_dupeQuery($obj, $type, &$query) {
if ($type == 'table' && empty($obj->noRules)) {
module_load_include('inc', 'woolman_website', 'woolman_dupe_query');
woolman_dupe_query($obj, $type, $query);
}
}
/**
* Implements hook_civicrm_tokens.
*/
function woolman_website_civicrm_tokens(&$tokens) {
$tokens['date'] = array(
'date.date_short' => 'Today\'s Date: dd/mm/yyyy',
'date.date_med' => 'Today\'s Date: Mon d yyyy',
'date.date_long' => 'Today\'s Date: Month dth, yyyy',
);
$tokens['relatives'] = array();
foreach (array('p1' => 'Parent 1', 'p2' => 'Parent 2', 'ec' => 'Emergency Contact') as $p => $n) {
$tokens['relatives']['relatives.' . $p . '_display_name'] = $n . ' Display Name';
$tokens['relatives']['relatives.' . $p . '_address'] = $n . ' Address';
$tokens['relatives']['relatives.' . $p . '_email'] = $n . ' Email';
$tokens['relatives']['relatives.' . $p . '_phone'] = $n . ' Phone';
if ($p != 'ec') {
$tokens['relatives']['relatives.' . $p . '_role'] = $n . ' Role';
}
}
$tokens['transcript'] = array(
'transcript.official' => 'Transcript: Official',
'transcript.narrative' => 'Transcript: Narrative',
);
$tokens['student'] = array(
'student.attendance_dates' => 'Student Attendance Dates',
'student.status' => 'Student Status',
);
$tokens['contact']['contact.address_format'] = 'Address (Full)';
$tokens['donor'] = array(
'donor.unthanked' => 'Donations: To Thank',
'donor.set_thank_you' => 'Donations: UPDATE unthanked (hidden)',
'donor.clear_thank_you' => 'Donations: CLEAR thanked today (hidden)',
);
}
/**
* Implements hook_civicrm_tokenValues.
*/
function woolman_website_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {
$contacts = implode(',', $cids);
$tokens += array(
'contact' => array(),
);
// Bug in CiviCRM displays empty options as a long string of zeroes
$sp = CRM_Core_DAO::VALUE_SEPARATOR;
foreach ($values as $key => $vals) {
foreach ($vals as $k => $v) {
if (is_string($v) && strpos($v, $sp . '0' . $sp) !== FALSE) {
$values[$key][$k] = '';
}
}
}
// Date tokens
if (!empty($tokens['date'])) {
$date = array(
'date.date_short' => date('m/d/Y'),
'date.date_med' => date('M j Y'),
'date.date_long' => date('F jS, Y'),
);
foreach ($cids as $cid) {
$values[$cid] = empty($values[$cid]) ? $date : $values[$cid] + $date;
}
}
// Fill first name and nick name with default values
if (in_array('first_name', $tokens['contact']) || in_array('nick_name', $tokens['contact'])) {
$dao = &CRM_Core_DAO::executeQuery("
SELECT first_name, nick_name, contact_type, id
FROM civicrm_contact
WHERE id IN ($contacts)"
);
while ($dao->fetch()) {
$cid = $dao->id;
if (!($values[$cid]['first_name'] = $dao->first_name)) {
$values[$cid]['first_name'] = $dao->contact_type == 'Individual' ? 'Friend' : 'Friends';
}
if (empty($values[$cid]['nick_name']) || $dao->contact_type != 'Individual') {
$values[$cid]['nick_name'] = $values[$cid]['first_name'];
}
}
}
// Format birth dates
if (in_array('birth_date', $tokens['contact'])) {
foreach ($values as $k => $v) {
if (!empty($v['birth_date']) && $bd = strtotime($v['birth_date'])) {
$values[$k]['birth_date'] = date('m/d/Y', $bd);
}
}
}
// Formatted address token
if (in_array('address_format', $tokens['contact'])) {
$dao = &CRM_Core_DAO::executeQuery('
SELECT *
FROM civicrm_address
WHERE contact_id IN (' . $contacts . ') AND is_primary = 1'
);
while ($dao->fetch()) {
$values[$dao->contact_id]['contact.address_format'] = woolman_format_address($dao);
}
}
// Related contact tokens
if (!empty($tokens['relatives'])) {
// Set defaults
foreach ($cids as $cid) {
$values[$cid]['relatives.p1_role'] = 'Parent 1';
$values[$cid]['relatives.p2_role'] = 'Parent 2';
$values[$cid]['relatives.ec_role'] = 'Emergency Contact';
}
$rtype = $rtokens = $found = array();
foreach ($tokens['relatives'] as $t) {
list($c, $d) = explode('_', $t, 2);
if ($c == 'ec') {
$rtype[19] = 19;
}
else {
$rtype[1] = 1;
}
$rtokens[$d] = $d;
}
$select = 'r.contact_id_a, r.relationship_type_id, c.id, c.display_name';
$join = 'INNER JOIN civicrm_contact c ON c.id = r.contact_id_b';
if (isset($rtokens['role'])) {
$select .= ', c.gender_id, cv.parental_role_8';
$join .= ' LEFT JOIN civicrm_value_parent_child_5 cv ON r.id = cv.entity_id';
}
if (isset($rtokens['address'])) {
$select .= ', a.street_address, a.supplemental_address_1, a.city, a.state_province_id, a.country_id, a.postal_code, a.postal_code_suffix';
$join .= ' LEFT JOIN civicrm_address a ON c.id = a.contact_id AND a.is_primary = 1';
}
if (isset($rtokens['email'])) {
$select .= ', e.email';
$join .= ' LEFT JOIN civicrm_email e ON c.id = e.contact_id AND e.is_primary = 1';
}
$dao = &CRM_Core_DAO::executeQuery('
SELECT ' . $select . '
FROM civicrm_relationship r
' . $join . '
WHERE r.contact_id_a IN (' . $contacts . ')
AND r.relationship_type_id IN (' . implode(',', $rtype) . ')
ORDER BY r.is_active DESC'
);
$loc_type = array();
foreach (woolman_get_civi_options('location_type') as $k => $v) {
$loc_type[$k] = $v == 'Mobile' ? 'c' : strtolower($v[0]);
}
while ($dao->fetch()) {
$cid = $dao->contact_id_a;
if ($dao->relationship_type_id == 1) {
$rel = isset($values[$cid]['relatives.p1_display_name']) ? 'p2' : 'p1';
if (isset($values[$cid]['relatives.p2_display_name'])) {
continue;
}
}
else {
$rel = 'ec';
if (isset($values[$cid]['relatives.ec_display_name'])) {
continue;
}
}
$values[$cid]['relatives.' . $rel . '_display_name'] = $dao->display_name;
if (in_array($rel . '_role', $tokens['relatives']) && $rel != 'ec') {
$role = $dao->parental_role_8 ? $dao->parental_role_8 : 'Parent';
if ($dao->gender_id == 1) {
$role = str_replace('Parent', 'Mother', $role);
}
if ($dao->gender_id == 2) {
$role = str_replace('Parent', 'Father', $role);
}
$values[$cid]['relatives.' . $rel . '_role'] = $role;
}
if (in_array($rel . '_address', $tokens['relatives'])) {
$values[$cid]['relatives.' . $rel . '_address'] = woolman_format_address($dao);
}
if (isset($dao->email)) {
$values[$cid]['relatives.' . $rel . '_email'] = $dao->email;
}
if (in_array($rel . '_phone', $tokens['relatives'])) {
$phone = array();
$result = woolman_civicrm_api('phone', 'get', array('contact_id' => $dao->id), 'values');
foreach($result as $p) {
$phone[$p['phone']] = $p['phone'] . ' (' . $loc_type[$p['location_type_id']] . ')';
}
$values[$cid]['relatives.' . $rel . '_phone'] = implode(', ', $phone);
}
}
}
// Transcript tokens
// <strong>Credits Earned:</strong> ' . $earned_credit . ' <br />' .
if (!empty($tokens['transcript'])) {
$dao = CRM_Core_DAO::executeQuery("
SELECT *
FROM civicrm_value_transcript_16
WHERE entity_id IN ($contacts)
ORDER BY IF(grade_56 IN ('P', 'F'), 1, 0), course_credits_57 DESC, course_name_55 ASC"
);
while ($dao->fetch()) {
$cid = $dao->entity_id;
$earned_credit = ($dao->grade_56 && $dao->grade_56 != 'W' && $dao->grade_56 != 'F' && $dao->grade_56 != 'I') ? $dao->course_credits_57 : '0.0';
if (empty($values[$cid]['transcript.official'])) {
$values[$cid]['transcript.official'] = '<table class="transcript"><thead><tr><th>Course Name</th><th>Credits Attempted</th><th>Credits Earned</th><th>Grade</th></tr></thead><tbody>';
}
$values[$cid]['transcript.official'] .= '
<tr>
<td>' . $dao->course_name_55 . '</td>
<td>' . $dao->course_credits_57 . '</td>
<td>' . $earned_credit . '</td>
<td>' . $dao->grade_56 . '</td>
</tr>';
if (empty($values[$cid]['transcript.narrative'])) {
$values[$cid]['transcript.narrative'] = '';
}
$values[$cid]['transcript.narrative'] .= '<hr />
<div>
<div>
<h3>' . $dao->course_name_55 . '</h3>
<strong>Grade:</strong> ' . $dao->grade_56 .'<br />
<strong>Credits Earned:</strong> ' . $earned_credit . ' <br />' .
'</div>
<div>' .
$dao->evaluation_58 .
'</div>
</div>';
}
foreach ($cids as $cid) {
if (!empty($values[$cid]['transcript.official'])) {
$values[$cid]['transcript.official'] .= '</tbody></table>';
}
}
}
// Student info
if (!empty($tokens['student'])) {
$dao = &CRM_Core_DAO::executeQuery('
SELECT r.contact_id_a, r.start_date, r.end_date, v.reason_for_leaving_52
FROM civicrm_relationship r
LEFT JOIN civicrm_value_student_info_14 v ON v.entity_id = r.id
WHERE r.contact_id_a IN (' . $contacts . ')
AND r.relationship_type_id = 10
AND r.contact_id_b = ' . WOOLMAN
);
while ($dao->fetch()) {
$cid = $dao->contact_id_a;
$start = $end = '';
if ($dao->start_date) {
$start = date('m/d/Y', strtotime($dao->start_date));
}
if ($dao->end_date) {
$end = date('m/d/Y', strtotime($dao->end_date));
}
$values[$cid]['student.attendance_dates'] = $start . ' - ' . $end;
if ($end && strtotime($dao->end_date) > time()) {
$values[$cid]['student.status'] = 'Enrolled';
}
else {
$values[$cid]['student.status'] = $dao->reason_for_leaving_52;
}
}
}
// Dontation info
if (!empty($tokens['donor'])) {
$spouses = array();
$contacts_and_spouses = $cids;
$dao = &CRM_Core_DAO::executeQuery("
SELECT contact_id_a, contact_id_b
FROM civicrm_relationship
WHERE relationship_type_id = 2
AND is_active = 1
AND (end_date IS NULL OR end_date > CURDATE())
AND (contact_id_a IN ($contacts) OR contact_id_b IN ($contacts))
");
while ($dao->fetch()) {
if (!in_array($dao->contact_id_a, $contacts_and_spouses)) {
$contacts_and_spouses[] = $dao->contact_id_a;
}
if (!in_array($dao->contact_id_b, $contacts_and_spouses)) {
$contacts_and_spouses[] = $dao->contact_id_b;
}
if (in_array($dao->contact_id_a, $cids)) {
$spouses[$dao->contact_id_b] = $dao->contact_id_a;
}
if (in_array($dao->contact_id_b, $cids)) {
$spouses[$dao->contact_id_a] = $dao->contact_id_b;
}
}
$contacts_and_spouses = implode(',', $contacts_and_spouses);
// Clear thank-yous from past 48hrs (a kind of crude UNDO)
if (in_array('clear_thank_you', $tokens['donor'])) {
CRM_Core_DAO::executeQuery("
UPDATE civicrm_contribution SET thankyou_date = NULL
WHERE is_test = 0 AND financial_type_id IN (1,9) AND contribution_status_id = 1
AND DATE(thankyou_date) + INTERVAL 2 DAY > CURDATE() AND contact_id IN ($contacts_and_spouses)"
);
}
if (in_array('unthanked', $tokens['donor'])) {
$dao = &CRM_Core_DAO::executeQuery("
SELECT
cc.contact_id, cc.total_amount, cc.receive_date, cc.check_number, cc.financial_type_id,
con.display_name,
hon.display_name as honoree,
ac.label AS account,
pi.label AS payment_instrument,
ht.label AS honor_type,
ca.designation_60 AS designation,
n.note
FROM civicrm_contribution cc
INNER JOIN civicrm_contact con ON con.id = cc.contact_id
LEFT JOIN civicrm_contribution_soft soft ON soft.contribution_id = cc.id and soft.soft_credit_type_id IN (1,2)
LEFT JOIN civicrm_contact hon ON hon.id = soft.contact_id
LEFT JOIN civicrm_value_contribution_accounts_10 ca ON ca.entity_id = cc.id
LEFT JOIN civicrm_option_value ac ON ca.account_18 = ac.value AND ac.option_group_id = 103
LEFT JOIN civicrm_option_value pi ON cc.payment_instrument_id = pi.value AND pi.option_group_id = (SELECT id FROM civicrm_option_group WHERE name = 'payment_instrument')
LEFT JOIN civicrm_option_value ht ON soft.soft_credit_type_id = ht.value AND ht.option_group_id = (SELECT id FROM civicrm_option_group WHERE name = 'soft_credit_type')
LEFT JOIN civicrm_note n ON n.entity_table = 'civicrm_contribution' AND n.entity_id = cc.id
WHERE cc.is_test = 0 AND cc.financial_type_id IN (1,9) AND cc.contribution_status_id = 1
AND cc.contact_id IN ($contacts_and_spouses) AND cc.thankyou_date IS NULL
ORDER BY cc.receive_date"
);
$header = '
<table class="donations" style="border-collapse:collapse; width: 100%; text-align:left;">
<thead><tr style="text-align:left;">
<th>Date</th>
<th>Donor</th>
<th>Amount</th>
<th>Paid By</th>
<th>Given for</th>
</tr></thead>
<tbody>';
$td = '<td style="border: 1px solid black;">';
while ($dao->fetch()) {
$cid = $dao->contact_id;
$row = '
<tr>' .
$td . date('m/d/Y', strtotime($dao->receive_date)) . '</td>' .
$td . $dao->display_name . '</td>' .
$td . '$' . number_format($dao->total_amount, 2) . '</td>' .
$td . ($dao->payment_instrument ? $dao->payment_instrument : 'In Kind')
. ($dao->check_number ? ' #' . $dao->check_number : '') . '</td>' .
$td . ($dao->account ? ($dao->designation ? $dao->designation : $dao->account) : $dao->note) . ($dao->honoree ? "<br />{$dao->honor_type} {$dao->honoree}" : '') . '</td>
</tr>';
if (in_array($cid, $cids)) {
$values[$cid]['donor.unthanked'] = woolman_aval($values[$cid], 'donor.unthanked', $header) . $row;
}
if (isset($spouses[$cid])) {
$values[$spouses[$cid]]['donor.unthanked'] = woolman_aval($values[$spouses[$cid]], 'donor.unthanked', $header) . $row;
}
}
foreach ($cids as $cid) {
if (!empty($values[$cid]['donor.unthanked'])) {
$values[$cid]['donor.unthanked'] .= '</tbody></table>';
}
}
}
if (in_array('set_thank_you', $tokens['donor'])) {
CRM_Core_DAO::executeQuery("
UPDATE civicrm_contribution SET thankyou_date = NOW()
WHERE is_test = 0 AND financial_type_id IN (1,9) AND contribution_status_id = 1
AND contact_id IN ($contacts_and_spouses) AND thankyou_date IS NULL"
);
}
}
// Medical Info
foreach ($cids as $cid) {
if (empty($values[$cid]['custom_48'])) {
$values[$cid]['custom_48'] = '<br /><strong>Please Circle:</strong> Tylenol, Ibuprofin, Antihistamine, Hydrocortisone, Tums, Anti-Dirrheal, Other______________________________';
}
}
}