You are here

function wf_crm_ajax in Webform CiviCRM Integration 7.3

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

Drupal page callback to serve AJAX requests.

Parameters

$key: Type of AJAX request

$input: User input

Prints json output

1 string reference to 'wf_crm_ajax'
webform_civicrm_menu in ./webform_civicrm.module
Implements hook_menu().

File

./contact_component.inc, line 551

Code

function wf_crm_ajax($key, $input = '') {
  civicrm_initialize();

  // Populate state/prov lists on the fly
  if ($key == 'state_province') {
    if (!$input || intval($input) != $input && $input != 'default') {
      drupal_json_output(array(
        '' => t('- first choose a country -'),
      ));
      exit;
    }
    drupal_json_output(wf_crm_get_options('state_province', $input));
    exit;
  }
  elseif ($key == 'county') {
    if (strpos($input, '-')) {
      list($state, $country) = explode('-', $input);
      $state_id = wf_crm_state_abbr($state, 'id', $country);
      drupal_json_output(wf_crm_get_options('county', $state_id));
      exit;
    }
  }
  elseif (strpos($key, '-')) {
    if (empty($_GET['str']) && (empty($_GET['load']) || empty($_GET['cid']))) {
      exit;
    }
    list($nid, $fid) = explode('-', $key, 2);
    $node = node_load($nid);
    if (!wf_crm_autocomplete_access($node, $fid)) {
      return drupal_access_denied();
    }
    $component = $node->webform['components'][$fid];
    $filters = wf_crm_search_filters($node, $component);

    // Bypass filters when choosing contact on component edit form
    if (!empty($_GET['admin']) && wf_crm_admin_access($node)) {
      $filters = array(
        'check_permissions' => 1,
        'is_deleted' => 0,
      );
      $component['extra']['allow_create'] = 0;
    }

    // Autocomplete contact names
    if (!empty($_GET['str'])) {
      if ($str = trim($_GET['str'])) {
        drupal_json_output(wf_crm_contact_search($node, $component, $filters, $str));
      }
      exit;
    }

    // Load contact by id
    $data = array();
    if ($name = wf_crm_contact_access($component, $filters, $_GET['cid'])) {
      if ($_GET['load'] == 'name') {
        if ($_GET['cid'][0] === '-') {

          // HTML hack to get prompt to show up different than search results
          $data = '<em><i>' . $component['extra']['none_prompt'] . '</i></em>';
        }
        else {
          $data = $name;
        }
      }

      // Fetch entire contact to populate form via ajax
      if ($_GET['load'] == 'full') {
        $sp = CRM_Core_DAO::VALUE_SEPARATOR;
        module_load_include('inc', 'webform_civicrm', 'webform_civicrm_forms');
        $enabled = wf_crm_enabled_fields($node);
        list(, $c, ) = explode('_', $component['form_key'], 3);
        $cids = array();
        foreach ($_GET as $k => $v) {
          if (substr($k, 0, 3) == 'cid' && $v && is_numeric($v)) {
            $cids[substr($k, 3)] = (int) $v;
          }
          $cids[$c] = (int) $_GET['cid'];
        }
        $contact = wf_crm_contact_get($node, $enabled, $c, $cids, $component['extra']['hide_fields']);

        // Flatten data into simple form keys & values
        foreach ($enabled as $fid => $f) {
          list(, $i, $ent, $n, $table, $field) = explode('_', $fid, 6);
          if ($i == $c && $ent == 'contact' && isset($contact[$table][$n][$field])) {
            $type = $table == 'contact' && strpos($field, 'name') ? 'name' : $table;

            // Exclude blank and hidden fields to save bandwidth
            if ($contact[$table][$n][$field] !== '' && $contact[$table][$n][$field] !== array() && !in_array($type, $component['extra']['hide_fields'])) {
              $val = $contact[$table][$n][$field];

              // Explode multivalue strings
              if (is_string($val) && strpos($val, $sp) !== FALSE) {
                $val = explode($sp, trim($val, $sp));
              }
              $data[str_replace('_', '-', $fid)] = $val;
            }
          }
        }
      }
    }
    drupal_json_output($data);
    exit;
  }
  drupal_access_denied();
}