You are here

protected function wf_crm_webform_base::saveCustomData in Webform CiviCRM Integration 7.4

Save custom data for an entity

Parameters

$entity: Array of values

$entity_id: Numeric id of entity

$entity_type: Type of crm entity, e.g. "Contact"

$known: Is this a known record (as opposed to a contact matched via dedupe rules)? We only allow saving blank fields for known contacts.

$contact_index: Contact's index

6 calls to wf_crm_webform_base::saveCustomData()
wf_crm_webform_postprocess::preSave in includes/wf_crm_webform_postprocess.inc
Process webform submission when it is about to be saved. Called by the following hook:
wf_crm_webform_postprocess::processActivities in includes/wf_crm_webform_postprocess.inc
Save activity data
wf_crm_webform_postprocess::processCases in includes/wf_crm_webform_postprocess.inc
Save case data
wf_crm_webform_postprocess::processContribution in includes/wf_crm_webform_postprocess.inc
Post-processing of contribution This happens during form post-processing
wf_crm_webform_postprocess::processGrants in includes/wf_crm_webform_postprocess.inc
Save grants

... See full list

File

includes/wf_crm_webform_base.inc, line 725

Class

wf_crm_webform_base
Class wf_crm_webform_base

Code

protected function saveCustomData($entity, $entity_id, $entity_type, $known = TRUE, $contact_index = 1) {
  $existing = FALSE;
  $params = array(
    'entityID' => $entity_id,
  );
  $createMultiValues = array();
  foreach ($entity as $table => $values) {
    if (substr($table, 0, 2) == 'cg' && is_array($values)) {
      if ($existing === FALSE) {
        $existing = $this
          ->getCustomData($entity_id, $entity_type, FALSE);
      }
      $existing += array(
        $table => array(),
      );
      $insert = 0;
      foreach ($values as $valuesIndex => $custom) {

        // Match to id of existing record (id will be 0 for non-multi-value custom sets, which is fine)
        if ($id = each($existing[$table])) {
          $suf = $id['key'];
        }
        else {
          $suf = --$insert;
        }
        $multivaluesCreateMode = NULL;

        // Handle 'Create mode' for multivalues custom fields of Contact entity.
        if ($entity_type === 'Contact') {
          $createModeKey = 'civicrm_' . $contact_index . '_contact_' . $valuesIndex . '_' . $table . '_createmode';
          $multivaluesCreateMode = isset($this->data['config']['create_mode'][$createModeKey]) ? (int) $this->data['config']['create_mode'][$createModeKey] : NULL;
        }
        $multiValueSuffix = '_' . $suf;
        if ($multivaluesCreateMode === self::MULTIVALUE_FIELDSET_MODE_CREATE_ONLY) {

          // Using 'Create only' mode we don't need custom value's suffix
          // so the value can be added as new instead of updated.
          $multiValueSuffix = '';
        }
        $customGroupParams = array();
        foreach ($custom as $k => $v) {

          // Only save if this is not blank or data already exists and record is known
          if ($v !== '' || $suf >= 0 && $known) {
            $customGroupParams[$k . $multiValueSuffix] = $v;
          }
        }
        if ($multivaluesCreateMode === self::MULTIVALUE_FIELDSET_MODE_CREATE_ONLY) {
          $createMultiValues[] = $customGroupParams;
        }
        else {
          $params = array_merge($params, $customGroupParams);
        }
      }
    }
  }
  if (count($params) > 1) {

    // Update existing values.
    $this
      ->saveCustomValues($entity_id, $params);
  }
  if (!empty($createMultiValues)) {

    // Add new values.
    foreach ($createMultiValues as $multiValuesSet) {
      $this
        ->saveCustomValues($entity_id, array_merge($multiValuesSet, array(
        'entityID' => $entity_id,
      )), TRUE);
    }
  }
}