X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fgeocoder.php;h=c5ff63e80200c56ae858d210e7527df40c6b63b5;hb=5a47c10b2cdfbcfe47769ef58930357563c44343;hp=357891e58616e44fea6e700f9e31ef3808e96b09;hpb=12ddbc2037b16496dfb9d91c4f7091850146fcde;p=platal.git diff --git a/classes/geocoder.php b/classes/geocoder.php index 357891e..c5ff63e 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)); - // 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); + // Adds empty lines to complete the $limit lines required. + for (; $i < $limit; ++$i) { + $firstLines .= "\n"; } - $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(); + return $firstLines; } }