You are here

function _webform_render_civicrm_contact in Webform CiviCRM Integration 7.3

Same name and namespace in other branches
  1. 7.5 includes/contact_component.inc \_webform_render_civicrm_contact()
  2. 7.4 includes/contact_component.inc \_webform_render_civicrm_contact()

Implements _webform_render_component().

File

./contact_component.inc, line 276

Code

function _webform_render_civicrm_contact($component, $value = NULL, $filter = TRUE) {
  $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  civicrm_initialize();
  list(, $c, ) = explode('_', $component['form_key'], 3);
  $js = '';
  $element = array(
    '#type' => $component['extra']['widget'] == 'autocomplete' ? 'textfield' : $component['extra']['widget'],
    '#weight' => $component['weight'],
    '#attributes' => $component['extra']['attributes'],
  );
  if ($component['extra']['widget'] != 'hidden') {
    $element += array(
      '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
      '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
      '#required' => $component['mandatory'],
      '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
      '#translatable' => array(
        'title',
        'description',
      ),
      '#theme_wrappers' => array(
        'webform_element',
      ),
    );

    // See if there are any fields to fetch via ajax
    $other_fields = wf_crm_contact_fields($node, $c);
    foreach ($component['extra']['hide_fields'] as $h) {
      unset($other_fields[$h]);
    }
    $ajax = $other_fields ? 'true' : 'false';
  }
  $callback_path = '"' . url('webform-civicrm/js/' . $node->nid . '-' . $component['cid'], array(
    'alias' => TRUE,
  )) . '"';

  // Render component based on type
  switch ($component['extra']['widget']) {
    case 'autocomplete':
      if ($node && isset($node->webform_civicrm)) {
        $element['#attached']['js'][] = wf_crm_tokeninput_path();
        $element['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/token-input.css';
        $settings = '{
          queryParam: "str",
          hintText: "' . $component['extra']['search_prompt'] . '",
          noResultsText: "' . $component['extra']['none_prompt'] . '",
          searchingText: "' . t('Searching...') . '",
          tokenLimit: 1,
          onAdd: function(item) {wfCivi.existingSelect(' . $c . ', ' . $node->nid . ', ' . $callback_path . ', toHide, item.id,' . $ajax . ');},
          onDelete: function(item) {wfCivi.existingSelect(' . $c . ', ' . $node->nid . ', ' . $callback_path . ', toHide, "", false);},
          prePopulate: prep
        }';
        $js = "var prep = wfCivi.existingInit(field, {$c}, {$node->nid}, {$callback_path}, toHide);\n                field.tokenInput({$callback_path}, {$settings});";
      }
      break;
    case 'select':
      $element['#options'] = array();
      if ($node && isset($node->webform_civicrm)) {
        $filters = wf_crm_search_filters($node, $component);
        $element['#options'] = wf_crm_contact_search($node, $component, $filters);
        $js = "field.change(function() {\n                  wfCivi.existingSelect({$c}, {$node->nid}, {$callback_path}, toHide, \$(this).val(), {$ajax});\n                });\n                wfCivi.existingInit(field, {$c}, {$node->nid}, {$callback_path}, toHide);";
      }

      // Display empty option unless there are no results
      if (!$component['extra']['allow_create'] || count($element['#options']) > 1) {
        $element['#empty_option'] = $component['extra'][$element['#options'] ? 'search_prompt' : 'none_prompt'];
      }
      break;
    case 'hidden':
      if (!empty($value[0])) {
        $element['#value'] = $value[0];
      }
      if ($node && isset($node->webform_civicrm)) {
        if ($component['extra']['show_hidden_contact']) {
          $element['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/token-input.css';
        }
        if ($component['extra']['hide_fields']) {
          $js = "wfCivi.existingInit(field, {$c}, {$node->nid}, {$callback_path}, toHide);";
        }
        $element['#theme_wrappers'] = array(
          'static_contact_element',
        );
      }
  }

  // Add inline javascript
  if ($js) {
    $js = '
      (function ($, D) {
        D.behaviors.webform_civicrm_' . $node->nid . '_' . $component['cid'] . ' = {
          attach: function (context) {
            var toHide = ' . json_encode(array_values($component['extra']['hide_fields'])) . ';
            var field = $(\'#webform-client-form-' . $component['nid'] . ' :input.civicrm-enabled[name*="' . $component['form_key'] . '"]\', context);
            ' . $js . '
          }
        };
      })(jQuery, Drupal);';
    $element['#attached']['js'][$js] = array(
      'type' => 'inline',
    );
  }

  // Enforce uniqueness.
  if ($component['extra']['unique']) {
    $element['#element_validate'][] = 'webform_validate_unique';
  }
  if ($cid = wf_crm_aval($value, 0)) {
    $element['#default_value'] = $cid;
  }
  wf_crm_fill_contact_value($node, $component, $element);
  return $element;
}