-
Notifications
You must be signed in to change notification settings - Fork 4
/
connect_actions.php
1527 lines (1357 loc) · 53.1 KB
/
connect_actions.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
// $Id: connect_actions.php,v 1.9.2.6 2010/02/28 01:12:03 stevem Exp $
/*
* action functions make themselves known and take effect when called as hooks from connect
*
* @params $op
* 'requires' -> describe parent & child data requirements for config UI
* 'menu' -> return array of cached menu items, used to register callbacks
* 'validate' -> evaluate child content (NB $child = (array) $form_data, not (obj) $child)
* 'form_alter' -> allows actions to affect the child node form when before it's displayed
* 'insert' -> handle changes to $target nodes
* 'update' -> handle changes to $target nodes
* 'delete' -> handle changes to $target nodes
* 'display' -> node is being displayed
* 'status' -> reports on status of child node interaction
* 'redirect' -> sets a destination instead of the form again on successful submit
* 'admin-validate' -> function settings are being validated
* ($parent->nid contains the parent id, and $parent->data holds the form values)
*
* @return
* if $op='display', an array('data' => string)
* if $op='status', an array('message' => string, 'show_form' => boolean)
* if $op='validate', array('status' => boolean, 'message' => string)
*/
/**
*
* Implementation of hook_connect
*
* declares any action functions to be made available to connect parent nodes
*
* return array('function callback name' => array('title' => string, 'desc' => string)
*
*
**/
function connect_connect() {
$actions = array(
'connect_action_basic' => array(
'title' => 'Basic settings',
),
'connect_action_redirect_submit' => array(
'title' => 'Redirect on submit',
'desc' => 'Allows you to specify a URL or node to present on successful form submission.',
),
'connect_action_display_progress' => array(
'title' => 'Display progress bar',
'desc' => 'Adds a CSS-based display showing how many of the target number have participated.',
),
'connect_action_voteonce' => array(
'title' => 'One vote per person',
'desc' => 'Prevent the same person from participating more than once.',
),
'connect_action_display_participants' => array(
'title' => 'Display participants',
'desc' => 'Select fields to display in a public listing of participants. (Double opt-in enabled.)',
),
'connect_action_content_replace' => array(
'title' => 'Content: rewrite',
'desc' => 'Allows participants to revise the content provided by the parent node.',
),
'connect_action_content_append' => array(
'title' => 'Content: append',
'desc' => 'Allows participants to add their own comments, etc. to the content provided by the parent node.',
),
'connect_action_send_email' => array(
'title' => 'Send email',
'desc' => 'Sends email to/cc/bcc addresses that you specify or that can be looked up on the basis of participant information.',
),
'connect_action_double_optin' => array(
'title' => 'Double opt-in',
'desc' => 'Sends an email to the participant confirming participation. <strong>Partly implemented. Right now only the "display participants" function respects it.</strong>',
),
);
return $actions;
}
/**
* Mandatory action, handles basic housekeeping
*/
function connect_action_basic(&$parent, &$child, $op='', $target='parent') {
switch($op) {
case 'requires' :
$return = array();
$default = connect_node_options($parent->nid, 'debug_hooks');
$default = $default ? $default : 'no';
$return['variables']['debug_hooks'] = array(
'#type' => 'radios',
'#title' => t('Turn on debugging?'),
'#description' => t('This option turns on a display of debugging messages that can be useful when troubleshooting connect problems. (Don\'t bother turning this on unless you are a developer.)'),
'#options' => array('yes' => 'Yes', 'no' => 'No'),
'#default_value' => $default,
'#required' => TRUE,
);
$types = connect_participant_types_options();
$types[0] = '';
asort($types);
$return['variables']['participant_type'] = array(
'#type' => 'select',
'#title' => 'Participant node type',
'#options' => $types,
'#default_value' => connect_node_options($parent->nid, 'participant_type'),
'#required' => TRUE,
);
$return['variables']['participant_title'] = array(
'#type' => 'textfield',
'#title' => 'Title for the participants',
'#default_value' => connect_node_options($parent->nid, 'participant_title'),
'#required' => TRUE,
);
$return['variables']['call_to_action'] = array(
'#type' => 'textfield',
'#title' => 'Call to action',
'#default_value' => connect_node_options($parent->nid, 'call_to_action'),
'#required' => TRUE,
);
$thank_you_description = t('This message will be shown when the action is complete. You can use the following placeholders, which will be filled in from the information they fill out:');
$child_type = connect_node_options($parent->nid, 'participant_type');
$child_fields = _connect_get_child_fields($child_type);
foreach($child_fields as $field_name => $field_label) {
if(!empty($field_name)) { $thank_you_description .= " %$field_name% "; }
}
$return['variables']['thankyou'] = array(
'#type' => 'textarea',
'#size' => 4,
'#title' => 'Thank-you message',
'#default_value' => connect_node_options($parent->nid, 'thankyou'),
'#description' => $thank_you_description,
'#required' => TRUE,
);
return $return;
break;
case 'status' :
// display 'thank you' message
if (isset($_SESSION['connect_action_thanks_'.$parent->nid])) {
$message = $_SESSION['connect_action_thanks_'.$parent->nid];
unset($_SESSION['connect_action_thanks_'.$parent->nid]);
return array(
'status' => $message,
'show_form' => FALSE,
);
}
break;
case 'insert' :
if ($target != 'child') break;
// create parent-child mapping
$sql = "INSERT INTO {connect_data} (nid,pid) VALUES (%d,%d);";
db_query($sql, $child->nid, $parent->nid);
// record fact of current user's participation
$message = connect_node_options($parent->nid, 'thankyou');
// @TODO - this would probably be better done with the token module.
$child_type = connect_node_options($parent->nid, 'participant_type');
$child_fields = _connect_get_child_fields($child_type);
foreach($child_fields as $fieldName => $fieldLabel) {
if(!empty($fieldName)) {
$fieldPath = _connect_get_field_path($child,$fieldName);
eval("\$fieldValue = \$child".$fieldPath.";");
$message = str_replace('%'.$fieldName.'%', $fieldValue, $message);
}
}
$_SESSION['connect_action_basic_'.$parent->nid] = TRUE;
$_SESSION['connect_action_thanks_'.$parent->nid] = $message;
break;
case 'delete' :
if ($target != 'child') break;
// delete parent-child mapping
$sql = "DELETE FROM {connect_data} WHERE nid=%d;";
db_query($sql, $child->nid);
break;
}
}
/**
* Set a custom redirect page post-connect-form-submission
*/
function connect_action_redirect_submit(&$parent, &$child, $op='', $target='parent') {
switch($op) {
case 'requires' :
$return = array();
$return['variables'] = array(
'redirect_submit_target' => array(
'#type' => 'textfield',
'#size' => 20,
'#title' => 'Target page',
'#default_value' => connect_node_options($parent->nid, 'redirect_submit_target'),
'#required' => TRUE,
),
);
return $return;
break;
case 'redirect' :
$return = connect_node_options($parent->nid, 'redirect_submit_target');
return $return;
break;
}
}
/**
* provide a signing block in addition to or instead of the regular form
* - blocks are declared to Drupal in connect.module
* - see connect_blocks.php for block generation code
*/
/*
function connect_action_provide_block(&$parent, &$child, $op='', $target='parent') {
switch($op) {
case 'describe' :
$return = array();
$return['title'] = 'Provide a block';
$return['desc'] = 'Makes the participation form available as a Drupal block.';
return $return;
break;
case 'requires' :
$return = array();
$visibility = connect_node_options($parent->nid, 'provide_block_visibility');
$visibility = empty($visibility) ? 0 : $visibility;
$return['variables'] = array(
'provide_block_text' => array(
'#type' => 'textfield',
'#size' => 20,
'#title' => 'Text for link to main node',
'#description' => 'If blank, no link to the main node will appear in the block',
'#default_value' => connect_node_options($parent->nid, 'provide_block_text'),
'#required' => FALSE,
),
'provide_block_noform' => array(
'#type' => 'checkbox',
'#title' => 'Prevent display of main form',
'#default_value' => connect_node_options($parent->nid, 'provide_block_noform'),
'#required' => TRUE,
),
'provide_block_visibility' => array(
'#type' => 'radios',
'#title' => 'Block visibility',
'#options' => array(
'use regular block configuration',
'only display on this page',
'display everywhere except this page',
),
'#default_value' => $visibility,
'#required' => TRUE,
),
);
return $return;
break;
case 'status' :
if (connect_node_options($parent->nid, 'provide_block_noform')) {
return array(
'show_form' => FALSE,
);
}
}
}
*/
/**
* Adds a CSS-based display to the parent node showing how many of the target number of participants have participated
*/
function connect_action_display_progress(&$parent, &$child, $op='', $target='parent') {
switch($op) {
case 'requires' :
$return = array();
$return['variables'] = array(
'display_progress_goal' => array(
'#type' => 'textfield',
'#size' => 20,
'#title' => 'Target no. of participants',
'#default_value' => connect_node_options($parent->nid, 'display_progress_goal'),
'#required' => TRUE,
),
);
return $return;
break;
case 'theme_register':
$return = array(
'connect_display_progress' => array(
'arguments' => array(
$goal => 0,
$pct => 0,
$pct_display => 0,
$title => NULL,
),
),
);
return $return;
break;
case 'display' :
if ($target != 'parent' || $parent == NULL) return;
$done = connect_participant_count($parent);
$title = connect_node_options($parent->nid, 'participant_title');
$goal = connect_node_options($parent->nid, 'display_progress_goal');
if ($goal > 0) {
$pct = round($done/$goal, 2) * 100;
$pct_display = $pct > 100 ? 100 : $pct;
}
else {
$goal = 0;
$pct = 0;
$pct_display = 0;
}
$return['data'] = theme('connect_display_progress', $goal, $pct, $pct_display, $title);
return $return;
break;
}
}
function theme_connect_display_progress($goal, $pct, $pct_display, $title) {
$progress_bar = <<<EOT
<div>
<div id="connect-progress-report">$goal $title</div>
<div id="connect-progress-border">
<div id="connect-progress-bar" style="width: $pct_display%;">
$pct%
</div>
</div>
</div>
EOT;
return $progress_bar;
}
/**
* Enforces a one person, one vote policy based on a unique identifier
*/
function connect_action_voteonce(&$parent, &$child, $op='', $target='child') {
$message = connect_node_options($parent->nid, 'voteonce_message');
$message = $message ? $message : t('You have already participated.');
switch($op) {
case 'requires' :
$return = array();
$return['child'] = array(
'voteonce_identifier' => 'Unique identifier (i.e., full name, membership #, or email address)',
);
$return['variables'] = array(
'voteonce_message' => array(
'#type' => 'textfield',
'#title' => 'Message to display when someone attempts to participate more than once',
'#default_value' => $message,
'#required' => TRUE,
),
);
return $return;
break;
case 'validate' :
// don't check for this when editing an existing child node
if (isset($child->status) && $child->status == 0) return;
$map = connect_get_map($parent->nid);
$test_value = connect_value('voteonce_identifier', $parent, $child, 'child');
$fieldname = $map['voteonce_identifier'];
$field_keys = connect_get_field_keys($fieldname);
$test_db = _connect_get_cck_db_info($fieldname);
$test_table = $test_db['table'];
$test_column = $test_db['columns'][$field_keys[0]]['column'];
$sql = "SELECT count(*) FROM {".$test_table."} t, {connect_data} p WHERE t.nid=p.nid AND t.$test_column = '%s' AND p.pid = %d";
$count = db_result(db_query($sql, array($test_value, $parent->nid)));
$already_voted = ($count != 0);
if ($already_voted) form_set_error('', t($message));
break;
case 'status' :
// if this user has already participated
if (isset($_SESSION['connect_action_basic_'.$parent->nid])) {
return array(
'status' => $message,
'show_form' => FALSE,
);
}
break;
}
}
/**
* provide a list of participants
*/
function connect_action_display_participants(&$parent, &$child, $op='', $target='child') {
switch($op) {
case 'requires' :
$return = array();
$return['variables'] = array(
'display_participants_pager' => array(
'#type' => 'textfield',
'#title' => 'How many participants would you like to display at a time?',
'#default_value' => connect_node_options($parent->nid, 'display_participants_pager') ? connect_node_options($parent->nid, 'display_participants_pager') : 25,
'#required' => TRUE,
),
);
$return['child'] = array(
'display_participants_displayme' => 'Display my name in public lists',
);
// does this action use double opt-in?
$double_opt_in = connect_value('double_optin_token', $parent, $child, 'child');
if ($double_opt_in) {
$return['child']['double_optin_token'] = 'Double opt-in confirmation field';
}
// select child fields to display
$idx = 0;
$child_type = connect_node_options($parent->nid, 'participant_type');
$cck_options = _connect_get_child_fields($child_type);
$map = connect_get_map($parent->nid);
if (!empty($cck_options[$map['display_participants_displayme']])) {
unset($cck_options[$map['display_participants_displayme']]);
if (!empty($cck_options)) {
$return['variables']['display_participants_fields_intro'] = array(
'#value' => 'Select the items to display, in order.',
);
foreach ($cck_options as $name=>$title) {
if (empty($name)) continue;
$return['variables']['display_participants_fields_' . $idx] = array(
'#type' => 'select',
'#title' => '',
'#options' => $cck_options,
'#default_value' => connect_node_options($parent->nid, 'display_participants_fields_' . $idx),
'#required' => FALSE,
);
// need at least one!
if ($idx == 0) {
$return['variables']['display_participants_fields_' . $idx]['#required'] = TRUE;
$return['variables']['display_participants_fields_' . $idx]['#title'] = 'participant info';
}
$idx++;
}
}
}
return $return;
case 'theme_register':
$return = array(
'connect_action_display_participants' => array(
'arguments' => array('parent_id' => NULL),
),
'connect_action_display_participants_return' => array(
'arguments' => array('parent_title' => NULL, 'arent_nid' => NULL),
),
);
return $return;
break;
case 'menu' :
$return = array(
'connect/participants' => array(
'title' => 'Display participants',
'type' => MENU_CALLBACK,
'page callback' => '_connect_action_display_participants',
'access arguments' => array('access content'),
),
);
return $return;
break;
case 'display' :
if ($target != 'parent' || $parent == NULL) return;
$return['data'] = theme('connect_action_display_participants', $parent->nid);
return $return;
break;
}
}
/* themeable function */
function theme_connect_action_display_participants($parent_id) {
$link = l(t('Display '). connect_node_options($parent_id, 'participant_title'), 'connect/participants/'. $parent_id);
return "<div id='connect-display-participants'>$link</div>";
}
/* themeable function */
function theme_connect_action_display_participants_return($parent_title = NULL, $parent_nid = NULL) {
$link = l(t('Return to ') . check_plain($parent_title), 'node/'. $parent_nid);
return "<div id='connect-returnto-link'>» ". $link .'</div>';
}
/**
* allows participant-generated content to replace the parent content
*/
function connect_action_content_replace(&$parent, &$child, $op='', $target='child') {
switch($op) {
case 'requires' :
$return = array();
$return['parent'] = array(
'data_replace_parent' => 'Content replace: the content that can be rewritten.',
);
$return['child'] = array(
'data_replace_child' => 'Content replace: the participant\'s version.',
);
return $return;
case 'form_alter':
$map = connect_get_map($parent->nid);
$field = $map['data_replace_child'];
$key = connect_get_field_keys($field);
// filter text if required
$cck_info = _content_type_info();
$cck_vars = $cck_info['content types'][connect_node_options($parent->nid, 'participant_type')]['fields'];
$text = connect_value('data_replace_parent', $parent, $child, 'parent');
if ($cck_vars[$field]['text_processing'] == 0) $text = strip_tags($text);
$child[$field][0]['#default_value']['value'] = $text;
break;
case 'insert' :
if ($target == 'child') {
$addition = connect_value('data_replace_child', $parent, $child, 'child');
connect_value('data_replace_parent', $parent, $child, 'parent', $addition);
}
break;
}
}
/**
* allows participant-generated content to be added to the parent content
*/
function connect_action_content_append(&$parent, &$child, $op = '', $target = 'child') {
switch($op) {
case 'requires' :
$return = array();
$return['parent'] = array(
'data_append_parent' => 'Content append: the content that can be added on to.',
);
$return['child'] = array(
'data_append_child' => 'Content append: the participant\'s addition.',
);
return $return;
case 'insert' :
if ($target == 'child') {
$original = connect_value('data_append_parent', $parent, $child, 'parent');
$addition = connect_value('data_append_child', $parent, $child, 'child');
connect_value('data_append_parent', $parent, $child, 'parent', "$original\n$addition");
}
break;
}
}
/* Allow form to be embedded in another site */
/*
function connect_action_embed(&$parent, &$child, $op = '', $target = 'child') {
switch ($op) {
case 'describe' :
$return = array();
$return['title'] = 'Make Embeddable';
$return['desc'] = 'Makes petition form embeddable in external site';
return $return;
break;
//case 'menu' :
//$items = array();
//$items[] = array(
//'path' => 'connect/embed',
//'title' => 'Connect Embed',
//'callback' => '_connect_embed',
//'type' => MENU_CALLBACK,
//'access' => TRUE,
//);
//return $items;
//break;
//case 'form_alter' : //loop through and reduce the length of textfields
//foreach ()
//$child[$field][0][$key[0]]['#default_value'] = connect_value('data_replace_parent', $parent, $child, 'parent');
//return;
}
}
function _connect_embed($nid = NULL) {
if (!is_numeric(arg(2)) || !arg(2)) {
print '<p>Please specify a parent ID</p>';
return;
}
$parent_node = node_load($nid);
if (!isset($parent_node)) {
print '<p>Invalid ID</p>';
return;
}
if (!connect_is_parent_node($parent_node)){
print '<p>Referenced node is not a parent type</p>';
return;
}
$parent_node =& _connect_parent_node($parent_node);
// require a CAPTCHA on the form?
//if (!_connect_captcha_test('connect_form_block')) {
//return $return;
//}
$form = drupal_get_form('connect_form_block');
//$link = empty($text) ? '' : '<p>» '. l($text, "node/$nid") .'</p>';
print '<h2>' . $parent_node->title . '</h2>' . $parent_node->body . $form;
}
*/
/**
* send email
*/
function connect_action_send_email(&$parent, &$child, $op = '', $target = 'child') {
$mail_op_list = array('to', 'cc', 'bcc');
switch($op) {
case 'requires' :
require_once(drupal_get_path('module','connect') . '/connect_lookup.php');
$return = array();
$default = connect_node_options($parent->nid, 'is_live');
$default = $default ? $default : 'no';
$return['variables']['is_live'] = array(
'#type' => 'radios',
'#title' => t('Is this campaign ready to go live?'),
'#description' => t('\'No\' means that no emails, faxes, etc. will not be sent; you will instead see a display of the message(s) that would have been sent.'),
'#options' => array('yes' => 'Yes', 'no' => 'No'),
'#default_value' => $default,
'#required' => TRUE,
);
$default = connect_node_options($parent->nid, 'send_test');
$default = $default ? $default : 'no';
$return['variables']['send_test'] = array(
'#type' => 'radios',
'#title' => t('Send a test message?'),
'#description' => t('Choose \'yes\' to send yourself a test version of the email if the campaign is not yet live.'),
'#options' => array('yes' => 'Yes', 'no' => 'No'),
'#default_value' => $default,
'#required' => TRUE,
);
// stringent validation?
$return['variables']['email_stringent'] = array(
'#type' => 'radios',
'#title' => 'Use stringent email address validation?',
'#description' => 'This requires that email addresses point to an existing, fully-qualified domain name.',
'#options' => array('yes' => 'Yes', 'no' => 'No'),
'#default_value' => connect_node_options($parent->nid, 'email_stringent'),
'#required' => TRUE,
);
// CC the participant?
$return['variables']['email_cc_participant'] = array(
'#type' => 'radios',
'#title' => 'CC the participant?',
'#description' => 'Should emails generated by the campaign also be CCed to the participant?',
'#options' => array('yes' => 'Yes', 'no' => 'No'),
'#default_value' => connect_node_options($parent->nid, 'email_cc_participant'),
'#required' => TRUE,
);
// re-sending batch size
$batch = connect_node_options($parent->nid, 'email_batch');
$batch = $batch ? $batch : 0;
$return['variables']['email_batch'] = array(
'#type' => 'textfield',
'#title' => 'Batch size for re-sending failed emails',
'#description' => 'How many failed emails should be re-tried in one session? Set to zero to process an unlimited number of emails.',
'#default_value' => $batch,
);
// send HTML mail?
if (module_exists('mimemail')) {
if (!variable_get('mimemail_alter', 0)) {
$return['variables']['email_send_html'] = array(
'#type' => 'radios',
'#title' => 'Send HTML email using the mimemail module?',
'#options' => array('yes' => 'Yes', 'no' => 'No'),
'#default_value' => connect_node_options($parent->nid, 'email_send_html'),
'#required' => TRUE,
);
}
} else {
$return['variables']['email_send_html'] = array(
'#value' => '<p><strong>Send HTML Email</strong><br />' . t('If you want to be able to send HTML emails, install the mimemail module.') . '</p>',
);
}
// TO/CC salutation
$return['variables']['email_salutation'] = array(
'#type' => 'radios',
'#title' => 'Prefix a list of the recipients\' names?',
'#description' => 'Connect can automatically add a list of the TO and CC target names above the message.',
'#options' => array('yes' => 'Yes', 'no' => 'No'),
'#default_value' => connect_node_options($parent->nid, 'email_salutation'),
'#required' => TRUE,
);
// defined target
$return['variables']['email_defined_targets'] = array (
'#type' => 'textarea',
'#title' => 'Email targets',
'#description' => 'Enter your targets one to a line, using the format "to,[email protected],person name". You can specify "to" or "cc" or "bcc" in the first field. There must be at least one "to" address (between the defined targets and lookup target). The name element is used as the salutation at the top of the email.',
'#default_value' => connect_node_options($parent->nid, 'email_defined_targets'),
'#required' => FALSE,
);
// target lookup
$lookup_types = connect_get_lookup_types(TRUE);
$lookup_actions = array('' => '');
foreach ($mail_op_list as $action) {
$lookup_actions[$action] = $action;
}
// setup for lookup function
$return['variables']['email_lookup'] = array(
'#type' => 'fieldset',
'#title' => 'Target lookup',
);
$return['variables']['email_lookup']['email_lookup_intro'] = array(
'#value' => '<p>You may define one target address that will be determined on the basis of information provided by the participants (such as an elected representative corresponding to a postal code).</p>',
);
$return['variables']['email_lookup']['email_lookup_action'] = array(
'#type' => 'select',
'#title' => 'Email action',
'#options' => $lookup_actions,
'#default_value' => connect_node_options($parent->nid, 'email_lookup_action'),
'#required' => FALSE
);
$return['variables']['email_lookup']['email_lookup_type'] = array(
'#type' => 'select',
'#title' => 'Type of lookup',
'#options' => $lookup_types,
'#default_value' => connect_node_options($parent->nid, 'email_lookup_type'),
'#required' => FALSE,
);
// participant signature
$child_type = connect_node_options($parent->nid, 'participant_type');
$child_fields = _connect_get_child_fields($child_type);
$return['variables']['email_signature'] = array(
'#type' => 'fieldset',
'#title' => 'Signature',
);
$return['variables']['email_signature']['email_signature_intro'] = array(
'#value' => '<p>Select the participant fields that will be appended to the message as a signature</p>',
);
for ($row = 1; $row <= 3; $row++) {
$return['variables']['email_signature']["email_signature_{$row}"] = array(
'#type' => 'fieldset',
'#title' => "Row $row",
);
for ($col = 1; $col <= 2; $col++) {
$return['variables']['email_signature']["email_signature_{$row}"]["email_signature_{$row}_{$col}"] = array(
'#type' => 'select',
'#title' => '',
'#options' => $child_fields,
'#default_value' => connect_node_options($parent->nid, "email_signature_{$row}_{$col}"),
'#required' => FALSE,
);
}
}
$return['parent'] = array(
'email_subject' => 'Send email: the subject line of the email',
'email_body' => 'Send email: the body of the email',
);
$return['child'] = array(
'email_from' => 'Participant\'s email address',
'email_defined_result' => 'Send email: record the e-mail success/failure message',
//'email_cc_me' => 'Send email: should the email be CCed to the participant?',
);
// add requirements from lookup type, if set
$lookup = connect_target_lookup($parent, $child, 'requires', 'email');
if (is_array($lookup)) {
foreach(array('variables','parent','child') as $key) {
if (isset($lookup[$key])) {
foreach($lookup[$key] as $newkey=>$add) {
if ($key == 'variables') {
$return[$key]['email_lookup'][$newkey] = $add;
}
else {
$return[$key][$newkey] = $add;
}
}
}
}
}
return $return;
break;
/*
case 'menu' :
$return = array(
array(
'path' => 'connect/email_resend',
'title' => 'Re-send email',
'callback' => '_connect_action_send_email_resend_failed',
'type' => MENU_LOCAL_TASK,
'access arguments' => array('administer content'),
),
);
return $return;
break;
*/
case 'menu' :
break;
case 'admin-validate' :
// are we imposing extra tests for email address validity?
$stringent = (connect_node_options($parent->nid, 'email_stringent') == 'yes');
// check targets
$targets = _connect_parse_email_targets($parent->data['campaign_variables']['variables_connect_action_send_email']['email_defined_targets']);
if (!empty($targets)) {
$processed = array();
$count = 0;
foreach ($targets as $target) {
// type of email activity
if (!in_array($target['type'], $mail_op_list)) {
form_set_error('email_defined_targets_'. $count++, 'Email targets: "'. htmlentities($target['type']) . '" must be one of ' . implode(', ', $mail_op_list));
}
// address correctness
if (($stringent && !_connect_valid_email_strict($target['email'])) || !valid_email_address($target['email'])) {
form_set_error('email_defined_targets_'. $count++, 'Email targets: "'. htmlentities($target['email']) .'" is not a valid email address');
}
elseif (in_array($target['email'], $processed)) {
form_set_error('email_defined_targets_'. $count++, 'Email targets: "'. htmlentities($target['email']) .'" should not appear more than once in the list');
}
else {
$processed[] =$target['email'];
}
// name
}
}
else {
$lookup_action = $parent->data['campaign_variables']['variables_connect_action_send_email']['email_lookup_action'];
$lookup_type = $parent->data['campaign_variables']['variables_connect_action_send_email']['email_lookup_type'];
if (empty($lookup_action) || empty($lookup_type)) form_set_error('email_defined_targets', 'You do not have any targets defined for your email function. Please set up a defined or a lookup target.');
}
break;
case 'validate' :
if ($target != 'child') {
return;
}
$stringent = (connect_node_options($parent->nid, 'email_stringent') == 'yes');
$email = connect_value('email_from', $parent, $child, 'child');
if (($stringent && !_connect_valid_email_strict($email)) || (!$stringent && !valid_email_address($email))) {
form_set_error('', 'Please enter a valid email address');
}
// call validation from lookup type
$lookup_type = connect_node_options($parent->nid, 'email_lookup_type');
if ($lookup_type) {
connect_target_lookup($parent, $child, 'validate', 'email');
}
break;
case 'insert' :
if ($target != 'child') {
return;
}
$active = (connect_node_options($parent->nid, 'is_live') == 'yes');
$html = (connect_node_options($parent->nid, 'email_send_html') == 'yes');
$addresses = array();
$headers = array();
$salutation = array();
// entities for adding elements to html or plaintext emails
$br = $html ? "<br />\r\n\r\n" : "\r\n\r\n";
$p_on = $html ? '<p>' : '';
$p_off = $html ? "</p>\r\n\r\n" : "\r\n\r\n";
// direct targets
$targets = _connect_parse_email_targets(connect_node_options($parent->nid, 'email_defined_targets'));
foreach ($targets as $target) {
if (!empty($target['email']) && in_array($target['type'], $mail_op_list)) {
eval('$addresses[' . $target['type'] . '][] = $target[\'email\'];');
$salutation[$target['type']][] = $target['name'];
}
}
// lookup target
require_once(drupal_get_path('module','connect') . '/connect_lookup.php');
$target = connect_target_lookup($parent, $child, 'lookup', 'email');
$target['type'] = connect_node_options($parent->nid, 'email_lookup_action');
if (!empty($target['email']) && in_array($target['type'], $mail_op_list)) {
eval('$addresses[' . $target['type'] . '][] = $target[\'email\'];');
$salutation[$target['type']][] = $target['name'];
}
// set salutation string
$salute = '';
if (connect_node_options($parent->nid, 'email_salutation') == 'yes') {
$salute = $p_on . 'TO: ' . implode(', ', $salutation['to']) . $p_off;
if (!empty($salutation['cc'])) {
$salute .= $p_on . 'CC: ' . implode(', ', $salutation['cc']) . $p_off;
}
}
// set signature
$signature = '';
for ($row = 1; $row <= 3; $row++) {
$line = '';
for ($col = 1; $col <= 2; $col++) {
$field = connect_node_options($parent->nid, "email_signature_{$row}_{$col}");
if ($field) {
$path = _connect_get_field_path($child, $field);
eval('$temp = $child'. $path .';');
if ($temp) {
$line .= check_plain($temp) . ' ';
}
}
}
if ($line) {
$signature .= "$line$br";
}
}
$signature = $signature ? "\r\n\r\n$p_on$signature$p_off" : '';
// do we cc the participant?
if (_connect_positive_value(connect_node_options($parent->nid, 'email_cc_participant'))) {
$addresses['cc'][] = connect_value('email_from', $parent, $child, 'child');
}
// basic message elements
$message->to = implode(', ', $addresses['to']);
$message->from = connect_value('email_from', $parent, $child, 'child');
$message->subject = connect_value('email_subject', $parent, $child, 'parent');
$message->body = $salute . connect_value('email_body', $parent, $child, 'parent') . $signature;
// add CC, BCC headers
if (!empty($addresses['cc'])) {
$headers['CC'] = implode(', ', $addresses['cc']);
}
if (!empty($addresses['bcc'])) {
$headers['BCC'] = implode(', ', $addresses['bcc']);
}
$message->headers = $headers;
// send test version?
$send_test = (connect_node_options($parent->nid, 'send_test') == 'yes');
if (!$active && $send_test) {
$author = user_load(array('uid' => $parent->uid));
$testmessage = $message;
$testmessage->to = $author->mail;
$testmessage->headers = array();
$testresult = _connect_send_email($testmessage, $html, TRUE);
drupal_set_message('A test message was sent to '. $author->mail);
}
// send it
$result = _connect_send_email($message, $html, $active);
// save result
$_SESSION['connect_'.$parent->nid.'_email_sent'] = $result ? implode(', ', $salutation['to']) : FALSE;
$result = $result ? "Success" : "Failed";
connect_value('email_defined_result', $parent, $child, 'child', $result);
node_save($child);
break;
case 'status' :
$message = '';
$active = (connect_node_options($parent->nid, 'is_live') == 'yes');
if (isset($_SESSION['connect_'.$parent->nid.'_email_sent'])) {
if ($active) {