-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmrf_form_processor.module
843 lines (783 loc) · 36.2 KB
/
cmrf_form_processor.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
<?php
require_once('cmrf_form_processor.cmrf.inc');
require_once('cmrf_form_processor_stream_wrapper.inc');
/**
* @var array
* Holds the original submission objects as soon as a submission is updated.
*/
$cmrf_form_processor_submissions_updated = array();
$cmrf_form_processor_latest_result = false;
function cmrf_form_processor_menu() {
$items['system/cmrfformprocessor/files'] = array(
'title' => 'File download',
'page callback' => 'file_download',
'page arguments' => array('cmrfformprocessor'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_cron().
*
* Remove all CiviCRM managed files from the system.
* It is not needed to keep them.
*/
function cmrf_form_processor_cron() {
db_query("DELETE FROM {file_usage} WHERE `module` = 'cmrf_form_processor' AND `type` = 'cmrfformprocessor'");
db_query("DELETE FROM {file_managed} WHERE `uri` LIKE 'cmrfformprocessor://%'");
}
/**
* Implements hook_stream_wrappers().
*/
function cmrf_form_processor_stream_wrappers() {
$info['cmrfformprocessor'] = array(
'name' => t('CiviMRF Form Processor File Url'),
'class' => 'cmrf_form_processor_stream_wrapper',
'description' => t('Remote files from CiviMRF Form Processor.'),
);
return $info;
}
function cmrf_form_processor_form_alter(&$form, &$form_state, $form_id) {
if (stripos($form_id, 'webform_client_form_')!==0) {
// Is not a webform;
return;
}
$node = $form['#node'];
if (!$node->cmrf_form_processor_enable) {
return;
}
if (!$node->cmrf_form_processor_default_data_enable) {
return;
}
$profile = !empty($node->cmrf_form_processor_profile) ? $node->cmrf_form_processor_profile : null;
$cache = $node->cmrf_form_processor_default_data_cache;
$defaultDataParams = array();
foreach($node->cmrf_form_processor_default_data as $key => $value) {
if (!empty($value)) {
$defaultDataParams[$key] = token_replace($value);
}
}
$defaultData = cmrf_form_processor_get_default_data($node->cmrf_form_processor_processor, $defaultDataParams, $cache, $profile);
if ($node->cmrf_form_processor_default_data_required && empty($defaultData)) {
$message = t('Invalid form');
if ($node->cmrf_form_processor_default_data_required_message) {
$message = $node->cmrf_form_processor_default_data_required_message;
}
drupal_set_message($message, 'error');
// Below an uggly hack.
// We clear the form array
// so basically the form is empty now and only the error is shown.
// We do this because there is no data coming from CiviCRM so basically the form
// is invalid.
$form = []; // Clear the form show the form becomes invisible.
return;
}
foreach($node->webform['components'] as $cid => $component) {
if ($component['type'] == 'file' && isset($defaultData[$component['form_key']]) && $defaultData[$component['form_key']]) {
$file = cmrf_form_processor_stream_wrapper::getAsManaged($defaultData[$component['form_key']], $cache);
$defaultData[$component['form_key']] = $file->fid;
}
if ($component['type'] == 'select' && $component['extra']['multiple'] && isset($defaultData[$component['form_key']]) && !is_array($defaultData[$component['form_key']])) {
$defaultData[$component['form_key']] = array($defaultData[$component['form_key']]);
}
if (isset($defaultData[$component['form_key']])) {
$node->webform['components'][$cid]['value'] = $defaultData[$component['form_key']];
}
}
_cmrf_form_processor_form_alter_walk_submitted_tree($form['submitted'], $defaultData);
}
/**
* Recursive function to prefill the default data onto the webform.
*/
function _cmrf_form_processor_form_alter_walk_submitted_tree(&$tree, $defaultData) {
foreach($tree as $name => $field) {
if (is_array($field) && array_key_exists('#default_value', $field) && isset($defaultData[$name])) {
$tree[$name]['#default_value'] = $defaultData[$name];
}
if (is_array($field)) {
_cmrf_form_processor_form_alter_walk_submitted_tree($tree[$name], $defaultData);
}
}
}
/**
* Implements hook_webform_submission_presave().
*/
function cmrf_form_processor_webform_submission_presave($node, &$submission) {
global $cmrf_form_processor_submissions_updated;
if ($submission->is_draft) {
return;
}
// Only submit webforms when civimrf is enabled.
if (!$node->cmrf_form_processor_enable) {
return;
}
$original_submissions = webform_get_submissions(array('nid' => $submission->nid, 'sid' => $submission->sid));
if (isset($original_submissions[$submission->sid])) {
$cmrf_form_processor_submissions_updated[$submission->sid] = $original_submissions[$submission->sid];
}
}
/**
* Implements hook_webform_submission_update().
*/
function cmrf_form_processor_webform_submission_update($node, $submission) {
global $cmrf_form_processor_submissions_updated;
if ($submission->is_draft) {
return;
}
// Only submit webforms when civimrf is enabled.
if (!$node->cmrf_form_processor_enable) {
return;
}
$sid = $submission->sid;
if (!isset($cmrf_form_processor_submissions_updated[$sid])) {
return;
}
if ($cmrf_form_processor_submissions_updated[$sid]->is_draft && !$submission->is_draft) {
cmrf_form_processor_submit($node, $submission);
}
}
function cmrf_form_processor_webform_submission_insert($node, $submission) {
cmrf_form_processor_submit($node, $submission);
}
/**
* Helper function to submit data
*
* @param $node
* @param $submission
*/
function cmrf_form_processor_submit($node, $submission) {
// Do not pass draft webforms.
if ($submission->is_draft) {
return;
}
// Only submit webforms when civimrf is enabled.
if (!$node->cmrf_form_processor_enable) {
return;
}
$profile = !empty($node->cmrf_form_processor_profile) ? $node->cmrf_form_processor_profile : null;
// Build the parameters for the call
$params = array();
$data = $submission->data;
$webform = $node->webform;
foreach($webform['components'] as $index => $component) {
if (isset($data[$index])) {
$value = $data[$index];
if (is_array($value) && empty($component['extra']['multiple'])) {
$value = reset($value);
} elseif (is_array($value) && !empty($component['extra']['multiple'])) {
$value = array();
foreach($data[$index] as $k=>$v) {
if (!empty($v)) {
$value[] = $v;
}
}
}
$params[$component['form_key']] = $value;
if ($component['type'] == 'file' && $value) {
$file = file_load($value);
$fileData = array();
if (stripos($file->uri, 'cmrfformprocessor://') === 0) {
$fileData['id'] = cmrf_form_processor_stream_wrapper::getFileIdFromUri($file->uri);
} else {
$fileData['name'] = $file->filename;
$fileData['mime_type'] = $file->filemime;
$fileData['url'] = file_create_url($file->uri);
}
$params[$component['form_key']] = $fileData;
}
}
}
drupal_alter('cmrf_form_processor_submit_params', $params, $node, $submission);
$options = array(
'nid' => $submission->nid,
'sid' => $submission->sid,
);
$callbacks = array();
if ($node->cmrf_form_processor_clear_submission) {
$callbacks[] = 'cmrf_webform_clear_submission';
}
if ($node->cmrf_form_processor_queue) {
cmrf_webform_queue_call('FormProcessor', $node->cmrf_form_processor_processor, $params, $options, $profile, $callbacks);
} else {
global $cmrf_form_processor_latest_result;
$call = cmrf_webform_send_call('FormProcessor', $node->cmrf_form_processor_processor, $params, $options, $profile, $callbacks);
$cmrf_form_processor_latest_result = $call->getReply();
}
}
function cmrf_form_processor_token_info() {
$types = array(
'name' => t('Form Processor'),
'description' => t('Result data from a form processor call'),
);
$tokens['action'] = array(
'name' => t("Action"),
'dynamic' => true,
'description' => t("Action output of a form processor submission."),
);
$tokens['default'] = array(
'name' => t("Default Data"),
'dynamic' => true,
'description' => t("Default Data of a form processor."),
);
return array(
'types' => array(
'form_processor' => $types
),
'tokens' => array(
'form_processor' => $tokens
)
);
}
function cmrf_form_processor_tokens($type, $tokens, array $data = array(), array $options = array()) {
global $cmrf_form_processor_latest_result;
global $cmrf_form_processor_latest_default_data;
$replacements = array();
if ($type == 'form_processor') {
foreach($tokens as $token => $original) {
// Token is action:contact.id or default.name
$elements = explode(":", $token);
$keyV2 = implode(".", $elements);
$key = array_shift($elements);
if ($key == 'default') {
$key = array_shift($elements);
if ($cmrf_form_processor_latest_default_data && isset($cmrf_form_processor_latest_default_data[$key])) {
$replacements[$original] = $cmrf_form_processor_latest_default_data[$key];
}
} else {
if ($cmrf_form_processor_latest_result && isset($cmrf_form_processor_latest_result[$key])) {
$value = _cmrf_form_processor_value_from_result($elements, $cmrf_form_processor_latest_result[$key]);
$replacements[$original] = $value;
} elseif (cmrf_form_processor_latest_result && isset($cmrf_form_processor_latest_result[$keyV2])) {
$replacements[$original] = $cmrf_form_processor_latest_result[$keyV2];
}
}
}
}
return $replacements;
}
function _cmrf_form_processor_value_from_result($elements, $data) {
$key = array_shift($elements);
if (isset($data[$key]) && is_array($data[$key])) {
return _cmrf_form_processor_value_from_result($elements, $data[$key]);
} elseif (isset($data[$key]) && !is_array($data[$key])) {
return $data[$key];
}
return null;
}
/**
* Implements hook_form_alter().
*
* Adds the possibility to use an option list from the form processor
* on a select field.
*/
function cmrf_form_processor_form_webform_component_edit_form_alter(&$form, &$form_state) {
if ($form['type']['#value'] != 'select') {
return;
}
$node = $form['#node'];
if (!$node->cmrf_form_processor_enable) {
return;
}
$enabled = false;
$force_load = false;
$option_field = null;
$option_field_params = null;
$cid = $form['cid']['#value'];
if ($cid) {
$component = isset($node->webform['components'][$cid]) ? $node->webform['components'][$cid] : FALSE;
if ($component) {
$enabled = isset($component['extra']['cmrf_form_processor_options_enabled']) && $component['extra']['cmrf_form_processor_options_enabled'] ? true : false;
$force_load = isset($component['extra']['cmrf_form_processor_options_force_load']) && $component['extra']['cmrf_form_processor_options_force_load'] ? true : false;
if ($enabled && isset($component['extra']['cmrf_form_processor_options'])) {
$option_field = $component['extra']['cmrf_form_processor_options'];
}
if ($enabled && isset($component['extra']['cmrf_form_processor_options_params'])) {
$option_field_params = $component['extra']['cmrf_form_processor_options_params'];
}
}
}
$profile = !empty($node->cmrf_form_processor_profile) ? $node->cmrf_form_processor_profile : null;
$form_processor = $node->cmrf_form_processor_processor;
$available_fields = cmrf_form_processor_get_fields($form_processor, $profile);
$fields_with_options = array();
foreach($available_fields as $field) {
if (isset($field['options']) && is_array($field['options'])) {
$fields_with_options[$field['name']] = $field['title'];
}
}
$form['extra']['cmrf_form_processor_options_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Load options from CiviMRF Form Processor'),
'#default_value' => $enabled,
);
$form['extra']['cmrf_form_processor_options_force_load'] = array(
'#type' => 'checkbox',
'#title' => t('Force load options from CiviMRF Form Processor (Enable this when you have enabled the entity cache module)'),
'#default_value' => $force_load,
'#states' => array(
'visible' => array(
'input[name="extra[cmrf_form_processor_options_enabled]"]' => array('checked' => true),
),
),
);
$form['extra']['cmrf_form_processor_options'] = array(
'#type' => 'select',
'#title' => t('Options from the CiviMRF form processor'),
'#options' => $fields_with_options,
'#default_value' => $option_field,
'#states' => array(
'visible' => array(
'input[name="extra[cmrf_form_processor_options_enabled]"]' => array('checked' => true),
),
),
);
$form['extra']['cmrf_form_processor_options_params'] = array(
'#type' => 'textarea',
'#title' => t('Additional parameters for retrieving the option list'),
'#description' => t('If you have a conditional option list, enter the parameters for that list here. Enter it in json and you could use tokens.').theme('webform_token_help'),
'#default_value' => $option_field_params,
'#states' => array(
'visible' => array(
'input[name="extra[cmrf_form_processor_options_enabled]"]' => array('checked' => true),
),
),
);
$form['extra']['items']['#states']['invisible']['input[name="extra[cmrf_form_processor_options_enabled]"]'] = array('checked' => true);
$form['extra']['items']['#states']['required']['input[name="extra[cmrf_form_processor_options_enabled]"]'] = array('checked' => false);
$form['extra']['options_source']['#states']['invisible']['input[name="extra[cmrf_form_processor_options_enabled]"]'] = array('checked' => true);
if (isset($form_state['input']['extra']['cmrf_form_processor_options_enabled']) && $form_state['input']['extra']['cmrf_form_processor_options_enabled']) {
$form['extra']['items']['#required'] = false;
}
}
/**
* Implements hook_form_alter().
*/
function cmrf_form_processor_form_webform_components_form_alter(&$form, &$form_state) {
$node = $form['#node'];
if (isset($node->cmrf_form_processor_enable) && $node->cmrf_form_processor_enable) {
$profiles = cmrf_core_list_profiles();
$profile = t('Default');
if (!empty($node->cmrf_form_processor_profile) && isset($profiles[$node->cmrf_form_processor_profile])) {
$profile = $profiles[$node->cmrf_form_processor_profile]['label'];
}
$form_processor = $node->cmrf_form_processor_processor;
$form['warning']['civimrf_form_processor'] = array(
'#markup' => '
<p class="civimrf_form_processor-description">
<span class="civimrf_form_processor-icon enable"></span>' .
t('CiviMRF Form Processor is enabled and this webform will be submitted to CiviCRM with the %profile profile to %form_processor', array('%profile' => $profile, '%form_processor' => $form_processor)) .
'</p>',
'#weight' => -1000,
);
// Get the fields
$fields = cmrf_form_processor_get_fields($form_processor, $node->cmrf_form_processor_profile);
foreach($node->webform['components'] as $comp) {
if (isset($fields[$comp['form_key']])) {
unset($fields[$comp['form_key']]);
}
}
drupal_alter('cmrf_form_processor_missing_fields', $fields, $node);
foreach($fields as $fieldName => $field) {
if (isset($field['api.required']) && !$field['api.required']) {
unset($fields[$fieldName]);
}
}
$missing_fields = array_keys($fields);
if (count($missing_fields)) {
drupal_set_message(t('The field(s) %fields are missing from the form', array('%fields' => implode(", ", $missing_fields))), 'error');
}
$form['#attached']['css'] = array(
drupal_get_path('module', 'cmrf_form_processor') . '/css/cmrf_form_processor.css',
);
}
return $form;
}
/**
* implements hook_webform_component_render_alter().
*
* Add the options from the form processor option list when the administrator
* has selected to use an option list from the form processor.
*/
function cmrf_form_processor_webform_component_render_alter(&$element, $component) {
if (!$component) {
return;
}
if ($component['type'] != 'select') {
return;
}
$node = node_load($component['nid']);
if (!$node->cmrf_form_processor_enable) {
return;
}
$profile = !empty($node->cmrf_form_processor_profile) ? $node->cmrf_form_processor_profile : NULL;
$form_processor = $node->cmrf_form_processor_processor;
$enabled = isset($component['extra']['cmrf_form_processor_options_enabled']) && $component['extra']['cmrf_form_processor_options_enabled'] ? TRUE : FALSE;
$force_load = isset($component['extra']['cmrf_form_processor_options_force_load']) && $component['extra']['cmrf_form_processor_options_force_load'] ? TRUE : FALSE;
if (isset($element['#options']) && !$force_load) {
if (is_array($element['#options']) && count($element['#options'])) {
$enabled = false;
}
}
if ($enabled && isset($component['extra']['cmrf_form_processor_options'])) {
if (isset($component['extra']['cmrf_loaded_options']) && is_array($component['extra']['cmrf_loaded_options']) && !$force_load) {
$available_options = $component['extra']['cmrf_loaded_options'];
} else {
$additional_params = [];
if (isset($component['extra']['cmrf_form_processor_options_params'])) {
$additional_params = json_decode(token_replace($component['extra']['cmrf_form_processor_options_params']), TRUE);
}
$option_field = $component['extra']['cmrf_form_processor_options'];
$available_options = cmrf_form_processor_get_options($form_processor, $option_field, $profile, $additional_params);
}
if (is_array($available_options)) {
$element['#options'] = $available_options;
$element['#access'] = TRUE;
}
}
}
function cmrf_form_processor_form_cmrf_webform_settings_alter(&$form, &$form_state) {
$profiles = cmrf_core_list_profiles();
$profiles_options = array('' => t(' - Default profile -'));
foreach($profiles as $profile_name => $profile) {
$profiles_options[$profile_name] = $profile['label'];
}
$node = $form['#node'];
$enabled = isset($node->cmrf_form_processor_enable) && $node->cmrf_form_processor_enable ? TRUE : FALSE;
$queue = isset($node->cmrf_form_processor_queue) && $node->cmrf_form_processor_queue ? TRUE : FALSE;
$clear_submission = isset($node->cmrf_form_processor_clear_submission) ? $node->cmrf_form_processor_clear_submission : TRUE;
$profile = isset($node->cmrf_form_processor_profile) ? $node->cmrf_form_processor_profile : '';
$processor = isset($node->cmrf_form_processor_processor) ? $node->cmrf_form_processor_processor : '';
$defaultData = isset($node->cmrf_form_processor_default_data) ? $node->cmrf_form_processor_default_data : array();
$defaultDataEnabled = isset($node->cmrf_form_processor_default_data_enable) && $node->cmrf_form_processor_default_data_enable ? TRUE : FALSE;
$defaultDataRequired = isset($node->cmrf_form_processor_default_data_required) && $node->cmrf_form_processor_default_data_required ? TRUE : FALSE;
$defaultDataRequiredMessage = isset($node->cmrf_form_processor_default_data_required_message) ? $node->cmrf_form_processor_default_data_required_message : t('Invalid link');
$defaultDataCache = isset($node->cmrf_form_processor_default_data_cache) ? $node->cmrf_form_processor_default_data_cache : '3 minutes';
$submitted_profile = !empty($form_state['values']['cmrf_form_processor_profile']) ? $form_state['values']['cmrf_form_processor_profile'] : $profile;
$processors = cmrf_form_processor_get_processor_list($submitted_profile);
if (empty($processor)) {
reset($processors);
$processor = key($processors);
}
$submitted_processor = !empty($form_state['values']['cmrf_form_processor_processor']) ? $form_state['values']['cmrf_form_processor_processor'] : $processor;
$form['cmrf_webform']['cmrf_enable']['#options']['cmrf_form_processor'] = t('Submit to CiviCRM Form Processor');
if ($enabled && empty($form['cmrf_webform']['cmrf_enable']['#default_value'])) {
$form['cmrf_webform']['cmrf_enable']['#default_value'] = 'cmrf_form_processor';
}
$form['cmrf_form_processor']['cmrf_form_processor_queue'] = array(
'#type' => 'checkbox',
'#title' => t('Handle the submission in the background'),
'#description' => t('Submit this webform in the background. This means that the user does not have to wait till the submission is processed. You have to enable the cron to get this working.'),
'#default_value' => $queue,
'#states' => array(
'visible' => array(
'select[name="cmrf_enable"]' => array('value' => 'cmrf_form_processor'),
),
),
);
$form['cmrf_form_processor']['cmrf_form_processor_clear_submission'] = array(
'#type' => 'checkbox',
'#title' => t('Delete the submission after processing.'),
'#description' => t('Deletes the submission form the webform results after the data has been submitted to CiviCRM.'),
'#default_value' => $clear_submission,
'#states' => array(
'visible' => array(
'select[name="cmrf_enable"]' => array('value' => 'cmrf_form_processor'),
),
),
);
$form['cmrf_form_processor']['cmrf_form_processor_profile'] = array(
'#type' => 'select',
'#title' => t('CiviMRF Connection profile'),
'#options' => $profiles_options,
'#default_value' => $profile,
'#states' => array(
'visible' => array(
'select[name="cmrf_enable"]' => array('value' => 'cmrf_form_processor'),
),
),
'#ajax' => array(
'callback' => 'cmrf_form_processor_settings_processor_options',
'wrapper' => 'cmrf-webform-settings',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['cmrf_form_processor']['cmrf_form_processor_processor'] = array(
'#type' => 'select',
'#title' => t('Form Processor'),
'#description' => t('CiviMRF Form Processor works with submitting data to the CiviCRM Form Processor API. This field specifies which form processor to use.'),
'#default_value' => $processor,
'#options' => $processors,
'#states' => array(
'visible' => array(
'select[name="cmrf_enable"]' => array('value' => 'cmrf_form_processor'),
),
'required' => array(
'select[name="cmrf_enable"]' => array('value' => 'cmrf_form_processor'),
),
),
'#prefix' => '<div id="wrapper-cmrf_form_processor_processor">',
'#suffix' => '</div>',
'#ajax' => array(
'callback' => 'cmrf_form_processor_settings_processor_default',
'wrapper' => 'wrapper-cmrf_form_processor_default',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['cmrf_form_processor_default'] = array(
'#type' => 'fieldset',
'#title' => t('Prefill the form with existing data'),
'#states' => array(
'visible' => array(
'select[name="cmrf_enable"]' => array('value' => 'cmrf_form_processor'),
),
),
);
$form['cmrf_form_processor_default']['cmrf_form_processor_default_data_enable'] = array(
'#type' => 'checkbox',
'#title' => t('Prefill the form with existing data'),
'#description' => t('When enabled you will be given options on how to retrieve the data.'),
'#default_value' => $defaultDataEnabled,
);
$form['cmrf_form_processor_default']['cmrf_form_processor_default_data_required'] = array(
'#type' => 'checkbox',
'#title' => t('Default data is required'),
'#description' => t('If checked the form is only visible when default data is succesfully loaded'),
'#default_value' => $defaultDataRequired,
'#states' => array(
'visible' => array(
'input[name="cmrf_form_processor_default_data_enable"]' => array('checked' => true),
),
),
);
$form['cmrf_form_processor_default']['cmrf_form_processor_default_data_required_message'] = array(
'#type' => 'textfield',
'#title' => t('Message when CiviCRM does not return any data'),
'#default_value' => $defaultDataRequiredMessage,
'#states' => array(
'visible' => array(
'input[name="cmrf_form_processor_default_data_required"]' => array('checked' => true),
),
),
);
$form['cmrf_form_processor_default']['cmrf_form_processor_default_data'] = array(
'#tree' => TRUE,
'#prefix' => '<div id="wrapper-cmrf_form_processor_default">',
'#suffix' => '</div>',
'#type' => 'container',
'#theme' => 'table',
'#header' => array(t('Field'), t('Value')),
'#rows' => array(),
'#states' => array(
'visible' => array(
'input[name="cmrf_form_processor_default_data_enable"]' => array('checked' => true),
),
),
);
if (!empty($submitted_processor)) {
$defaultFields = cmrf_form_processor_get_default_fields($submitted_processor, $submitted_profile);
$i=0;
foreach($defaultFields as $field) {
$required = '';
$defaultFieldName = array(
'#id' => 'default_field_name_'.$i,
'#type' => 'hidden',
'#default_value' => $field['name'],
'#prefix' => $field['title'],
'#suffix' => $field['api.required'] ? '<span class="form-required">*</span>' : '',
);
$defaultFieldData = array(
'#id' => 'default_field_data_'.$i,
'#default_value' => isset($defaultData[$field['name']]) ? $defaultData[$field['name']] : '',
'#type' => 'textfield',
'#required' => false, //$field['api.required'],
'#description' => theme('webform_token_help'),
);
$form['cmrf_form_processor_default']['cmrf_form_processor_default_data'][$i] = array(
'default_field_name' => &$defaultFieldName,
'default_field_data' => &$defaultFieldData,
);
$form['cmrf_form_processor_default']['cmrf_form_processor_default_data']['#rows'][] = array(
array('data' => &$defaultFieldName),
array('data' => &$defaultFieldData),
);
unset($defaultFieldName);
unset($defaultFieldData);
$i++;
}
}
$form['cmrf_form_processor_default']['cmrf_form_processor_default_data_cache'] = array(
'#type' => 'textfield',
'#title' => t('Cache time for retrieved default values'),
'#description' => t('Leave empty to disable the cache. Or enter a value in the format as 1 minute, 30 minutes, 2 seconds, etc.'),
'#default_value' => $defaultDataCache,
'#states' => array(
'visible' => array(
'input[name="cmrf_form_processor_default_data_enable"]' => array('checked' => true),
),
),
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit']['#type'] = 'submit';
$form['actions']['submit']['#value'] = t('Save');
$form['#validate'][] = 'cmrf_form_processor_settings_validate';
$form['#submit'][] = 'cmrf_form_processor_settings_submit';
return $form;
}
function cmrf_form_processor_settings_processor_options($form, $form_state) {
return $form;
}
function cmrf_form_processor_settings_processor_default($form, $form_state) {
return $form['cmrf_webform']['cmrf_form_processor_default'];
}
/**
* Validate the webform settings form.
*
* @param array $form
* The form.
* @param array $form_state
* The form state.
*/
function cmrf_form_processor_settings_validate(array &$form, array &$form_state) {
if ($form_state['values']['cmrf_enable'] == 'cmrf_form_processor') {
if (empty($form_state['values']['cmrf_form_processor_processor'])) {
form_set_error('cmrf_form_processor_processor', t('Form Processor is required.'));
} else {
$submitted_profile = $form_state['values']['cmrf_form_processor_profile'];
$submitted_processor = $form_state['values']['cmrf_form_processor_processor'];
$defaultFields = cmrf_form_processor_get_default_fields($submitted_processor, $submitted_profile);
if (isset($form_state['values']['cmrf_form_processor_default_data_enable']) && $form_state['values']['cmrf_form_processor_default_data_enable']) {
foreach($form_state['input']['cmrf_form_processor_default_data'] as $default_data) {
if (empty($default_data['default_field_data']) && !empty($defaultFields[$default_data['default_field_name']]['api.required'])) {
form_set_error('cmrf_form_processor_default_data', t('Not all required parameters are set'));
}
}
}
}
}
}
/**
* Submit the webform settings form and store the civimrf settings.
*
* @param array $form
* The form.
* @param array $form_state
* The form state.
*/
function cmrf_form_processor_settings_submit(array &$form, array &$form_state) {
$node = &$form['#node'];
$node->cmrf_form_processor_enable = $form_state['values']['cmrf_enable'] == 'cmrf_form_processor' ? TRUE : FALSE;
$node->cmrf_form_processor_queue = $form_state['values']['cmrf_form_processor_queue'] ? TRUE : FALSE;
$node->cmrf_form_processor_clear_submission = $form_state['values']['cmrf_form_processor_clear_submission'] ? TRUE : FALSE;
$node->cmrf_form_processor_profile = $form_state['values']['cmrf_form_processor_profile'] ? $form_state['values']['cmrf_form_processor_profile'] : '';
$node->cmrf_form_processor_processor = $form_state['values']['cmrf_form_processor_processor'] ? $form_state['values']['cmrf_form_processor_processor'] : '';
$node->cmrf_form_processor_default_data_cache = isset($form_state['values']['cmrf_form_processor_default_data_cache']) ? $form_state['values']['cmrf_form_processor_default_data_cache'] : '';
$node->cmrf_form_processor_default_data_enable = isset($form_state['values']['cmrf_form_processor_default_data_enable']) && $form_state['values']['cmrf_form_processor_default_data_enable'] ? true : false;
$node->cmrf_form_processor_default_data_required = isset($form_state['values']['cmrf_form_processor_default_data_required']) && $form_state['values']['cmrf_form_processor_default_data_required'] ? true : false;
$node->cmrf_form_processor_default_data_required_message = isset($form_state['values']['cmrf_form_processor_default_data_required_message']) ? $form_state['values']['cmrf_form_processor_default_data_required_message'] : '';
$node->cmrf_form_processor_default_data = array();
if (isset($form_state['input']['cmrf_form_processor_default_data'])) {
foreach($form_state['input']['cmrf_form_processor_default_data'] as $default_data) {
$node->cmrf_form_processor_default_data[$default_data['default_field_name']] = $default_data['default_field_data'];
}
}
node_save($node);
if ($form_state['values']['cmrf_enable'] == 'cmrf_form_processor') {
drupal_set_message(t('Webform %name CiviMRF Form Processor enabled.', array('%name' => $node->title)));
}
}
/**
* Deferred hook_node_load for webforms.
*
* @param array $nodes
* Array of nodes.
* @param array $types
* Array of types.
*/
function cmrf_form_processor_node_load($nodes, $types) {
$db = db_query('SELECT * FROM {cmrf_form_processor_webforms} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)));
foreach ($db as $cmrf_webform) {
$node = $nodes[$cmrf_webform->nid];
$node->cmrf_form_processor_enable = $cmrf_webform->cmrf_form_processor_enable ? TRUE : FALSE;
$node->cmrf_form_processor_queue = $cmrf_webform->cmrf_form_processor_queue ? TRUE : FALSE;
$node->cmrf_form_processor_clear_submission = $cmrf_webform->cmrf_form_processor_clear_submission ? TRUE : FALSE;
$node->cmrf_form_processor_profile = $cmrf_webform->cmrf_form_processor_profile ? $cmrf_webform->cmrf_form_processor_profile : '';
$node->cmrf_form_processor_processor = $cmrf_webform->cmrf_form_processor_processor ? $cmrf_webform->cmrf_form_processor_processor : '';
$node->cmrf_form_processor_default_data_enable = $cmrf_webform->cmrf_form_processor_default_data_enable ? TRUE : FALSE;
$node->cmrf_form_processor_default_data_required = $cmrf_webform->cmrf_form_processor_default_data_required ? TRUE : FALSE;
$node->cmrf_form_processor_default_data_required_message = $cmrf_webform->cmrf_form_processor_default_data_required_message;
$node->cmrf_form_processor_default_data_cache = $cmrf_webform->cmrf_form_processor_default_data_cache;
$node->cmrf_form_processor_default_data = !empty($cmrf_webform->cmrf_form_processor_default_data) ? json_decode($cmrf_webform->cmrf_form_processor_default_data, true) : array();
if ($node->cmrf_form_processor_enable) {
$profile = !empty($node->cmrf_form_processor_profile) ? $node->cmrf_form_processor_profile : NULL;
$form_processor = $node->cmrf_form_processor_processor;
foreach($node->webform['components'] as $cid => $component) {
if (isset($component['extra']['cmrf_form_processor_options_enabled']) && $component['extra']['cmrf_form_processor_options_enabled']) {
$force_load = isset($component['extra']['cmrf_form_processor_options_force_load']) && $component['extra']['cmrf_form_processor_options_force_load'] ? true : false;
if (!$force_load) {
$additional_params = [];
if (isset($component['extra']['cmrf_form_processor_options_params'])) {
$additional_params = json_decode(token_replace($component['extra']['cmrf_form_processor_options_params']), TRUE);
}
$option_field = $component['extra']['cmrf_form_processor_options'];
$available_options = cmrf_form_processor_get_options($form_processor, $option_field, $profile, $additional_params);
$nodes[$cmrf_webform->nid]->webform['components'][$cid]['extra']['items'] = _webform_select_options_to_text($available_options);
$nodes[$cmrf_webform->nid]->webform['components'][$cid]['extra']['cmrf_loaded_options'] = $available_options;
}
}
}
}
}
}
/**
* Implements hook_node_insert().
*/
function cmrf_form_processor_node_insert($node) {
db_insert('cmrf_form_processor_webforms')
->fields(array(
'nid' => $node->nid,
'cmrf_form_processor_enable' => !empty($node->cmrf_form_processor_enable) ? 1 : 0,
'cmrf_form_processor_queue' => !empty($node->cmrf_form_processor_queue) ? 1 : 0,
'cmrf_form_processor_clear_submission' => !empty($node->cmrf_form_processor_clear_submission) ? 1: 0,
'cmrf_form_processor_profile' => !empty($node->cmrf_form_processor_profile) ? $node->cmrf_form_processor_profile : '',
'cmrf_form_processor_processor' => !empty($node->cmrf_form_processor_processor) ? $node->cmrf_form_processor_processor : '',
'cmrf_form_processor_default_data_enable' => !empty($node->cmrf_form_processor_default_data_enable) ? 1 : 0,
'cmrf_form_processor_default_data_required' => !empty($node->cmrf_form_processor_default_data_required) ? 1 : 0,
'cmrf_form_processor_default_data_required_message' => $node->cmrf_form_processor_default_data_required_message ? $node->cmrf_form_processor_default_data_required_message : '',
'cmrf_form_processor_default_data_cache' => !empty($node->cmrf_form_processor_default_data_cache) ? $node->cmrf_form_processor_default_data_cache : '',
'cmrf_form_processor_default_data' => !empty($node->cmrf_form_processor_default_data) ? json_encode($node->cmrf_form_processor_default_data) : '',
))
->execute();
}
/**
* Implements hook_node_update().
*/
function cmrf_form_processor_node_update($node) {
db_merge('cmrf_form_processor_webforms')
->key(array(
'nid' => $node->nid,
))
->fields(array(
'cmrf_form_processor_enable' => !empty($node->cmrf_form_processor_enable) ? 1 : 0,
'cmrf_form_processor_queue' => !empty($node->cmrf_form_processor_queue) ? 1 : 0,
'cmrf_form_processor_clear_submission' => !empty($node->cmrf_form_processor_clear_submission) ? 1: 0,
'cmrf_form_processor_profile' => !empty($node->cmrf_form_processor_profile) ? $node->cmrf_form_processor_profile : '',
'cmrf_form_processor_processor' => !empty($node->cmrf_form_processor_processor) ? $node->cmrf_form_processor_processor : '',
'cmrf_form_processor_default_data_enable' => !empty($node->cmrf_form_processor_default_data_enable) ? 1 : 0,
'cmrf_form_processor_default_data_required' => !empty($node->cmrf_form_processor_default_data_required) ? 1 : 0,
'cmrf_form_processor_default_data_required_message' => $node->cmrf_form_processor_default_data_required_message ? $node->cmrf_form_processor_default_data_required_message : '',
'cmrf_form_processor_default_data_cache' => $node->cmrf_form_processor_default_data_cache ? $node->cmrf_form_processor_default_data_cache : '',
'cmrf_form_processor_default_data' => !empty($node->cmrf_form_processor_default_data) ? json_encode($node->cmrf_form_processor_default_data) : '',
))
->execute();
}
/**
* Implements hook_node_delete().
*/
function cmrf_form_processor_node_delete($node) {
db_delete('cmrf_form_processor_webforms')
->condition('nid', $node->nid)
->execute();
}