You are here

public function wf_crm_admin_form::postProcess 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::postProcess()

Submission handler, saves CiviCRM options for a Webform node

File

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

Class

wf_crm_admin_form
@file Webform CiviCRM module's admin form.

Code

public function postProcess() {
  $button = $this->form_state['clicked_button']['#id'];
  $nid = $this->node->nid;
  $this->settings = wf_crm_aval($this->form_state, 'storage:vals', $this->form_state['values']);
  if (empty($this->node->webform_civicrm) && !$this->settings['nid'] || $button == 'edit-cancel') {
    $this->form_state['rebuild'] = TRUE;
    unset($this->form_state['storage']['msg']);
    return;
  }
  unset($this->form_state['storage']);
  $enabled = $existing = wf_crm_enabled_fields($this->node, NULL, TRUE);
  $delete_me = $this
    ->getFieldsToDelete($enabled);

  // Display a confirmation before deleting fields
  if ($delete_me && $button == 'edit-submit') {
    $msg = '<p>' . t('These existing fields are no longer needed for CiviCRM processing based on your new form settings.') . '</p><ul>';
    foreach ($delete_me as $key => $id) {
      list(, $c, $ent, $n, $table, $name) = explode('_', $key, 6);
      $info = '';
      if ($ent == 'contact' || $ent == 'participant') {
        $info = '<em>' . wf_crm_contact_label($c, wf_crm_aval($this->node, 'webform_civicrm:data'));
      }
      if ($info && isset($this->sets[$table]['max_instances'])) {
        $info .= ' ' . $this->sets[$table]['label'] . ' ' . $n;
      }
      $info .= $info ? ':</em> ' : '';
      $msg .= '<li>' . $info . $this->node->webform['components'][$id]['name'] . '</li>';
    }
    $msg .= '</ul><p>' . t('Would you like them to be automatically removed from the webform? This is recommended unless you need to keep webform-results information from these fields. (They can still be deleted manually later if you choose not to remove them now.)') . '</p><p><em>' . t('Note: Deleting webform components cannot be undone, and will result in the loss of webform-results info for those elements. Data in the CiviCRM database will not be affected.') . '</em></p>';
    $this->form_state['storage']['msg'] = $msg;
    $this->form_state['storage']['vals'] = $this->settings;
    $this->form_state['rebuild'] = TRUE;
    return;
  }
  module_load_include('inc', 'webform', 'includes/webform.components');
  module_load_include('inc', 'webform_civicrm', 'includes/contact_component');
  $this->form_state['redirect'] = 'node/' . $nid . '/webform';

  // Delete/disable fields
  $deleted = 0;
  if ($button === 'edit-delete' || $button === 'edit-disable' && $this->settings['nid']) {
    foreach ($delete_me as $id) {
      $field = $this->node->webform['components'][$id];
      unset($enabled[$field['form_key']]);
      ++$deleted;
      if ($button === 'edit-delete') {
        webform_component_delete($this->node, $field);
      }
      else {
        $field['form_key'] = 'disabled' . substr($field['form_key'], 7);
        webform_component_update($field);
      }
    }
    if ($deleted == 1) {
      $p = array(
        '%name' => $field['name'],
      );
      drupal_set_message($button === 'edit-delete' ? t('Deleted field: %name', $p) : t('Disabled field: %name', $p));
    }
    else {
      $p = array(
        '!num' => $deleted,
      );
      drupal_set_message($button === 'edit-delete' ? t('Deleted !num fields.', $p) : t('Disabled !num fields.', $p));
    }
    if ($button === 'edit-disable') {
      drupal_set_message(t('Disabled fields will still be processed as normal Webform fields, but they will not be autofilled from or saved to the CiviCRM database.'));
    }
    else {

      // Remove empty fieldsets for deleted contacts
      foreach ($enabled as $key => $id) {
        if (substr($key, -8) == 'fieldset') {
          list(, $c, $ent, $i) = explode('_', $key);
          if ($ent == 'contact' && $i == 1 && (!$this->settings['nid'] || $c > $this->settings['number_of_contacts'])) {
            if (!_wf_crm_child_components($this->node->nid, $id)) {
              webform_component_delete($this->node, $this->node->webform['components'][$id]);
            }
          }
        }
      }
    }
  }

  // Disable CiviCRM for this form
  if (!$this->settings['nid']) {
    $this
      ->disable();
    drupal_set_message(t('CiviCRM processing for this form is now disabled.'));
  }
  else {
    webform_ensure_record($this->node);
    $this
      ->rebuildData();
    if (!$this->settings['toggle_message']) {
      $this->settings['message'] = '';
    }

    // Index disabled components
    $disabled = array();
    foreach (wf_crm_aval($this->node->webform, 'components', array()) as $field) {
      if (substr($field['form_key'], 0, 9) === 'disabled_') {
        $field['form_key'] = 'civicrm' . substr($field['form_key'], 8);
        $disabled[$field['form_key']] = $field;
      }
    }
    $i = 0;
    $created = array();
    foreach ($this->settings as $key => $val) {
      if (substr($key, 0, 7) == 'civicrm') {
        ++$i;
        $field = wf_crm_get_field($key);
        if (!isset($enabled[$key])) {
          $val = (array) $val;
          if (in_array('create_civicrm_webform_element', $val, TRUE) || !empty($val[0]) && $field['type'] == 'hidden') {

            // Restore disabled component
            if (isset($disabled[$key])) {
              webform_component_update($disabled[$key]);
              $enabled[$key] = $disabled[$key]['cid'];
              drupal_set_message(t('Re-enabled field: %name', array(
                '%name' => $disabled[$key]['name'],
              )));
            }
            else {
              $field += array(
                'nid' => $nid,
                'form_key' => $key,
                'weight' => $i,
              );
              self::insertComponent($field, $enabled, $this->settings, $this
                ->isNewFieldset($key));
              $created[] = $field['name'];
              if (isset($field['civicrm_condition'])) {
                $this
                  ->addConditionalRule($field, $enabled);
              }
            }
          }
        }
        elseif ($field['type'] == 'hidden' && !empty($field['expose_list'])) {
          $component = $this->node->webform['components'][$enabled[$key]];
          $component['value'] = $val;
          webform_component_update($component);
        }
        if (substr($key, -11) === '_createmode') {

          // Update webform's settings with 'Create mode' value for custom group.
          $this->settings['data']['config']['create_mode'][$key] = $val;
        }
      }
      elseif (strpos($key, 'settings_dynamic_custom') && $val == 1) {
        $emptySets = wf_crm_get_empty_sets();
        list($ent, $n, , , , $cgId) = explode('_', $key, 6);
        $fieldsetKey = "civicrm_{$n}_{$ent}_1_{$cgId}_fieldset";
        if (array_key_exists($cgId, $emptySets) && !isset($existing[$fieldsetKey])) {
          $fieldset = array(
            'nid' => $nid,
            'pid' => 0,
            'form_key' => $fieldsetKey,
            'name' => $emptySets[$cgId]['label'],
            'type' => 'fieldset',
            'weight' => $i,
          );
          webform_component_insert($fieldset);
        }
      }
    }
    if (count($created) == 1) {
      drupal_set_message(t('Added field: %name', array(
        '%name' => $created[0],
      )));
    }
    elseif ($created) {
      drupal_set_message(t('Added !num fields to the form.', array(
        '!num' => count($created),
      )));
    }

    // Create record
    if (empty($this->node->webform_civicrm)) {
      drupal_write_record('webform_civicrm_forms', $this->settings);
      drupal_set_message(t('CiviCRM processing for this form is now enabled.'));
    }
    else {
      drupal_write_record('webform_civicrm_forms', $this->settings, 'nid');
      drupal_set_message(t('Your CiviCRM form settings have been updated.'));
    }

    // Update existing contact fields
    foreach ($existing as $fid => $id) {
      if (substr($fid, -8) === 'existing') {
        wf_crm_update_existing_component($this->node->webform['components'][$id], $enabled, $this->data);
      }
    }
  }

  // Make sure the updates are visible to anonymous users.
  cache_clear_all();

  // Clear the entity cache.
  if (module_exists('entitycache')) {
    cache_clear_all($nid, 'cache_entity_node');
  }
}