X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fgeocoder.php;h=01d5e3e048efd0327e1374955a3f746aa3700087;hb=86668a58a94fc57493a335eb75a37171238c0220;hp=357891e58616e44fea6e700f9e31ef3808e96b09;hpb=245923e335d0da5bec9495991a7846be9f0563ab;p=platal.git diff --git a/classes/geocoder.php b/classes/geocoder.php index 357891e..01d5e3e 100644 --- a/classes/geocoder.php +++ b/classes/geocoder.php @@ -1,6 +1,6 @@ 'geoloc_administrativeareas', - 'subAdministrativeArea' => 'geoloc_subadministrativeareas', - 'locality' => 'geoloc_localities', - ); + $where = ''; + foreach ($component['types'] as $type) { + $where .= XDB::format(' AND FIND_IN_SET({?}, types)', $type); + } - $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; + $id = XDB::fetchOneCell('SELECT id + FROM profile_addresses_components_enum + WHERE short_name = {?} AND long_name = {?}' . $where, + $component['short_name'], $component['long_name']); + if (is_null($id)) { + XDB::execute('INSERT INTO profile_addresses_components_enum (short_name, long_name, types) + VALUES ({?}, {?}, {?})', + $component['short_name'], $component['long_name'], implode(',', $component['types'])); + $id = XDB::insertId(); } + return $id; } // 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); + $text = str_replace("\r", '', $text); + $textArray = explode("\n", $text); + $linesNb = $limit; + for ($i = 0; $i < count($textArray); ++$i) { - if ($i > $limit || strpos($textLine, $postalCode) !== false) { - $limit = $i; + if ($i > $limit || strpos($textArray[$i], $postalCode) !== false) { + $linesNb = $i; break; } } - return implode("\n", array_slice($textArray, 0, $limit)); + $firstLines = implode("\n", array_slice($textArray, 0, $linesNb)); + + // Adds empty lines to complete the $limit lines required. + for (; $i < $limit; ++$i) { + $firstLines .= "\n"; + } + return $firstLines; } - // Returns the number of non geocoded addresses for a user. - static public function countNonGeocoded($pid, $jobid = null, $type = Address::LINK_PROFILE) + // Returns the number of non geocoded addresses for a profile. + static public function countNonGeocoded($pid) { - $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(); + $count = XDB::fetchOneCell('SELECT COUNT(*) + FROM profile_addresses AS pa + WHERE pid = {?} AND type = \'home\' + AND NOT EXISTS (SELECT * + FROM profile_addresses_components AS pc + WHERE pa.pid = pc.pid AND pa.jobid = pc.jobid AND pa.groupid = pc.groupid + AND pa.type = pc.type AND pa.id = pc.id)', + $pid); + return $count; } }