-
Notifications
You must be signed in to change notification settings - Fork 108
/
webform_civicrm.module
926 lines (840 loc) · 30.6 KB
/
webform_civicrm.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
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
<?php
use Drupal\webform\Entity\Webform;
/**
* @file
* Webform CiviCRM Integration Module:
* Links webform submissions to contacts in a CiviCRM database.
* @author Coleman Watts
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\node\Entity\Node;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\Core\Link;
/**
* The versions of CiviCRM and WebForm. Min is >=. Max is <. FALSE = no MAX
*/
define('WEBFORM_CIVICRM_CIVICRM_VERSION_MIN', '5.12');
define('WEBFORM_CIVICRM_CIVICRM_VERSION_MAX', FALSE);
define('WEBFORM_CIVICRM_WEBFORM_VERSION', '5.0');
define('WEBFORM_CIVICRM_DEFAULT_CONTACT_ID', 1);
/**
* Add webform receipt params to contribution emails.
*
* @param array $params
* @param string $context
*/
function webform_civicrm_civicrm_alterMailParams(&$params, $context) {
if (!empty($params['valueName'])) {
if (in_array($params['valueName'], ['contribution_online_receipt', 'membership_online_receipt']) && !empty($params['tplParams']['contributionID'])) {
$query = \Drupal::database()
->select('webform_civicrm_submissions', 'wcs')
->condition('civicrm_data', "%{$params['tplParams']['contributionID']}%", 'LIKE')
->condition('contact_id', "%-{$params['contactId']}-%", 'LIKE')
->fields('wcs', ['sid', 'civicrm_data']);
$query->leftJoin('webform_submission', 'ws', 'ws.sid = wcs.sid');
$query->isNotNull('ws.sid');
$results = $query->execute();
while ($content = $results->fetchAssoc()) {
$civicrm_data = unserialize($content['civicrm_data']);
if (!empty($content['sid']) && !empty($civicrm_data['contribution'][1]['id']) && $civicrm_data['contribution'][1]['id'] == $params['tplParams']['contributionID']) {
$submission = WebformSubmission::load($content['sid']);
$webform = $submission->getWebform();
$handler = $webform->getHandlers('webform_civicrm');
$config = $handler->getConfiguration();
if (empty($config['webform_civicrm'])) {
continue;
}
$settings = &$config['webform_civicrm']['settings'];
$utils = \Drupal::service('webform_civicrm.utils');
$submissionData = array_merge($settings['data'], $submission->getData());
$receiptParams = $utils->getReceiptParams($submissionData, $params['tplParams']['contributionID']);
$params['tplParams']= array_merge($params['tplParams'], $receiptParams);
}
}
}
}
}
/**
* Implements hook_element_info_alter().
*/
function webform_civicrm_element_info_alter(array &$types) {
$types['datetime']['#process'][] = 'webform_civicrm_datetime_set_format';
}
/**
* Remove seconds from the HTML5 datetime widget.
*/
function webform_civicrm_datetime_set_format($element) {
if (!empty($element['#date_time_element']) && $element['#date_time_element'] == 'time' && !empty($element['time']['#value'])) {
$parts = explode(':', $element['time']['#value']);
$parts = array_splice($parts, 0, 2);
$element['time']['#value'] = implode(':', $parts);
}
return $element;
}
/**
* Implements hook_form_alter().
*/
function webform_civicrm_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'webform_edit_form' && $form_state->getFormObject()->getEntity()->getHandlers()->has('webform_civicrm')) {
\Drupal::service('civicrm')->initialize();
\CRM_Core_Resources::singleton()->addCoreResources();
}
//Do not load civicrm elements on the "Add element" form.
if ($form_id == 'webform_ui_element_type_select_form') {
foreach ($form as $k => $fieldset) {
if (is_array($fieldset) && !empty($fieldset['#title']) && strtolower($fieldset['#title']) == 'civicrm') {
unset($form[$k]);
return;
}
}
}
/*
// Alter back-end webform component edit forms
if ($form_id == 'webform_component_edit_form') {
if ($form['type']['#value'] == 'pagebreak') {
form_load_include($form_state, 'inc', 'webform_civicrm', 'includes/wf_crm_admin_component');
$admin_form = new wf_crm_admin_component($form, $form_state);
$admin_form->adjustPageBreak();
}
}
// Validation for webform components tab
elseif ($form_id == 'webform_components_form') {
form_load_include($form_state, 'inc', 'webform_civicrm', 'includes/wf_crm_admin_component');
$form['#validate'][] = 'wf_crm_components_form_validate';
if (empty($form_state['input'])) {
wf_crm_admin_component::checkBillingPagination($form['#node']);
}
}
*/
}
/**
* Implements hook_webform_autocomplete_options()
*
* Invoked in webform_autocomplete module.
* This appends options to civicrm custom fields rendered as autocomplete.
*
* @param array $results
* @param object $node
* @param int $cid component id
* @param string $str
*/
function webform_civicrm_webform_autocomplete_options_alter(&$results, $node, $cid, $str) {
$utils = \Drupal::service('webform_civicrm.utils');
if (\Drupal::service('webform_civicrm.webform_ajax')->autocompleteAccess($node, $cid)) {
$key = $utils->wf_crm_explode_key($node->webform['components'][$cid]['form_key']);
}
if (isset($key) && substr($key[5], 0, 7) == 'custom_') {
\Drupal::service('civicrm')->initialize();
$customField = $utils->wf_civicrm_api('CustomField', 'getsingle', [
'id' => substr($key[5], 7),
'return' => 'option_group_id',
]);
if (!empty($customField['option_group_id'])) {
$options = $utils->wf_crm_apivalues('OptionValue', 'get', [
'label' => ['LIKE' => "%{$str}%"],
'return' => 'label',
'option_group_id' => $customField['option_group_id'],
'limit' => $node->webform['components'][$cid]['extra']['autocomplete_result_count'],
], 'label');
$results = array_combine($options, $options);
}
}
}
/**
* Implements hook_theme().
*
* @return array
*/
function webform_civicrm_theme() {
return [
'webform_civicrm_contact' => [
'base hook' => 'input',
],
];
}
/**
* Implements hook_entity_load()
* Display entity links on submission page.
*
* @param array $entities
*/
function webform_civicrm_webform_submission_load($entities) {
foreach ($entities as $entity) {
$data = _fillCiviCRMData($entity->getData(), $entity);
$entity->setData($data);
}
}
/**
* Fill civicrm data to the submission object.
*
* @param array $data
* @param object $webformSubmission
*/
function _fillCiviCRMData($data, $webformSubmission) {
if (!empty($data['civicrm'])) {
return $data;
}
\Drupal::service('civicrm')->initialize();
$utils = \Drupal::service('webform_civicrm.utils');
$webform = $webformSubmission->getWebform();
if (\Drupal::routeMatch()->getRouteName() == 'entity.webform.results_submissions') {
foreach ($data as $key => $val) {
$element = $webform->getElement($key);
if ($element && !empty($val) && $element['#type'] == 'civicrm_options') {
if (!empty($element['#webform_multiple'])) {
foreach ($val as $k => $v) {
if (isset($element['#options'][$v])) {
$data[$key]["{$k}_raw"] = $data[$key][$k];
$data[$key][$k] = $element['#options'][$v];
}
}
}
elseif (isset($element['#options'][$val])) {
$data["{$key}_raw"] = $data[$key];
$data[$key] = $element['#options'][$val];
}
elseif (strpos($key, 'state_province_id') !== false) {
$country_key = str_replace('state_province_id', 'country_id', $key);
$country_id = $data["{$country_key}_raw"] ?? $data[$country_key] ?? NULL;
$params = [
'sequential' => 1,
'country_id' => $country_id,
];
$is_abbr = $utils->wf_civicrm_api4('StateProvince', 'get', [
'select' => ['row_count'],
'where' => [['abbreviation', '=', $val]],
])->count() > 0;
if (is_numeric($val)) {
$params['id'] = $val;
}
elseif ($is_abbr) {
$params['abbreviation'] = $val;
}
else {
continue;
}
$data[$key] = $utils->wf_crm_apivalues('StateProvince', 'get', $params, 'name')[0] ?? $data[$key];
}
}
}
}
$contacts = [];
$query = \Drupal::database()->select('webform_civicrm_submissions', 'wcs')
->fields('wcs', ['contact_id', 'civicrm_data'])
->condition('sid', $webformSubmission->id(), '=');
$results = $query->execute();
while ($content = $results->fetchAssoc()) {
$civicrm_data = unserialize($content['civicrm_data']) + ['contact' => []];
if ($content['contact_id']) {
foreach (explode('-', trim($content['contact_id'], '-')) as $c => $cid) {
$civicrm_data['contact'][$c + 1]['id'] = $cid;
$civicrm_data['contact'][$c + 1]['display_name'] = '';
if ($c == 0 && $cid) {
$contacts[$cid] = '';
}
}
}
$data['civicrm'] = $civicrm_data;
}
if ($contacts) {
// Retrieve contact names and add to submission objects
$contacts = $utils->wf_crm_apivalues('contact', 'get', ['id' => ['IN' => array_keys($contacts)]], 'display_name') + $contacts;
if (!empty($data['civicrm']['contact'][1]['id'])) {
$data['civicrm']['contact'][1]['display_name'] = $contacts[$data['civicrm']['contact'][1]['id']];
}
}
return $data;
}
/**
* Implements hook_webform_submission_view_alter().
*
* Add display name to title while viewing a submission.
*/
function webform_civicrm_webform_submission_view_alter(array &$build, WebformSubmission $submission) {
$webform = $submission->getWebform();
$config = $webform->getHandlers('webform_civicrm')->getConfiguration();
if (!empty($config['webform_civicrm']) && !empty($submission->getData()['civicrm']) && \Drupal::currentUser()->hasPermission('access CiviCRM')) {
$data = $submission->getData()['civicrm'];
$links = [];
$entity_links = [
'contact' => 'contact/view',
'activity' => 'activity',
'contribution' => 'contact/view/contribution',
'participant' => 'contact/view/participant'
];
foreach ($entity_links as $entity => $link) {
if (!empty($data[$entity][1]['id'])) {
$query = ['action' => 'view', 'reset' => 1, 'cid' => $data['contact'][1]['id'], 'id' => $data[$entity][1]['id']];
$name = ucfirst($entity);
if ($entity == 'contact') {
$query = ['reset' => 1, 'cid' => $data['contact'][1]['id']];
$name = $data['contact'][1]['display_name'];
}
$url = Url::fromUri('internal:/civicrm/' . $link, ['query' => $query, 'absolute' => TRUE]);
$links[$entity] = Link::fromTextAndUrl(t('View @entity', ['@entity' => $name]), $url)->toRenderable();
}
}
if (!empty($links)) {
$links_markup = '';
foreach ($links as $link) {
$links_markup .= '<li>' . \Drupal::service('renderer')->renderPlain($link) . '</li>';
}
$build['civicrm_actions'] = [
'#markup' => '<ul class="inline">' . $links_markup . '</ul>',
'#weight' => -10,
];
}
}
}
/**
* Implements hook_civicrm_postSave_tableName().
*
* Handles adding/editing a custom field.
*
* @param CRM_Core_DAO_CustomField $dao
*/
function webform_civicrm_civicrm_postSave_civicrm_custom_field($dao) {
if (empty($dao->custom_group_id)) {
$dao->find(TRUE);
}
if ($dao->is_active == 1) {
$admin_form = \Drupal::service('webform_civicrm.admin_form');
$admin_form::handleDynamicCustomField('create', $dao->id, $dao->custom_group_id);
}
}
/**
* Implements hook_civicrm_post().
*
* Handles delete of a custom field.
*
* TODO: In theory, this could also handle save, and we don't need to implement the above hook.
* However, this hook dosen't support CustomField in CiviCRM < 4.7.14 (or LTS < 4.6.24).
*
* @param string $op
* @param string $name
* @param int $id
* @param CRM_Core_DAO $dao
*/
function webform_civicrm_civicrm_post($op, $name, $id, $dao) {
if ($name == 'CustomField' && $op == 'delete') {
$admin_form = \Drupal::service('webform_civicrm.admin_form');
$admin_form::handleDynamicCustomField($op, $id, $dao->custom_group_id);
}
}
/**
* Implements hook_civicrm_postSave_tableName().
*
* Handles adding/editing a custom group.
*
* @param CRM_Core_DAO_CustomGroup $dao
*/
function webform_civicrm_civicrm_postSave_civicrm_custom_group($dao) {
// @todo Webform elements are not stored in the table and this does not work.
// @todo evaluate how to update webform when custom groups change.
}
/**
* Implements hook_civicrm_buildForm().
* @param string $formName
* @param CRM_Core_Form $form
*/
function webform_civicrm_civicrm_buildForm($formName, $form) {
// Warn user when deleting custom fields used by webforms
if ($formName == 'CRM_Custom_Form_DeleteField') {
$nodes = [];
$fid = $form->getVar('_id');
if ($fid) {
// @todo Start using webform_civicrm_forms to track enabled webforms.
/** @var \Drupal\webform\WebformInterface[] $webforms */
$webforms = Webform::loadMultiple();
foreach ($webforms as $webform) {
$handler_collection = $webform->getHandlers('webform_civicrm');
if (!$handler_collection->has('webform_civicrm')) {
continue;
}
$elements = $webform->getElementsDecodedAndFlattened();
foreach (array_keys($elements) as $element_form_key) {
if (strpos($element_form_key, "custom_$fid") !== FALSE) {
$nodes[] = $webform->toLink()->toString();
}
}
}
}
if ($nodes) {
$list = '<ul><li>' . implode('</li><li>', $nodes) . '</li></ul>';
CRM_Core_Region::instance('page-body')->add([
'markup' => '<strong>' . t('This field is used in the following webforms:') . '</strong>' . $list,
]);
}
}
}
/**
* Implements hook_civicrm_merge().
* Update submission data to reflect new cids when contacts are merged.
*/
function webform_civicrm_civicrm_merge($type, $data, $new_id = NULL, $old_id = NULL, $tables = NULL) {
if (!empty($new_id) && !empty($old_id) && $type == 'sqls') {
$connection = \Drupal::database();
$connection->update('webform_civicrm_submissions')
->expression('contact_id', 'REPLACE(contact_id, :old, :new)', [':old' => '-' . $old_id . '-', ':new' => '-' . $new_id . '-'])
->condition('contact_id', '%-' . $old_id . '-%', 'LIKE')
->execute();
$connection->update('webform_submission_data')
->expression('value', ':new_cid', [':new_cid' => $new_id])
->condition('value', $old_id)
->condition('name', '%contact_id', 'LIKE')
->execute();
}
}
/**
* Implements hook_help().
*/
function webform_civicrm_help($section) {
if ($section == 'help.page.webform_civicrm') {
// Return a line-break version of the module README.md
return nl2br(file_get_contents(\Drupal::service('extension.list.module')->getPath('webform_civicrm') . '/README.md'));
}
}
/**
* Implements hook_preprocess_HOOK().
* Add CiviCRM names to webform submission results table.
*/
function webform_civicrm_preprocess_webform_results_submissions(&$vars) {
$utils = \Drupal::service('webform_civicrm.utils');
if (count($vars['table']['#rows']) && !empty($vars['node']->webform_civicrm) && webform_results_access($vars['node'])) {
$access = user_access('access CiviCRM');
$temp = $vars['table']['#header'];
$vars['table']['#header'] = [];
// Move contact col to position 2
foreach ($temp as $k => $v) {
$vars['table']['#header'][] = $v;
if ($k == 1) {
$vars['table']['#header'][] = $utils->wf_crm_contact_label(1, $vars['node']->webform_civicrm['data']);
}
}
foreach ($vars['table']['#rows'] as &$row) {
$name = '';
// Get submission id from url
preg_match('#/submission/(\d+)#', $row[4], $preg);
$sid = $preg[1];
if (!empty($vars['submissions'][$sid]->civicrm['contact'][1])) {
$data = $vars['submissions'][$sid]->civicrm;
$name = $data['contact'][1]['display_name'];
if ($name !== '' && $access) {
$name = l($name, 'civicrm/contact/view', [
'query' => ['reset' => 1, 'cid' => $data['contact'][1]['id']],
'attributes' => ['title' => t('View CiviCRM contact')],
'alias' => TRUE,
]);
}
}
$temp = $row;
$row = [];
// Move name to position 2
foreach ($temp as $k => $v) {
$row[] = $v;
if ($k == 1) {
$row[] = $name;
}
}
}
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function webform_civicrm_preprocess_webform_components_form(&$vars) {
\Drupal::ModuleHandler()->loadInclude('webform_civicrm', 'inc', 'includes/wf_crm_admin_component');
wf_crm_admin_component::preprocessComponentsForm($vars['form'], $vars['rows'], $vars['form']['#node']);
}
/**
* Return a value from nested arrays or objects.
*
* @param array|object $haystack
* The array to search
* @param string $keys
* Pass a single key, or multiple keys separated by : to get a nested value
* @param mixed $default
* Value to return if given array key does not exist
* @param bool $strict
* Should we use empty or isset to determine if array key exists? If TRUE, use isset
*
* @return mixed
* found value or default
*/
function wf_crm_aval($haystack, $keys, $default = NULL, $strict = FALSE) {
foreach (explode(':', $keys) as $key) {
if (is_object($haystack)) {
$haystack = (array) $haystack;
}
if (!is_array($haystack) || !isset($haystack[$key]) || (empty($haystack[$key]) && $default !== NULL && !$strict)) {
return $default;
}
$haystack = $haystack[$key];
}
// $haystack has been reduced down to the item we want
return $haystack;
}
/**
* Checks dependencies.
*
* @return array
* Array with TRUE/FALSE for each dependency.
*
* @see webform_civicrm_requirements
*/
function _webform_civicrm_status() {
$status = [];
$status['webform_civicrm'] = FALSE;
$civicrm = \Drupal::service('extension.list.module')->getExtensionInfo('civicrm');
$webform = \Drupal::service('extension.list.module')->getExtensionInfo('webform');
if (version_compare($civicrm['version'], WEBFORM_CIVICRM_CIVICRM_VERSION_MIN, '>=') &&
version_compare($webform['version'], WEBFORM_CIVICRM_WEBFORM_VERSION, '>=')) {
$status['webform_civicrm'] = TRUE;
}
// If there is a max version of CiviCRM supported, check it too.
if (WEBFORM_CIVICRM_CIVICRM_VERSION_MAX && version_compare($civicrm['version'], WEBFORM_CIVICRM_CIVICRM_VERSION_MAX, '>=')) {
$status['webform_civicrm'] = FALSE;
}
return $status;
}
/**
* Implements hook_preprocess_webform_civicrm_contact().
*
* @param array $variables
*/
function webform_civicrm_preprocess_webform_civicrm_contact(&$variables) {
$element = &$variables['element'] ?? NULL;
if (!empty($element['#description'])) {
$variables['description']['content'] = $element['#description'];
$variables['description_display'] = $element['#description_display'];
if (empty($variables['description']['attributes'])) {
$variables['description']['attributes'] = new Attribute();
}
$variables['description']['attributes']->addClass('description');
}
if (!empty($element['#_title_display']) && $element['#_title_display'] == 'inline') {
$variables['title_attributes'] = new Attribute();
$variables['title_attributes']->addClass('webform-element--title-inline');
}
}
/**
* Implements hook_token_info().
*/
function webform_civicrm_token_info() {
$info = [];
$info['contact-id'] = [
'name' => t('Webform CiviCRM Contacts IDs'),
'description' => t('The IDs of Contacts that got created after submitting the webform. Replace the "?" with the contact number starting from 1'),
'dynamic' => TRUE,
];
$info['contact-link'] = [
'name' => t('Webform CiviCRM Contacts Links'),
'description' => t('The links to Contacts that got created after submitting the webform. Replace the "?" with the contact number starting from 1'),
'dynamic' => TRUE,
];
$info['activity-id'] = [
'name' => t('Webform CiviCRM Activity IDs'),
'description' => t('The IDs of activities that got created after submitting the webform. Replace the "?" with the activity number starting from 1'),
'dynamic' => TRUE,
];
$info['activity-link'] = [
'name' => t('Webform CiviCRM Activity Links'),
'description' => t('The links to activities that got created after submitting the webform. Replace the "?" with the activity number starting from 1'),
'dynamic' => TRUE,
];
$info['case-id'] = [
'name' => t('Webform CiviCRM Case IDs'),
'description' => t('The IDs of cases that got created after submitting the webform. Replace the "?" with the case number starting from 1'),
'dynamic' => TRUE,
];
$info['case-link'] = [
'name' => t('Webform CiviCRM Case Links'),
'description' => t('The links to cases that got created after submitting the webform. Replace the "?" with the case number starting from 1'),
'dynamic' => TRUE,
];
return ['tokens' => ['webform_submission' => $info]];
}
/**
* Implements hook_tokens().
*/
function webform_civicrm_tokens($type, $tokens = '', array $data = [], array $options = []) {
// Skip token processing if this is not a webform submission
if (!_webform_civicrm_isWebformSubmission($type, $data)) {
return [];
}
$replacedTokens = [];
$webformSubmissionData = $data['webform_submission']->getData();
$webformSubmissionData = _fillCiviCRMData($webformSubmissionData, $data['webform_submission']);
$contactIdsReplacedTokens = _webform_civicrm_replaceContactIdTokens($tokens, $webformSubmissionData);
$replacedTokens = array_merge($replacedTokens, $contactIdsReplacedTokens);
$contactLinksReplacedTokens = _webform_civicrm_replaceContactLinkTokens($tokens, $webformSubmissionData);
$replacedTokens = array_merge($replacedTokens, $contactLinksReplacedTokens);
$activityIdsReplacedTokens = _webform_civicrm_replaceActivityIdTokens($tokens, $webformSubmissionData);
$replacedTokens = array_merge($replacedTokens, $activityIdsReplacedTokens);
$activityLinksReplacedTokens = _webform_civicrm_replaceActivityLinkTokens($tokens, $webformSubmissionData);
$replacedTokens = array_merge($replacedTokens, $activityLinksReplacedTokens);
$caseIdsReplacedTokens = _webform_civicrm_replaceCaseIdTokens($tokens, $webformSubmissionData);
$replacedTokens = array_merge($replacedTokens, $caseIdsReplacedTokens);
$caseLinksReplacedTokens = _webform_civicrm_replaceCaseLinkTokens($tokens, $webformSubmissionData);
$replacedTokens = array_merge($replacedTokens, $caseLinksReplacedTokens);
return $replacedTokens;
}
/**
* Determines if there is a webform get submitted
*
* @param $tokenType
* @param $webformData
*
* @return bool
* True if this is a webform submisstion and false if not
*/
function _webform_civicrm_isWebformSubmission($tokenType, $webformData) {
return (
$tokenType === 'webform_submission' &&
!empty($webformData['webform_submission'])
);
}
/**
* Replaces contact-id tokens with civicrm contact IDs
*
* @param array $tokens
* Tokens to process
* @param array $webformSubmissionData
* Data submitted by the webform
*
* @return array
* List of replaced contact-id tokens replaced with actual contacts IDs
*/
function _webform_civicrm_replaceContactIdTokens($tokens, $webformSubmissionData) {
$replacedTokens = [];
$tokenValues = \Drupal::token()->findWithPrefix($tokens, 'contact-id');
if (!$tokenValues) {
return $replacedTokens;
}
foreach ($tokenValues as $entityID => $tokenName) {
$tokenNewValue = '';
if (!empty($webformSubmissionData['civicrm']['contact'][$entityID]['id'])) {
$contactID = $webformSubmissionData['civicrm']['contact'][$entityID]['id'];
$tokenNewValue = $contactID;
}
$replacedTokens[$tokenName] = $tokenNewValue;
}
return $replacedTokens;
}
/**
* Replaces contact-link tokens with civicrm contact page links
*
* @param array $tokens
* Tokens to process
* @param array $webformSubmissionData
* Data submitted by the webform
*
* @return array
* List of replaced contact-link tokens replaced with actual contacts links
*/
function _webform_civicrm_replaceContactLinkTokens($tokens, $webformSubmissionData) {
$replacedTokens = [];
$tokenValues = \Drupal::token()->findWithPrefix($tokens, 'contact-link');
if (!$tokenValues) {
return $replacedTokens;
}
foreach ($tokenValues as $entityID => $tokenName) {
$tokenNewValue = '';
if (!empty($webformSubmissionData['civicrm']['contact'][$entityID]['id'])) {
$contactID = $webformSubmissionData['civicrm']['contact'][$entityID]['id'];
$tokenNewValue = Url::fromUri('internal:/civicrm/contact/view', [
'absolute' => TRUE,
'query' => ['reset' => 1, 'cid' => $contactID]
])->toString();
}
$replacedTokens[$tokenName] = $tokenNewValue;
}
return $replacedTokens;
}
/**
* Replaces activity-id tokens with civicrm activity IDs
*
* @param array $tokens
* Tokens to process
* @param array $webformSubmissionData
* Data submitted by the webform
*
* @return array
* List of replaced activity-id tokens replaced with actual activity IDs
*/
function _webform_civicrm_replaceActivityIdTokens($tokens, $webformSubmissionData) {
$replacedTokens = [];
$tokenValues = \Drupal::token()->findWithPrefix($tokens, 'activity-id');
if (!$tokenValues) {
return $replacedTokens;
}
foreach ($tokenValues as $entityID => $tokenName) {
$tokenNewValue = '';
if (!empty($webformSubmissionData['civicrm']['activity'][$entityID]['id'])) {
$activityId = $webformSubmissionData['civicrm']['activity'][$entityID]['id'];
$tokenNewValue = $activityId;
}
$replacedTokens[$tokenName] = $tokenNewValue;
}
return $replacedTokens;
}
/**
* Replaces activity-link tokens with civicrm activity page links
*
* @param array $tokens
* Tokens to process
* @param array $webformSubmissionData
* Data submitted by the webform
*
* @return array
* List of replaced activity-link tokens replaced with actual activity links
*/
function _webform_civicrm_replaceActivityLinkTokens($tokens, $webformSubmissionData) {
$replacedTokens = [];
$tokenValues = \Drupal::token()->findWithPrefix($tokens, 'activity-link');
if (!$tokenValues) {
return $replacedTokens;
}
foreach ($tokenValues as $entityID => $tokenName) {
$tokenNewValue = '';
if (!empty($webformSubmissionData['civicrm']['activity'][$entityID]['id'])) {
$activityId = $webformSubmissionData['civicrm']['activity'][$entityID]['id'];
$tokenNewValue = Url::fromUri('internal:/civicrm/activity', [
'absolute' => TRUE,
'query' => ['action' => 'view', 'reset' => 1, 'id' => $activityId]
])->toString();
}
$replacedTokens[$tokenName] = $tokenNewValue;
}
return $replacedTokens;
}
/**
* Replaces case-id tokens with civicrm case IDs
*
* @param array $tokens
* Tokens to process
* @param array $webformSubmissionData
* Data submitted by the webform
*
* @return array
* List of replaced case-id tokens replaced with actual case IDs
*/
function _webform_civicrm_replaceCaseIdTokens($tokens, $webformSubmissionData) {
$replacedTokens = [];
$tokenValues = \Drupal::token()->findWithPrefix($tokens, 'case-id');
if (!$tokenValues) {
return $replacedTokens;
}
foreach ($tokenValues as $entityID => $tokenName) {
$tokenNewValue = '';
if (!empty($webformSubmissionData['civicrm']['case'][$entityID]['id'])) {
$tokenNewValue = $webformSubmissionData['civicrm']['case'][$entityID]['id'];
}
$replacedTokens[$tokenName] = $tokenNewValue;
}
return $replacedTokens;
}
/**
* Replaces case-link tokens with civicrm case page links
*
* @param array $tokens
* Tokens to process
* @param array $webformSubmissionData
* Data submitted by the webform
*
* @return array
* List of replaced case-link tokens replaced with actual case links
*/
function _webform_civicrm_replaceCaseLinkTokens($tokens, $webformSubmissionData) {
$replacedTokens = [];
$tokenValues = \Drupal::token()->findWithPrefix($tokens, 'case-link');
if (!$tokenValues) {
return $replacedTokens;
}
foreach ($tokenValues as $entityID => $tokenName) {
$tokenNewValue = '';
if (!empty($webformSubmissionData['civicrm']['case'][$entityID]['id'])) {
$caseID = $webformSubmissionData['civicrm']['case'][$entityID]['id'];
$caseContactID = _webform_civicrm_getCaseContactID($caseID);
$tokenNewValue = Url::fromUri('internal:/civicrm/contact/view/case', [
'absolute' => TRUE,
'query' => [
'reset' => 1,
'id' => $caseID,
'cid' => $caseContactID,
'action' => 'view',
]
])->toString();
}
$replacedTokens[$tokenName] = $tokenNewValue;
}
return $replacedTokens;
}
/**
* Gets specified case contact ID or the default
* contact ID if the case contact ID is not found
*
* @param int $caseID
*
* @return int
*/
function _webform_civicrm_getCaseContactID($caseID) {
\Drupal::service('civicrm')->initialize();
$caseEntity = civicrm_api3('Case', 'get', [
'return' => ['contact_id'],
'id' => $caseID,
]);
$caseContactID = WEBFORM_CIVICRM_DEFAULT_CONTACT_ID;
// Check that contact_id: Is an array, Has at least one value, The first value is not falsey
if (
!empty($caseEntity['values'][$caseID]['contact_id'])
&& is_array($caseEntity['values'][$caseID]['contact_id'])
&& reset($caseEntity['values'][$caseID]['contact_id'])
) {
$caseContactID = reset($caseEntity['values'][$caseID]['contact_id']);
}
return $caseContactID;
}
/**
* Implementation of hook_civicrm_pre()
*
* Handles enabling/disabling of custom fields
*
*
* @param string $op
* @param string $objectName
* @param integer $id
* @param array $params
*/
function webform_civicrm_civicrm_pre($op, $objectName, $id, &$params) {
if ($op == 'edit' && $objectName == 'CustomField') {
// Run only if is_active is set, i.e. custom field is being enabled/disabled
if (isset($params['is_active'])) {
$statusToSet = $params['is_active'];
$queryParams = [
'sequential' => 1,
'return' => "custom_group_id, is_active",
'id' => $id,
'options' => ['limit' => 1],
];
$result = civicrm_api3('CustomField', 'get', $queryParams);
// run only if this field already exist in db to make sure we donot run it for create op
if ($result['count'] == 1) {
$admin_form = \Drupal::service('webform_civicrm.admin_form');
$previousStatus = $result['values'][0]['is_active'] ?? 0;
$customGroupId = $result['values'][0]['custom_group_id'];
if ($statusToSet == FALSE && $previousStatus == TRUE) {
$opName = 'disable';
}
else {
$opName = 'enable';
}
if (isset($opName)) {
$admin_form::handleDynamicCustomField($opName, $id, $customGroupId);
}
}
}
}
}