You are here

private function wf_crm_admin_component::buildOptionsTable in Webform CiviCRM Integration 7.4

Same name and namespace in other branches
  1. 7.5 includes/wf_crm_admin_component.inc \wf_crm_admin_component::buildOptionsTable()

Interface similar to options_element module but with the important difference that options already exist in the civi db so this does not allow create/delete of options

Return value

bool

1 call to wf_crm_admin_component::buildOptionsTable()
wf_crm_admin_component::alterForm in includes/wf_crm_admin_component.inc
Alter back-end webform component edit forms. Called by hook_form_alter() whenever editing a webform component.

File

includes/wf_crm_admin_component.inc, line 230

Class

wf_crm_admin_component

Code

private function buildOptionsTable() {
  $options = $sort = wf_crm_field_options($this->component, 'component_edit', $this->data);
  if (!$options) {
    return FALSE;
  }
  $this->form['#attached']['js'] = array(
    drupal_get_path('module', 'webform_civicrm') . '/js/webform_civicrm_options.js',
  );
  $defaults_selected = array();
  if (isset($this->component['value']) && strlen($this->component['value'])) {
    foreach (explode(',', trim($this->component['value'])) as $v) {
      $defaults_selected[] = '_web_civi_option_selected_' . $v;
    }
  }

  // Get rid of stuff related to options_element module
  unset($this->form['items']);
  $this->form['value']['#type'] = 'hidden';
  $this->form['civicrm_options_fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Options'),
    '#id' => 'wf-crm-options-fieldset',
    '#theme' => 'webform_civicrm_options_table',
  );
  $option_keys = array();
  foreach ($options as $k => $v) {
    $option_keys['_web_civi_option_selected_' . $k] = '';
    $this->form['civicrm_options_fieldset']['civicrm_option_name_' . $k] = array(
      '#markup' => '<span class="civicrm-option-name">' . $v . '</span>',
    );
  }
  if ($this->component['type'] === 'select') {
    $this->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($this->component['extra']['civicrm_live_options'])),
      '#parents' => array(
        'extra',
        'civicrm_live_options',
      ),
    );
    $this->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($this->field, 'data_type') == 'ContactReference') {
      $this->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($this->component['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] = '';
    }
    $this->form['extra']['items']['#type'] = 'hidden';
    $this->form['extra']['items']['#required'] = FALSE;
    $this->form['extra']['items']['#value'] = $this->component['extra']['items'];
    $this->form['extra']['options_source']['#access'] = FALSE;
    $this->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);
      $this->form['civicrm_options_fieldset']['civicrm_option_label_' . $k] = array(
        '#type' => 'textfield',
        '#size' => 30,
        '#default_value' => !empty($options_selected[$k]) ? $options_selected[$k] : $options[$k],
      );
      $this->form['civicrm_options_fieldset']['civicrm_option_weight_' . $k] = array(
        '#type' => 'textfield',
        '#size' => 3,
        '#default_value' => ++$w,
      );
    }
  }
  $this->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($this->field['extra']['multiple']) && $this->component['type'] === 'select') {
    $this->form['extra']['multiple']['#type'] = 'hidden';
    $this->form['extra']['multiple']['#value'] = 0;
  }
  elseif ($this->component['type'] === 'select') {
    $this->form['extra']['multiple']['#type'] = 'checkbox';
    $this->form['extra']['multiple']['#title'] = t('Multiple');
    $this->form['extra']['multiple']['#description'] = t('Check this option if the user should be allowed to choose multiple values.');
    $this->form['extra']['multiple']['#default_value'] = !empty($this->component['extra']['multiple']);
  }
  else {
    $this->form['extra']['multiple']['#type'] = 'hidden';
    $this->form['extra']['multiple']['#value'] = (int) (!empty($this->field['extra']['multiple']));
  }
  array_unshift($this->form['#submit'], 'wf_crm_process_options_selection');
  return TRUE;
}