You are here

public function AdminForm::rebuildData in Webform CiviCRM Integration 8.5

Build $this->data array for webform settings; called while rebuilding or post-processing the admin form.

Made public so we can invoke from D8 form, for now.

2 calls to AdminForm::rebuildData()
AdminForm::postProcess in src/AdminForm.php
Submission handler, saves CiviCRM options for a Webform node
AdminForm::rebuildForm in src/AdminForm.php
On rebuilding the form

File

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

Class

AdminForm
@file Webform CiviCRM module's admin form.

Namespace

Drupal\webform_civicrm

Code

public function rebuildData() {
  $utils = \Drupal::service('webform_civicrm.utils');
  $this->settings['data'] = [
    'contact' => [],
  ];
  $this->data =& $this->settings['data'];
  list($contact_types, $sub_types) = $utils
    ->wf_crm_get_contact_types();
  for ($c = 1; $c <= $this->settings['number_of_contacts']; ++$c) {

    // Contact settings
    $contact_type_key = $c . '_contact_type';
    if (isset($this->settings[$contact_type_key])) {
      $contact_type = $this->settings[$contact_type_key];
      $this->data['contact'][$c] = [
        'contact' => [
          1 => [
            'contact_type' => $contact_type,
            'contact_sub_type' => [],
            'webform_label' => $this->settings[$c . '_webform_label'],
          ],
        ],
      ];
      $sub_type = $this->settings["civicrm_{$c}_contact_1_contact_contact_sub_type"] ?? NULL;
      if ($sub_type) {
        $allowed = $sub_types[$contact_type] ?? [];
        foreach ($sub_type as $sub) {
          if (isset($allowed[$sub])) {
            $this->data['contact'][$c]['contact'][1]['contact_sub_type'][$sub] = $sub;
          }
        }
      }
    }
    else {
      $this->data['contact'][$c] = [
        'contact' => [
          1 => [
            'contact_type' => 'individual',
            'contact_sub_type' => [],
          ],
        ],
        'matching_rule' => 'Unsupervised',
      ];

      // Set defaults for new contact
      $this->settings += [
        'civicrm_' . $c . '_contact_1_contact_first_name' => 'create_civicrm_webform_element',
        'civicrm_' . $c . '_contact_1_contact_last_name' => 'create_civicrm_webform_element',
      ];

      // ToDo: investigate how to re-instate this feature
      // $link = [':link' => 'href="https://docs.civicrm.org/sysadmin/en/latest/integration/drupal/webform/#cloning-a-contact" target="_blank"'];
      // \Drupal::messenger()->addStatus(t('Tip: Consider using the clone feature to add multiple similar contacts. (<a :link>more info</a>)', $link));
    }
  }

  // Store meta settings, i.e. number of email for contact 1
  foreach ($this->settings as $key => $val) {
    if (strpos($key, '_number_of_') !== FALSE) {
      list($ent, $c, $key) = explode('_', $key, 3);
      if (isset($this->data[$ent][$c]) || $ent === 'participant' || $ent === 'membership') {
        $this->data[$ent][$c][$key] = $val;
      }
      elseif (in_array($ent, [
        'grant',
        'activity',
        'case',
        'lineitem',
        'receipt',
        'billing',
      ])) {
        $this->data[$ent]["number_{$key}"] = $val;
      }
    }
    elseif (strpos($key, '_settings_') !== FALSE) {
      list($ent, $c, , $key) = explode('_', $key, 4);
      $val = is_array($val) ? array_filter($val) : $val;

      // Don't store settings for nonexistant contacts. Todo: check other entities
      if (isset($this->data[$ent][$c]) || $ent !== 'contact') {
        $this->data[$ent][$c][$key] = $val;
      }
    }
  }

  // Enable billing address by default when contribution is enabled.
  if ($this->settings['civicrm_1_contribution_1_contribution_enable_contribution'] && empty($this->data['billing'])) {
    $this->data['billing'] = [
      'number_number_of_billing' => 1,
    ];
    $billingFields = [
      'first_name',
      'last_name',
      'street_address',
      'city',
      'postal_code',
      'state_province_id',
      'country_id',
    ];
    foreach ($billingFields as $field) {
      $this->settings["civicrm_1_contribution_1_contribution_billing_address_{$field}"] = 1;
    }
  }

  // Defaults when adding an activity
  for ($i = 1; $i <= $this->settings['activity_number_of_activity']; ++$i) {
    if (!isset($this->settings["activity_{$i}_settings_existing_activity_status"])) {
      $this->data['activity'][$i]['activity'][1]['target_contact_id'] = range(1, $this->settings['number_of_contacts']);
    }
  }

  // Defaults when adding a case
  for ($i = 1, $iMax = wf_crm_aval($this->settings, 'case_number_of_case'); $i <= $iMax; ++$i) {
    if (!isset($this->settings["civicrm_{$i}_case_1_case_case_type_id"])) {
      $case_types = array_keys($utils
        ->wf_crm_apivalues('Case', 'getoptions', [
        'field' => 'case_type_id',
      ]));
      $this->data['case'][$i]['case'][1]['case_type_id'] = $case_types[0];
    }
  }

  // Store event settings
  if (isset($this->settings['participant_reg_type'])) {
    $this->data['participant_reg_type'] = $this->settings['participant_reg_type'];
    $this->data['reg_options'] = $this->settings['reg_options'];
  }

  // Add settings exposed to the back-end to data
  foreach ($this->settings as $key => $val) {
    if (strpos($key, 'civicrm') === 0) {
      list(, $c, $ent, $n, $table, $name) = explode('_', $key, 6);
      if (is_array($val)) {

        // Git rid of the "User Select" and "None" options
        unset($val['create_civicrm_webform_element'], $val['']);
      }
      elseif ($val === 'create_civicrm_webform_element') {
        $val = '';
      }

      // Saves all non-empty values with a hack for fields which needs to be saved even when 0
      // FIXME: Really ought to change the select placeholder value to be '' instead of 0
      if (isset($this->fields[$table . '_' . $name]['expose_list']) && (!empty($val) || in_array($name, [
        'num_terms',
        'is_active',
        'is_test',
        'payment_processor_id',
      ]))) {

        // Don't add data for non-existent contacts
        if (!in_array($ent, [
          'contact',
          'participant',
          'membership',
        ]) || isset($this->data['contact'][$c])) {
          if ($name == 'payment_processor_id' && !empty($val) && is_numeric($val)) {
            $val = $this
              ->getPaymentProcessorValue($val);
          }
          $this->data[$ent][$c][$table][$n][$name] = $val;
        }
      }
    }
  }
}