You are here

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

Add CiviCRM info and theming to webform components form.

Parameters

array $form:

array $rows:

stdClass $node:

1 call to wf_crm_admin_component::preprocessComponentsForm()
webform_civicrm_preprocess_webform_components_form in ./webform_civicrm.module
Implements hook_preprocess_HOOK().

File

includes/wf_crm_admin_component.inc, line 449

Class

wf_crm_admin_component

Code

public static function preprocessComponentsForm(&$form, &$rows, $node) {

  // Prepare for CiviCRM processing
  $enabled = $data = array();
  if (!empty($node->webform_civicrm)) {
    civicrm_initialize();
    $enabled = wf_crm_enabled_fields($node);
    $data = $node->webform_civicrm['data'];
    $form['components']['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/css/webform_civicrm_admin.css';
  }
  foreach ($rows as &$row) {

    // Empty message when no components have been added
    if (!empty($row[0]['colspan'])) {
      if (empty($node->webform['components']) && wf_crm_admin_access($node)) {
        $form['components']['#attached']['js'][] = drupal_get_path('module', 'webform_civicrm') . '/js/webform_tab.js';
        $row[0]['data'] = t('Add a webform component below, or click the <a class="webform-civicrm-tab-link" href="!url">CiviCRM tab</a> to add CRM fields.', array(
          '!url' => url("node/{$node->nid}/civicrm"),
        ));
      }
    }
    elseif (in_array('webform-add-form', $row['class'])) {
      unset($form['add']['type']['#options']['civicrm_contact']);
      $form['add']['type']['#printed'] = FALSE;
      $row['data'][1] = drupal_render($form['add']['type']);
    }
    elseif (!empty($row['data-cid']) && isset($node->webform['components'][$row['data-cid']])) {
      $cid = $row['data-cid'];
      $component = $node->webform['components'][$cid];
      $type =& $row['data'][2]['data'];
      if ($component['type'] == 'civicrm_contact') {
        $types = array(
          'autocomplete' => t('Contact - Autocomplete'),
          'select' => t('Contact - Select List'),
          'hidden' => $component['extra']['show_hidden_contact'] ? t('Contact - Static') : t('Contact - Hidden'),
        );
        $type = $types[$component['extra']['widget']];
      }
      elseif ($component['type'] == 'select') {
        if ($component['extra']['aslist']) {
          $type = $component['extra']['multiple'] ? t('Multi-select') : t('Select');
        }
        else {
          $type = $component['extra']['multiple'] ? t('Checkboxes') : t('Radio buttons');
        }
      }
      if (in_array($cid, $enabled)) {
        $fields = wf_crm_get_fields();
        $sets = wf_crm_get_fields('sets');
        $class = 'civi-icon';
        list(, $c, $ent, $n, $table, $name) = explode('_', $component['form_key'], 6);
        $field = wf_crm_aval($fields, $table . '_' . $name, array(
          'type' => 'fieldset',
        ));

        // Don't allow CiviCRM fields to be cloned
        $row['data'][7]['data'] = '';
        if ($component['type'] == 'fieldset' && $ent == 'contact') {
          $title = t('Contact !num', array(
            '!num' => $c,
          ));
          $type = t('Fieldset for !contact', array(
            '!contact' => wf_crm_contact_label($c, $data),
          ));
          $row['data'][7]['data'] = l(t('Clone Contact'), "node/{$node->nid}/webform/components/{$cid}/clone", array(
            'attributes' => array(
              'title' => t('Add a new contact to the form with the same fields and settings'),
            ),
          ));
          $class .= ' fieldset';
        }
        elseif ($ent == 'contact') {
          $field_type = $table == 'contact' || $table == 'other' ? $field['name'] : $sets[$table]['label'];
          $title = t('!type Field for !contact', array(
            '!contact' => wf_crm_contact_label($c, $data),
            '!type' => $field_type,
          ));
        }
        elseif ($component['type'] != 'fieldset') {
          $title = t('Field for !type', array(
            '!type' => $sets[$table]['label'],
          ));
        }
        else {
          $title = $type = t('Fieldset for !type !num', array(
            '!type' => $sets[$ent]['label'],
            '!num' => $c,
          ));
        }
        if ($table === 'address' && $component['type'] === 'textfield') {
          if ($name === 'state_province_id' || $name === 'county_id') {
            $type = t('AJAX Select');
          }
        }
        if ($component['type'] == 'civicrm_contact' || $component['type'] == 'fieldset') {
          $class .= ' ' . ($ent == 'contact' ? $node->webform_civicrm['data']['contact'][$c]['contact'][1]['contact_type'] : $ent);
        }
        if ($component['type'] == 'select') {
          $type .= ' (' . (empty($component['extra']['civicrm_live_options']) ? t('static') : t('live')) . ')';
        }

        // Show defaults with labels instead of keys
        if ($component['type'] == 'civicrm_contact') {
          if ($component['extra']['default'] == 'contact_id') {
            $row['data'][3]['data'] = check_plain(wf_crm_display_name($component['extra']['default_contact_id']));
          }
          elseif ($component['extra']['default'] == 'user') {
            $row['data'][3]['data'] = t('Current User');
          }
          elseif ($component['extra']['default'] == 'auto') {
            $row['data'][3]['data'] = t('Auto - From Filters');
          }
          elseif ($component['extra']['default'] == 'relationship' && $component['extra']['default_relationship']) {
            $relationship_labels = array();
            $relationship_types = wf_crm_get_relationship_types();
            foreach ($component['extra']['default_relationship'] as $default_relationship) {
              list($rid, $rtype) = explode('_', $default_relationship);
              $label_type = $rtype == 'b' ? 'b_a' : 'a_b';
              $relationship_labels[] = $relationship_types[$rid]["label_{$label_type}"] . ' ' . wf_crm_contact_label($component['extra']['default_relationship_to'], $data);
            }
            $row['data'][3]['data'] = implode(' ' . ts('or') . ' ', $relationship_labels);
          }
        }
        elseif (isset($component['value']) && strlen($component['value']) && ($field['type'] == 'select' || !empty($field['expose_list']))) {
          if ($component['type'] == 'select') {
            $items = wf_crm_str2array($component['extra']['items']);
          }
          else {
            $items = wf_crm_field_options($component, 'components_form', $node->webform_civicrm['data']);
          }
          $val = '';
          foreach (explode(',', $component['value']) as $v) {
            if (isset($items[trim($v)])) {
              $val .= ($val ? ', ' : '') . $items[trim($v)];
            }
          }
          $row['data'][3]['data'] = $val;
        }

        // Contribution page - link to civicrm config form instead of component edit form
        if ($name == 'contribution_page_id') {
          $type = t('CiviCRM Billing Fields');
          $class .= ' contribution';
          $row['data'][6]['data'] = l(t('Configure'), 'civicrm/admin/contribute/settings', array(
            'query' => array(
              'reset' => 1,
              'action' => 'update',
              'id' => $component['value'],
            ),
            'attributes' => array(
              'title' => t('Edit Contribution Page in CiviCRM'),
            ),
          ));
        }
        $type = '<span class="' . $class . '"> </span>' . $type;
        $row['data'][2]['title'] = $title;
      }
    }
  }
}