This repository has been archived by the owner on Sep 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wf_crm_admin_form.inc
1587 lines (1528 loc) · 67.3 KB
/
wf_crm_admin_form.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Webform CiviCRM module's admin form.
*/
class wf_crm_admin_form {
private $form;
private $form_state;
private $node;
private $fields;
private $sets;
private $settings;
private $data;
function __construct($form, &$form_state, $node) {
module_load_include('inc', 'webform_civicrm', 'includes/utils');
module_load_include('inc', 'webform_civicrm', 'includes/wf_crm_admin_help');
civicrm_initialize();
$this->form = $form;
$this->form_state = &$form_state;
$this->node = &$node;
$this->fields = wf_crm_get_fields();
$this->sets = wf_crm_get_fields('sets');
}
/**
* Build admin form for civicrm tab of a webform
* @return array
*/
public function buildForm() {
$this->form_state['storage']['nid'] = $this->node->nid;
// Display confirmation message before deleting fields
if (!empty($this->form_state['storage']['msg'])) {
return $this->buildConfirmationForm();
}
// Add css & js
$this->addResources();
if (empty($this->form_state['values'])) {
$this->initializeForm();
}
else {
$this->rebuildForm();
}
// Merge in existing fields
$existing = array_keys(wf_crm_enabled_fields($this->node, NULL, TRUE));
$this->settings += array_fill_keys($existing, 'create_civicrm_webform_element');
// Sort fields by set
foreach ($this->fields as $fid => $field) {
list($group) = explode('_', $fid, 2);
$this->sets[$group]['fields'][$fid] = $field;
}
// Build form fields
$this->buildFormIntro();
foreach ($this->data['contact'] as $n => $c) {
$this->buildContactTab($n, $c);
}
$this->buildMessageTabs();
$this->buildActivityTab();
if (isset($this->sets['participant'])) {
$this->buildParticipantTab();
}
if (isset($this->sets['membership'])) {
$this->buildMembershipTab();
}
if (isset($this->sets['contribution'])) {
$this->buildContributionTab();
}
if (isset($this->sets['grant'])) {
$this->buildGrantTab();
}
$this->buildOptionsTab();
$this->form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Settings'),
);
return $this->form;
}
/**
* Initialize form on first view
*/
private function initializeForm() {
$this->settings = isset($this->node->webform_civicrm) ? $this->node->webform_civicrm : $this->defaultSettings();
$this->data = &$this->settings['data'];
// Warn of incompatible modules
$incompatibilities = array(
'form_builder_webform' => t('Form builder Webform UI'),
'webform_alt_ui' => t('Webform Alternate UI'),
);
foreach ($incompatibilities as $module => $label) {
if (module_exists($module)) {
drupal_set_message(t('The module %module is not compatible with Webform CiviCRM Integration. Please disable it before continuing.', array('%module' => $label)), 'error');
}
}
}
/**
* On rebuilding the form
*/
private function rebuildForm() {
$this->settings = wf_crm_aval($this->form_state['storage'], 'vals', $this->form_state['values']);
$this->rebuildData();
// Hack for nicer UX: pre-check phone, email, etc when user increments them
if (!empty($_POST['_triggering_element_name'])) {
foreach (array('phone' => 'phone', 'email' => 'email', 'website' => 'url', 'im' => 'name') as $ent => $field) {
if (strpos($_POST['_triggering_element_name'], "_number_of_$ent")) {
list(, $c) = explode('_', $_POST['_triggering_element_name']);
for ($n = 1; $n <= $this->data['contact'][$c]["number_of_$ent"]; ++$n) {
$this->settings["civicrm_{$c}_contact_{$n}_{$ent}_{$field}"] = 1;
}
}
}
}
unset($this->form_state['storage']['vals']);
}
/**
* Display confirmation message and buttons before deleting webform components
* @return array
*/
private function buildConfirmationForm() {
$this->form['#prefix'] = $this->form_state['storage']['msg'];
$this->form['cancel'] = $this->form['disable'] = $this->form['delete'] = array('#type' => 'submit');
$this->form['delete']['#value'] = t('Remove Fields and Save Settings');
$this->form['disable']['#value'] = t('Leave Fields and Save Settings');
$this->form['cancel']['#value'] = t('Cancel (go back)');
return $this->form;
}
/**
* Add necessary css & js
*/
private function addResources() {
$this->form['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/css/webform_civicrm_admin.css';
$this->form['#attached']['js'][] = drupal_get_path('module', 'webform_civicrm') . '/js/webform_civicrm_admin.js';
$this->form['#attached']['js'][] = array(
'data' => array('webform_civicrm' => array('rTypes' => wf_crm_get_relationship_types())),
'type' => 'setting',
);
// Add CiviCRM resources to take advantage of popup notifications, etc.
CRM_Core_Resources::singleton()->addCoreResources();
$this->form['#suffix'] = wf_crm_admin_help::helpTemplate();
}
/**
* Build fields for form intro
*/
private function buildFormIntro() {
$this->form['nid'] = array(
'#type' => 'checkbox',
'#title' => t('Enable CiviCRM Processing'),
'#default_value' => !empty($this->settings['nid']),
'#return_value' => $this->node->nid,
);
$this->help($this->form['nid'], 'intro', t('Webform-CiviCRM Integration'));
$this->form['number_of_contacts'] = array(
'#type' => 'select',
'#title' => t('Number of Contacts'),
'#default_value' => count($this->data['contact']),
'#options' => drupal_map_assoc(range(1, 30)),
);
$this->form['change_form_settings'] = array(
'#type' => 'button',
'#value' => t('Change Form Settings'),
'#prefix' => '<div id="no-js-button-wrapper" class="messages warning">',
'#suffix' => '<div>' . t('You have Javascript disabled. You will need to click this button after changing any option to see the result.') . '</div></div>',
);
$this->form['webform_civicrm'] = array('#type' => 'vertical_tabs');
// Display intro help to virgins
if (!variable_get('webform_civicrm_help_seen')) {
variable_set('webform_civicrm_help_seen', TRUE);
drupal_add_js('cj(function() {CRM.help("' . t('Welcome to Webform-CiviCRM') . '", {}, "' . url('webform-civicrm/help/intro') . '");});', array('type' => 'inline', 'scope' => 'footer'));
}
}
/**
* Build fields for a contact
* @param int $n Contact number
* @param array $c Contact info
*/
private function buildContactTab($n, $c) {
list($contact_types, $sub_types) = wf_crm_get_contact_types();
$this->form['contact_' . $n] = array(
'#type' => 'fieldset',
'#title' => $n . '. ' . wf_crm_contact_label($n, $this->data),
'#description' => $n > 1 ? NULL : t('Primary contact. Usually assumed to be the person filling out the form.') . '<br />' . t('Enable the "Existing Contact" field to autofill with the current user (or another contact).'),
'#group' => 'webform_civicrm',
'#attributes' => array('class' => array('contact-icon-' . $c['contact'][1]['contact_type'])),
);
$this->form['contact_' . $n][$n . '_contact_type'] = array(
'#type' => 'select',
'#title' => t('Contact Type'),
'#default_value' => $c['contact'][1]['contact_type'],
'#options' => $contact_types,
'#prefix' => '<div class="contact-type-select">',
);
$this->form['contact_' . $n][$n . '_webform_label'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#default_value' => wf_crm_contact_label($n, $this->data, 'plain'),
'#suffix' => '</div>',
);
$this->help($this->form['contact_' . $n][$n . '_webform_label'], 'webform_label', t('Contact Label'));
$this->addAjaxItem('contact_' . $n, $n . '_contact_type', 'contact_subtype_wrapper', 'contact-subtype-wrapper');
// Contact sub-type
$fid = 'civicrm_' . $n . '_contact_1_contact_contact_sub_type';
if (!empty($sub_types[$c['contact'][1]['contact_type']])) {
$field = $this->fields['contact_contact_sub_type'];
$field['name'] = t('Type of @contact', array('@contact' => $contact_types[$c['contact'][1]['contact_type']]));
$this->form['contact_' . $n]['contact_subtype_wrapper'][$fid] = $this->addItem($fid, $field);
$this->addAjaxItem('contact_' . $n . ':contact_subtype_wrapper', $fid, 'contact_custom_wrapper');
}
// If no sub-types
else {
$this->form['contact_' . $n]['contact_subtype_wrapper'][$fid] = array(
'#type' => 'value',
'#value' => array(),
);
}
$this->form['contact_' . $n]['contact_subtype_wrapper']['clear'] = array(
'#markup' => '<div class="clearfix"> </div>',
);
foreach ($this->sets as $sid => $set) {
if ($set['entity_type'] != 'contact') {
continue;
}
if ($sid == 'relationship' && !($set['max_instances'] = $n - 1)) {
continue;
}
if (!empty($set['contact_type']) && $set['contact_type'] != $c['contact'][1]['contact_type']) {
continue;
}
if (!empty($set['sub_types'])) {
if (!array_intersect($c['contact'][1]['contact_sub_type'], $set['sub_types'])) {
continue;
}
$pos = &$this->form['contact_' . $n]['contact_subtype_wrapper']['contact_custom_wrapper'];
$path = 'contact_' . $n . ':contact_subtype_wrapper:contact_custom_wrapper';
}
elseif (!empty($set['contact_type']) || $sid == 'contact') {
$pos = &$this->form['contact_' . $n]['contact_subtype_wrapper'];
$path = 'contact_' . $n . ':contact_subtype_wrapper';
}
else {
$pos = &$this->form['contact_' . $n];
$path = 'contact_' . $n;
}
if (!empty($set['max_instances'])) {
if (!isset($c['number_of_' . $sid])) {
$c['number_of_' . $sid] = 0;
}
$selector = array(
'#type' => 'select',
'#default_value' => $c['number_of_' . $sid],
'#prefix' => '<div class="number-of">',
'#suffix' => '</div>',
);
if ($set['max_instances'] > 1) {
$selector['#options'] = range(0, $set['max_instances']);
$selector['#title'] = t('Number of %type Fields', array('%type' => $set['label']));
}
else {
$selector['#options'] = array(t('No'), t('Yes'));
$selector['#title'] = t('Enable %type Fields', array('%type' => $set['label']));
}
if (!empty($set['help_text'])) {
$this->help($selector, "fieldset_$sid", $set['label']);
}
$pos['contact_' . $n . '_number_of_' . $sid] = $selector;
$this->addAjaxItem($path, 'contact_' . $n . '_number_of_' . $sid, $n . $sid . '_wrapper');
}
else {
$c['number_of_' . $sid] = 1;
}
for ($i = 1; $i <= $c['number_of_' . $sid]; ++$i) {
$fsid = 'civicrm_' . $n . $sid . $i . '_fieldset';
$fieldset = array(
'#type' => 'fieldset',
'#title' => $set['label'],
'#attributes' => array('id' => $fsid, 'class' => array('web-civi-checkbox-set')),
'js_select' => $this->addToggle($fsid),
);
if ($sid == 'relationship') {
$fieldset['#title'] = t('Relationship to !contact', array('!contact' => wf_crm_contact_label($i, $this->data, 'wrap')));
}
elseif ((isset($set['max_instances']) && $set['max_instances'] > 1)) {
$fieldset['#title'] .= ' ' . $i;
if (in_array($sid, wf_crm_location_fields()) && $i == 1) {
$fieldset['#title'] .= ' ' . t('(primary)');
}
}
foreach ($set['fields'] as $fid => $field) {
if ($fid == 'contact_contact_sub_type' ||
($fid == 'address_master_id' && count($this->data['contact']) == 1) ||
(isset($field['contact_type']) && $field['contact_type'] != $c['contact'][1]['contact_type'])) {
continue;
}
$fid = 'civicrm_' . $n . '_contact_' . $i . '_' . $fid;
$fieldset[$fid] = $this->addItem($fid, $field);
}
if (isset($set['max_instances'])) {
$pos[$n . $sid . '_wrapper'][$n . $sid . $i . '_fieldset'] = $fieldset;
}
else {
$pos[$n . $sid . $i . '_fieldset'] = $fieldset;
}
}
}
}
/**
* Configure messages
*/
private function buildMessageTabs() {
$tokens = '<strong>' . t('Tokens for !contact', array('!contact' => wf_crm_contact_label(1, $this->data, TRUE))) . ':</strong> [' . implode('], [', wf_crm_get_fields('tokens')) . '].';
$this->form['prefix'] = array(
'#type' => 'fieldset',
'#title' => t('Introduction Text'),
'#description' => t('This text will appear at the top of the form. You may configure separate messages for known contacts (logged in users, or users following a hashed link from civimail) and unknown (anonymous) users.'),
'#group' => 'webform_civicrm',
'#attributes' => array('class' => array('civi-icon-text')),
);
$this->form['prefix']['prefix_known'] = array(
'#type' => 'textarea',
'#title' => t('Introduction text for known contacts'),
'#default_value' => wf_crm_aval($this->settings, 'prefix_known'),
'#description' => $tokens,
);
$this->form['prefix']['prefix_unknown'] = array(
'#type' => 'textarea',
'#title' => t('Introduction text for unknown contacts'),
'#default_value' => wf_crm_aval($this->settings, 'prefix_unknown'),
'#description' => t('No tokens available for unknown contacts.'),
);
$this->form['st_message'] = array(
'#type' => 'fieldset',
'#title' => t('"Not You?" Message'),
'#description' => t('Prompt for users who are logged in as, or following a hashed link for, someone else.'),
'#group' => 'webform_civicrm',
'#attributes' => array('class' => array('civi-icon-message')),
);
$this->form['st_message']['toggle_message'] = array(
'#type' => 'checkbox',
'#title' => t('Display message to known contacts?'),
'#default_value' => !empty($this->settings['message']),
);
$this->form['st_message']['message'] = array(
'#type' => 'textfield',
'#title' => t('Text (displayed as a status message)'),
'#default_value' => wf_crm_aval($this->settings, 'message', t("You are viewing this form as [display name]. Please {click here if that's not you}.")),
'#size' => 100,
'#maxlength' => 255,
'#description' => t('Enclose your "not you" link text in curly brackets {like this}.') . '<p>' . $tokens . '</p>',
);
}
/**
* Case and activity settings
*/
private function buildActivityTab() {
$case_types = isset($this->sets['case']) ? wf_crm_apivalues('case', 'getoptions', array('field' => 'case_type_id')) : array();
$this->form['act'] = array(
'#type' => 'fieldset',
'#title' => $case_types ? t('Activity/Case') : t('Activity'),
'#group' => 'webform_civicrm',
'#attributes' => array('class' => array('civi-icon-activity')),
);
if ($case_types) {
$this->form['act']['case_type_id'] = array(
'#type' => 'select',
'#title' => t('CiviCase Type'),
'#options' => array(t('- No Case -')) + $case_types,
);
$this->addAjaxItem('act', 'case_type_id', 'case');
}
$case_type = $activity_type = $case_type_name = NULL;
$campaign_act_types = array();
if ($case_types && !empty($this->data['case'][1]['case'][1]['case_type_id'])) {
$case_type = $this->form['act']['case_type_id']['#default_value'] = $this->data['case'][1]['case'][1]['case_type_id'];
$case_type_names = wf_crm_apivalues('case', 'getoptions', array('field' => 'case_type_id', 'context' => 'validate'));
$case_type_name = $case_type_names[$case_type];
$this->form['act']['case']['case_fields'] = array(
'#type' => 'fieldset',
'#title' => $case_types[$case_type] . ' ' . t('Case'),
);
$this->form['act']['case']['case_fields']['existing_case_status'] = array(
'#type' => 'select',
'#title' => t('Update Existing Case'),
'#options' => array('' => '- ' . t('None') . ' -') + wf_crm_apivalues('case', 'getoptions', array('field' => 'status_id')),
'#default_value' => wf_crm_aval($this->data, 'case:1:existing_case_status', array()),
'#multiple' => TRUE,
);
$this->help($this->form['act']['case']['case_fields']['existing_case_status'], 'existing_case_status');
}
$act_types = wf_crm_get_activity_types($case_type_name, $campaign_act_types);
$this->form['act']['case']['activity_type_id'] = array(
'#type' => 'select',
'#title' => t('Activity Type'),
'#options' => array(t('- No Activity -')) + $act_types,
);
$this->addAjaxItem('act:case', 'activity_type_id', 'activity_fields');
if (!empty($this->data['activity'][1]['activity'][1]['activity_type_id']) && isset($act_types[$this->data['activity'][1]['activity'][1]['activity_type_id']])) {
$this->form['act']['case']['activity_fields']['#type'] = 'fieldset';
$activity_type = $this->form['act']['case']['activity_type_id']['#default_value'] = $this->settings['activity_type_id'] = $this->data['activity'][1]['activity'][1]['activity_type_id'];
$this->form['act']['case']['activity_fields']['#title'] = $act_types[$activity_type] . ' ' . t('Activity');
$this->form['act']['case']['activity_fields']['existing_activity_status'] = array(
'#type' => 'select',
'#title' => t('Update Existing Activity'),
'#options' => array('' => '- ' . t('None') . ' -') + wf_crm_apivalues('activity', 'getoptions', array('field' => 'status_id')),
'#default_value' => wf_crm_aval($this->data, 'activity:1:existing_activity_status', array()),
'#multiple' => TRUE,
);
$this->help($this->form['act']['case']['activity_fields']['existing_activity_status'], $case_type ? 'existing_case_activity_status' : 'existing_activity_status');
$this->form['act']['case']['activity_fields']['activity_subject'] = array(
'#type' => 'textfield',
'#title' => t('Default Activity Subject'),
'#maxlength' => 255,
'#required' => TRUE,
'#default_value' => wf_crm_aval($this->data, 'activity:1:activity:1:subject', check_plain($this->node->title)),
);
$this->help($this->form['act']['case']['activity_fields']['activity_subject'], 'activity_default_subject');
$this->form['act']['case']['activity_fields']['activity_details'] = array(
'#type' => 'checkboxes',
'#options' => array(
'entire_result' => t('Include <em>entire</em> webform submission in activity details'),
'view_link' => t('Include link to <em>view</em> webform submission in activity details'),
'edit_link' => t('Include link to <em>edit</em> webform submission in activity details'),
),
'#default_value' => wf_crm_aval($this->data, 'activity:1:details', array('view_link'), TRUE),
);
}
foreach ($this->sets as $sid => $set) {
$sid .= '_fieldset';
$ent = $set['entity_type'];
if (($ent == 'activity' && $type = $activity_type) || ($ent == 'case' && $type = $case_type)) {
if ((!empty($set['sub_types']) && !in_array($type, $set['sub_types'])) || empty($set['fields'])) {
continue;
}
$this->form['act']['case'][$ent . '_fields'][$sid] = array(
'#type' => 'fieldset',
'#title' => $set['label'],
'#attributes' => array('id' => $sid, 'class' => array('web-civi-checkbox-set')),
'js_select' => $this->addToggle($sid),
);
foreach ($set['fields'] as $fid => $field) {
if ($fid !== 'activity_survey_id') {
$complete_fid = "civicrm_1_{$ent}_1_$fid";
$form_item = $this->addItem($complete_fid, $field);
$this->form['act']['case'][$ent . '_fields'][$sid][$complete_fid] = $form_item;
// If the activity type is part of CiviCampaign
// then attach an AJAX select box for the survey/petition ID
if (isset($campaign_act_types[$activity_type]) && $fid === 'activity_campaign_id') {
$pathstr = 'act:case:activity_fields:' . $sid;
$this->addAjaxItem($pathstr, $complete_fid, 'campaign_fields');
$survey_fid = 'activity_survey_id';
$complete_survey_fid = "civicrm_1_{$ent}_1_{$survey_fid}";
$survey_form_item = $this->addItem($complete_survey_fid, $this->sets['activity']['fields']['activity_survey_id']);
$this->form['act']['case']['activity_fields'][$sid]['campaign_fields'][$complete_survey_fid] = $survey_form_item;
}
}
}
}
}
}
/**
* Event participant settings
*/
private function buildParticipantTab() {
$this->form['participant'] = array(
'#type' => 'fieldset',
'#title' => t('Event Registration'),
'#group' => 'webform_civicrm',
'#attributes' => array('class' => array('civi-icon-participant')),
);
$reg_type = wf_crm_aval($this->data, 'participant_reg_type');
$this->form['participant']['participant_reg_type'] = array(
'#type' => 'select',
'#title' => t('Registration Method'),
'#default_value' => $reg_type,
'#options' => array(
t('- None -'),
'all' => t('Register all contacts for the same event(s)'),
'separate' => t('Register each contact separately'),
),
);
$this->help($this->form['participant']['participant_reg_type'], 'participant_reg_type');
$this->form['participant']['event_type'] = array(
'#type' => 'select',
'#title' => t('Show Events of Type'),
'#options' => array('any' => t('- Any Type -')) + wf_crm_apivalues('event', 'getoptions', array('field' => 'event_type_id')),
'#default_value' => wf_crm_aval($this->data, 'reg_options:event_type', 'any'),
'#prefix' => '<div id="event-reg-options-wrapper"><div class="web-civi-checkbox-set">',
'#parents' => array('reg_options', 'event_type'),
'#tree' => TRUE,
);
$this->form['participant']['show_past_events'] = array(
'#type' => 'select',
'#title' => t('Show Past Events'),
'#default_value' => wf_crm_aval($this->data, 'reg_options:show_past_events', 0),
'#parents' => array('reg_options', 'show_past_events'),
'#tree' => TRUE,
'#options' => array(
0 => t('- None -'),
1 => t('All'),
'-1 day' => t('Past Day'),
'-1 week' => t('Past Week'),
'-1 month' => t('Past Month'),
'-2 month' => t('Past 2 Months'),
'-3 month' => t('Past 3 Months'),
'-6 month' => t('Past 6 Months'),
'-1 year' => t('Past Year'),
'-2 year' => t('Past 2 Years'),
),
);
$this->help($this->form['participant']['show_past_events'], 'reg_options_show_past_events');
$this->form['participant']['title_display'] = array(
'#type' => 'select',
'#title' => t('Title Display'),
'#default_value' => wf_crm_aval($this->data, 'reg_options:title_display', 'title'),
'#suffix' => '</div>',
'#parents' => array('reg_options', 'title_display'),
'#tree' => TRUE,
'#options' => array(
'title' => t('Title Only'),
'title type' => t('Title + Event Type'),
'title start dateformatYear' => t('Title + Year'),
'title start dateformatPartial' => t('Title + Month + Year'),
'title start dateformatFull' => t('Title + Start-Date'),
'title start dateformatTime' => t('Title + Start-Time'),
'title start dateformatDatetime' => t('Title + Start-Date-Time'),
'title start end dateformatFull' => t('Title + Start-Date + End'),
'title start end dateformatTime' => t('Title + Start-Time + End'),
'title start end dateformatDatetime' => t('Title + Start-Date-Time + End'),
),
);
$this->help($this->form['participant']['title_display'], 'reg_options_title_display');
$this->form['participant']['reg_options'] = array(
'#prefix' => '<div class="clearfix"> </div>',
'#suffix' => '</div>',
'#type' => 'fieldset',
'#title' => t('Registration Options'),
'#collapsible' => TRUE,
'#collapsed' => isset($this->data['participant']),
'#tree' => TRUE,
);
$field = array(
'#type' => 'select',
'#title' => t('Show Remaining Space in Events'),
'#default_value' => wf_crm_aval($this->data, 'reg_options:show_remaining', 0),
'#options' => array(
t('Never'),
'always' => t('Always'),
'0_full' => t('When full - 0 spaces left'),
),
);
$this->help($field, 'reg_options_show_remaining');
foreach (array(5, 10, 20, 50, 100, 200, 500, 1000) as $num) {
$field['#options'][$num] = t('When under !num spaces left', array('!num' => $num));
}
$this->form['participant']['reg_options']['show_remaining'] = $field;
$this->form['participant']['reg_options']['validate'] = array(
'#type' => 'checkbox',
'#title' => t('Prevent Registration for Past/Full Events'),
'#default_value' => (bool) wf_crm_aval($this->data, 'reg_options:validate'),
);
$this->help($this->form['participant']['reg_options']['validate'], 'reg_options_validate');
$this->form['participant']['reg_options']['block_form'] = array(
'#type' => 'checkbox',
'#title' => t('Block Form Access when Event(s) are Full/Ended'),
'#default_value' => (bool) wf_crm_aval($this->data, 'reg_options:block_form'),
);
$this->help($this->form['participant']['reg_options']['block_form'], 'reg_options_block_form');
$this->addAjaxItem('participant', 'participant_reg_type', 'participants');
$this->addAjaxItem('participant', 'event_type', 'participants');
$this->addAjaxItem('participant', 'show_past_events', 'participants');
$this->addAjaxItem('participant', 'title_display', 'participants');
for ($n = 1; $reg_type && (($n <= count($this->data['contact']) && $reg_type != 'all') || $n == 1); ++$n) {
$this->form['participant']['participants'][$n] = array(
'#type' => 'fieldset',
'#title' => $reg_type == 'all' ? t('Registration') : wf_crm_contact_label($n, $this->data, 'wrap'),
);
$num = wf_crm_aval($this->data, "participant:{$n}:number_of_participant");
if (!$num || ($n > 1 && $reg_type == 'all')) {
$num = 0;
}
$this->form['participant']['participants'][$n]['participant_' . $n . '_number_of_participant'] = array(
'#type' => 'select',
'#title' => $reg_type == 'all' ? t('Number of Event Sets') : t('Number of Event Sets for !contact', array('!contact' => wf_crm_contact_label($n, $this->data, 'wrap'))),
'#default_value' => $num,
'#options' => range(0, $this->sets['participant']['max_instances']),
'#prefix' => '<div class="number-of">',
'#suffix' => '</div>',
);
$this->addAjaxItem("participant:participants:{$n}", 'participant_' . $n . '_number_of_participant', 'div');
$particpant_extensions = array(
1 => 'role_id',
2 => 'event_id',
3 => 'event_type'
);
for ($e = 1; $e <= $num; ++$e) {
$fs = "participant_{$n}_event_{$e}_fieldset";
$this->form['participant']['participants'][$n]['div'][$fs] = array(
'#type' => 'fieldset',
'#title' => t('Event !num', array('!num' => $e)),
'#attributes' => array('id' => $fs),
);
foreach ($this->sets as $sid => $set) {
if ($set['entity_type'] == 'participant') {
$sid = 'civicrm_' . $n . '_participant_' . $e . '_' . $sid . '_fieldset';
$class = 'web-civi-checkbox-set';
if (!empty($set['sub_types'])) {
$role_id = wf_crm_aval($this->data, "participant:$n:particpant:$e:role_id", '');
$event_id = wf_crm_aval($this->data, "participant:$n:particpant:$e:event_id", '');
$event_type = wf_crm_aval($this->data, 'reg_options:event_type', '');
if ($event_id && $event_id !== 'create_civicrm_webform_element') {
list($event_id, $event_type) = explode('-', $event_id);
}
$ext = $particpant_extensions[$set['extension_of']];
if (!in_array($$ext, $set['sub_types'])) {
$class .= ' hidden';
}
$class .= ' extends-condition ' . str_replace('_', '', $ext) . '-' . implode('-', $set['sub_types']);
}
$this->form['participant']['participants'][$n]['div'][$fs][$sid] = array(
'#type' => 'fieldset',
'#title' => $set['label'],
'#attributes' => array('id' => $sid, 'class' => array($class)),
'js_select' => $this->addToggle($sid),
);
foreach ($set['fields'] as $fid => $field) {
$id = 'civicrm_' . $n . '_participant_' . $e . '_' . $fid;
$item = $this->addItem($id, $field);
if ($fid == 'participant_event_id') {
$item['#prefix'] = '<div class="auto-width">';
$item['#suffix'] = '</div>';
}
if ($fid == 'participant_event_id' || $fid == 'participant_role_id') {
$item['#attributes']['onchange'] = "wfCiviAdmin.participantConditional('#$fs');";
$item['#attributes']['class'] = array($fid);
$$fid = wf_crm_aval($item, '#default_value');
}
$this->form['participant']['participants'][$n]['div'][$fs][$sid][$id] = $item;
}
}
}
}
}
}
/**
* Membership settings
*/
private function buildMembershipTab() {
$this->form['membership'] = array(
'#type' => 'fieldset',
'#title' => t('Memberships'),
'#group' => 'webform_civicrm',
'#attributes' => array('class' => array('civi-icon-membership')),
);
for ($c = 1; $c <= count($this->data['contact']); ++$c) {
$num = wf_crm_aval($this->data, "membership:{$c}:number_of_membership", 0);
$this->form['membership'][$c]["membership_{$c}_number_of_membership"] = array(
'#type' => 'select',
'#title' => t('Number of Memberships for !contact', array('!contact' => wf_crm_contact_label($c, $this->data, 'wrap'))),
'#default_value' => $num,
'#options' => range(0, 9),
'#prefix' => '<div class="number-of">',
'#suffix' => '</div>',
);
$this->addAjaxItem("membership:$c", "membership_{$c}_number_of_membership", "membership");
for ($n = 1; $n <= $num; ++$n) {
$fs = "membership_{$c}_membership_{$n}_fieldset";
$this->form['membership'][$c]['membership'][$fs] = array(
'#type' => 'fieldset',
'#title' => t('Membership !num for !contact', array('!num' => $n, '!contact' => wf_crm_contact_label($c, $this->data, 'wrap'))),
'#attributes' => array('id' => $fs, 'class' => array('web-civi-checkbox-set')),
'js_select' => $this->addToggle($fs),
);
foreach ($this->sets as $sid => $set) {
if ($set['entity_type'] == 'membership') {
foreach ($set['fields'] as $fid => $field) {
$fid = "civicrm_{$c}_membership_{$n}_$fid";
$this->form['membership'][$c]['membership'][$fs][$fid] = $this->addItem($fid, $field);
}
}
}
}
}
}
/**
* Contribution settings
*/
private function buildContributionTab() {
$this->form['contribution'] = array(
'#type' => 'fieldset',
'#title' => t('Contribution'),
'#group' => 'webform_civicrm',
'#description' => t('In order to process live transactions for events, memberships, or contributions, select a contribution page and its billing fields will be included on the webform.'),
'#attributes' => array('class' => array('civi-icon-contribution')),
);
$fid = 'civicrm_1_contribution_1_contribution_contribution_page_id';
$this->form['contribution'][$fid] = $this->addItem($fid, $this->fields['contribution_contribution_page_id']);
unset($this->sets['contribution']['fields']['contribution_contribution_page_id']);
$this->addAjaxItem('contribution', $fid, 'sets');
$page_id = wf_crm_aval($this->data, 'contribution:1:contribution:1:contribution_page_id');
if ($page_id) {
$page = wf_civicrm_api('contribution_page', 'getsingle', array('id' => $page_id));
}
if (!$page_id || empty($page['financial_type_id'])) {
return;
}
// Make sure webform is set-up to prevent credit card abuse.
$this->checkSubmissionLimit();
// Add contribution fields
foreach ($this->sets as $sid => $set) {
if ($set['entity_type'] == 'contribution' && (empty($set['sub_types']) || $set['sub_types'][0] == $page['financial_type_id'])) {
$this->form['contribution']['sets'][$sid] = array(
'#type' => 'fieldset',
'#title' => $set['label'],
'#attributes' => array('id' => $sid, 'class' => array('web-civi-checkbox-set')),
'js_select' => $this->addToggle($sid),
);
foreach ($set['fields'] as $fid => $field) {
$fid = "civicrm_1_contribution_1_$fid";
$this->form['contribution']['sets'][$sid][$fid] = $this->addItem($fid, $field);
}
}
}
}
/**
* Grant settings
*/
private function buildGrantTab() {
$this->form['grant'] = array(
'#type' => 'fieldset',
'#title' => t('Grants'),
'#group' => 'webform_civicrm',
'#attributes' => array('class' => array('civi-icon-grant')),
);
$num = wf_crm_aval($this->data, "grant:number_of_grant", 0);
$this->form['grant']["grant_number_of_grant"] = array(
'#type' => 'select',
'#title' => t('Number of Grants'),
'#default_value' => $num,
'#options' => range(0, $this->sets['grant']['max_instances']),
'#prefix' => '<div class="number-of">',
'#suffix' => '</div>',
);
$this->addAjaxItem("grant", "grant_number_of_grant", "grant");
for ($n = 1; $n <= $num; ++$n) {
$fs = "grant_grant_{$n}_fieldset";
$this->form['grant']['grant'][$fs] = array(
'#type' => 'fieldset',
'#title' => t('Grant !num', array('!num' => $n)),
'#attributes' => array('id' => $fs, 'class' => array('web-civi-checkbox-set')),
'js_select' => $this->addToggle($fs),
);
foreach ($this->sets['grant']['fields'] as $fid => $field) {
$fid = "civicrm_{$n}_grant_1_$fid";
$this->form['grant']['grant'][$fs][$fid] = $this->addItem($fid, $field);
}
$this->addAjaxItem("grant:grant:$fs", "civicrm_{$n}_grant_1_grant_grant_type_id", "custom");
foreach ($this->sets as $sid => $set) {
if ($set['entity_type'] == 'grant' && $sid != 'grant'
&& (empty($this->data['grant'][$n]['grant'][1]['grant_type_id']) || empty($set['sub_types']) ||
in_array($this->data['grant'][$n]['grant'][1]['grant_type_id'], $set['sub_types']))
) {
$fs1 = "grant_grant_{$n}_fieldset_$sid";
$this->form['grant']['grant'][$fs]['custom'][$fs1] = array(
'#type' => 'fieldset',
'#title' => $set['label'],
'#attributes' => array('id' => $fs1, 'class' => array('web-civi-checkbox-set')),
'js_select' => $this->addToggle($fs1),
);
foreach ($set['fields'] as $fid => $field) {
$fid = "civicrm_{$n}_grant_1_$fid";
$this->form['grant']['grant'][$fs]['custom'][$fs1][$fid] = $this->addItem($fid, $field);
}
}
}
}
}
/**
* Configure additional options
*/
private function buildOptionsTab() {
$this->form['options'] = array(
'#type' => 'fieldset',
'#title' => t('Additional Options'),
'#group' => 'webform_civicrm',
'#attributes' => array('class' => array('civi-icon-prefs')),
'#description' => '<p>' .
t('To have this form auto-filled for anonymous users, enable the "Existing Contact" field for !contact and send the following link from CiviMail:', array('!contact' => wf_crm_contact_label(1, $this->data, TRUE))) .
'<br /><code>' . url("node/{$this->node->nid}", array('absolute' => TRUE, 'query' => array('cid1' => ''))) . '{contact.contact_id}&{contact.checksum}</code></p>',
);
$this->form['options']['create_fieldsets'] = array(
'#type' => 'checkbox',
'#title' => t('Create Fieldsets'),
'#default_value' => (bool) $this->settings['create_fieldsets'],
'#description' => t('Create a fieldset around each contact. Provides visual organization of your form. Also allows the contact clone feature to work.'),
);
$this->form['options']['confirm_subscription'] = array(
'#type' => 'checkbox',
'#title' => t('Confirm Subscriptions'),
'#default_value' => (bool) $this->settings['confirm_subscription'],
'#description' => t('Recommended. Send a confirmation email before adding contacts to publicly subscribable mailing list groups.') . '<br />' . t('Your public mailing lists:') . ' <em>',
);
$ml = wf_crm_apivalues('group', 'get', array('is_hidden' => 0, 'visibility' => 'Public Pages', 'group_type' => 2), 'title');
if ($ml) {
if (count($ml) > 4) {
$ml = array_slice($ml, 0, 3);
$ml[] = t('etc.');
}
$this->form['options']['confirm_subscription']['#description'] .= implode(', ', $ml) . '</em>';
}
else {
$this->form['options']['confirm_subscription']['#description'] .= t('none') . '</em>';
}
$this->form['options']['block_unknown_users'] = array(
'#type' => 'checkbox',
'#title' => t('Block Unknown Users'),
'#default_value' => !empty($this->settings['block_unknown_users']),
'#description' => t('Only allow users to see this form if they are logged in or following a personalized link from CiviMail.'),
);
$this->form['options']['new_contact_source'] = array(
'#type' => 'textfield',
'#title' => t('Source Label'),
'#maxlength' => 255,
'#size' => 30,
'#default_value' => $this->settings['new_contact_source'],
'#description' => t('Optional "source" label for any new contact/participant/membership created by this webform.'),
);
}
private function checkSubmissionLimit() {
$webform = &$this->node->webform;
// If anonymous users don't have access to the form, no need for a warning.
if (!in_array('1', $webform['roles'])) {
return;
}
// Handle ajax submission from "submit_limit" mini-form below
if (!empty($_POST['submit_limit']) && !empty($_POST['submit_interval'])) {
$submit_limit = (int) $_POST['submit_limit'];
$submit_interval = (int) $_POST['submit_interval'];
if ($submit_limit > 0 && $submit_interval != 0) {
$webform['submit_limit'] = $submit_limit;
$webform['submit_interval'] = $submit_interval;
db_update('webform')
->condition('nid', $this->node->nid)
->fields(array('submit_limit' => $submit_limit, 'submit_interval' => $submit_interval))
->execute();
}
drupal_set_message(t('Per-user submission limit has been updated. You may revisit these options any time on the <em>Form Settings</em> tab of this webform.'));
}
// Handle ajax submission from "webform_tracking_mode" mini-form below
if (!empty($_POST['webform_tracking_mode']) && $_POST['webform_tracking_mode'] == 'strict') {
variable_set('webform_tracking_mode', 'strict');
drupal_set_message(t('Webform anonymous user tracking has been updated to use the strict method. You may revisit this option any time on the global <a !link>Webform Settings</a> page.',
array('!link' => 'href="/admin/config/content/webform" target="_blank"')
));
}
// Mini-form to configure submit limit without leaving the page
if ($webform['submit_limit'] == -1) {
$this->form['contribution']['sets']['submit_limit'] = array(
'#markup' => '<div class="messages warning">' .
t('To prevent Credit Card abuse, it is recommended to set the per-user submission limit for this form.') .
' <button id="configure-submit-limit" type="button">' . t('Configure') . '</button>' .
'<div id="submit-limit-wrapper" style="display:none">' .
t('Limit each user to') .
' <input class="form-text" type="number" maxlength="2" size="2" name="submit_limit"> ' .
t('submission(s)') . ' <select class="form-select" name="submit_interval">' .
'<option value="-1">' .t('ever') . '</option>' .
'<option value="3600" selected="selected">' .t('every hour') . '</option>' .
'<option value="86400">' .t('every day') . '</option>' .
'<option value="604800">' .t('every week') . '</option>'.
'</select> ' .
' <button id="configure-submit-limit-save" type="button">' . t('Save') . '</button>' .
' <button id="configure-submit-limit-cancel" type="button">' . t('Cancel') . '</button>' .
'</div>' .
'</div>',
);
}
// Mini-form to conveniently update global cookie setting
elseif (variable_get('webform_tracking_mode', 'cookie') == 'cookie') {
$this->form['contribution']['sets']['webform_tracking_mode'] = array(
'#markup' => '<div class="messages warning">' .
t('Per-user submission limit is enabled for this form, however the webform anonymous user tracking method is configured to use cookies only, which is not secure enough to prevent Credit Card abuse.') .
' <button id="webform-tracking-mode" type="button">' . t('Change Now') . '</button>' .
' <input type="hidden" value="" name="webform_tracking_mode"> ' .
'</div>'
);
}
}
/**
* Set defaults when visiting the civicrm tab for the first time
*/
private function defaultSettings() {
return array(
'data' => array(
'contact' => array(
1 => array(
'contact' => array(
1 => array(
'contact_type' => 'individual',
'contact_sub_type' => array(),
),
),
),
),
'reg_options' => array(
'validate' => 1,
),
),
'confirm_subscription' => 1,
'create_fieldsets' => 1,
'new_contact_source' => check_plain($this->node->title),
'civicrm_1_contact_1_contact_first_name' => 'create_civicrm_webform_element',
'civicrm_1_contact_1_contact_last_name' => 'create_civicrm_webform_element',
'civicrm_1_contact_1_contact_existing' => 'create_civicrm_webform_element',
);
}
/**
* Build a field item for the admin form
*
* @param string $fid
* civicrm field id
* @param array $field
* Webform field info
*
* @return array
* FAPI form item array for the admin form
*/
private function addItem($fid, $field) {
list(, $c, $ent, $n, $table, $name) = explode('_', $fid, 6);
$item = array(
// We don't need numbers on the admin form since they are already grouped in fieldsets
'#title' => str_replace('#', '', $field['name']),
'#attributes' => wf_crm_aval($field, 'attributes'),
);
// Create dropdown list
if (!empty($field['expose_list'])) {
$field['form_key'] = $fid;
// Retrieve option list
$options = array();
// Placeholder empty option - used by javascript when displaying multiselect as single
if (!empty($field['extra']['multiple'])) {
$options += array('' => '- ' . t('None') . ' -');
}
if ($field['type'] != 'hidden') {
$options += array('create_civicrm_webform_element' => t('- User Select -'));
}
$options += wf_crm_field_options($field, 'config_form', $this->data);
$item += array(
'#type' => 'select',
'#options' => $options,
'#multiple' => !empty($field['extra']['multiple']),
'#default_value' => !empty($field['empty_option']) ? 0 : NULL,
);
if (isset($field['empty_option'])) {
$item['#empty_option'] = '- ' . $field['empty_option'] . ' -';
$item['#empty_value'] = 0;
}
if (isset($field['data_type'])) {
$item['#attributes']['data-type'] = $field['data_type'];
}
// Five ways to get default value...
// 1: From current form state
if (isset($this->settings[$fid]) && ($field['type'] != 'hidden')) {
$item['#default_value'] = $this->settings[$fid];
}
// 2: From saved settings
elseif (isset($this->data[$ent][$c][$table][$n][$name])) {
$item['#default_value'] = $this->data[$ent][$c][$table][$n][$name];
}
// 3: From callback function
elseif (isset($field['value_callback'])) {
$method = 'get_default_' . $table . '_' . $name;
$item['#default_value'] = self::$method($fid, $options);