You are here

function _webform_civicrm_webform_component_form_alter in Webform CiviCRM Integration 7.2

Same name and namespace in other branches
  1. 6.2 webform_civicrm_admin.inc \_webform_civicrm_webform_component_form_alter()
  2. 6 webform_civicrm_forms.inc \_webform_civicrm_webform_component_form_alter()
  3. 7 webform_civicrm_forms.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
Implements hook_form_alter().

File

./webform_civicrm_admin.inc, line 985

Code

function _webform_civicrm_webform_component_form_alter(&$form, $form_state) {
  $node = node_load($form['nid']['#value']);

  // Is this a CiviCRM-enabled webform?
  if (!empty($node->webform_civicrm)) {
    civicrm_initialize();
    $fid = $form['form_key']['#default_value'];

    // Is this a civicrm component?
    if (!($pieces = webform_civicrm_explode_key($fid))) {
      return;
    }
    list($lobo, $i, $ent, $n, $table, $key) = $pieces;
    $fields = webform_civicrm_get_fields();

    // Is this component a CiviCRM field?
    if (($field = webform_civicrm_aval($fields, $table . '_' . $key)) || $lobo == 'civicrm' && $table == 'fieldset') {
      drupal_add_css(drupal_get_path('module', 'webform_civicrm') . '/webform_civicrm_admin.css');
      $element = $node->webform['components'][$form['cid']['#value']];
      if (arg(5) != 'clone' || $key == 'fieldset') {

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

        // Clone an entire contact when cloning their fieldset
        if (arg(5) == 'clone') {
          $form['submit']['#value'] = t('Clone Contact');
          $new = count($node->webform_civicrm['data']['contact']) + 1;
          $form['form_key']['#value'] = implode('_', array(
            $lobo,
            $new,
            $ent,
            $n,
            $table,
            $key,
          ));
          $form['name']['#default_value'] = str_replace($i, $new, $form['name']['#default_value']);
          array_unshift($form['#submit'], 'webform_civicrm_contact_clone');
          if (empty($form_state[WEBFORM_CIVICRM_FAPI_INPUT])) {
            drupal_set_message(t('Press the button below to clone this contact. A new CiviCRM contact will be added to the form with all the settings for contact !num. All fields from within this fieldset will be cloned (be careful, that may include non-contact !num fields).', array(
              '!num' => $i,
            )), 'status', FALSE);
          }
        }
      }
      elseif (empty($form_state[WEBFORM_CIVICRM_FAPI_INPUT])) {

        // Clone a single CiviCRM field
        drupal_set_message(t('You are cloning a CiviCRM component. Refer to the Webform CiviCRM instructions for how to set the key if you want the new field to be processed by CiviCRM. You can also clone an entire contact by clicking the clone button by their fieldset.'), 'status', FALSE);
      }
      if ($table === 'address' && $key === 'state_province_id') {
        $form['validation']['maxlength']['#type'] = 'hidden';
        $form['validation']['maxlength']['#value'] = 5;
        $form['value']['#description'] = t('To set a default value, enter the state/province abbreviation in all caps.');
      }

      // For ContactReference fields, choose a group
      if (webform_civicrm_aval($field, 'data_type') === 'ContactReference') {
        if ($element['type'] === 'select') {

          // Get rid of options_element stuff
          unset($form['items']);
          $component = $node->webform['components'][$form['cid']['#value']];
          $form['extra']['civicrm_group'] = array(
            '#type' => 'select',
            '#default_value' => webform_civicrm_aval($component['extra'], 'civicrm_group', 0),
            '#title' => t('Choose Contact From'),
            '#options' => array(
              t('- Webform Contacts -'),
            ) + webform_civicrm_get_options('group'),
            '#description' => t('Group from which this contact reference will be selected'),
          );
          $form['extra']['items']['#type'] = 'hidden';
          $form['extra']['items']['#required'] = FALSE;
          $form['extra']['options_source']['#access'] = FALSE;
          $form['extra']['multiple']['#type'] = 'hidden';
          $form['extra']['multiple']['#value'] = 0;
          array_unshift($form['#submit'], 'webform_civicrm_process_group_selection');
        }
        else {
          $form['#prefix'] = '<p>' . t('Enter either a numeric contact id, or #1, #2, etc. to use contact 1, 2, etc. from the webform.') . '</p>';
        }
      }
      elseif (($element['type'] === 'hidden' || $element['type'] === 'select') && array_key_exists($key, webform_civicrm_get_fields('lists'))) {
        webform_civicrm_add_js('webform_civicrm_options.js');
        $options = $sort = webform_civicrm_field_options($element, 'component_edit', $node->webform_civicrm['data']);
        $defaults_selected = array();
        if (!empty($element['value'])) {
          foreach (explode(',', trim($element['value'])) as $v) {
            $defaults_selected[] = '_web_civi_option_selected_' . $v;
          }
        }

        // Get rid of options_element stuff
        unset($form['items']);
        $form['value']['#type'] = 'hidden';
        $form['civicrm_options_fieldset'] = array(
          '#type' => 'fieldset',
          '#title' => t('Options'),
          '#description' => t('Enable the options you wish for this hidden form element.'),
          '#theme' => 'webform_civicrm_options',
        );
        $option_keys = array();
        foreach ($options as $k => $v) {
          $option_keys['_web_civi_option_selected_' . $k] = '';
          $form['civicrm_options_fieldset']['civicrm_option_name_' . $k] = array(
            WEBFORM_CIVICRM_MARKUP => '<span class="civicrm-option-name">' . $v . '</span>',
          );
        }
        if ($element['type'] !== 'hidden') {
          $form['civicrm_options_fieldset']['#description'] = t('Drag the arrows to re-order these options. Click the "enabled" checkbox to show/remove an item from the form. Set the label as you want it to appear on the form. Check the "default" box for an option to be selected by default when a user views the form.');
          $options_selected = webform_civicrm_str2array($element['extra']['items']);

          // Sort weights. Unselected options will be at the bottom.
          $option_keys = $option_selected_keys = array();
          foreach ($options_selected as $k => $v) {
            if (isset($options[$k])) {
              $option_keys['_web_civi_option_selected_' . $k] = '';
              $option_selected_keys[] = '_web_civi_option_selected_' . $k;
              unset($sort[$k]);
            }
          }
          foreach ($sort as $k => $v) {
            $option_keys['_web_civi_option_selected_' . $k] = '';
          }
          $form['extra']['items']['#type'] = 'hidden';
          $form['extra']['items']['#required'] = FALSE;
          $form['extra']['options_source']['#access'] = FALSE;
          $form['civicrm_options_fieldset']['civicrm_options'] = array(
            '#type' => 'checkboxes',
            '#required' => TRUE,
            '#options' => $option_keys,
            '#default_value' => $option_selected_keys,
          );
          $w = 0;
          foreach ($option_keys as $k => $v) {
            $k = str_replace('_web_civi_option_selected_', '', $k);
            $form['civicrm_options_fieldset']['civicrm_option_label_' . $k] = array(
              '#type' => 'textfield',
              '#size' => 30,
              '#default_value' => !empty($options_selected[$k]) ? $options_selected[$k] : $options[$k],
            );
            $form['civicrm_options_fieldset']['civicrm_option_weight_' . $k] = array(
              '#type' => 'textfield',
              '#size' => 3,
              '#default_value' => ++$w,
            );
          }
        }
        $form['civicrm_options_fieldset']['civicrm_defaults'] = array(
          '#type' => 'checkboxes',
          '#options' => $option_keys,
          '#default_value' => $defaults_selected,
        );

        // Auto set multi-value option for single-valued entities
        if (empty($field['extra']['multiple']) && $element['type'] != 'hidden' && $key != 'event_id') {
          $form['extra']['multiple']['#type'] = 'hidden';
          $form['extra']['multiple']['#value'] = 0;
          $form['civicrm_options_fieldset']['civicrm_defaults']['#type'] = 'radios';
          $form['civicrm_options_fieldset']['civicrm_defaults']['#options'] += array(
            '' => '',
          );
          $form['civicrm_options_fieldset']['civicrm_defaults']['#default_value'] = $element['value'] ? '_web_civi_option_selected_' . $element['value'] : '';
        }
        array_unshift($form['#submit'], 'webform_civicrm_process_options_selection');
      }
      elseif (($key == 'contact_id' || $key == 'external_identifier') && $element['type'] == 'hidden') {
        $form['value']['#value'] = '';
        $form['value']['#type'] = 'textfield';
        $form['value']['#disabled'] = TRUE;
        $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><p>' . t('The webform token for this field is:') . '<br /><strong>' . webform_civicrm_component_token($node, $form['cid']['#value']) . '</strong><br /><em>' . t('Note: this token will change if you move the field in or out of a fieldset.') . '</em></p>';
      }
      elseif ($key == 'cs') {
        $form['value']['#title'] = t('Checksum Lifespan');
        $form['value']['#required'] = TRUE;
        $form['value']['#type'] = 'textfield';
        $form['value']['#description'] = t('Enter the number of days for which this checksum will be valid. Enter 0 to never expire.');
        $form['#validate'][] = 'webform_civicrm_cs_validate';
        $enabled = webform_civicrm_enabled_fields($node);
        $form['#prefix'] = '<p>' . t('This field will generate a checksum for contact !num after the form is submitted. You could use it to send a personalized link for anonymous users.', array(
          '!num' => $i,
        )) . '</p><p>';
        if ($cid_field = webform_civicrm_aval($enabled, 'civicrm_' . $i . '_contact_1_contact_contact_id')) {
          $form['#prefix'] .= t('Example: to link users back to this form, place this line in a webform-generated email:') . '<br /><em>' . url('node/' . $node->nid, array(
            'absolute' => TRUE,
          )) . '?cid' . $i . '=' . webform_civicrm_component_token($node, $cid_field) . '&amp;cs=' . webform_civicrm_component_token($node, $form['cid']['#value']) . '</em></p>';
        }
        else {
          $form['#prefix'] .= t('Consider enabling the hidden contact_id field - hashed links usually require this value.') . '</p>';
        }
      }
    }
  }
}