You are here

function _webform_civicrm_webform_component_form_alter in Webform CiviCRM Integration 6

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

Alter back-end webform component edit forms. Called by hook_form_alter() whenever editing a webform component.

1 call to _webform_civicrm_webform_component_form_alter()
webform_civicrm_form_alter in ./webform_civicrm.module
Implementation of hook_form_alter().

File

./webform_civicrm_forms.inc, line 398

Code

function _webform_civicrm_webform_component_form_alter(&$form) {

  // Is this a CiviCRM-enabled webform?
  if (db_result(db_query('SELECT nid FROM {webform_civicrm_forms} WHERE nid = %d', $form['nid']['#value']))) {
    civicrm_initialize();
    $fields = webform_civicrm_get_fields();
    $key = $form['form_key']['#default_value'];

    // Is this component a CiviCRM field?
    if (array_key_exists($key, $fields)) {
      drupal_add_css(drupal_get_path('module', 'webform_civicrm') . '/webform_civicrm_style.css');

      // Prevent users from editing the form_key and breaking things
      $form['form_key']['#disabled'] = TRUE;
      $form['form_key']['#value'] = $key;
      $form['form_key']['#description'] = t('Automatically set for use by CiviCRM processing.');

      // Adds the ability to reload option lists via AJAX (also works without JS)
      $lists = webform_civicrm_get_fields('lists');
      if (array_key_exists($key, $lists)) {
        unset($form['extra']['options_source']);
        $complete_msg = t('The options have been reloaded.');
        $form['extra']['items']['#description'] = t("These options were automatically selected from the CiviCRM database. You may change their order, or delete unwanted options as you see fit. You may also change the labels (text after the |) to be more descriptive. However, adding new items here that are <em>not</em> in CiviCRM won't work. Instead, create your new options in CiviCRM, then click here to refresh this list:");
        if (!empty($_GET['reset'])) {
          $form['extra']['items']['#description'] .= '<div style="color:green; font-weight:bold; margin-top:0.5em;">' . $complete_msg . '</div>';
          $form['extra']['items']['#default_value'] = webform_civicrm_get_options($lists[$key]);
        }
        else {
          $form['extra']['items']['#description'] .= '<div id="civicrm-ajax" style="color:green; font-weight:bold; margin-top:0.5em;">' . l(t('Reload options'), $_GET['q'], array(
            'query' => 'reset=1',
            'attributes' => array(
              'style' => 'color:blue;',
              'onclick' => "\$(this).addClass('views-throbbing');\n            \$.ajax({\n              url: '/webform-civicrm/js/" . arg(1) . "/{$key}',\n              success: function(data) {\n                \$('#edit-extra-items').val(data);\n                \$('#civicrm-ajax').html('{$complete_msg}');\n              }\n            }); return false;",
            ),
          )) . '</div>';
        }
      }
      elseif ($key == 'civicrm_contact_id' || $key == 'civicrm_external_identifier') {
        $form['value']['#type'] = 'hidden';
        $form['value']['#value'] = 0;
        $form['#prefix'] = '<p>' . t('There are no configuration options for this hidden field. You can use it for post processing, for example to include a link to the CiviCRM contact in an email.') . '</p>';
      }

      // Special options for CiviCRM groups
      if ($key == 'civicrm_groups' || $key == 'civicrm_groups_hidden') {
        if ($key == 'civicrm_groups') {
          $form['extra']['items']['#type'] = 'hidden';
          $form['extra']['items']['#required'] = FALSE;
          unset($form['extra']['options_source']);
          $selected = array();
          $defaults = explode("\n", trim($form['extra']['items']['#default_value']));
          foreach ($defaults as $d) {
            list($k, $v) = explode('|', $d);
            $selected[] = trim($k);
          }
          $form['extra']['items']['#default_value'] = '';
        }
        $groups = webform_civicrm_get_options('groups', 'arr');
        $form['value']['#type'] = 'hidden';
        $defaults_selected = explode(',', $form['value']['#default_value']);
        $form['civicrm_groups_fieldset'] = array(
          '#type' => 'fieldset',
          '#title' => t('Groups'),
          '#description' => t('Which group(s) is the user allowed to join on this form? (Often used for letting people subscribe to mailing lists)'),
        );
        if ($key == 'civicrm_groups') {
          $form['civicrm_groups_fieldset']['civicrm_groups'] = array(
            '#title' => t('Allowed Groups'),
            '#type' => 'checkboxes',
            '#required' => TRUE,
            '#options' => $groups,
            '#default_value' => $selected,
            '#prefix' => '<div class="web-civi-3-col">',
            '#suffix' => '</div>',
          );
        }
        $form['civicrm_groups_fieldset']['civicrm_group_defaults'] = array(
          '#title' => t('Checked by Default on Webform?'),
          '#type' => 'checkboxes',
          '#options' => $groups,
          '#default_value' => $defaults_selected,
        );
        if ($key == 'civicrm_groups') {
          $form['civicrm_groups_fieldset']['civicrm_group_defaults']['#prefix'] = '<div class="web-civi-3-col">';
          $form['civicrm_groups_fieldset']['civicrm_group_defaults']['#suffix'] = '</div>';
        }
        else {
          $form['civicrm_groups_fieldset']['#description'] = t('To which group(s) should users be automatically added when submitting this form?');
          $form['civicrm_groups_fieldset']['civicrm_group_defaults']['#title'] = t('Add to Group');
        }
        array_unshift($form['#submit'], 'webform_civicrm_process_group_selection');
      }
      elseif ($key == 'civicrm_tags') {
        $form['value']['#title'] = t('Tags');
        $form['value']['#description'] = t('Enter a comma-separated list of tags to add to contacts who submit this webform.') . '<br />' . t('Use the autocomplete to select existing tags, or you may add new ones.');
        $form['value']['#type'] = 'textfield';
        $form['value']['#size'] = 100;
        $form['value']['#autocomplete_path'] = 'webform-civicrm/js/' . $form['nid']['#value'] . '/tags';
      }
      elseif (!empty($form['extra']['multiple'])) {
        $form['extra']['multiple']['#type'] = 'hidden';
        $form['extra']['multiple']['#value'] = $fields[$key]['extra']['multiple'];
      }
    }
  }
}