forked from tadpolecc/cc.tadpole.petitionemail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpetitionemail.php
1581 lines (1483 loc) · 57.3 KB
/
petitionemail.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
require_once 'petitionemail.civix.php';
// You can define multiple pairs of target groups to
// matching field. This constant defines how many are
// presented in the user interface.
define('PETITIONEMAIL_ALLOWED_GROUP_FIELD_COMBINATIONS_COUNT', 3);
/**
* Implementation of hook_civicrm_config
*/
function petitionemail_civicrm_config(&$config) {
_petitionemail_civix_civicrm_config($config);
}
/**
* Implementation of hook_civicrm_xmlMenu
*
* @param $files array(string)
*/
function petitionemail_civicrm_xmlMenu(&$files) {
_petitionemail_civix_civicrm_xmlMenu($files);
}
/**
* Implementation of hook_civicrm_install
*/
function petitionemail_civicrm_install() {
return _petitionemail_civix_civicrm_install();
}
/**
* Implementation of hook_civicrm_uninstall
*/
function petitionemail_civicrm_uninstall() {
// Clear out our variables.
petitionemail_remove_profiles();
petitionemail_remove_custom_fields();
petitionemail_remove_variables();
return _petitionemail_civix_civicrm_uninstall();
}
/**
* Implementation of hook_civicrm_enable
*/
function petitionemail_civicrm_enable() {
// Ensure the profile id is created.
petitionemail_create_custom_fields();
petitionemail_get_profile_id('petitionemail_profile_matching_fields');
petitionemail_get_profile_id('petitionemail_profile_default_contact');
petitionemail_get_profile_id('petitionemail_profile_default_activity');
return _petitionemail_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_disable
*/
function petitionemail_civicrm_disable() {
return _petitionemail_civix_civicrm_disable();
}
/**
* Implementation of hook_civicrm_upgrade
*
* @param $op string, the type of operation being performed; 'check' or 'enqueue'
* @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
*
* @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
* for 'enqueue', returns void
*/
function petitionemail_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _petitionemail_civix_civicrm_upgrade($op, $queue);
}
/**
* Implementation of hook_civicrm_managed
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*/
function petitionemail_civicrm_managed(&$entities) {
return _petitionemail_civix_civicrm_managed($entities);
}
/**
* Implemention of hook_civicrm_buildForm
*/
function petitionemail_civicrm_buildForm( $formName, &$form ) {
if ($formName == 'CRM_Campaign_Form_Petition_Signature') {
$survey_id = $form->getVar('_surveyId');
if ($survey_id) {
$sql = "SELECT petition_id,
default_message,
subject,
message_field,
subject_field,
subject
FROM civicrm_petition_email
WHERE petition_id = %1";
$params = array( 1 => array( $survey_id, 'Integer' ) );
$dao = CRM_Core_DAO::executeQuery( $sql, $params );
$defaults = array();
$dao->fetch();
if($dao->N == 0) {
// Not a email enabled petition
return;
}
$message_field = $dao->message_field;
$subject_field = $dao->subject_field;
$defaults[$message_field] = $dao->default_message;
$defaults[$subject_field] = $dao->subject;
$form->setDefaults($defaults);
}
}
if ($formName == 'CRM_Campaign_Form_Petition') {
CRM_Core_Resources::singleton()->addScriptFile('cc.tadpole.petitionemail', 'petitionemail.js');
$survey_id = $form->getVar('_surveyId');
if ($survey_id) {
// Set default values for saved petitions.
$sql = "SELECT petition_id,
default_message,
message_field,
subject_field,
subject,
recipients,
location_type_id
FROM civicrm_petition_email
WHERE petition_id = %1";
$params = array( 1 => array( $survey_id, 'Integer' ) );
$dao = CRM_Core_DAO::executeQuery( $sql, $params );
$dao->fetch();
if($dao->N > 0) {
// Base table values.
$defaults['email_petition'] = 1;
$defaults['recipients'] = $dao->recipients;
$defaults['default_message'] = $dao->default_message;
$defaults['message_field'] = $dao->message_field;
$defaults['subject_field'] = $dao->subject_field;
$defaults['subject'] = $dao->subject;
$defaults['location_type_id'] = $dao->location_type_id;
// Now get matching fields.
$sql = "SELECT matching_field, matching_group_id FROM
civicrm_petition_email_matching_field WHERE petition_id = %1";
$dao = CRM_Core_DAO::executeQuery($sql, $params);
$matching_fields = array();
$i = 1;
while($dao->fetch()) {
$defaults['matching_field' . $i] = $dao->matching_field;
$defaults['matching_group_id' . $i] = $dao->matching_group_id;
$i++;
}
// We have to build this URL by hand to avoid having the curly
// braces get escaped.
$base_url = preg_match('#/$#', CIVICRM_UF_BASEURL) ? CIVICRM_UF_BASEURL : CIVICRM_UF_BASEURL . '/';
$base_url = $base_url . "civicrm/petition/sign?sid=$survey_id&reset=1";
$personal_url = $base_url . '&{contact.checksum}&cid={contact.contact_id}';
$defaults['links'] = ts("Personal link (use this link if you are sending it via CiviMail,
it will auto fill with the user's address): ") . "\n" .
$personal_url . "\n\n" . ts("General link: ") . $base_url;
$form->setDefaults($defaults);
}
}
else {
$form->setDefaults(
array(
'links' => ts("Please save the petition first, then you can copy and
paste the link to sign the petition.")
)
);
}
// Now add our extra fields to the form.
$form->add('checkbox', 'email_petition', ts('Send an email to a target'));
// Get the Profiles in use by this petition so we can find out
// if there are any potential fields for an extra message to the
// petition target.
$params = array('module' => 'CiviCampaign',
'entity_table' => 'civicrm_survey',
'entity_id' => $survey_id,
'rowCount' => 0);
$join_results = civicrm_api3('UFJoin','get', $params);
$custom_fields = array();
$profile_ids = array();
if ($join_results['is_error'] == 0) {
foreach ($join_results['values'] as $join_value) {
$profile_ids[] = $join_value['uf_group_id'];
}
}
$custom_fields = petitionemail_get_text_fields($profile_ids);
$custom_field_options = array();
if(count($custom_fields) == 0) {
$custom_field_options = array(
'' => ts('- No Text or TextArea fields defined in your profiles -')
);
}
else {
$custom_field_options = array('' => t('- Select -'));
$custom_field_options = $custom_field_options + $custom_fields;
}
$choose_one = array('0' => ts('Primary'));
$group_options = $choose_one + CRM_Core_PseudoConstant::group('Mailing');
$location_options = $choose_one +
CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
$field_options = petitionemail_get_matching_field_options();
$field_options_count = count($field_options);
if($field_options_count == 0) {
// No matching fields!
$field_options[''] = ts("No fields are configured");
}
else {
array_unshift($field_options, ts("--Choose one--"));
}
$form->assign('petitionemail_matching_fields_count', $field_options_count);
$url_params = array(
'gid' => petitionemail_get_profile_id('petitionemail_profile_matching_fields'),
'action' => 'browse'
);
$url = CRM_Utils_System::url("civicrm/admin/uf/group/field", $url_params);
$form->assign('petitionemail_profile_edit_link', $url);
$i = 1;
while($i <= PETITIONEMAIL_ALLOWED_GROUP_FIELD_COMBINATIONS_COUNT) {
$form->add('select', 'matching_group_id' . $i, ts('Matching Target Group'), $group_options);
$form->add('select', 'matching_field' . $i, ts('Matching field(s)'), $field_options);
$i++;
}
// We can't make default message and default subject required,
// otherwise users will get an error if they submit a petition
// that doesn't want to send an email to the target. So we have
// our own custom validation checking that only complains if
// the user wants to send an email and we have to insert the
// asterisk ourselves, manually.
$required = ' <span title="This field is required." class="crm-marker">*</span>';
$form->add('select', 'location_type_id', ts('Email'), $location_options);
$form->add('textarea', 'recipients', ts("Send petitions to"), 'rows=20 cols=100');
$form->add('select', 'message_field', ts('Custom Message Field'),
$custom_field_options);
$form->add('select', 'subject_field', ts('Custom Subject Field'),
$custom_field_options);
$form->add('textarea', 'default_message', ts('Default Message') . $required, 'rows=20 cols=100');
$form->add('text', 'subject', ts('Default Email Subject Line') . $required, array('size' => 70));
$form->add('textarea', 'links', ts('Links to sign the petition'), 'rows=5')->freeze();
}
}
/**
* Get fields from the special petition email profile.
*
* Filter out un-supported fields.
*/
function petitionemail_get_matching_field_options() {
$session = CRM_Core_Session::singleton();
$ret = array();
$uf_group_id = petitionemail_get_profile_id('petitionemail_profile_matching_fields');
$fields = CRM_Core_BAO_UFGroup::getFields($uf_group_id);
$allowed = petitionemail_get_allowed_matching_fields();
if(is_array($fields)) {
reset($fields);
while(list($id, $value) = each($fields)) {
$include = FALSE;
// Check to see if it's a custom field
if(preg_match('/^custom_/', $id)) {
$ret[$id] = $value['title'];
continue;
}
else {
// Check to see if it's an address field
$field_pieces = petitionemail_split_address_field($id);
if($field_pieces) {
if($field_pieces['location_name'] != 'Primary') {
$session->setStatus(ts("Only primary address fields are support at this time."));
continue;
}
if(array_key_exists($field_pieces['field_name'], $allowed)) {
$ret[$id] = $value['title'];
continue;
}
}
}
// Warn the user about a field that is not allowed
$session->setStatus(ts("The field $id is not supported as a matching field at this time."));
}
}
return $ret;
}
/**
* Validate the petition form
*
* Ensure our values are consistent to avoid broken petitions.
*/
function petitionemail_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
if ($formName == 'CRM_Campaign_Form_Petition_Signature') {
// Do some basic sanity checking to prevent spammers
if(empty($form->_surveyId)) {
// Can't do much without the survey_id
return;
}
$survey_id = $form->_surveyId;
// Check to see if it's an email petition
$sql = "SELECT message_field FROM civicrm_petition_email WHERE
petition_id = %0";
$dao = CRM_Core_DAO::executeQuery($sql, array(0 => array($survey_id, 'Integer')));
$dao->fetch();
if($dao->N == 0) {
// Nothing to do
return;
}
if(!empty($dao->message_field)) {
$field_name = 'custom_' . $dao->message_field;
// If we are allowing a user-supplied message field, ensure it doesn't
// have any URLs or HTML in it.
if(array_key_exists($field_name, $fields)) {
if(preg_match('#https?://#i', $fields[$field_name])) {
$errors[$field_name] = ts("To avoid spammers, you are not allowed to put web addresses in your message. Please revise your message and try again.");
}
// Now ensure we have no html tag
if (preg_match('/([\<])([^\>]{1,})*([\>])/i', $fields[$field_name] )) {
$errors[$field_name] = ts("To avoid spammers, you are not allowed to put HTML code in your message. Please revise your message and try again.");
}
}
}
}
if($formName == 'CRM_Campaign_Form_Petition') {
if(CRM_Utils_Array::value('email_petition', $fields)) {
// Make sure we have a subject field and a default message.
if(!CRM_Utils_Array::value('subject', $fields)) {
$msg = ts("You must enter an email subject line.");
$errors['subject'] = $msg;
}
if(!CRM_Utils_Array::value('default_message', $fields)) {
$msg = ts("You must enter a default message.");
$errors['default_message'] = $msg;
}
// For each matching_group_id, make sure we have a corresponding
// matching field.
$i = 1;
$using_dynamic_method = FALSE;
while($i <= PETITIONEMAIL_ALLOWED_GROUP_FIELD_COMBINATIONS_COUNT) {
$matching_group_id = CRM_Utils_Array::value('matching_group_id' . $i, $fields);
$matching_field = CRM_Utils_Array::value('matching_field' . $i, $fields);
if(!empty($matching_group_id) && empty($matching_field)) {
$msg = ts("If you select a matching target group you must select
a corresponding matching field.");
$errors['matching_field' . $i] = $msg;
}
if(empty($matching_group_id) && !empty($matching_field)) {
$msg = ts("If you select a matching field you must select a
corresponding matching target group.");
$errors['matching_group_id' . $i] = $msg;
}
// Keep track to see if there are using the dynamic method
if(!empty($matching_group_id)) {
$using_dynamic_method = TRUE;
}
$i++;
}
// If additional email targets have been provided, make sure they are
// all syntactically correct.
$recipients = CRM_Utils_Array::value('recipients', $fields);
if(!empty($recipients)) {
$recipient_array = explode("\n", $recipients);
while(list(,$line) = each($recipient_array)) {
if(FALSE === petitionemail_parse_email_line($line)) {
$errors['recipients'] = ts("Invalid email address listed: %1.", array(1 => $line));
}
}
}
if(!$using_dynamic_method && empty($recipients)) {
$msg = ts("You must select either one target matching group/field or list
at least one address to send all petitions to.");
$errors['recipients'] = $msg;
}
}
}
}
/**
* Given an array of profile ids, list all text area fields
*/
function petitionemail_get_text_fields($profile_ids) {
// Now get all fields in this profile
$custom_fields = array();
while(list(,$uf_group_id) = each($profile_ids)) {
$params = array('uf_group_id' => $uf_group_id, 'rowCount' => 0);
$field_results = civicrm_api3('UFField', 'get', $params);
if ($field_results['is_error'] == 0) {
foreach ($field_results['values'] as $field_value) {
$field_name = $field_value['field_name'];
if(!preg_match('/^custom_[0-9]+/', $field_name)) {
// We only know how to lookup field types for custom
// fields. Skip core fields.
continue;
}
$id = substr(strrchr($field_name, '_'), 1);
// Finally, see if this is a text or textarea field.
$params = array('id' => $id);
$custom_results = civicrm_api3('CustomField', 'get', $params);
if ($custom_results['is_error'] == 0) {
$field_value = array_pop($custom_results['values']);
$html_type = $field_value['html_type'];
$label = $field_value['label'];
$id = $field_value['id'];
if($html_type == 'Text' || $html_type == 'TextArea') {
$custom_fields['custom_' . $id] = $label;
}
}
}
}
}
return $custom_fields;
}
function petitionemail_civicrm_postProcess( $formName, &$form ) {
if ($formName != 'CRM_Campaign_Form_Petition') {
return;
}
$email_petition = CRM_Utils_Array::value('email_petition', $form->_submitValues);
$survey_id = $form->getVar('_surveyId');
if($email_petition && $email_petition == 1 ) {
$lastmoddate = 0;
if (!$survey_id) { // Ugly hack because the form doesn't return the id
$params = array('title' =>$form->_submitValues['title']);
$surveys = civicrm_api3("Survey", "get", $params);
if (is_array($surveys['values'])) {
foreach($surveys['values'] as $survey) {
if ($lastmoddate > strtotime($survey['last_modified_date'])) {
continue;
}
$lastmoddate = strtotime($survey['last_modified_date']);
$survey_id = $survey['id'];
}
}
}
if (!$survey_id) {
$msg = ts('Cannot find the petition for saving email delivery fields.');
CRM_Core_Session::setStatus($msg);
return;
}
$default_message = $form->_submitValues['default_message'];
$message_field = $form->_submitValues['message_field'];
$subject_field = $form->_submitValues['subject_field'];
$subject = $form->_submitValues['subject'];
$recipients = $form->_submitValues['recipients'];
$location_type_id = $form->_submitValues['location_type_id'];
$sql = "REPLACE INTO civicrm_petition_email (
petition_id,
default_message,
message_field,
subject_field,
subject,
recipients,
location_type_id
) VALUES (
%1,
%2,
%3,
%4,
%5,
%6,
%7
)";
$params = array(
1 => array( $survey_id, 'Integer' ),
2 => array( $default_message, 'String' ),
3 => array( $message_field, 'String' ),
4 => array( $subject_field, 'String' ),
5 => array( $subject, 'String' ),
6 => array( $recipients, 'String' ),
7 => array( $location_type_id, 'Integer' ),
);
$petitionemail = CRM_Core_DAO::executeQuery( $sql, $params );
// delete any existing ones
$sql = "DELETE FROM civicrm_petition_email_matching_field WHERE
petition_id = %0";
$params = array(0 => array($survey_id, 'Integer'));
CRM_Core_DAO::executeQuery($sql, $params);
$i = 1;
while($i <= PETITIONEMAIL_ALLOWED_GROUP_FIELD_COMBINATIONS_COUNT) {
$matching_group_id = CRM_Utils_Array::value('matching_group_id' . $i, $form->_submitValues);
$matching_field = CRM_Utils_Array::value('matching_field' . $i, $form->_submitValues);
if(!empty($matching_group_id) && !empty($matching_field)) {
$sql = "INSERT INTO civicrm_petition_email_matching_field SET
petition_id = %0, matching_field = %1, matching_group_id = %2";
$params = array(
0 => array($survey_id, 'Integer'),
1 => array($matching_field, 'String'),
2 => array($matching_group_id, 'Integer')
);
CRM_Core_DAO::executeQuery($sql, $params);
}
$i++;
}
} else {
// This removes targets from petition
// If a petitions is initially configured to have targets, but they need to be removed.
$target_delete_params = array(0 => array($survey_id, 'Integer'));
$matching_target_delete_sql = "DELETE FROM civicrm_petition_email_matching_field WHERE petition_id = %0";
CRM_Core_DAO::executeQuery($matching_target_delete_sql, $target_delete_params);
$target_delete_sql = "DELETE FROM civicrm_petition_email WHERE petition_id = %0";
CRM_Core_DAO::executeQuery($target_delete_sql, $target_delete_params);
}
}
/**
* Implementation of hook_civicrm_post
*
* Run everytime a post is made to see if it's a new profile/activity
* that should trigger a petition email to be sent. Also clean up
* our tables if a petition is deleted.
*/
function petitionemail_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {
static $profile_fields = NULL;
if($objectName == 'Profile' && is_array($objectRef)) {
// This is hacky but seems to be unavoidable. We really want to run
// on the activity post create hook. However, the activity post create
// hook is called *before* the custom fields are saved for the activity
// record. That means that none of the custom fields are available when
// it is called, so we can't provide a custom subject or custom message
// field to the petitionemail_process_signature function.
//
// However, the profile post hook is called before the activity post
// hook is called. So, we set a static variable when the profile post
// hook is called to save all the fields being submitted and then make
// that available when the activity post hook is called.
$profile_fields = $objectRef;
}
if ($objectName == 'Activity') {
$activity_id = $objectId;
// Only run on creation. For petitions that require a confirmation,
// after the petition has been created, see petitionemail_civicrm_pageRun().
if($op == 'create') {
if(petitionemail_is_actionable_activity($activity_id)) {
petitionemail_process_signature($activity_id, $profile_fields);
}
}
}
}
/**
* Implementation of hook_civicrm_pageRun
*/
function petitionemail_civicrm_pageRun(&$page) {
// This should be fired after most of the parent run()
// code is done, which means the activity status should
// be converted to "complete" if it has been properly
// verified.
$pageName = $page->getVar('_name');
if ($pageName == 'CRM_Campaign_Page_Petition_Confirm') {
// Get the activity id from the URL
$activity_id = CRM_Utils_Request::retrieve('a', 'String', CRM_Core_DAO::$_nullObject);
if(petitionemail_is_actionable_activity($activity_id)) {
petitionemail_process_signature($activity_id);
}
}
}
function petitionemail_get_petition_details($petition_id) {
$ret = array();
$sql = "SELECT default_message,
message_field,
subject_field,
subject,
location_type_id,
recipients
FROM civicrm_petition_email
WHERE petition_id = %1 GROUP BY petition_id";
$params = array( 1 => array( $petition_id, 'Integer' ) );
$petition_email = CRM_Core_DAO::executeQuery( $sql, $params );
$petition_email->fetch();
if($petition_email->N == 0) {
// Must not be a petition with a target.
return FALSE;;
}
// Store variables we need
$ret['default_message'] = $petition_email->default_message;
$ret['subject'] = $petition_email->subject;
$ret['location_type_id'] = $petition_email->location_type_id;
$ret['message_field'] = $petition_email->message_field;
$ret['subject_field'] = $petition_email->subject_field;
$ret['recipients'] = $petition_email->recipients;
// Now retrieve the matching fields, if any
$sql = "SELECT matching_field, matching_group_id FROM
civicrm_petition_email_matching_field WHERE petition_id = %1";
$params = array( 1 => array( $petition_id, 'Integer' ) );
$dao = CRM_Core_DAO::executeQuery($sql, $params);
$ret['matching'] = array();
while($dao->fetch()) {
$ret['matching'][$dao->matching_field] = $dao->matching_group_id;
}
return $ret;
}
/**
* This function handles all petition signature processing.
*
* @activity_id integer The activity id of the signature activity
* @profile_fields array An array of fields submitted by the user, which
* may include the custom subject and custom message values.
*/
function petitionemail_process_signature($activity_id, $profile_fields = NULL) {
$petition_id = petitionemail_get_petition_id_for_activity($activity_id);
if(empty($petition_id)) {
$log = "Failed to find petition id for activity id: $activity_id";
CRM_Core_Error::debug_log_message($log);
return FALSE;
}
$petition_vars = petitionemail_get_petition_details($petition_id);
if(!$petition_vars) {
// Nothing to process, this isn't an email target enabled petition
return;
}
$default_message = $petition_vars['default_message'];
$default_subject = $petition_vars['subject'];
$message_field = $petition_vars['message_field'];
$subject_field = $petition_vars['subject_field'];
$activity = civicrm_api3("Activity", "getsingle", array ('id' => $activity_id));
$contact_id = $activity['source_contact_id'];
$contact = civicrm_api3("Contact", "getsingle", array ('id' => $contact_id));
// Figure out whether to use the user-supplied message/subject or the default
// message/subject.
$petition_message = NULL;
$subject = NULL;
// If the petition has specified a message field
if(!empty($message_field)) {
// Check for a custom message field value in the passed in profile fields.
// This field will be populated if we are operating on a new activity via
// the post hook.
if(is_array($profile_fields) && !empty($profile_fields[$message_field])) {
$petition_message = $profile_fields[$message_field];
}
else {
// Retrieve the value of the field for this activity (this may happen
// if we are operating on a confirmation click from pageRun hook).
$params = array(
'id' => $activity_id,
'return' => $message_field
);
$result = civicrm_api3('Activity', 'getsingle', $params);
if(!empty($result[$message_field])) {
$petition_message = $result[$message_field];
}
}
}
if(is_null($petition_message)) {
$petition_message = $default_message;
}
// CiviCRM seems to htmlentitize everything submitted, but we are
// preventing any html tags in our validation and we want to avoid
// weird htmlentites being added to text messages.
$petition_message = html_entity_decode($petition_message);
// Add the sending contacts address info
$address_block = petitionemail_get_address_block($contact_id);
if($address_block) {
$petition_message = strip_tags($address_block) . "\n\n" . $petition_message;
}
// If the petition has specified a subject field
if(!empty($subject_field)) {
// Check for a custom subject field value in the passed in profile fields.
// This field will be populated if we are operating on a new activity via
// the post hook.
if(is_array($profile_fields) && !empty($profile_fields[$subject_field])) {
$subject = $profile_fields[$subject_field];
}
else {
// Retrieve the value of the field for this activity (this may happen
// if we are operating on a confirmation click from pageRun hook).
$params = array(
'id' => $activity_id,
'return' => $subject_field
);
$result = civicrm_api3('Activity', 'getsingle', $params);
if(!empty($result[$subject_field])) {
$subject = $result[$subject_field];
}
}
}
// No user supplied message/subject, use the default
if(is_null($subject)) {
$subject = $default_subject;
}
// CiviCRM seems to htmlentitize everything submitted, but we don't
// want 3 > 1 in a subject line get converted to 3 > 1
$subject = html_entity_decode($subject);
$from = NULL;
if (empty($contact['email'])) {
$domain = civicrm_api3("Domain", "get", array ());
if ($domain['is_error'] != 0 || !is_array($domain['values'])) {
// Can't send email without a from address.
$msg = "petition_email: Failed to send petition email because from
address not sent.";
CRM_Core_Error::debug_log_message($msg);
return;
}
$contact['email'] = $domain['values']['from_email'];
}
$from = $contact['display_name'] . ' <' . $contact['email'] . '>';
// Setup email message (except to address)
$email_params = array(
'from' => $from,
'toName' => NULL,
'toEmail' => NULL,
'subject' => $subject,
'text' => $petition_message,
'html' => NULL,
);
// Get array of recipients
$recipients = petitionemail_get_recipients($contact_id, $petition_id);
// Keep track of the targets we actually send the message to so we can
// email the petition signer to let them now.
$message_sent_to = array();
while(list(, $recipient) = each($recipients)) {
if(!empty($recipient['email'])) {
$log = "petition email: contact id ($contact_id) sending to email (" .
$recipient['email'] . ")";
CRM_Core_Error::debug_log_message($log);
if(!empty($recipient['contact_id'])) {
// Since we're sending to a recipient in the database, create an
// email activity. Note: we are not using the built-in
// function to create (and send) and email activity because it won't
// send if the contact has DoNotEmail they won't get it. However, it's
// normal to have DoNotEmail for your targets, but you still want them
// to get the petition.
$log = "petition email: recording email as activity against ".
"target contact id: " . $recipient['contact_id'];
CRM_Core_Error::debug_log_message($log);
$contactDetails = array(0 => $recipient);
// We are sending a text message, so ensure it's the preferred one
$contactDetails[0]['preferred_mail_format'] = 'Text';
$subject = $email_params['subject'];
$text = $email_params['text'];
$html = $email_params['html'];
$emailAddress = $recipient['email'];
$userID = $contact_id;
$from = NULL; // This will be pulled from $contact_id,
$attachments = NULL;
$cc = NULL;
$bcc = NULL;
$contactIds = array($recipient['contact_id']);
// Create the activity first, then we will send the email.
$activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name');
$activity_type_id = array_search('Email', $activityTypes);
$activity_status = CRM_Core_PseudoConstant::activityStatus();
$status_id = array_search('Completed', $activity_status);
$params = array(
'activity_type_id' => $activity_type_id,
'subject' => $subject,
'details' => $text,
'source_contact_id' => $contact_id,
'target_contact_id' => $recipient['contact_id'],
'status_id' => $status_id
);
$activity_id = NULL;
try {
$ret = civicrm_api3('Activity', 'create', $params);
$value = array_pop($ret['values']);
$activity_id = $value['id'];
}
catch (CiviCRM_API3_Exception $e) {
$log = "petition email: email activity not created";
$log .= $e->getMessage();
CRM_Core_Error::debug_log_message($log);
return FALSE;
}
if($activity_id) {
// Update the activity with the petition id so we can properly
// report on the email messages sent as a result of this petition.
$params = array(
'activity_id' => $activity_id,
'source_record_id' => $petition_id
);
$result = civicrm_api3('Activity', 'update', $params);
if($result['is_error'] != 0) {
$log = "civicrm petition: failed to update activity with ".
"source_record_id";
CRM_Core_Error::debug_log_message($log);
}
}
}
// Now send all email.
// Handle targets not in the database.
$email_params['toName'] = $recipient['name'];
$email_params['toEmail'] = $recipient['email'];
$to = $email_params['toName'] . ' <' . $email_params['toEmail'] . '>';
$log = "petition_email: sending petition to '$to' via mail function.";
CRM_Core_Error::debug_log_message($log);
$success = CRM_Utils_Mail::send($email_params);
if($success == 1) {
CRM_Core_Session::setStatus( ts('Message sent successfully to: ') . htmlentities($to), '', 'success' );
$log = "petition_email: message sent.";
$message_sent_to[] = $to;
} else {
$log = "petition_email: message was not sent.";
}
CRM_Core_Error::debug_log_message($log);
}
}
}
function petitionemail_get_address_block($contact_id) {
$sql = "SELECT display_name, street_address, city, ".
"s.abbreviation AS state_province, postal_code FROM civicrm_contact c JOIN ".
"civicrm_address a ON c.id = a.contact_id JOIN civicrm_state_province s ".
"on a.state_province_id = s.id WHERE is_primary = 1 ".
"AND c.id = %0";
$params = array(0 => array($contact_id, 'Integer'));
$dao = CRM_Core_DAO::executeQuery($sql, $params);
$dao->fetch();
if($dao->N == 0) {
return NULL;
}
$block = $dao->display_name . "\n" .
$dao->street_address . "\n" .
$dao->city . ", " .
$dao->state_province .
" " .
$dao->postal_code;
return $block;
}
/**
* Non custom data fields allowed to be a matching field.
*
* All custom fields can be used as matching fields, but only
* a subset of non-custom fields (so we can be sure to build
* a working query to retrieve them).
*/
function petitionemail_get_allowed_matching_fields() {
$ret = array(
'street_name' => 'civicrm_address',
'street_number' => 'civicrm_address',
'street_name' => 'civicrm_address',
'city' => 'civicrm_address',
'county_id' => 'civicrm_address',
'state_province' => 'civicrm_address',
'postal_code' => 'civicrm_address',
'postal_code_suffix' => 'civicrm_address',
'country_id' => 'civicrm_address',
);
return $ret;
}
function petitionemail_get_recipients($contact_id, $petition_id) {
$petition_vars = petitionemail_get_petition_details($petition_id);
if(!$petition_vars) {
// Not an email target enabled petition
return;
}
$ret = array();
// First, parse the additional recipients, if any. These get the email
// regarldess of who signs it.
if(!empty($petition_vars['recipients'])) {
$recipients = explode("\n", $petition_vars['recipients']);
while(list(,$recipient) = each($recipients)) {
$email_parts = petitionemail_parse_email_line($recipient);
if(FALSE !== $email_parts) {
$ret[] = array(
'contact_id' => NULL,
'name' => $email_parts['name'],
'email' => $email_parts['email']
);
}
}
}
// If there are any matching criteria (for a dynamic lookup) we do a
// complex query to figure out which members of the group should be
// included as recipients.
if(count($petition_vars['matching']) > 0) {
// This comes as an array with the key being the matching field and
// the value being the matching_group_id.
$matching_fields = $petition_vars['matching'];
// Get the values of the matching fields for the contact. These values
// are used to match the contact who signed the petition with the
// contact or contacts in the target group.
// Given the matching fields, we're going to do an API call against
// the contact to get the values that we will be matching on.
// Build a return_fields array that we will pass to the api call to
// specify the fields we want returned with this query.
$field_names = array_keys($matching_fields);
$return_fields = array();
reset($field_names);
while(list(, $field_name) = each($field_names)) {
// If the field_name starts with custom_ we can add it straight
// away.
if(preg_match('/^custom_/', $field_name)) {
$return_fields[] = $field_name;
continue;
}
// Look for field names with a - in them - that's an indication
// that it's an address field which will have the location part
// stuck into the name.
$field_pieces = petitionemail_split_address_field($field_name);
if($field_pieces) {
if($field_pieces['location_name'] == 'Primary') {
// Primary will be included via the api call, so we just need
// the field name. If it's not primary, we'll have to do a
// manual SQL call below to get the value.
$return_fields[] = $field_pieces['field_name'];
continue;
}
}
// FIXME If we get here, this is an error
}
$contact_params = array('return' => $return_fields, 'id' => $contact_id);
$contact = civicrm_api3('Contact', 'getsingle', $contact_params);
while(list($matching_field) = each($matching_fields)) {
// Check if the field was returned. If not, it's probably an address field
if(array_key_exists($matching_field, $contact)) {
$matching_fields[$matching_field] = $contact[$matching_field];
continue;
}
// This means it's probably an address field.
$field_pieces = petitionemail_split_address_field($matching_field);
if(!$field_pieces) {
// FIXME This is an error
continue;
}
$location_name = $field_pieces['location_name'];
$field_name = $field_pieces['field_name'];
// NOTE: we only work with primary fields.
if($location_name == 'Primary' && array_key_exists($field_name, $contact)) {
// The field name returned by the API won't have the -location part.
$matching_fields[$matching_field] = $contact[$field_name];
continue;
}
else {
// FIXME This is an error
continue;
}
}
// Initialize variables to build the SQL statement
$from = array();
// The master $where clause will be put together using AND
$where = array();
$params = array();
$added_tables = array();
// Initialize the from clause and where clause
$from[] = 'civicrm_contact c';
$where[] = 'c.is_deleted = 0';
// We build a sub where clause that limits results based on the
// matching group and matching field that will be put together using
// OR since we match any any of the matching field => group
// combinations.
$sub_where = array();
reset($matching_fields);
$id = 0;
while(list($matching_field, $value) = each($matching_fields)) {
// The $where_fragment will be put together using AND because
// you have to match both the group and the field.
$where_fragment = array();
// Gather information about the group that is paired with this
// matching field.