You are here

private function WebformCivicrmPostProcess::fillContactRefs in Webform CiviCRM Integration 8.5

Recursive function to fill ContactRef fields with contact IDs

@internal param $values null|array Leave blank - used internally to recurse through data @internal param $depth int Leave blank - used internally to track recursion level

Parameters

$customOnly: TRUE if only custom contact reference needs to be filled.

1 call to WebformCivicrmPostProcess::fillContactRefs()
WebformCivicrmPostProcess::preSave in src/WebformCivicrmPostProcess.php
Process webform submission when it is about to be saved. Called by the following hook:

File

src/WebformCivicrmPostProcess.php, line 2391
Front-end form validation and post-processing.

Class

WebformCivicrmPostProcess

Namespace

Drupal\webform_civicrm

Code

private function fillContactRefs($customOnly = FALSE, $values = NULL, $depth = 0) {
  $order = [
    'ent',
    'c',
    'table',
    'n',
    'name',
  ];
  static $ent = '';
  static $c = '';
  static $table = '';
  static $n = '';
  $customName = NULL;
  if ($values === NULL) {
    $values = $this->data;
  }
  foreach ($values as $key => $val) {
    ${$order[$depth]} = $key;
    if ($depth < 4 && is_array($val)) {
      $this
        ->fillContactRefs($customOnly, $val, $depth + 1);
    }
    elseif ($depth == 4 && $val && wf_crm_aval($this->all_fields, "{$table}_{$name}:data_type") == 'ContactReference') {
      if ($customOnly && substr($name, 0, 6) != 'custom') {
        return;
      }
      if (is_array($val)) {
        $this->data[$ent][$c][$table][$n][$name] = [];
        foreach ($val as $v) {
          if (is_numeric($v) && !empty($this->ent['contact'][$v]['id'])) {
            $tableName = $customOnly ? $ent : $table;
            $this->data[$ent][$c][$tableName][$n][$name][] = $this->ent['contact'][$v]['id'];
          }
        }
      }
      else {
        $createModeKey = 'civicrm_' . $c . '_contact_' . $n . '_' . $table . '_createmode';
        $multivaluesCreateMode = $this->data['config']['create_mode'][$createModeKey] ?? NULL;
        $cgMaxInstance = $this->all_sets[$table]['max_instances'] ?? 1;
        $key = $n;
        if (substr($name, 0, 6) == 'custom' && $cgMaxInstance > 1) {

          // Retrieve name for multi value custom fields.
          $customName = $this
            ->getNameForMultiValueFields($multivaluesCreateMode, $name, $table, $c, $n);
          $key = 1;
        }
        if (!empty($this->ent['contact'][$val]['id'])) {
          unset($this->data[$ent][$c][$table][$n][$name]);
          $tableName = substr($name, 0, 6) == 'custom' ? $ent : $table;
          $fld = $customName ?? $name;
          $this->data[$ent][$c][$tableName][$key][$fld] = $this->ent['contact'][$val]['id'];
        }
        elseif (substr($name, 0, 6) == 'custom') {
          $this->data[$ent][$c]['update_contact_ref'][$n][$name] = $val;
        }
      }
    }
  }
}