You are here

private function wf_crm_webform_preprocess::fillForm in Webform CiviCRM Integration 7.4

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

Recursively walk through form array and set properties of CiviCRM fields

Parameters

array $elements (reference): FAPI form array

array $submitted: Existing submission (optional)

1 call to wf_crm_webform_preprocess::fillForm()
wf_crm_webform_preprocess::alterForm in includes/wf_crm_webform_preprocess.inc
Alter front-end of webforms: Called by hook_form_alter() when rendering a civicrm-enabled webform Add custom prefix. Display messages. Block users who should not have access. Set webform default values.

File

includes/wf_crm_webform_preprocess.inc, line 434

Class

wf_crm_webform_preprocess

Code

private function fillForm(&$elements, $submitted = array()) {
  foreach ($elements as $eid => &$element) {
    if ($eid[0] == '#' || !is_array($element)) {
      continue;
    }

    // Recurse through nested elements
    $this
      ->fillForm($element, $submitted);
    if (empty($element['#type']) || $element['#type'] == 'fieldset') {
      continue;
    }
    if (!empty($element['#webform_component']) && ($pieces = wf_crm_explode_key($eid))) {
      list(, $c, $ent, $n, $table, $name) = $pieces;

      // Separate out time fields
      if (substr($name, -8) === 'timepart') {
        $name = str_replace('_timepart', '', $name);
      }
      if ($field = wf_crm_aval($this->all_fields, $table . '_' . $name)) {
        $component = $element['#webform_component'];
        $element['#attributes']['class'][] = 'civicrm-enabled';
        $dt = NULL;
        if (!empty($field['data_type'])) {
          $dt = $element['#civicrm_data_type'] = $field['data_type'];
        }
        $element['#attributes']['data-civicrm-field-key'] = $eid;

        // For contribution line-items
        if ($table == 'contribution' && in_array($name, array(
          'line_total',
          'total_amount',
        ))) {
          $element['#attributes']['class'][] = 'contribution-line-item';
        }

        // Provide live options from the Civi DB
        if (!empty($component['extra']['civicrm_live_options']) && isset($element['#options'])) {
          $params = array(
            'extra' => wf_crm_aval($field, 'extra', array()),
          ) + $component;
          $new = wf_crm_field_options($params, 'live_options', $this->data);
          $old = $element['#options'];
          $resave = FALSE;

          // If an item doesn't exist, we add it. If it's changed, we update it.
          // But we don't subtract items that have been removed in civi - this prevents
          // breaking the display of old submissions.
          foreach ($new as $k => $v) {
            if (!isset($old[$k]) || $old[$k] != $v) {
              $old[$k] = $v;
              $resave = TRUE;
            }
          }
          if ($resave) {
            $component['extra']['items'] = wf_crm_array2str($old);
            webform_component_update($component);
          }
          $element['#options'] = $new;
        }

        // If the user has already entered a value for this field, don't change it
        if (isset($this->info[$ent][$c][$table][$n][$name]) && !(isset($component['cid']) && isset($submitted[$component['cid']]))) {
          $val = $this->info[$ent][$c][$table][$n][$name];
          if ($ent === 'contact') {
            $createModeKey = 'civicrm_' . $c . '_contact_' . $n . '_' . $table . '_createmode';
            $multivaluesCreateMode = isset($this->data['config']['create_mode'][$createModeKey]) ? (int) $this->data['config']['create_mode'][$createModeKey] : NULL;

            // Set empty value for field with 'Create only' mode.
            if ($multivaluesCreateMode === self::MULTIVALUE_FIELDSET_MODE_CREATE_ONLY) {
              $val = wf_crm_aval($element, '#default_value', '');
            }
          }
          if (($element['#type'] == 'checkboxes' || !empty($element['#multiple'])) && !is_array($val)) {
            $val = wf_crm_explode_multivalue_str($val);
          }
          if ($element['#type'] != 'checkboxes' && $element['#type'] != 'date' && empty($element['#multiple']) && is_array($val)) {

            // If there's more than one value for a non-multi field, pick the most appropriate
            if (!empty($element['#options'])) {
              foreach ($element['#options'] as $k => $v) {
                if (in_array($k, $val)) {
                  $val = $k;
                  break;
                }
              }
            }
            else {
              $val = array_pop($val);
            }
          }
          if ($component['type'] == 'autocomplete' && is_string($val) && strlen($val)) {
            $options = wf_crm_field_options($component, '', $this->data);
            $val = wf_crm_aval($options, $val);
          }

          // Contact image & custom file fields
          if ($dt == 'File') {
            $fileInfo = $this
              ->getFileInfo($name, $val, $ent, $n);
            if ($fileInfo && in_array($element['#type'], array(
              'file',
              'managed_file',
            ))) {
              $fileInfo = json_encode($fileInfo);
              $js = "jQuery(function() {wfCivi.initFileField('{$eid}', {$fileInfo})});";
              $element['#attached']['js'][$js] = array(
                'type' => 'inline',
              );
            }
          }
          elseif ($element['#type'] == 'value') {
            $element['#value'] = $val;
          }
          else {
            $element['#default_value'] = $val;
          }
        }
        if ($name == 'existing') {
          wf_crm_fill_contact_value($this->node, $component, $element, $this->ent);
        }
        if ($name == 'contribution_page_id') {
          $element['#prefix'] = $this
            ->displayLineItems();
          $element['#suffix'] = '<div class="crm-container crm-public" id="billing-payment-block"></div>';
          $element['#value'] = wf_crm_aval($this->data, 'contribution:1:contribution:1:contribution_page_id');
          unset($element['#default_value']);
        }
      }
    }
  }
}