You are here

function getlocations_blocks_country_autocomplete in Get Locations 7.2

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

autocomplete for country

Parameters

string $string:

Return value

Returns country names

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

File

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

Code

function getlocations_blocks_country_autocomplete($string = '') {
  $settings = getlocations_blocks_get_var();
  $matches = array();
  if ($string) {
    if ($settings['country_full']) {
      $countries = getlocations_get_countries_list();
      foreach ($countries as $code => $country) {
        $s = drupal_strtolower($string);
        $c = drupal_strtolower($country);
        preg_match_all("/^{$s}/", $c, $m);
        if (count($m[0])) {
          $matches[$code] = $country;
        }
      }
    }
    else {

      // need to make a list of existing countries, these are stored as 2 letter codes so need to be converted
      $query = db_select('getlocations_fields', 'f');
      $query
        ->fields('f', array(
        'country',
      ));
      if ($settings['country_filter'] && $settings['country_filter'] == 'field_name' && $settings['country_filter_fieldname']) {

        #        $query->join('getlocations_fields_entities', 'e', 'f.glid=e.glid');
        $query
          ->condition('f.field_name', $settings['country_filter_fieldname']);
      }
      elseif ($settings['country_filter'] && $settings['country_filter'] == 'bundle' && $settings['country_filter_bundle']) {

        #        $query->join('getlocations_fields_entities', 'e', 'f.glid=e.glid');
        $query
          ->join('field_config_instance', 'i', 'f.field_name=i.field_name');
        $query
          ->condition('i.bundle', $settings['country_filter_bundle']);
      }
      $result = $query
        ->execute();
      foreach ($result as $row) {
        $country = getlocations_get_country_name($row->country);
        $s = drupal_strtolower($string);
        $c = drupal_strtolower($country);
        preg_match_all("/^{$s}/", $c, $m);
        if (count($m[0])) {
          $matches[$row->country] = $country;
        }
      }
    }
  }
  drupal_json_output($matches);
}