View source
<?php
use Drupal\webform\Entity\Webform;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\node\Entity\Node;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
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);
function webform_civicrm_library_info_alter(array &$libraries, $extension) {
if ($extension === 'webform_civicrm') {
\Drupal::service('civicrm')
->initialize();
$user_framework_resource_url = CRM_Core_Config::singleton()->resourceBase;
unset($libraries['civicrm_contact']['js']['/libraries/civicrm/packages/jquery/plugins/jquery.tokeninput.js']);
if (substr($user_framework_resource_url, -5) === 'core/') {
$libraries['civicrm_contact']['js'][substr($user_framework_resource_url, 0, -5) . 'packages/jquery/plugins/jquery.tokeninput.js'] = [
'preprocess' => FALSE,
'minified' => FALSE,
];
}
}
}
function webform_civicrm_element_info_alter(array &$types) {
$types['datetime']['#process'][] = 'webform_civicrm_datetime_set_format';
}
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;
}
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();
}
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;
}
}
}
}
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_') {
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);
}
}
}
function webform_civicrm_theme() {
return [
'webform_civicrm_contact' => [
'base hook' => 'input',
],
];
}
function webform_civicrm_webform_submission_load($entities) {
foreach ($entities as $entity) {
$data = _fillCiviCRMData($entity
->getData(), $entity);
$entity
->setData($data);
}
}
function _fillCiviCRMData($data, $webformSubmission) {
if (!empty($data['civicrm'])) {
return $data;
}
$utils = \Drupal::service('webform_civicrm.utils');
$webform = $webformSubmission
->getWebform();
foreach ($data as $key => $val) {
$element = $webform
->getElement($key);
if (!empty($val) && $element['#type'] == 'civicrm_options') {
if (!empty($element['#webform_multiple'])) {
foreach ($val as $k => $v) {
if (isset($element['#options'][$v])) {
$data[$key][$k] = $element['#options'][$v];
}
}
}
elseif (isset($element['#options'][$val])) {
$data[$key] = $element['#options'][$val];
}
}
}
$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) {
$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;
}
function webform_civicrm_webform_submission_render_alter(&$sub) {
if (!empty($sub['#submission']->civicrm['contact'][1]['display_name']) && empty($sub['#email']) && $sub['#format'] == 'html') {
drupal_set_title(t('Submission #@num by @name', [
'@num' => $sub['#submission']->sid,
'@name' => $sub['#submission']->civicrm['contact'][1]['display_name'],
]));
}
}
function webform_civicrm_webform_submission_actions($node, $submission) {
$actions = [];
if (!empty($node->webform_civicrm) && !empty($submission->civicrm) && webform_results_access($node) && user_access('access CiviCRM')) {
$data = $submission->civicrm;
if (!empty($data['contact'][1]['display_name'])) {
$actions['civicrm_action contact_view'] = [
'title' => t('View @name', [
'@name' => $data['contact'][1]['display_name'],
]),
'href' => 'civicrm/contact/view',
'query' => [
'reset' => 1,
'cid' => $data['contact'][1]['id'],
],
];
if (!empty($data['activity'][1]['id'])) {
$actions['civicrm_action activity_view'] = [
'title' => t('View Activity'),
'href' => 'civicrm/activity',
'query' => [
'action' => 'view',
'reset' => 1,
'cid' => $data['contact'][1]['id'],
'id' => $data['activity'][1]['id'],
],
];
}
if (!empty($data['contribution'][1]['id'])) {
$actions['civicrm_action contribution_view'] = [
'title' => t('View Contribution'),
'href' => 'civicrm/contact/view/contribution',
'query' => [
'action' => 'view',
'reset' => 1,
'cid' => $data['contact'][1]['id'],
'id' => $data['contribution'][1]['id'],
],
];
}
if (!empty($data['participant'][1]['id'])) {
$actions['civicrm_action participant_view'] = [
'title' => t('View Participant'),
'href' => 'civicrm/contact/view/participant',
'query' => [
'action' => 'view',
'reset' => 1,
'cid' => $data['contact'][1]['id'],
'id' => $data['participant'][1]['id'],
],
];
}
}
}
return $actions;
}
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);
}
}
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);
}
}
function webform_civicrm_civicrm_postSave_civicrm_custom_group($dao) {
}
function webform_civicrm_civicrm_buildForm($formName, $form) {
if ($formName == 'CRM_Custom_Form_DeleteField') {
$nodes = [];
$fid = $form
->getVar('_id');
if ($fid) {
$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,
]);
}
}
}
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();
}
}
function webform_civicrm_admin_paths() {
return [
'node/*/civicrm' => TRUE,
];
}
function webform_civicrm_help($section) {
if ($section == 'admin/help#webform_civicrm') {
return nl2br(file_get_contents(drupal_get_path('module', 'webform_civicrm') . '/README.md'));
}
}
function webform_civicrm_webform_component_presave(&$component) {
if ($c = wf_crm_contact_clone_storage()) {
$component['form_key'] = str_replace($c['old'], $c['new'], $component['form_key']);
if ($component['type'] == 'civicrm_contact') {
if (wf_crm_aval($component, 'extra:default') == 'user') {
unset($component['extra']['default']);
}
}
}
}
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'] = [];
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 = '';
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 = [];
foreach ($temp as $k => $v) {
$row[] = $v;
if ($k == 1) {
$row[] = $name;
}
}
}
}
}
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']);
}
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];
}
return $haystack;
}
function wf_crm_contact_clone_storage($input = NULL) {
static $storage = NULL;
if ($input) {
$storage = $input;
}
return $storage;
}
function wf_crm_contact_clone($form, $form_state) {
$fid = $form['form_key']['#default_value'];
$utils = \Drupal::service('webform_civicrm.utils');
list(, $old) = $utils
->wf_crm_explode_key($fid);
$node = Node::load($form['nid']['#value']);
$settings = $node->webform_civicrm;
$new = count($settings['data']['contact']) + 1;
$settings['data']['contact'][$new] = $settings['data']['contact'][$old];
$settings['data']['contact'][$new]['contact'][1]['webform_label'] = $form_state['input']['name'];
$storage = [
'old' => [
"civicrm_{$old}_contact_",
],
'new' => [
"civicrm_{$new}_contact_",
],
];
if (wf_crm_aval($settings['data'], 'participant_reg_type') == 'separate') {
$settings['data']['participant'][$new] = $settings['data']['participant'][$old];
$storage['old'][] = "civicrm_{$old}_participant_";
$storage['new'][] = "civicrm_{$new}_participant_";
}
drupal_write_record('webform_civicrm_forms', $settings, 'nid');
wf_crm_contact_clone_storage($storage);
}
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 (WEBFORM_CIVICRM_CIVICRM_VERSION_MAX && version_compare($civicrm['version'], WEBFORM_CIVICRM_CIVICRM_VERSION_MAX, '>=')) {
$status['webform_civicrm'] = FALSE;
}
return $status;
}
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');
}
}
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,
],
];
}
function webform_civicrm_tokens($type, $tokens = '', array $data = [], array $options = []) {
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;
}
function _webform_civicrm_isWebformSubmission($tokenType, $webformData) {
return $tokenType === 'webform_submission' && !empty($webformData['webform_submission']);
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
function _webform_civicrm_getCaseContactID($caseID) {
civicrm_initialize();
$caseEntity = civicrm_api3('Case', 'get', [
'return' => [
'contact_id',
],
'id' => $caseID,
]);
$caseContactID = WEBFORM_CIVICRM_DEFAULT_CONTACT_ID;
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;
}
function webform_civicrm_civicrm_pre($op, $objectName, $id, &$params) {
if ($op == 'edit' && $objectName == 'CustomField') {
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);
if ($result['count'] == 1) {
$admin_form = \Drupal::service('webform_civicrm.admin_form');
$previousStatus = $result['values'][0]['is_active'];
$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);
}
}
}
}
}