You are here

function _wf_crm_component_form_alter in Webform CiviCRM Integration 7.3

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

1 call to _wf_crm_component_form_alter()
webform_civicrm_form_alter in ./webform_civicrm.module
Implements hook_form_alter().

File

./webform_civicrm_admin.inc, line 954

Code

function _wf_crm_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 = wf_crm_explode_key($fid))) {
      return;
    }
    list(, $i, $ent, $n, $table, $name) = $pieces;
    $fields = wf_crm_get_fields();

    // Is this component a CiviCRM field?
    if (($field = wf_crm_aval($fields, $table . '_' . $name)) || $table == 'fieldset') {
      $form['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/webform_civicrm_admin.css';
      $element = $node->webform['components'][$form['cid']['#value']];
      if (empty($form['clone']['#value']) || $name == '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 (!empty($form['clone']['#value'])) {
          $form['submit']['#value'] = t('Clone Contact');
          $new = count($node->webform_civicrm['data']['contact']) + 1;
          $form['form_key']['#value'] = implode('_', array(
            'civicrm',
            $new,
            $ent,
            $n,
            $table,
            $name,
          ));
          $form['name']['#default_value'] = str_replace($i, $new, $form['name']['#default_value']);
          ++$form['position']['weight']['#default_value'];
          array_unshift($form['#submit'], 'wf_crm_contact_clone');
          if (empty($form_state['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['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' && $name === '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.');
        $form['value']['#attributes']['onkeyup'] = 'this.value = this.value.toUpperCase();';
      }
      elseif (($element['type'] === 'hidden' || $element['type'] === 'select') && wf_crm_get_list($table, $name)) {
        $form['#attached']['js'] = array(
          drupal_get_path('module', 'webform_civicrm') . '/' . 'webform_civicrm_options.js',
        );
        $options = $sort = wf_crm_field_options($element, 'component_edit', $node->webform_civicrm['data']);
        $defaults_selected = array();
        if (isset($element['value']) && strlen($element['value'])) {
          foreach (explode(',', trim($element['value'])) as $v) {
            $defaults_selected[] = '_web_civi_option_selected_' . $v;
          }
        }

        // Get rid of stuff related to options_element module
        unset($form['items']);
        $form['value']['#type'] = 'hidden';
        $form['civicrm_options_fieldset'] = array(
          '#type' => 'fieldset',
          '#title' => t('Options'),
          '#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(
            '#markup' => '<span class="civicrm-option-name">' . $v . '</span>',
          );
        }
        if ($element['type'] === 'select') {
          $form['civicrm_options_fieldset']['civicrm_live_options'] = array(
            '#type' => 'radios',
            '#options' => array(
              t('<strong>Static Options</strong> (fully configurable)'),
              t('<strong>Live Options</strong> (update automatically)'),
            ),
            '#default_value' => (int) (!empty($element['extra']['civicrm_live_options'])),
            '#parents' => array(
              'extra',
              'civicrm_live_options',
            ),
          );
          $form['civicrm_options_fieldset']['intro'] = array(
            '#markup' => '<p><div class="live-options-hide">' . 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.') . '</div><div class="live-options-show">' . t('You cannot control the presentation of live options. They will be loaded from the CiviCRM database every time the form is displayed.') . '</div><div>' . t('Check the "default" box for an option to be selected by default when a user views the form.') . '</div></p>',
          );

          // Special instructions for contact reference fields
          if (wf_crm_aval($field, 'data_type') == 'ContactReference') {
            $form['civicrm_options_fieldset']['intro'] = array(
              '#markup' => '<p>' . t('This is a contact reference field. It points to another contact on the webform. You can configure how that contact is presented by editing their "Existing Contact" field.') . '</p>' . '<p>' . t("Note: In most cases it is not desirable to have the selection of webform contacts exposed to the end-user so you may wish to set this field's value on the CiviCRM tab instead.") . '</p>',
            );
          }
          $options_selected = wf_crm_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']['items']['#value'] = $element['extra']['items'];
          $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' => array(
            '' => '',
          ) + $option_keys,
          '#default_value' => $defaults_selected,
        );

        // Auto set multi-value option for single-valued entities
        if (empty($field['extra']['multiple']) && $element['type'] === 'select') {
          $form['extra']['multiple']['#type'] = 'hidden';
          $form['extra']['multiple']['#value'] = 0;
        }
        elseif ($element['type'] === 'select') {
          $form['extra']['multiple']['#type'] = 'checkbox';
          $form['extra']['multiple']['#title'] = t('Multiple');
          $form['extra']['multiple']['#description'] = t('Check this option if the user should be allowed to choose multiple values.');
          $form['extra']['multiple']['#default_value'] = !empty($element['extra']['multiple']);
        }
        else {
          $form['extra']['multiple']['#type'] = 'hidden';
          $form['extra']['multiple']['#value'] = (int) (!empty($field['extra']['multiple']));
        }
        array_unshift($form['#submit'], 'wf_crm_process_options_selection');
      }
      elseif (($name === 'contact_id' || $name === '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>' . wf_crm_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 ($name == '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'][] = 'wf_crm_cs_validate';
        $enabled = wf_crm_enabled_fields($node);
        $form['#prefix'] = '<p>' . t('This field will generate a checksum for contact !num after the form is submitted. You could use its webform token to create a personalized link for anonymous users.', array(
          '!num' => $i,
        )) . '</p><p>';
        if ($cid_field = wf_crm_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 /><code>' . url('node/' . $node->nid, array(
            'absolute' => TRUE,
          )) . '?cid' . $i . '=';
          $form['#prefix'] .= wf_crm_component_token($node, $cid_field) . '&amp;cs' . $i . '=' . wf_crm_component_token($node, $form['cid']['#value']) . '</code></p>';
          $form['#prefix'] .= '<p>' . t('Example: to redirect to a contribution page, paste this into the redirect field on the Webform "Form Settings" tab (change id from 1 to the real id of your contribution page):') . '<br /><code>' . 'civicrm/contribute/transact?reset=1&amp;id=1' . '&amp;cid=' . wf_crm_component_token($node, $cid_field) . '&amp;cs=' . wf_crm_component_token($node, $form['cid']['#value']) . '</code></p>';
        }
        else {
          $form['#prefix'] .= t('Consider enabling the hidden contact_id field - hashed links usually require this value.') . '</p>';
        }
      }

      // Alter weights for contact component
      if ($form['type']['#value'] == 'civicrm_contact') {
        $form['display']['#weight'] = -1;
      }
      elseif ($field && $field['type'] !== 'civicrm_contact' && empty($_GET['type'])) {
        $widgets = array();
        foreach (webform_components() as $k => $v) {
          if ($k !== 'civicrm_contact' && $k !== 'pagebreak' && $k !== 'markup' && empty($v['features']['group'])) {
            $widgets[$k] = $v['label'];
          }
        }
        if ($form['type']['#value'] == 'textfield' && $table === 'address') {
          if ($name === 'state_province_id' || $name === 'county_id') {
            $widgets['textfield'] = t('Textfield / AJAX Select');
          }
        }
        $form['widget'] = array(
          '#type' => 'fieldset',
          '#title' => t('Widget: %type', array(
            '%type' => wf_crm_aval($widgets, $form['type']['#value'], t('Unknown')),
          )),
          '#description' => t('The default widget for this %type field is %widget. You may change it if you wish - for example, a hidden field allows you to set the value without exposing this field to the form.<br />Note: Not all widgets may be compatible with this CiviCRM field - test your form after changing widgets.', array(
            '%type' => str_replace('#', '', $field['name']),
            '%widget' => wf_crm_aval($widgets, $field['type'], t('Disabled')),
          )),
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
        );
        $form['widget']['widget'] = array(
          '#type' => 'select',
          '#options' => $widgets,
          '#default_value' => $form['type']['#value'],
        );
        $form['widget']['change_widget'] = array(
          '#type' => 'submit',
          '#value' => t('Change Widget'),
          '#submit' => array(
            'webform_component_edit_form_submit',
            'wf_crm_change_widget',
          ),
          '#states' => array(
            'invisible' => array(
              'select[name="widget[widget]"]' => array(
                'value' => $form['type']['#value'],
              ),
            ),
            'disabled' => array(
              'select[name="widget[widget]"]' => array(
                'value' => $form['type']['#value'],
              ),
            ),
          ),
        );
      }
    }
  }
}