You are here

function webform_civicrm_js_options in Webform CiviCRM Integration 7

Same name and namespace in other branches
  1. 6.2 webform_civicrm_utils.inc \webform_civicrm_js_options()
  2. 6 webform_civicrm_utils.inc \webform_civicrm_js_options()
  3. 7.2 webform_civicrm_utils.inc \webform_civicrm_js_options()

Return CiviCRM options via AJAX request

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

File

./webform_civicrm_utils.inc, line 308
Webform CiviCRM module's utility functions.

Code

function webform_civicrm_js_options($field_key, $str = '') {
  if (!user_access('access CiviCRM')) {
    return drupal_access_denied();
  }
  civicrm_initialize();

  // Autocomplete callback for tags
  if ($field_key == 'tags') {
    if (!$str) {
      exit;
    }

    // Handle a comma-separated list of tags.
    $existing = drupal_explode_tags($str);
    $last_str = trim(array_pop($existing));
    $matches = array();
    $prefix = count($existing) ? implode(', ', $existing) . ', ' : '';
    $params = array(
      1 => array(
        '%' . $last_str . '%',
        'String',
      ),
    );
    $sql = "SELECT name FROM civicrm_tag WHERE is_selectable = 1 AND used_for = 'civicrm_contact' AND name LIKE %1 LIMIT 0, 12";
    $dao =& CRM_Core_DAO::executeQuery($sql, $params);
    while ($dao
      ->fetch()) {
      $matches[$prefix . $dao->name] = check_plain($dao->name);
    }
    exit(drupal_json_output($matches));
  }

  // Get option lists
  $lists = webform_civicrm_get_fields('lists');
  if (array_key_exists($field_key, $lists)) {
    print webform_civicrm_get_options($lists[$field_key]);
    exit;
  }
  return drupal_not_found();
}