/** * Retrieves cities listing using GeoNames Service * @param int $cid The id of the country to retrieve cities for * Returns a JSON response */ function updateCities($cid = 0) { $this->template = ""; $this->auto_render = FALSE; $cities = 0; // Get country ISO code from DB $country = ORM::factory('country', (int) $cid); if ($country->loaded == true) { $iso = $country->iso; // GeoNames WebService URL + Country ISO Code $geonames_url = "http://ws.geonames.org/search?country=" . $iso . "&featureCode=PPL&featureCode=PPLA&featureCode=PPLC"; // Use Curl $ch = curl_init(); $timeout = 20; curl_setopt($ch, CURLOPT_URL, $geonames_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $xmlstr = curl_exec($ch); $err = curl_errno($ch); curl_close($ch); // $xmlstr = file_get_contents($geonames_url); // No Timeout Error, so proceed if ($err == 0) { // Reset All Countries City Counts to Zero $countries = ORM::factory('country')->find_all(); foreach ($countries as $country) { $country->cities = 0; $country->save(); } // Delete currently loaded cities ORM::factory('city')->delete_all(); $sitemap = new SimpleXMLElement($xmlstr); foreach ($sitemap as $city) { if ($city->name && $city->lng && $city->lat) { $newcity = new City_Model(); $newcity->country_id = $cid; $newcity->city = mysql_real_escape_string($city->name); $newcity->city_lat = mysql_real_escape_string($city->lat); $newcity->city_lon = mysql_real_escape_string($city->lng); $newcity->save(); $cities++; } } // Update Country With City Count $country = ORM::factory('country', $cid); $country->cities = $cities; $country->save(); echo json_encode(array("status" => "success", "response" => "{$cities} Cities Loaded!")); } else { echo json_encode(array("status" => "error", "response" => "0 Cities Loaded. Geonames Timeout Error!")); } } else { echo json_encode(array("status" => "error", "response" => "0 Cities Loaded. Country Not Found!")); } }
/** * Retrieves cities listing using GeoNames Service * @param int $cid The id of the country to retrieve cities for * Returns a JSON response */ function updateCities($cid = 0) { $this->template = ""; $this->auto_render = FALSE; $cities = 0; // Get country ISO code from DB $country = ORM::factory('country', (int) $cid); if ($country->loaded == true) { $iso = $country->iso; // GeoNames WebService URL + Country ISO Code $geonames_url = "http://ws.geonames.org/search?country=" . $iso . "&featureCode=PPL&featureCode=PPLA&featureCode=PPLC"; // Grabbing GeoNames requires cURL so we will check for that here. if (!function_exists('curl_exec')) { throw new Kohana_Exception('settings.updateCities.cURL_not_installed'); return false; } // Use Curl $ch = curl_init(); $timeout = 20; curl_setopt($ch, CURLOPT_URL, $geonames_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $xmlstr = curl_exec($ch); $err = curl_errno($ch); curl_close($ch); // $xmlstr = file_get_contents($geonames_url); // No Timeout Error, so proceed if ($err == 0) { // Reset All Countries City Counts to Zero $countries = ORM::factory('country')->find_all(); foreach ($countries as $country) { $country->cities = 0; $country->save(); } // Delete currently loaded cities ORM::factory('city')->delete_all(); $sitemap = new SimpleXMLElement($xmlstr); foreach ($sitemap as $city) { if ($city->name && $city->lng && $city->lat) { $newcity = new City_Model(); $newcity->country_id = $cid; $newcity->city = mysql_real_escape_string($city->name); $newcity->city_lat = mysql_real_escape_string($city->lat); $newcity->city_lon = mysql_real_escape_string($city->lng); $newcity->save(); $cities++; } } // Update Country With City Count $country = ORM::factory('country', $cid); $country->cities = $cities; $country->save(); echo json_encode(array("status" => "success", "response" => "{$cities} " . Kohana::lang('ui_admin.cities_loaded'))); } else { echo json_encode(array("status" => "error", "response" => "0 " . Kohana::lang('ui_admin.cities_loaded') . ". " . Kohana::lang('ui_admin.geonames_timeout'))); } } else { echo json_encode(array("status" => "error", "response" => "0 " . Kohana::lang('ui_admin.cities_loaded') . ". " . Kohana::lang('ui_admin.country_not_found'))); } }
/** * Retrieves Cities * @param int $country_id Id of the country whose cities are to be fetched * @return array */ private function _get_cities($country_id) { // Get the cities $cities = Kohana::config('settings.multi_country') ? City_Model::get_all() : ORM::factory('country', $country_id)->get_cities(); $city_select = array('' => Kohana::lang('ui_main.reports_select_city')); foreach ($cities as $city) { $city_select[$city->city_lon . "," . $city->city_lat] = $city->city; } return $city_select; }