You are here

private function AdminForm::buildContactTab in Webform CiviCRM Integration 8.5

Build fields for a contact

Parameters

int $n Contact number:

array $c Contact info:

1 call to AdminForm::buildContactTab()
AdminForm::buildForm in src/AdminForm.php
Build admin form for civicrm tab of a webform

File

src/AdminForm.php, line 250
Webform CiviCRM module's admin form.

Class

AdminForm
@file Webform CiviCRM module's admin form.

Namespace

Drupal\webform_civicrm

Code

private function buildContactTab($n, $c) {
  $utils = \Drupal::service('webform_civicrm.utils');
  list($contact_types, $sub_types) = $utils
    ->wf_crm_get_contact_types();
  $this->form['contact_' . $n] = [
    '#type' => 'details',
    '#title' => $n . '. ' . $utils
      ->wf_crm_contact_label($n, $this->data),
    '#description' => $n > 1 ? NULL : t('Primary contact. Usually assumed to be the person filling out the form.') . '<br />' . t('Enable the "Existing Contact" field to autofill with the current user (or another contact).'),
    '#group' => 'webform_civicrm',
    '#attributes' => [
      'class' => [
        'contact-icon-' . $c['contact'][1]['contact_type'],
      ],
    ],
  ];
  $this->form['contact_' . $n][$n . '_contact_type'] = [
    '#type' => 'select',
    '#title' => t('Contact Type'),
    '#default_value' => $c['contact'][1]['contact_type'],
    '#options' => $contact_types,
    '#prefix' => '<div class="contact-type-select">',
  ];
  $this->form['contact_' . $n][$n . '_webform_label'] = [
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#default_value' => $utils
      ->wf_crm_contact_label($n, $this->data, 'plain'),
    '#suffix' => '</div>',
  ];
  $this
    ->help($this->form['contact_' . $n][$n . '_webform_label'], 'webform_label', t('Contact Label'));
  $this
    ->addAjaxItem('contact_' . $n, $n . '_contact_type', 'contact_subtype_wrapper', 'contact-subtype-wrapper');

  // Contact sub-type
  $fid = 'civicrm_' . $n . '_contact_1_contact_contact_sub_type';
  $subTypeIsUserSelect = FALSE;
  if (!empty($sub_types[$c['contact'][1]['contact_type']])) {
    $field = $this->fields['contact_contact_sub_type'];
    $field['name'] = t('Type of @contact', [
      '@contact' => $contact_types[$c['contact'][1]['contact_type']],
    ]);
    $this->form['contact_' . $n]['contact_subtype_wrapper'][$fid] = $subTypeField = $this
      ->addItem($fid, $field);
    $subTypeIsUserSelect = in_array('create_civicrm_webform_element', $subTypeField['#default_value']);
    $this
      ->addAjaxItem('contact_' . $n . ':contact_subtype_wrapper', $fid, 'contact_custom_wrapper');
  }
  else {
    $this->form['contact_' . $n]['contact_subtype_wrapper'][$fid] = [
      '#type' => 'value',
      '#value' => [],
    ];
  }
  foreach ($this->sets as $sid => $set) {
    if ($set['entity_type'] != 'contact') {
      continue;
    }
    if ($sid == 'relationship' && !($set['max_instances'] = $n - 1)) {
      continue;
    }
    if (!empty($set['contact_type']) && $set['contact_type'] != $c['contact'][1]['contact_type']) {
      continue;
    }
    if (!empty($set['sub_types'])) {
      if (!$subTypeIsUserSelect && !array_intersect($c['contact'][1]['contact_sub_type'], array_map('strtolower', $set['sub_types']))) {
        continue;
      }
      $pos =& $this->form['contact_' . $n]['contact_subtype_wrapper']['contact_custom_wrapper'];
      $path = 'contact_' . $n . ':contact_subtype_wrapper:contact_custom_wrapper';
    }
    elseif (!empty($set['contact_type']) || $sid == 'contact') {
      $pos =& $this->form['contact_' . $n]['contact_subtype_wrapper'];
      $path = 'contact_' . $n . ':contact_subtype_wrapper';
    }
    else {
      $pos =& $this->form['contact_' . $n];
      $path = 'contact_' . $n;
    }
    if (!empty($set['max_instances'])) {
      if (!isset($c['number_of_' . $sid])) {
        $c['number_of_' . $sid] = 0;
      }
      $selector = [
        '#type' => 'select',
        '#default_value' => $c['number_of_' . $sid],
        '#prefix' => '<div class="number-of">',
        '#suffix' => '</div>',
      ];
      if ($set['max_instances'] > 1) {
        $selector['#options'] = range(0, $set['max_instances']);
        $selector['#title'] = t('Number of %type Fields', [
          '%type' => $set['label'],
        ]);
      }
      else {
        $selector['#options'] = [
          t('No'),
          t('Yes'),
        ];
        $selector['#title'] = t('Enable %type Fields', [
          '%type' => $set['label'],
        ]);
      }
      if (!empty($set['help_text'])) {
        $this
          ->help($selector, "fieldset_{$sid}");
      }
      $pos['contact_' . $n . '_number_of_' . $sid] = $selector;
      $this
        ->addAjaxItem($path, 'contact_' . $n . '_number_of_' . $sid, $n . $sid . '_wrapper');
    }
    else {
      $c['number_of_' . $sid] = 1;
    }
    for ($i = 1; $i <= $c['number_of_' . $sid]; ++$i) {
      $fsid = 'civicrm_' . $n . $sid . $i . '_fieldset';
      $fieldset = [
        '#type' => 'fieldset',
        '#title' => $set['label'],
        '#attributes' => [
          'id' => $fsid,
          'class' => [
            'web-civi-checkbox-set',
          ],
        ],
        'js_select' => $this
          ->addToggle($fsid),
      ];
      if ($sid == 'relationship') {
        $fieldset['#title'] = t('Relationship to @contact', [
          '@contact' => $utils
            ->wf_crm_contact_label($i, $this->data, 'wrap'),
        ]);
      }
      elseif (isset($set['max_instances']) && $set['max_instances'] > 1) {
        $fieldset['#title'] .= ' ' . $i;
        if (in_array($sid, $utils
          ->wf_crm_location_fields()) && $i == 1) {
          $fieldset['#title'] .= ' ' . t('(primary)');
        }
      }
      else {
        $this
          ->addDynamicCustomSetting($fieldset, $sid, 'contact', $n);
      }
      if (isset($set['fields'])) {
        foreach ($set['fields'] as $fid => $field) {
          if ($fid == 'contact_contact_sub_type' || $fid == 'address_master_id' && count($this->data['contact']) == 1 || isset($field['contact_type']) && $field['contact_type'] != $c['contact'][1]['contact_type']) {
            continue;
          }
          $fid = 'civicrm_' . $n . '_contact_' . $i . '_' . $fid;
          $fieldset[$fid] = $this
            ->addItem($fid, $field);
        }
      }

      // Add 'Create mode' select field to multiple custom group fieldset.
      if (substr($sid, 0, 2) == 'cg' && wf_crm_aval($set, 'max_instances') > 1) {
        $createModeKey = 'civicrm_' . $n . '_contact_' . $i . '_' . $sid . '_createmode';
        $createModeValue = isset($this->settings['data']['config']['create_mode'][$createModeKey]) ? $this->settings['data']['config']['create_mode'][$createModeKey] : NULL;
        $multivalueFieldsetCreateMode = [
          '#type' => 'select',
          '#default_value' => $createModeValue,
          '#prefix' => '<div class="multivalue-fieldset-create-mode">',
          '#suffix' => '</div>',
          '#options' => [
            WebformCivicrmBase::MULTIVALUE_FIELDSET_MODE_CREATE_OR_EDIT => t('Create/ Edit'),
            WebformCivicrmBase::MULTIVALUE_FIELDSET_MODE_CREATE_ONLY => t('Create Only'),
          ],
          '#title' => t('Create mode'),
          '#weight' => -1,
        ];
        $this
          ->help($multivalueFieldsetCreateMode, 'multivalue_fieldset_create_mode');
        $fieldset[$createModeKey] = $multivalueFieldsetCreateMode;
      }
      if (isset($set['max_instances'])) {
        $pos[$n . $sid . '_wrapper'][$n . $sid . $i . '_fieldset'] = $fieldset;
      }
      else {
        $pos[$n . $sid . $i . '_fieldset'] = $fieldset;
      }
    }
    if ($sid == 'contact') {

      // Matching rule
      $rule_field = $this->form['contact_' . $n]['contact_subtype_wrapper']["contact_{$n}_settings_matching_rule"] = [
        '#type' => 'select',
        '#options' => [
          0 => t('- None -'),
          'Unsupervised' => t('Default Unsupervised'),
          'Supervised' => t('Default Supervised'),
        ] + $utils
          ->wf_crm_get_matching_rules($c['contact'][1]['contact_type']),
        '#title' => t('Matching Rule'),
        '#prefix' => '<div class="number-of">',
        '#suffix' => '</div>',
        '#default_value' => wf_crm_aval($this->data['contact'][$n], 'matching_rule', 'Unsupervised', TRUE),
      ];
      $rule_field =& $this->form['contact_' . $n]['contact_subtype_wrapper']["contact_{$n}_settings_matching_rule"];

      // Reset to default if selected rule doesn't exist or isn't valid for this contact type
      if (!array_key_exists($rule_field['#default_value'], $rule_field['#options'])) {
        $rule_field['#default_value'] = $this->form_state['input']["contact_{$n}_settings_matching_rule"] = 'Unsupervised';
      }
      $this
        ->help($rule_field, 'matching_rule');
    }
  }
}