You are here

public function wf_crm_admin_component::alterForm 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::alterForm()

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 34

Class

wf_crm_admin_component

Code

public function alterForm() {
  $fid = $this->form['form_key']['#default_value'];
  $pieces = wf_crm_explode_key($fid);

  // Is this a crm component of a CiviCRM-enabled webform?
  if (!$this->data || !$pieces) {
    return;
  }
  list(, $i, $ent, $n, $table, $name) = $pieces;
  $this->field = wf_crm_aval(wf_crm_get_fields(), $table . '_' . $name);

  // Is this a crm custom group fieldset?
  if (isCustomGroupFieldset($fid)) {
    $this->form['form_key']['#disabled'] = TRUE;
    $this->form['form_key']['#value'] = $fid;
  }

  // Is this a crm field or fieldset?
  if (!$this->field && $table != 'fieldset') {
    return;
  }
  $this->form['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/css/webform_civicrm_admin.css';
  if (empty($this->form['clone']['#value']) || $name == 'fieldset') {

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

    // Clone an entire contact when cloning their fieldset
    if (!empty($this->form['clone']['#value'])) {
      $this->form['actions']['submit']['#value'] = t('Clone Contact');
      $new = count($this->data['contact']) + 1;
      $this->form['form_key']['#value'] = implode('_', array(
        'civicrm',
        $new,
        $ent,
        $n,
        $table,
        $name,
      ));
      $this->form['name']['#default_value'] = wf_crm_contact_label($new, array(), 'plain');
      ++$this->form['position']['weight']['#default_value'];
      array_unshift($this->form['#submit'], 'wf_crm_contact_clone');
      if (empty($this->form_state['input'])) {
        $types = wf_crm_get_contact_types();
        $type = $types[0][$this->data['contact'][$i]['contact'][1]['contact_type']];
        drupal_set_message(t('Press the "Clone Contact" button below and a new @type will be added to the form with all the settings for !contact. All fields within this fieldset will be cloned (be careful, that may include non-contact fields).', array(
          '@type' => $type,
          '!contact' => wf_crm_contact_label($i, $this->data),
        )), 'status', FALSE);
      }
    }
  }
  elseif (empty($this->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);
  }

  // Show dropdown list of states
  if ($table === 'address' && $name === 'state_province_id') {
    $this->form['validation']['maxlength']['#type'] = 'hidden';
    $this->form['validation']['maxlength']['#value'] = 5;
    $this->form['value']['#type'] = 'select';
    unset($this->form['value']['#size'], $this->form['value']['#maxlength'], $this->form['value']['#description']);
    $this->form['value']['#options'] = wf_crm_get_states();
    $this->form['value']['#empty_value'] = '';
  }
  elseif ($name == 'contribution_page_id') {
    $this->form['value']['#access'] = FALSE;
    $this->form['#prefix'] = '<p>' . t('This is a placeholder for a Contribution Page.') . '</p>';
  }
  elseif ($this->component['type'] === 'hidden' || $this->component['type'] === 'select') {
    $this->hasOptionsTable = $this
      ->buildOptionsTable();
  }

  // Customize options_element a bit
  if (!$this->hasOptionsTable && $this->component['type'] === 'select') {
    $this
      ->alterOptionsElement();
  }

  // Money fields
  if (!$this->hasOptionsTable && wf_crm_aval($this->field, 'data_type') == 'Money') {
    $this
      ->moneyOptions();
  }
  elseif ($this->component['type'] === 'autocomplete') {
    $this->form['options']['#access'] = FALSE;
  }
  elseif (($name === 'contact_id' || $name === 'external_identifier') && $this->component['type'] === 'hidden') {
    $this->form['value']['#value'] = '';
    $this->form['value']['#access'] = FALSE;
    $this->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>' . $this
      ->componentToken($this->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') {
    $this->form['value']['#title'] = t('Checksum Lifespan');
    $this->form['value']['#required'] = TRUE;
    $this->form['value']['#type'] = 'textfield';
    $this->form['value']['#description'] = t('Enter the number of days for which this checksum will be valid. Enter 0 to never expire.');
    $this->form['#validate'][] = 'wf_crm_cs_validate';
    $enabled = wf_crm_enabled_fields($this->node);
    $this->form['#prefix'] = '<p>' . t('This field will generate a checksum for !contact after the form is submitted. You could use its webform token to create a personalized link for anonymous users.', array(
      '!contact' => wf_crm_contact_label($i, $this->data),
    )) . '</p><p>';
    if ($cid_field = wf_crm_aval($enabled, 'civicrm_' . $i . '_contact_1_contact_contact_id')) {
      $this->form['#prefix'] .= t('Example: to link users back to this form, place this line in a webform-generated email:') . '<br /><code>' . url('node/' . $this->node->nid, array(
        'absolute' => TRUE,
      )) . '?cid' . $i . '=';
      $this->form['#prefix'] .= $this
        ->componentToken($cid_field) . '&amp;cs' . $i . '=' . $this
        ->componentToken($this->form['cid']['#value']) . '</code></p>';
      $this->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=' . $this
        ->componentToken($cid_field) . '&amp;cs=' . $this
        ->componentToken($this->form['cid']['#value']) . '</code></p>';
    }
    else {
      $this->form['#prefix'] .= t('Consider enabling the hidden contact_id field - hashed links usually require this value.') . '</p>';
    }
  }

  // Alter weights for contact component
  if ($this->form['type']['#value'] == 'civicrm_contact') {
    $this->form['display']['#weight'] = -1;
  }
  elseif ($this->field && !in_array($this->field['type'], array(
    'civicrm_contact',
    'file',
  )) && empty($_GET['type'])) {
    $unusable_widgets = array(
      'civicrm_contact',
      'pagebreak',
      'markup',
      'layout_box',
      'fieldset',
    );
    $widgets = array_diff_key(webform_component_options(), array_flip($unusable_widgets));
    if ($this->form['type']['#value'] == 'textfield' && $table === 'address') {
      if ($name === 'state_province_id' || $name === 'county_id') {
        $widgets['textfield'] = t('Textfield / AJAX Select');
      }
    }
    $this->form['widget'] = array(
      '#type' => 'fieldset',
      '#title' => t('Widget: %type', array(
        '%type' => wf_crm_aval($widgets, $this->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('#', '', $this->field['name']),
        '%widget' => wf_crm_aval($widgets, $this->field['type'], t('Disabled')),
      )),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $this->form['widget']['type'] = array(
      '#type' => 'select',
      '#options' => $widgets,
      '#default_value' => $this->form['type']['#value'],
    );
    $this->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[type]"]' => array(
            'value' => $this->form['type']['#value'],
          ),
        ),
        'disabled' => array(
          'select[name="widget[type]"]' => array(
            'value' => $this->form['type']['#value'],
          ),
        ),
      ),
    );
  }
}