Moving to GitHub.
[platal.git] / classes / geocoder.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2014 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
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);
28
29 // Cleans the address from its geocoded data
30 abstract public function stripGeocodingFromAddress(Address $address);
31
32 // Updates profile_addresses_components_enum, if needed, with new
33 // geocoded data and returns the corresponding id.
34 static public function getComponentId(array $component)
35 {
36 $where = '';
37 foreach ($component['types'] as $type) {
38 $where .= XDB::format(' AND FIND_IN_SET({?}, types)', $type);
39 }
40
41 $id = XDB::fetchOneCell('SELECT id
42 FROM profile_addresses_components_enum
43 WHERE short_name = {?} AND long_name = {?}' . $where,
44 $component['short_name'], $component['long_name']);
45 if (is_null($id)) {
46 XDB::execute('INSERT INTO profile_addresses_components_enum (short_name, long_name, types)
47 VALUES ({?}, {?}, {?})',
48 $component['short_name'], $component['long_name'], implode(',', $component['types']));
49 $id = XDB::insertId();
50 }
51 return $id;
52 }
53
54 // Returns the part of the text preceeding the line with the postal code
55 // and the city name, within the limit of $limit number of lines.
56 static public function getFirstLines($text, $postalCode, $limit)
57 {
58 $text = str_replace("\r", '', $text);
59 $textArray = explode("\n", $text);
60 $linesNb = $limit;
61
62 for ($i = 0; $i < count($textArray); ++$i) {
63 if ($i > $limit || strpos($textArray[$i], $postalCode) !== false) {
64 $linesNb = $i;
65 break;
66 }
67 }
68 $firstLines = implode("\n", array_slice($textArray, 0, $linesNb));
69
70 // Adds empty lines to complete the $limit lines required.
71 for (; $i < $limit; ++$i) {
72 $firstLines .= "\n";
73 }
74 return $firstLines;
75 }
76
77 // Returns the number of non geocoded addresses for a profile.
78 static public function countNonGeocoded($pid)
79 {
80 $count = XDB::fetchOneCell('SELECT COUNT(*)
81 FROM profile_addresses AS pa
82 WHERE pid = {?} AND type = \'home\'
83 AND NOT EXISTS (SELECT *
84 FROM profile_addresses_components AS pc
85 WHERE pa.pid = pc.pid AND pa.jobid = pc.jobid AND pa.groupid = pc.groupid
86 AND pa.type = pc.type AND pa.id = pc.id)',
87 $pid);
88 return $count;
89 }
90 }
91
92 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
93 ?>