<?php
defined('COT_CODE') or die('Wrong URL');

if(cot_plugin_active('locationselector')) {
  require_once cot_incfile('locationselector', 'plug');

  $return = array();

  $term = cot_import('term', 'G', 'TXT');

  if(!empty($term)) {
    if($a == 'country') {
      foreach($cot_countries as $key => $val) {
        if(!(stripos($val, $term) === false)) {
          //$val
          $return[] = array(
           'id' => $key,
           'value' => $val,
           'hasCities' => ($db->query("SELECT COUNT(*) FROM $db_ls_cities WHERE city_country='".$key."' LIMIT 1")->fetchColumn() > 0 ? true : false)
          );
        }
      }
    }
    if($a == 'city') {
      $geo_search = $db->query("SELECT * FROM $db_ls_cities WHERE city_name LIKE '%".$term."%'")->fetchAll();
      foreach($geo_search as $city) {
        $return[] = array(
           'id' => $city['city_id'],
           'value' => $city['city_name'],
           'label' => $city['city_name'],
           'country_id' => $city['city_country'],
           'country_name' => $cot_countries[$city['city_country']]
        );
      }
    }
  }
}

?>
