Corrects birthdate when birthdate is null and birthdate_ref is correct.
[platal.git] / classes / geocoder.php
CommitLineData
7e640aad
SJ
1<?php
2/***************************************************************************
c441aabe 3 * Copyright (C) 2003-2014 Polytechnique.org *
7e640aad
SJ
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.
24abstract 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.
26ba053e 27 abstract public function getGeocodedAddress(Address $address);
7e640aad
SJ
28
29 // Cleans the address from its geocoded data
26ba053e 30 abstract public function stripGeocodingFromAddress(Address $address);
7e640aad 31
0f5f1b70
SJ
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)
7e640aad 35 {
5a47c10b 36 $where = '';
0f5f1b70 37 foreach ($component['types'] as $type) {
5a47c10b 38 $where .= XDB::format(' AND FIND_IN_SET({?}, types)', $type);
0f5f1b70 39 }
9895bd60 40
0f5f1b70
SJ
41 $id = XDB::fetchOneCell('SELECT id
42 FROM profile_addresses_components_enum
5a47c10b 43 WHERE short_name = {?} AND long_name = {?}' . $where,
0f5f1b70
SJ
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();
7e640aad 50 }
0f5f1b70 51 return $id;
7e640aad
SJ
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 {
256922dd
SJ
58 $text = str_replace("\r", '', $text);
59 $textArray = explode("\n", $text);
60 $linesNb = $limit;
61
7e640aad 62 for ($i = 0; $i < count($textArray); ++$i) {
491f402b 63 if ($i > $limit || strpos($textArray[$i], $postalCode) !== false) {
256922dd 64 $linesNb = $i;
7e640aad
SJ
65 break;
66 }
67 }
256922dd
SJ
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;
7e640aad 75 }
6859200d
SJ
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 }
7e640aad
SJ
90}
91
448c8cdc 92// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
7e640aad 93?>