You are here

private function wf_crm_admin_form::rebuildData 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::rebuildData()

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

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

File

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

Class

wf_crm_admin_form
@file Webform CiviCRM module's admin form.

Code

private function rebuildData() {
  $this->settings['data'] = array(
    'contact' => array(),
  );
  $this->data =& $this->settings['data'];
  list($contact_types, $sub_types) = wf_crm_get_contact_types();
  for ($c = 1; $c <= $this->settings['number_of_contacts']; ++$c) {

    // Contact settings
    if (isset($this->settings[$c . '_contact_type'])) {
      $this->data['contact'][$c] = array(
        'contact' => array(
          1 => array(
            'contact_type' => $this->settings[$c . '_contact_type'],
            'contact_sub_type' => array(),
            'webform_label' => $this->settings[$c . '_webform_label'],
          ),
        ),
      );
      if ($sub_type = wf_crm_aval($this->settings, 'civicrm_' . $c . '_contact_1_contact_contact_sub_type')) {
        $allowed = wf_crm_aval($sub_types, $this->settings[$c . '_contact_type'], array());
        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] = array(
        'contact' => array(
          1 => array(
            'contact_type' => 'individual',
            'contact_sub_type' => array(),
          ),
        ),
        'matching_rule' => 'Unsupervised',
      );

      // Set defaults for new contact
      $this->settings += array(
        'civicrm_' . $c . '_contact_1_contact_first_name' => 'create_civicrm_webform_element',
        'civicrm_' . $c . '_contact_1_contact_last_name' => 'create_civicrm_webform_element',
      );
      $link = array(
        '!link' => 'href="https://docs.civicrm.org/sysadmin/en/latest/integration/drupal/webform/#cloning-a-contact" target="_blank"',
      );
      drupal_set_message(t('Tip: Consider using the clone feature to add multiple similar contacts. (<a !link>more info</a>)', $link), 'status', FALSE);
    }
  }

  // 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 ($ent == 'grant' || $ent == 'activity' || $ent == 'case' || $ent == 'lineitem') {
        $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;
      }
    }
  }

  // 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; $i <= wf_crm_aval($this->settings, 'case_number_of_case'); ++$i) {
    if (!isset($this->settings["civicrm_{$i}_case_1_case_case_type_id"])) {
      $case_types = array_keys(wf_crm_apivalues('Case', 'getoptions', array(
        '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 (substr($key, 0, 7) == 'civicrm') {
      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, array(
        'num_terms',
        'is_active',
        'is_test',
        'payment_processor_id',
      )))) {

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