You are here

function webform_civicrm_configure_form_submit in Webform CiviCRM Integration 6

Same name and namespace in other branches
  1. 6.2 webform_civicrm_admin.inc \webform_civicrm_configure_form_submit()
  2. 7 webform_civicrm_forms.inc \webform_civicrm_configure_form_submit()
  3. 7.2 webform_civicrm_admin.inc \webform_civicrm_configure_form_submit()

Submission handler, saves CiviCRM options for a Webform node

File

./webform_civicrm_forms.inc, line 218

Code

function webform_civicrm_configure_form_submit($form, &$form_state) {
  civicrm_initialize();
  $vals = $form_state['values'];
  $node = node_load($nid = arg(1));
  $fields = webform_civicrm_get_fields();
  $enabled = webform_civicrm_enabled_fields($node);
  $form_state['redirect'] = 'node/' . $nid . '/webform';

  // Disable CiviCRM for this form
  if (isset($node->webform_civicrm) && !$vals['nid']) {
    db_query('DELETE FROM {webform_civicrm_forms} WHERE nid = %d', $nid);
    drupal_set_message(t('CiviCRM processing for this form is now disabled. You may delete any fields you no longer want.'));
  }
  else {
    if (!$vals['toggle_message']) {
      $vals['message'] = '';
    }

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

    // Add webform components (fields)
    $i = 0;
    $lists = webform_civicrm_get_fields('lists');
    foreach ($fields as $id => $field) {
      if (empty($enabled[$id]) && $vals[$id]) {
        $field['nid'] = $nid;
        $field['form_key'] = $id;
        $field['weight'] = $i;
        $field['pid'] = 0;
        unset($field['custom_group']);
        if (($field['type'] == 'textfield' || $field['type'] == 'email') && !$field['extra']['width']) {
          $field['extra']['width'] = 20;
        }

        // Set default country
        if ($id == 'civicrm_country_id') {
          $config = CRM_Core_Config::singleton();
          $field['value'] = $config->defaultContactCountry;
        }

        // Retrieve option list
        if (array_key_exists($id, $lists)) {
          $field['extra']['items'] = webform_civicrm_get_options($lists[$id]);
          if (!isset($field['extra']['aslist'])) {
            $field['extra']['aslist'] = 1;
          }
          if (strpos($id, 'custom') === FALSE) {
            unset($field['extra']['description']);
          }
        }

        // Create webform component
        $fid = webform_component_insert($field);
        if ($id == 'civicrm_groups') {
          drupal_set_message(t('Please choose which group(s) you want to be selectable by users of this form.'));
          $form_state['redirect'] = 'node/' . $nid . '/webform/components/' . $fid;
        }
        else {
          $new_fields = TRUE;
        }
      }
      ++$i;
    }
    if (!empty($new_fields)) {
      drupal_set_message(t('You may now customize your new fields however you wish.'));
    }
  }
}