You are here

function wf_crm_add_remove in Webform CiviCRM Integration 7.3

Handle adding/removing multivalued data for a contact/activity/etc. Currently used only for groups and tags, but written with expansion in mind.

Parameters

$data_type: 'group' or 'tag'

$entity_type: Parent entity: 'contact' etc.

$node: Node object

$id: Entity id

$add: Groups/tags to add

$remove: Groups/tags to remove

1 call to wf_crm_add_remove()
wf_crm_process_submission in ./webform_civicrm_forms.inc
Webform submission handler Create/update CiviCRM contacts and related data Called by presave, insert and update webform hooks

File

./webform_civicrm_forms.inc, line 1133

Code

function wf_crm_add_remove($data_type, $entity_type, $node, $id, $add, $remove = array()) {
  $confirmations_sent = $existing = $params = array();
  $add = drupal_map_assoc($add);
  static $mailing_lists = array();
  switch ($data_type) {
    case 'group':
      $api = 'group_contact';
      break;
    case 'tag':
      $api = 'entity_tag';
      break;
    default:
      $api = $data_type;
  }
  if (!empty($add) || !empty($remove)) {

    // Retrieve current records for this entity
    if ($entity_type == 'contact') {
      $params['contact_id'] = $id;
    }
    else {
      $params['entity_id'] = $id;
      $params['entity_type'] = 'civicrm_' . $entity_type;
    }
    $fetch = wf_civicrm_api($api, 'get', $params);
    foreach (wf_crm_aval($fetch, 'values', array()) as $i) {
      $existing[] = $i[$data_type . '_id'];
      unset($add[$i[$data_type . '_id']]);
    }
    foreach ($remove as $i => $name) {
      if (!in_array($i, $existing)) {
        unset($remove[$i]);
      }
    }
  }
  if (!empty($add)) {

    // Prepare for sending subscription confirmations
    if ($data_type == 'group' && !empty($node->webform_civicrm['confirm_subscription'])) {

      // Retrieve this contact's primary email address and perform error-checking
      $result = wf_civicrm_api('email', 'get', array(
        'contact_id' => $id,
        'options' => array(
          'sort' => 'is_primary DESC',
        ),
      ));
      if (!empty($result['values'])) {
        foreach ($result['values'] as $value) {
          if (($value['is_primary'] || empty($email)) && strpos($value['email'], '@')) {
            $email = $value['email'];
          }
        }
        $mailer_params = array(
          'contact_id' => $id,
          'email' => $email,
        );
        if (empty($mailing_lists)) {
          $mailing_lists = wf_crm_get_options('mailing_lists');
        }
      }
    }
    foreach ($add as $a) {
      $params[$data_type . '_id'] = $mailer_params['group_id'] = $a;
      if ($data_type == 'group' && isset($mailing_lists[$a]) && !empty($email)) {
        $result = wf_civicrm_api('mailing_group', 'event_subscribe', $mailer_params);
        if (empty($result['is_error'])) {
          $confirmations_sent[] = check_plain($mailing_lists[$a]);
        }
        else {
          wf_civicrm_api($api, 'create', $params);
        }
      }
      else {
        wf_civicrm_api($api, 'create', $params);
      }
    }
    if ($confirmations_sent) {
      drupal_set_message(t('A message has been sent to %email to confirm subscription to !group.', array(
        '%email' => $email,
        '!group' => '<em>' . implode('</em> ' . t('and') . ' <em>', $confirmations_sent) . '</em>',
      )));
    }
  }

  // Remove data from entity
  foreach ($remove as $a => $name) {
    $params[$data_type . '_id'] = $a;
    wf_civicrm_api($api, 'delete', $params);
  }
  if (!empty($remove) && $data_type == 'group') {
    $display_name = wf_civicrm_api('contact', 'get', array(
      'contact_id' => $id,
      'return.display_name' => 1,
    ));
    $display_name = wf_crm_aval($display_name, "values:{$id}:display_name", t('Contact'));
    drupal_set_message(t('%contact has been removed from !group.', array(
      '%contact' => $display_name,
      '!group' => '<em>' . implode('</em> ' . t('and') . ' <em>', $remove) . '</em>',
    )));
  }
}