You are here

function webform_civicrm_process_submission in Webform CiviCRM Integration 7.2

Same name and namespace in other branches
  1. 6.2 webform_civicrm_forms.inc \webform_civicrm_process_submission()
  2. 6 webform_civicrm_forms.inc \webform_civicrm_process_submission()
  3. 7 webform_civicrm_forms.inc \webform_civicrm_process_submission()

Webform submission handler Create/update CiviCRM contacts and related data Called by presave, insert and update webform hooks

Parameters

$op: Which hook is being called:

3 calls to webform_civicrm_process_submission()
webform_civicrm_webform_submission_insert in ./webform_civicrm.module
Implements hook_webform_submission_insert(). Final submission processing - saves activity and writes webform_civicrm record.
webform_civicrm_webform_submission_presave in ./webform_civicrm.module
Implements hook_webform_submission_presave(). Initial submission processing - saves contacts.
webform_civicrm_webform_submission_update in ./webform_civicrm.module
Implements hook_webform_submission_update(). Final submission processing - saves activity and updates webform_civicrm record.

File

./webform_civicrm_forms.inc, line 451

Code

function webform_civicrm_process_submission($node, &$submission, $op) {
  static $data = array();
  static $id = array();
  static $update = array();
  $settings = $node->webform_civicrm;
  require_once 'CRM/Core/BAO/Setting.php';

  // Presave processing - save contact data
  if ($op == 'presave') {
    civicrm_initialize();
    $config = CRM_Core_Config::singleton();
    require_once 'CRM/Core/BAO/CustomValueTable.php';
    $enabled = webform_civicrm_enabled_fields($node);
    $fields = webform_civicrm_get_fields();
    $data = $settings['data'];
    $sets = webform_civicrm_get_fields('sets');
    $sp = CRM_Core_DAO::VALUE_SEPARATOR;
    $shared_address = array();

    // Retrieve stored ids
    $id = $updating = webform_civicrm_aval(webform_civicrm_storage($node->nid), 'id');

    // If this is an update op, set param for drupal_write_record()
    if (!empty($submission->sid)) {
      $submitted = array(
        $submission->sid => new stdClass(),
      );
      webform_civicrm_webform_submission_load($submitted);
      if (isset($submitted[$submission->sid]->civicrm)) {
        $update = 'sid';
      }
    }

    // While saving a draft, just skip to the end and write the record
    if (!empty($submission->is_draft)) {
      return;
    }

    // Fill entity arrays with field values
    $contact_ref = array();
    foreach ($enabled as $field_key => $fid) {
      if (isset($submission->data[$fid]['value'])) {
        list($lobo, $c, $ent, $n, $table, $name) = explode('_', $field_key, 6);
        $field = $fields[$table . '_' . $name];
        $val = $submission->data[$fid]['value'];
        if ($name === 'privacy') {
          foreach (array_keys(webform_civicrm_str2array($node->webform['components'][$fid]['extra']['items'])) as $key) {
            $data[$ent][$c][$table][$n][$key] = in_array($key, $val);
          }
          continue;
        }
        if (!empty($field['extra']['multiple'])) {
          if (!empty($data[$ent][$c][$table][$n][$name]) && is_array($data[$ent][$c][$table][$n][$name])) {
            $val = array_unique(array_merge($val, $data[$ent][$c][$table][$n][$name]));
          }
          if ($table !== 'other' && $name !== 'event_id') {
            $val = $sp . implode($sp, $val) . $sp;
          }
        }
        elseif ($name === 'image_URL') {
          if (empty($val[0]) || !($val = webform_civicrm_filepath($val[0]))) {
            continue;
          }
        }
        elseif ($field['type'] === 'date') {
          $val = empty($val[0]) ? '' : str_replace('-', '', $val[0]);
        }
        elseif ($name !== 'event_id') {
          $val = isset($val[0]) ? $val[0] : '';
        }

        // ContactRef from webform contacts must wait until contact IDs have been processed
        if (webform_civicrm_aval($field, 'data_type') === 'ContactReference' && $val && $val[0] === '#') {
          $contact_ref[$field_key] = substr($val, 1);
        }
        elseif ($val !== '' && $val !== NULL || !empty($updating['cid'][$c])) {
          $data[$ent][$c][$table][$n][$name] = $val;
        }
      }
    }

    // Hack for activity assignee contactRef set in back-end
    if ($val = webform_civicrm_aval($data, 'activity:1:activity:1:assignee_contact_id') && $val[0] === '#') {
      $contact_ref['civicrm_1_activity_1_activity_assignee_contact_id'] = substr($val, 1);
    }

    // Create/update contacts
    foreach ($data['contact'] as $c => $contact) {
      if (empty($id['cid'][$c])) {

        // Don't create contact if we don't have a name or email
        if (empty($contact['contact'][1]['first_name']) && empty($contact['contact'][1]['last_name']) && empty($contact['contact'][1]['organization_name']) && empty($contact['contact'][1]['legal_name']) && empty($contact['contact'][1]['household_name']) && empty($contact['email'][1]['email'])) {
          $id['cid'][$c] = 0;
          continue;
        }

        // Search for an existing contact using default strict rule
        require_once 'CRM/Dedupe/Finder.php';
        $params = array(
          'check_permission' => FALSE,
        );
        foreach ($contact as $table => $field) {
          if (is_array($field) && !empty($field[1])) {
            if (substr($table, 0, 2) == 'cg') {

              //TODO pass custom data to deduper
            }
            elseif ($table == 'address' && !empty($field[1]['master_id'])) {
              $m = $field[1]['master_id'];

              // If master address is exposed to the form, use it
              if (!empty($contact[$m]['address'][1])) {
                $params['civicrm_address'] = $contact[$m]['address'][1];
              }
              elseif (!empty($updating['cid'][$m])) {
                $masters = webform_civicrm_api('address', 'get', array(
                  'contact_id' => $id['cid'][$m],
                  'options' => array(
                    'sort' => 'is_primary DESC',
                  ),
                ));
                if (!empty($masters['values'])) {
                  $params['civicrm_address'] = array_shift($masters['values']);
                }
              }
            }
            elseif (in_array($table, array(
              'contact',
              'address',
              'email',
              'phone',
              'website',
            ))) {
              $params['civicrm_' . $table] = $field[1];
            }
          }
        }
        if ($dupes = CRM_Dedupe_Finder::dupesByParams($params, ucfirst($contact['contact'][1]['contact_type']))) {
          $id['cid'][$c] = $dupes[0];
        }
      }
      $params = $contact['contact'][1];
      $params['contact_type'] = ucfirst($params['contact_type']);
      if (empty($params['contact_sub_type'])) {
        unset($params['contact_sub_type']);
      }

      // Create new contact
      if (empty($id['cid'][$c])) {
        unset($params['contact_id']);
        $params['source'] = $settings['new_contact_source'];
        $result = webform_civicrm_api('contact', 'create', $params);
        if (!empty($result['id'])) {
          $id['cid'][$c] = $result['id'];
        }
        else {
          $id['cid'][$c] = 0;
        }
      }
      else {
        $params['contact_id'] = $id['cid'][$c];
        $result = webform_civicrm_api('contact', 'create', $params);
      }
    }

    // Fill ContactRef fields with contact IDs
    foreach ($contact_ref as $key => $val) {
      if (!empty($id['cid'][$val])) {
        list($lobo, $c, $ent, $n, $table, $name) = explode('_', $key, 6);
        $data[$ent][$c][$table][$n][$name] = $id['cid'][$val];
      }
    }

    // Create/update other data associated with contacts
    foreach ($data['contact'] as $c => $contact) {
      if (!($cid = $id['cid'][$c])) {
        continue;
      }
      webform_civicrm_save_custom($contact, $cid);

      // Fill values for hidden ID & CS fields
      $fid = 'civicrm_' . $c . '_contact_1_contact_';
      if (!empty($enabled[$fid . 'contact_id'])) {
        $submission->data[$enabled[$fid . 'contact_id']]['value'] = array(
          $cid,
        );
      }
      if (!empty($enabled[$fid . 'external_identifier']) && !empty($updating['cid'][$c])) {
        $exid = webform_civicrm_api('contact', 'get', array(
          'contact_id' => $cid,
          'return.external_identifier' => 1,
        ));
        $submission->data[$enabled[$fid . 'external_identifier']]['value'] = array(
          webform_civicrm_aval($exid, "values:{$cid}:external_identifier"),
        );
      }
      if (!empty($enabled[$fid . 'cs'])) {
        if (empty($params['cs'])) {
          $life = 'inf';
        }
        else {
          $life = intval(24 * $params['cs']);
        }
        require_once 'CRM/Contact/BAO/Contact/Utils.php';
        $cs = CRM_Contact_BAO_Contact_Utils::generateChecksum($cid, NULL, $life);
        $submission->data[$enabled[$fid . 'cs']]['value'] = array(
          $cs,
        );
      }

      // Save location data
      foreach (array(
        'address',
        'email',
        'phone',
        'website',
      ) as $location) {
        if (!empty($contact[$location])) {
          $existing = array();
          $result = webform_civicrm_api($location, 'get', array(
            'contact_id' => $cid,
            'options' => array(
              'sort' => 'is_primary DESC',
            ),
          ));
          if (!empty($result['values'])) {

            // start array index at 1
            $existing = array_merge(array(
              array(),
            ), $result['values']);
          }
          foreach ($contact[$location] as $i => $params) {

            // Translate state/prov abbr to id
            if (!empty($params['state_province_id'])) {
              if (!($params['state_province_id'] = webform_civicrm_state_abbr($params['state_province_id'], 'id', webform_civicrm_aval($params, 'country_id', $config->defaultContactCountry)))) {
                $params['state_province_id'] = '';
              }
            }

            // Check if anything was changed, else skip the update
            if (!empty($existing[$i])) {
              $same = TRUE;
              foreach ($params as $param => $val) {
                if ($val != (string) webform_civicrm_aval($existing[$i], $param, '')) {
                  $same = FALSE;
                }
              }
              if ($same) {
                continue;
              }
            }
            $empty = FALSE;
            if ($location == 'address') {

              // Check if nothing was entered for address
              $empty = empty($params['street_address']) && empty($params['city']) && empty($params['state_province_id']) && empty($params['country_id']) && empty($params['postal_code']) && empty($params['master_id']);

              // Store shared addresses for later since we haven't necessarily processed
              // the contact this address is shared with yet.
              if (!empty($params['master_id'])) {
                $shared_address[$cid][$i] = array(
                  'id' => !empty($existing[$i]) ? $existing[$i]['id'] : NULL,
                  'mc' => $params['master_id'],
                  'loc' => $params['location_type_id'],
                );
                continue;
              }

              // Reset calculated values when updating an address
              $params['master_id'] = $params['geo_code_1'] = $params['geo_code_2'] = 'null';
            }
            elseif ($location == 'website') {
              $empty = empty($params['url']);
            }
            elseif (empty($params[$location])) {
              $empty = TRUE;
            }
            elseif ($location == 'email' && $i == 1 && ($uid = webform_civicrm_user_cid($cid, 'contact'))) {

              // Update drupal email address
              $user = user_load($uid);
              if ($params['email'] != $user->mail) {
                user_save($user, array(
                  'mail' => $params['email'],
                ));
              }
            }
            $params['contact_id'] = $cid;
            if (!empty($existing[$i])) {
              $params['id'] = $existing[$i]['id'];
            }
            if ($empty) {

              // Delete this location if nothing was entered and this is a known contact
              if (!empty($updating['cid'][$c]) && !empty($params['id'])) {
                webform_civicrm_api($location, 'delete', $params);
              }
              continue;
            }
            if ($location != 'website' && empty($params['location_type_id'])) {
              $params['location_type_id'] = webform_civicrm_aval($existing, $i . ':location_type_id', 1);
            }
            $params['is_primary'] = $i == 1 ? 1 : 0;
            $result = webform_civicrm_api($location, 'create', $params);
            if ($location == 'address' && empty($result['is_error'])) {

              // Process custom data for address
              $custom = array();
              foreach ($params as $param => $val) {
                if (strpos($param, 'custom_') !== FALSE) {
                  $custom[$param] = $val;
                }
              }
              if ($custom) {
                $custom['entityID'] = $result['id'];
                CRM_Core_BAO_CustomValueTable::setValues($custom);
              }
            }
          }
        }
      }

      // Process Relationships
      if (!empty($contact['relationship'])) {
        foreach ($contact['relationship'] as $i => $params) {
          if (!empty($params['relationship_type_id']) && !empty($id['cid'][$i])) {
            list($type, $a_b) = explode('_', $params['relationship_type_id']);
            $b_a = $a_b == 'a' ? 'b' : 'a';
            $params['contact_id_' . $a_b] = $cid;
            $params['contact_id_' . $b_a] = $id['cid'][$i];
            if ($type == 'ce') {
              foreach (webform_civicrm_get_relationship_types() as $t) {
                if ($t['name_a_b'] == 'Employee of' && $t['name_b_a'] == 'Employer of' && $t['id'] != 'ce') {
                  $type = $t['id'];
                  break;
                }
              }
              webform_civicrm_api('contact', 'create', array(
                'contact_id' => $params['contact_id_a'],
                'employer_id' => $params['contact_id_b'],
                'contact_type' => 'Individual',
              ));
            }
            $params['relationship_type_id'] = $type;
            if ($perm = webform_civicrm_aval($params, 'relationship_permission')) {
              $params['is_permission_a_b'] = $params['is_permission_b_a'] = $perm == 3 ? 1 : 0;
              if ($perm == 1 || $perm == 2) {
                $params['is_permission_' . ($perm == 1 ? $a_b . '_' . $b_a : $b_a . '_' . $a_b)] = 1;
              }
            }
            unset($params['relationship_permission']);
            webform_civicrm_api('relationship', 'create', $params);
          }
        }
      }

      // Process groups & tags
      $field = 'civicrm_' . $c . '_contact_1_other_';
      foreach (array(
        'group',
        'tag',
      ) as $type) {
        if (!empty($contact['other'][1][$type]) || isset($enabled[$field . $type])) {
          $fid = webform_civicrm_aval($enabled, $field . $type, 0);
          $add = webform_civicrm_aval($contact, "other:1:{$type}", array());
          $remove = empty($updating['cid'][$c]) ? array() : webform_civicrm_remove_fields($node, $fid, $add);
          webform_civicrm_add_remove($type, 'contact', $node, $cid, $add, $remove);
        }
      }

      // Process event participation
      if (in_array('CiviEvent', $config->enableComponents, TRUE) && !empty($data['participant_reg_type'])) {
        $n = $data['participant_reg_type'] == 'separate' ? $c : 1;
        if ($p = webform_civicrm_aval($data, "participant:{$n}:participant")) {

          // Fetch existing participant records
          $existing = array();
          $dao =& CRM_CORE_DAO::executeQuery("SELECT id, event_id FROM civicrm_participant WHERE contact_id = {$cid} AND is_test = 0");
          while ($dao
            ->fetch()) {
            $existing[$dao->event_id] = $dao->id;
          }
          foreach ($p as $e => $params) {
            $remove = array();
            if ($fid = webform_civicrm_aval($enabled, 'civicrm_' . $c . '_participant_' . $e . '_participant_event_id')) {
              foreach (webform_civicrm_str2array($node->webform['components'][$fid]['extra']['items']) as $eid => $title) {
                list($eid) = explode('-', $eid);
                if (isset($existing[$eid])) {
                  $remove[$eid] = $title;
                }
              }
            }
            if (!empty($params['event_id'])) {

              // Loop through event ids to support multi-valued form elements
              $events = (array) $params['event_id'];
              foreach ($events as $i => $eid) {
                if (!empty($eid)) {
                  $params['contact_id'] = $cid;
                  list($eid) = explode('-', $eid);
                  $params['event_id'] = $eid;
                  unset($remove[$eid]);
                  if (empty($params['campaign_id']) || !in_array('CiviCampaign', $config->enableComponents, TRUE)) {
                    unset($params['campaign_id']);
                  }

                  // Is existing participant?
                  if (!empty($existing[$eid])) {
                    $params['id'] = $existing[$params['event_id']];
                  }
                  else {
                    $params['source'] = check_plain($node->title);
                    if ($c > 1 && !empty($registered_by_id[$e][$i])) {
                      $params['registered_by_id'] = $registered_by_id[$e][$i];
                    }
                  }
                  $result = webform_civicrm_api('participant', 'create', $params);
                  if ($c == 1 && $data['participant_reg_type'] == 'all') {
                    $registered_by_id[$e][$i] = $result['id'];
                  }

                  // Reformat custom data from nested arrays
                  $custom = array();
                  foreach ($data['participant'][$n] as $key => $vals) {
                    if (substr($key, 0, 2) == 'cg' && isset($vals[$e])) {
                      $custom[$key][1] = $vals[$e];
                    }
                  }
                  if ($custom) {
                    webform_civicrm_save_custom($custom, $result['id'], 'Participant');
                  }
                }
              }
            }
            foreach ($remove as $eid => $title) {
              webform_civicrm_api('participant', 'create', array(
                'status_id' => 4,
                'id' => $existing[$eid],
              ));
              drupal_set_message(t('Registration canceled for !event', array(
                '!event' => $title,
              )));
            }
          }
        }
      }
    }

    // Process shared addresses. We do this last after all contacts and addresses exist.
    foreach ($shared_address as $cid => $shared) {
      foreach ($shared as $i => $addr) {
        if (!empty($id['cid'][$addr['mc']])) {
          $masters = webform_civicrm_api('address', 'get', array(
            'contact_id' => $id['cid'][$addr['mc']],
            'options' => array(
              'sort' => 'is_primary DESC',
            ),
          ));
          if (!empty($masters['values'])) {
            $masters = array_values($masters['values']);

            // Pick the address with the same location type; default to primary.
            $params = $masters[0];
            foreach ($masters as $m) {
              if ($m['location_type_id'] == $addr['loc']) {
                $params = $m;
                break;
              }
            }
            $params['master_id'] = $params['id'];
            $params['id'] = $addr['id'];
            $params['contact_id'] = $cid;
            $params['is_primary'] = $i == 1;
            webform_civicrm_api('address', 'create', $params);
          }
        }
      }
    }
  }
  elseif (empty($submission->is_draft)) {
    $config = CRM_Core_Config::singleton();

    // Open new case if needed.
    if (empty($id['case'][1]) && empty($id['act'][1]) && !empty($data['case'][1]) && !empty($id['cid'][1])) {
      $data['case'][1]['case'][1]['contact_id'] = $id['cid'][1];
      $result = webform_civicrm_api('case', 'create', $data['case'][1]['case'][1]);
      if (!empty($result['values']['id'])) {
        webform_civicrm_save_custom($data['case'][1], $result['values']['id'], 'Case');
        $id['case'][1] = $result['values']['id'];
        $id['act'][1] = webform_civicrm_activity_find(array(
          'contact_id' => $id['cid'][1],
          'activity_type_id' => $data['activity'][1]['activity'][1]['activity_type_id'],
          'status_id' => $data['activity'][1]['existing_activity_status'],
          'case_id' => $id['case'][1],
        ));
      }
    }
    if (!empty($data['activity'][1])) {
      $params = $data['activity'][1]['activity'][1];
      $params['activity_date_time'] = date('YmdHis');
      if ($cid = webform_civicrm_user_cid()) {
        $params['source_contact_id'] = $cid;
      }
      else {
        foreach ($id['cid'] as $cid) {
          if ($cid) {
            $params['source_contact_id'] = $cid;
            break;
          }
        }
      }
      $params['target_contact_id'] = array();
      foreach ($data['contact'] as $c => $contact) {
        if (!empty($contact['activity_target']) && !empty($id['cid'][$c])) {
          $params['target_contact_id'][$id['cid'][$c]] = $id['cid'][$c];
        }
      }
      if (empty($params['assignee_contact_id'])) {
        unset($params['assignee_contact_id']);
      }

      // Format details as html
      $params['details'] = nl2br(webform_civicrm_aval($params, 'details', ''));
      if (empty($params['subject'])) {
        $params['subject'] = $settings['data']['activity'][1]['activity'][1]['subject'];
      }
      if (!empty($data['activity'][1]['details']['entire_result'])) {
        module_load_include('inc', 'webform', 'includes/webform.submissions');
        $params['details'] .= webform_submission_render($node, $submission, NULL, 'html');
      }
      if (!empty($data['activity'][1]['details']['view_link'])) {
        $params['details'] .= '<p>' . l(t('View Webform Submission'), 'node/' . $node->nid . '/submission/' . $submission->sid, array(
          'absolute' => TRUE,
          'alias' => TRUE,
        )) . '</p>';
      }
      if (!empty($data['activity'][1]['details']['edit_link'])) {
        $params['details'] .= '<p>' . l(t('Edit Submission'), 'node/' . $node->nid . '/submission/' . $submission->sid . '/edit', array(
          'absolute' => TRUE,
          'alias' => TRUE,
        )) . '</p>';
      }
      if (!empty($id['act'][1])) {
        $params['id'] = $id['act'][1];
      }
      elseif (!empty($id['case'][1])) {
        $params['case_id'] = $id['case'][1];
        $params['medium_id'] = $data['case'][1]['case'][1]['medium_id'];
      }
      if (empty($params['campaign_id']) || !in_array('CiviCampaign', $config->enableComponents, TRUE)) {
        unset($params['campaign_id']);
      }
      $result = webform_civicrm_api('activity', 'create', $params);
      if (!empty($result['id'])) {
        $id['act'][1] = $result['id'];
        webform_civicrm_save_custom($data['activity'][1], $id['act'][1], 'Activity');
      }
      if (!empty($id['act'][1]) && !empty($params['assignee_contact_id']) && CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'activity_assignee_notification')) {

        // Send email to assignees. TODO: Move to CiviCRM API?
        $assignee = webform_civicrm_api('contact', 'get', array(
          'id' => $params['assignee_contact_id'],
        ));
        $assignee = webform_civicrm_aval($assignee, 'values:' . $params['assignee_contact_id']);
        if (!empty($assignee['email'])) {
          $mail = array(
            $assignee['email'] => $assignee,
          );

          //include attachments while sendig a copy of activity.
          require_once 'CRM/Core/BAO/File.php';
          $attachments =& CRM_Core_BAO_File::getEntityFile('civicrm_activity', $id['act'][1]);
          require_once 'CRM/Case/BAO/Case.php';
          $result = CRM_Case_BAO_Case::sendActivityCopy(NULL, $id['act'][1], $mail, $attachments, NULL);
        }
      }
    }
  }

  // Write record; we do this when creating, updating, or saving a draft of a webform submission.
  if ($op != 'presave') {
    $cid = '-';
    foreach (array_keys($data['contact']) as $c) {
      $cid .= (empty($id['cid'][$c]) ? 0 : $id['cid'][$c]) . '-';
    }
    $record = array(
      'sid' => $submission->sid,
      'contact_id' => $cid,
      'activity_id' => empty($id['act'][1]) ? 0 : $id['act'][1],
    );
    drupal_write_record('webform_civicrm_submissions', $record, $update);
  }
}