From 7e640aad8f8b36ec7bf09d85d794f82521a3b81e Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Jacob?= Date: Sun, 19 Sep 2010 11:53:26 +0200 Subject: [PATCH] Moves geocoder into approrpiate classes. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Jacob --- classes/address.php | 2 - classes/geocoder.php | 97 ++++++++++++++++++++++ .../geocoding.inc.php => classes/gmapsgeocoder.php | 74 ----------------- include/webservices/manageurs.server.inc.php | 1 - modules/admin.php | 2 - modules/payment/money/paypal.inc.php | 2 - modules/search.php | 1 - plugins/function.display_address.php | 1 - 8 files changed, 97 insertions(+), 83 deletions(-) create mode 100644 classes/geocoder.php rename include/geocoding.inc.php => classes/gmapsgeocoder.php (84%) diff --git a/classes/address.php b/classes/address.php index 16ecd80..ca1a384 100644 --- a/classes/address.php +++ b/classes/address.php @@ -150,7 +150,6 @@ class Address return true; } - require_once 'geocoding.inc.php'; if ($format['requireGeocoding'] || $this->changed == 1) { $gmapsGeocoder = new GMapsGeocoder(); $gmapsGeocoder->getGeocodedAddress($this); @@ -257,7 +256,6 @@ class Address $this->format(); if (!$this->isEmpty()) { - require_once 'geocoding.inc.php'; foreach ($areas as $area) { Geocoder::getAreaId($this, $area); } diff --git a/classes/geocoder.php b/classes/geocoder.php new file mode 100644 index 0000000..357891e --- /dev/null +++ b/classes/geocoder.php @@ -0,0 +1,97 @@ + 'geoloc_administrativeareas', + 'subAdministrativeArea' => 'geoloc_subadministrativeareas', + 'locality' => 'geoloc_localities', + ); + + $areaName = $area . 'Name'; + $areaId = $area . 'Id'; + if (!is_null($address->$areaName) && isset($databases[$area])) { + $res = XDB::query('SELECT id + FROM ' . $databases[$area] . ' + WHERE name = {?}', + $address->$areaName); + if ($res->numRows() == 0) { + XDB::execute('INSERT INTO ' . $databases[$area] . ' (name, country) + VALUES ({?}, {?})', + $address->$areaName, $address->countryId); + $address->$areaId = XDB::insertId(); + } else { + $address->$areaId = $res->fetchOneCell(); + } + } elseif (empty($address->$areaId)) { + $address->$areaId = null; + } + } + + // Returns the part of the text preceeding the line with the postal code + // and the city name, within the limit of $limit number of lines. + static public function getFirstLines($text, $postalCode, $limit) + { + $textArray = explode("\n", $text); + for ($i = 0; $i < count($textArray); ++$i) { + if ($i > $limit || strpos($textLine, $postalCode) !== false) { + $limit = $i; + break; + } + } + return implode("\n", array_slice($textArray, 0, $limit)); + } + + // Returns the number of non geocoded addresses for a user. + static public function countNonGeocoded($pid, $jobid = null, $type = Address::LINK_PROFILE) + { + $where = array(); + if (!is_null($pid)) { + $where[] = XDB::format('pid = {?}', $pid); + } + if (!is_null($jobid)) { + $where[] = XDB::format('jobid = {?}', $jobid); + } + $where[] = XDB::format('FIND_IN_SET({?}, type) AND accuracy = 0', $type); + $res = XDB::query('SELECT COUNT(*) + FROM profile_addresses + WHERE ' . implode(' AND ', $where), + $pid); + return $res->fetchOneCell(); + } +} + +// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: +?> diff --git a/include/geocoding.inc.php b/classes/gmapsgeocoder.php similarity index 84% rename from include/geocoding.inc.php rename to classes/gmapsgeocoder.php index 99c571c..8347026 100644 --- a/include/geocoding.inc.php +++ b/classes/gmapsgeocoder.php @@ -19,80 +19,6 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ***************************************************************************/ -// Interface for an address geocoder. It provides support for transforming a free -// form address into a fully structured one. -abstract class Geocoder { - // Geocodes @p the address, and returns the corresponding updated address. - // Unknown key-value pairs available in the input map are retained as-is. - abstract public function getGeocodedAddress(Address &$address); - - // Cleans the address from its geocoded data - abstract public function stripGeocodingFromAddress(Address &$address); - - // Updates geoloc_administrativeareas, geoloc_subadministrativeareas and - // geoloc_localities databases with new geocoded data and returns the - // corresponding id. - static public function getAreaId(Address &$address, $area) - { - static $databases = array( - 'administrativeArea' => 'geoloc_administrativeareas', - 'subAdministrativeArea' => 'geoloc_subadministrativeareas', - 'locality' => 'geoloc_localities', - ); - - $areaName = $area . 'Name'; - $areaId = $area . 'Id'; - if (!is_null($address->$areaName) && isset($databases[$area])) { - $res = XDB::query('SELECT id - FROM ' . $databases[$area] . ' - WHERE name = {?}', - $address->$areaName); - if ($res->numRows() == 0) { - XDB::execute('INSERT INTO ' . $databases[$area] . ' (name, country) - VALUES ({?}, {?})', - $address->$areaName, $address->countryId); - $address->$areaId = XDB::insertId(); - } else { - $address->$areaId = $res->fetchOneCell(); - } - } elseif (empty($address->$areaId)) { - $address->$areaId = null; - } - } - - // Returns the part of the text preceeding the line with the postal code - // and the city name, within the limit of $limit number of lines. - static public function getFirstLines($text, $postalCode, $limit) - { - $textArray = explode("\n", $text); - for ($i = 0; $i < count($textArray); ++$i) { - if ($i > $limit || strpos($textLine, $postalCode) !== false) { - $limit = $i; - break; - } - } - return implode("\n", array_slice($textArray, 0, $limit)); - } - - // Returns the number of non geocoded addresses for a user. - static public function countNonGeocoded($pid, $jobid = null, $type = Address::LINK_PROFILE) - { - $where = array(); - if (!is_null($pid)) { - $where[] = XDB::format('pid = {?}', $pid); - } - if (!is_null($jobid)) { - $where[] = XDB::format('jobid = {?}', $jobid); - } - $where[] = XDB::format('FIND_IN_SET({?}, type) AND accuracy = 0', $type); - $res = XDB::query('SELECT COUNT(*) - FROM profile_addresses - WHERE ' . implode(' AND ', $where), - $pid); - return $res->fetchOneCell(); - } -} - // Implementation of a Geocoder using the Google Maps API. Please refer to // the following links for details: // http://code.google.com/apis/maps/documentation/services.html#Geocoding diff --git a/include/webservices/manageurs.server.inc.php b/include/webservices/manageurs.server.inc.php index 0373eb8..061a1df 100644 --- a/include/webservices/manageurs.server.inc.php +++ b/include/webservices/manageurs.server.inc.php @@ -15,7 +15,6 @@ $error_key = "You didn't provide me with a valid cipher key..."; // IF YOU HAVE TO MAKE SOME MODIFICATION, FIRST CONTACT: // admin@manageurs.com function get_annuaire_infos($method, $params) { - require 'geocoding.inc.php'; global $error_mat, $error_key, $globals; // Password verification. diff --git a/modules/admin.php b/modules/admin.php index 122492b..fe21416 100644 --- a/modules/admin.php +++ b/modules/admin.php @@ -1435,8 +1435,6 @@ class AdminModule extends PLModule $page->trigSuccess("L'entreprise a bien été remplacée."); } else { - require_once 'geocoding.inc.php'; - $gmapsGeocoder = new GMapsGeocoder(); $address = array('text' => Env::t('address')); $address = $gmapsGeocoder->getGeocodedAddress($address); diff --git a/modules/payment/money/paypal.inc.php b/modules/payment/money/paypal.inc.php index be1f2d0..e7ae70d 100644 --- a/modules/payment/money/paypal.inc.php +++ b/modules/payment/money/paypal.inc.php @@ -42,8 +42,6 @@ class PayPal // the user must come back on the site. global $globals, $platal; - require_once 'geocoding.inc.php'; - $this->urlform = 'https://' . $globals->money->paypal_site . '/cgi-bin/webscr'; $user = S::user(); $name = $user->lastName(); diff --git a/modules/search.php b/modules/search.php index b55348e..6ffb45c 100644 --- a/modules/search.php +++ b/modules/search.php @@ -150,7 +150,6 @@ class SearchModule extends PLModule function handler_advanced(&$page, $model = null, $byletter = null) { global $globals; - require_once 'geocoding.inc.php'; $page->assign('advanced',1); $page->addJsLink('jquery.autocomplete.js'); diff --git a/plugins/function.display_address.php b/plugins/function.display_address.php index 4e51c99..85a9210 100644 --- a/plugins/function.display_address.php +++ b/plugins/function.display_address.php @@ -38,7 +38,6 @@ function display_address_isIdentity($idt, $value, $test_reverse = true) function smarty_function_display_address($param, &$smarty) { - require_once('geocoding.inc.php'); $adr = $param['adr']; $txtad = $adr->text; if (!$txtad) { -- 2.1.4