2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 // Interface for an address geocoder. It provides support for transforming a
23 // free form address into a fully structured one.
24 abstract class Geocoder
{
25 // Geocodes @p the address, and returns the corresponding updated address.
26 // Unknown key-value pairs available in the input map are retained as-is.
27 abstract public function getGeocodedAddress(Address
&$address);
29 // Cleans the address from its geocoded data
30 abstract public function stripGeocodingFromAddress(Address
&$address);
32 // Updates geoloc_administrativeareas, geoloc_subadministrativeareas and
33 // geoloc_localities databases with new geocoded data and returns the
35 static public function getAreaId(Address
&$address, $area)
37 static $databases = array(
38 'administrativeArea' => 'geoloc_administrativeareas',
39 'subAdministrativeArea' => 'geoloc_subadministrativeareas',
40 'locality' => 'geoloc_localities',
43 $areaName = $area . 'Name';
44 $areaId = $area . 'Id';
45 if (!is_null($address->$areaName) && isset($databases[$area])) {
46 $res = XDB
::query('SELECT id
47 FROM ' . $databases[$area] . '
50 if ($res->numRows() == 0) {
51 XDB
::execute('INSERT INTO ' . $databases[$area] . ' (name, country)
53 $address->$areaName, $address->countryId
);
54 $address->$areaId = XDB
::insertId();
56 $address->$areaId = $res->fetchOneCell();
58 } elseif (empty($address->$areaId)) {
59 $address->$areaId = null
;
63 // Returns the part of the text preceeding the line with the postal code
64 // and the city name, within the limit of $limit number of lines.
65 static public function getFirstLines($text, $postalCode, $limit)
67 $textArray = explode("\n", $text);
68 for ($i = 0; $i < count($textArray); ++
$i) {
69 if ($i > $limit ||
strpos($textLine, $postalCode) !== false
) {
74 return implode("\n", array_slice($textArray, 0, $limit));
77 // Returns the number of non geocoded addresses for a user.
78 static public function countNonGeocoded($pid, $jobid = null
, $type = Address
::LINK_PROFILE
)
82 $where[] = XDB
::format('pid = {?}', $pid);
84 if (!is_null($jobid)) {
85 $where[] = XDB
::format('jobid = {?}', $jobid);
87 $where[] = XDB
::format('FIND_IN_SET({?}, type) AND accuracy = 0', $type);
88 $res = XDB
::query('SELECT COUNT(*)
89 FROM profile_addresses
90 WHERE ' . implode(' AND ', $where),
92 return $res->fetchOneCell();
96 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: