You are here

private function wf_crm_admin_form::buildContactTab in Webform CiviCRM Integration 7.4

Same name and namespace in other branches
  1. 7.5 includes/wf_crm_admin_form.inc \wf_crm_admin_form::buildContactTab()

Build fields for a contact

Parameters

int $n Contact number:

array $c Contact info:

1 call to wf_crm_admin_form::buildContactTab()
wf_crm_admin_form::buildForm in includes/wf_crm_admin_form.inc
Build admin form for civicrm tab of a webform

File

includes/wf_crm_admin_form.inc, line 204
Webform CiviCRM module's admin form.

Class

wf_crm_admin_form
@file Webform CiviCRM module's admin form.

Code

private function buildContactTab($n, $c) {
  list($contact_types, $sub_types) = wf_crm_get_contact_types();
  $this->form['contact_' . $n] = array(
    '#type' => 'fieldset',
    '#title' => $n . '. ' . 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' => array(
      'class' => array(
        'contact-icon-' . $c['contact'][1]['contact_type'],
      ),
    ),
  );
  $this->form['contact_' . $n][$n . '_contact_type'] = array(
    '#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'] = array(
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#default_value' => 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', array(
      '@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] = array(
      '#type' => 'value',
      '#value' => array(),
    );
  }
  $this->form['contact_' . $n]['contact_subtype_wrapper']['clear'] = array(
    '#markup' => '<div class="clearfix"> </div>',
  );
  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'], $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 = array(
        '#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', array(
          '%type' => $set['label'],
        ));
      }
      else {
        $selector['#options'] = array(
          t('No'),
          t('Yes'),
        );
        $selector['#title'] = t('Enable %type Fields', array(
          '%type' => $set['label'],
        ));
      }
      if (!empty($set['help_text'])) {
        $this
          ->help($selector, "fieldset_{$sid}", $set['label']);
      }
      $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 = array(
        '#type' => 'fieldset',
        '#title' => $set['label'],
        '#attributes' => array(
          'id' => $fsid,
          'class' => array(
            'web-civi-checkbox-set',
          ),
        ),
        'js_select' => $this
          ->addToggle($fsid),
      );
      if ($sid == 'relationship') {
        $fieldset['#title'] = t('Relationship to !contact', array(
          '!contact' => wf_crm_contact_label($i, $this->data, 'wrap'),
        ));
      }
      elseif (isset($set['max_instances']) && $set['max_instances'] > 1) {
        $fieldset['#title'] .= ' ' . $i;
        if (in_array($sid, 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 = array(
          '#type' => 'select',
          '#default_value' => $createModeValue,
          '#prefix' => '<div class="multivalue-fieldset-create-mode">',
          '#suffix' => '</div>',
          '#options' => array(
            wf_crm_webform_base::MULTIVALUE_FIELDSET_MODE_CREATE_OR_EDIT => t('Create/ Edit'),
            wf_crm_webform_base::MULTIVALUE_FIELDSET_MODE_CREATE_ONLY => t('Create Only'),
          ),
          '#title' => t('Create mode'),
          '#weight' => -1,
        );
        $this
          ->help($multivalueFieldsetCreateMode, 'multivalue_fieldset_create_mode', t('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"] = array(
        '#type' => 'select',
        '#options' => array(
          0 => t('- None -'),
          'Unsupervised' => t('Default Unsupervised'),
          'Supervised' => t('Default Supervised'),
        ) + 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');
    }
  }
}