You are here

function getlocations_blocks_city_autocomplete in Get Locations 7

Same name and namespace in other branches
  1. 7.2 modules/getlocations_blocks/getlocations_blocks.module \getlocations_blocks_city_autocomplete()

autocomplete for city

Parameters

string $string:

Return value

array cities

1 string reference to 'getlocations_blocks_city_autocomplete'
getlocations_blocks_menu in modules/getlocations_blocks/getlocations_blocks.module
Implements hook_menu().

File

modules/getlocations_blocks/getlocations_blocks.module, line 701
getlocations_blocks.module @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

function getlocations_blocks_city_autocomplete($string = '') {
  $settings = getlocations_blocks_get_var();
  $matches = array();
  if ($string) {
    $query = db_select('getlocations_fields', 'f');
    $query
      ->fields('f', array(
      'city',
    ));
    if ($settings['city_filter'] && $settings['city_filter'] == 'field_name' && $settings['city_filter_fieldname']) {
      $query
        ->join('getlocations_fields_entities', 'e', 'f.glid=e.glid');
      $query
        ->where("LOWER(f.city) LIKE LOWER(:st)", array(
        ':st' => $string . '%',
      ));
      $query
        ->condition('e.field_name', $settings['city_filter_fieldname']);
    }
    elseif ($settings['city_filter'] && $settings['city_filter'] == 'bundle' && $settings['city_filter_bundle']) {
      $query
        ->join('getlocations_fields_entities', 'e', 'f.glid=e.glid');
      $query
        ->join('field_config_instance', 'i', 'e.field_name=i.field_name');
      $query
        ->where("LOWER(f.city) LIKE LOWER(:st)", array(
        ':st' => $string . '%',
      ));
      $query
        ->condition('i.bundle', $settings['city_filter_bundle']);
    }
    else {
      $query
        ->where("LOWER(f.city) LIKE LOWER(:st)", array(
        ':st' => $string . '%',
      ));
    }
    $query
      ->range(0, 15);
    $result = $query
      ->execute();
    foreach ($result as $row) {
      $matches[$row->city] = check_plain($row->city);
    }
  }
  drupal_json_output($matches);
}