From: Stéphane Jacob Date: Fri, 13 Feb 2009 00:56:24 +0000 (+0100) Subject: Switches geolocalization to Google Maps. X-Git-Tag: xorg/1.0.0~332^2~344^2~22 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=041a5cecedbd57a539f4562b7c5c4387a38ac950;p=platal.git Switches geolocalization to Google Maps. --- diff --git a/htdocs/javascript/profile.js b/htdocs/javascript/profile.js index 9e58fa9..19589e4 100644 --- a/htdocs/javascript/profile.js +++ b/htdocs/javascript/profile.js @@ -34,6 +34,9 @@ function wizPage_onLoad(id) prepareType(j); } break; + case 'adresses': + checkCurrentAddress(); + break; case 'poly': updateGroupSubLink(document.forms.prof_annu.groupesx_sub); break; @@ -283,102 +286,69 @@ function updateNetworking(i) // Addresses {{{1 -function removeObject(id, pref) -{ - document.getElementById(id).style.display = "none"; - document.forms.prof_annu[pref + "[removed]"].value = "1"; -} - -function restoreObject(id, pref) +function toggleAddress(id, val) { - document.getElementById(id).style.display = ''; - document.forms.prof_annu[pref + "[removed]"].value = "0"; + $('#addresses_' + id + '_grayed').toggle(); + $('#addresses_' + id).toggle(); + $('#addresses_' + id + '_cont').find('[name*=removed]').val(val); + checkCurrentAddress(); } -function getAddressElement(adrid, adelement) +function checkCurrentAddress(id) { - return document.forms.prof_annu["addresses[" + adrid + "][" + adelement + "]"]; -} - -function checkCurrentAddress(newCurrent) -{ - var hasCurrent = false; + var hasCurrentAddress = id ? true : false; var i = 0; - while (getAddressElement(i, 'pub') != null) { - var radio = getAddressElement(i, 'current'); - var removed = getAddressElement(i, 'removed'); - if (removed.value == "1" && radio.checked) { - radio.checked = false; - } else if (radio.checked && radio != newCurrent) { - radio.checked = false; - } else if (radio.checked) { - hasCurrent = true; + while ($('#addresses_' + i + '_cont').length != 0) { + if ($('#addresses_' + i + '_cont').find('[name*=removed]').val() == 1) { + $('#addresses_' + i + '_cont').find('[name*=current]').attr('checked', false); + } + if (!hasCurrentAddress && $('#addresses_' + i + '_cont').find('[name*=current]:checked').length != 0) { + hasCurrentAddress = true; + } else { + $('#addresses_' + i + '_cont').find('[name*=current]').attr('checked', false); } i++; } - if (!hasCurrent) { + if (!hasCurrentAddress) { i = 0; - while (getAddressElement(i, 'pub') != null) { - var radio = getAddressElement(i, 'current'); - var removed = getAddressElement(i, 'removed'); - if (removed.value != "1") { - radio.checked= true; - return; - } - i++; + while ($('#addresses_' + i + '_cont').length != 0) { + if ($('#addresses_' + i + '_cont').find('[name*=removed]').val() == 0) { + $('#addresses_' + i + '_cont').find('[name*=current]').attr('checked', 'checked'); + break; + } + i++; } } -} - -function removeAddress(id, pref) -{ - removeObject(id, pref); - checkCurrentAddress(null); - if (document.forms.prof_annu[pref + '[datemaj]'].value != '') { - document.getElementById(id + '_grayed').style.display = ''; + if (id) { + $('#addresses_' + id + '_cont').find('[name*=current]').attr('checked', 'checked'); } } -function restoreAddress(id, pref) -{ - document.getElementById(id + '_grayed').style.display = 'none'; - checkCurrentAddress(null); - restoreObject(id, pref); -} - function addAddress() { var i = 0; - while (getAddressElement(i, 'pub') != null) { + while ($('#addresses_' + i + '_cont').length != 0) { i++; } - $("#add_adr").before('
'); - Ajax.update_html('addresses_' + i + '_cont', 'profile/ajax/address/' + i, checkCurrentAddress); + $('#add_address').before('
'); + Ajax.update_html('addresses_' + i + '_cont', 'profile/ajax/address/' + i, checkCurrentAddress()); } -function validGeoloc(id, pref) +function addressChanged(id) { - document.getElementById(id + '_geoloc').style.display = 'none'; - document.getElementById(id + '_geoloc_error').style.display = 'none'; - document.getElementById(id + '_geoloc_valid').style.display = 'none'; - document.forms.prof_annu[pref + "[parsevalid]"].value = "1"; - document.forms.prof_annu[pref + "[text]"].value = document.forms.prof_annu[pref + "[geoloc]"].value; - document.forms.prof_annu[pref + "[cityid]"].value = document.forms.prof_annu[pref + "[geoloc_cityid]"].value; - $(document.forms.prof_annu[pref + "[text]"]).click(function() { document.forms.prof_annu[pref + "[text]"].blur(); }); - document.forms.prof_annu[pref + "[text]"].className = ''; + $('#addresses_' + id + '_cont').find('[name*=changed]').val("1"); } -function validAddress(id, pref) +function validGeoloc(id, geoloc) { - document.getElementById(id + '_geoloc').style.display = 'none'; - document.getElementById(id + '_geoloc_error').style.display = 'none'; - document.getElementById(id + '_geoloc_valid').style.display = 'none'; - document.forms.prof_annu[pref + "[parsevalid]"].value = "1"; - $(document.forms.prof_annu[pref + "[text]"]).click(function() { document.forms.prof_annu[pref + "[text]"].blur(); }); - document.forms.prof_annu[pref + "[text]"].className = ''; + if (geoloc == 1) { + $('#addresses_' + id + '_cont').find('[name*=text]').val($('#addresses_' + id + '_cont').find('[name*=geoloc]').val()); + } + $('#addresses_' + id + '_cont').find('[name*=text]').removeClass('error'); + $('#addresses_' + id + '_cont').find('[name*=geoloc_choice]').val(geoloc); + $('.addresses_' + id + '_geoloc').remove(); } -// }}} // {{{1 Phones function addTel(prefid, prefname) @@ -587,7 +557,6 @@ function addEntreprise(id) $('.entreprise_' + id).toggle(); } -// }}} // {{{1 Skills function updateSkill(cat) diff --git a/include/geoloc.inc.php b/include/geoloc.inc.php index 00e8fa9..c776e42 100644 --- a/include/geoloc.inc.php +++ b/include/geoloc.inc.php @@ -19,7 +19,7 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ***************************************************************************/ -// {{{ liste les pays ou les régions d'un pays +// {{{ geoloc_country($current, $avail_only = false) /** donne la liste déroulante des pays * @param $current pays actuellement selectionné */ @@ -42,6 +42,8 @@ function geoloc_country($current, $avail_only = false) return $html; } +// }}} +// {{{ geoloc_region($country, $current, $avail_only = false) /** donne la liste deroulante des regions pour un pays * @param $pays le pays dont on veut afficher les regions * @param $current la region actuellement selectionnee @@ -69,89 +71,6 @@ function geoloc_region($country, $current, $avail_only = false) return $html; } // }}} - -// {{{ get_address_infos($txt) -/** retrieve the infos on a text address - * store on the fly the info of the city concerned - * @param $txt the raw text of an address - */ -function get_address_infos($txt) -{ - global $globals; - - $url = $globals->geoloc->webservice_url."address.php?precise=1&txt=" . urlencode($txt); - if ($globals->debug & DEBUG_BT) { - if (!isset(PlBacktrace::$bt['Geoloc'])) { - new PlBacktrace('Geoloc'); - } - PlBacktrace::$bt['Geoloc']->start($url); - } - $f = @fopen($url, 'r'); - if ($f === false) { - if ($globals->debug & DEBUG_BT) { - PlBacktrace::$bt['Geoloc']->stop(0, 'Can\'t fetch result'); - } - return false; - } - $keys = explode('|',fgets($f)); - $vals = explode('|',fgets($f)); - if ($globals->debug & DEBUG_BT) { - $data = array(); - for ($i = 0 ; $i < count($keys) ; ++$i) { - $data[] = array($keys[$i], $vals[$i]); - } - PlBacktrace::$bt['Geoloc']->stop(count($keys), null, $data); - } - $infos = empty_address(); - foreach ($keys as $i=>$key) { - if($vals[$i]) { - if ($key == 'sql') { - $infos[$key] = $vals[$i]; - } else { - $val = strtr($vals[$i], array(chr(197).chr(147) => "œ")); - $infos[$key] = $val; - } - } - } - if (empty($infos['country'])) { - $infos['country'] = '00'; - } - if (isset($infos['sql']) && $infos['sql']) { - $sql = explode(', ', trim($infos['sql'], '()')); - if (count($sql) == 16) { - for ($i = 0 ; $i < 16 ; ++$i) { - $sql[$i] = stripslashes(trim($sql[$i], ' \'')); - } - XDB::execute("REPLACE INTO geoloc_city - VALUES ({?}, {?}, {?}, {?}, {?}, {?}, - {?}, {?}, {?}, {?}, {?}, {?}, - {?}, {?}, {?}, {?})", - $sql[0], $sql[1], $sql[2], $sql[3], $sql[4], $sql[5], - $sql[6], $sql[7], $sql[8], $sql[9], $sql[10], $sql[11], - $sql[12], $sql[13], $sql[14], $sql[15]); - } - } - if (isset($infos['display']) && $infos['display']) - XDB::execute("UPDATE geoloc_pays - SET display = {?} - WHERE a2 = {?}", $infos['display'], $infos['country']); - if (isset($infos['cityid'])) { - fix_cities_not_on_map(1, $infos['cityid']); - if (floatval($infos['precise_lat']) && floatval($infos['precise_lon'])) { - $res = XDB::query("SELECT c.lat / 100000, c.lon / 100000 - FROM geoloc_city AS c - WHERE c.id = {?}", $infos['cityid']); - if ($res->numRows()) { - list($glat, $glng) = $res->fetchOneRow(); - $infos['precise_lat'] = $glat; - $infos['precise_lon'] = $glng; - } - } - } - return $infos; -} -// }}} - // {{{ get_cities_maps($array) /* get all the maps id of the cities contained in an array */ function get_cities_maps($array) @@ -172,7 +91,6 @@ function get_cities_maps($array) return $maps; } // }}} - // {{{ get_new_maps($url) /** set new maps from url **/ function get_new_maps($url) @@ -195,7 +113,293 @@ function get_new_maps($url) return true; } // }}} +// {{{ geolocGoogle (array $address) +// retrieve the infos on a text address + +function geolocGoogle (array &$address) +{ + /* keys + * www.polytechnique.org: + * ABQIAAAAIlFNe1A494mwR9Zf4R3t0xRsw9kzQBeaENRP66lRw7Ru3uVJcRR73lY1tmAdYGqw-pyHTdynmicz0w + * www.polytechnique.net and dev.polytechnique.net: + * ABQIAAAAIlFNe1A494mwR9Zf4R3t0xT8SmDPc83znji5QwIVTgAvxgX5zRRMagHx_rmGeQF5SnCzmyqiSeSAxA + * dev.m4x.org: + * ABQIAAAAIlFNe1A494mwR9Zf4R3t0xQ31muaRX97DHXrOFfMwMMCxEnhaxQIPDe9Ct3D6ZvWuGiWllkGAP3IqA + * + * Documentation: + * http://code.google.com/intl/fr/apis/maps/documentation/geocoding/ + * http://code.google.com/apis/maps/documentation/reference.html#GGeoAddressAccuracy */ + + $success = true; + $key = 'ABQIAAAAIlFNe1A494mwR9Zf4R3t0xQ31muaRX97DHXrOFfMwMMCxEnhaxQIPDe9Ct3D6ZvWuGiWllkGAP3IqA'; + $webservice = "http://maps.google.com/maps/geo?"; + $baseurl = $webservice . "&key=$key" . "&sensor=false&output=json&oe=utf8&gl=fr&hl=fr&q="; + + $url = $baseurl . urlencode($address['text']); + if (!geolocalizeAddress($url, $gAddress)) { + $addressLines = explode("\n", $address['text']); + $nbLines = count($addressLines); + $currentState = array(); + $success = false; + for ($i = 1; !$success && ($i < $nbLines); $i++) { + for ($j = 0; $j < $i; $j++) { + $currentState[$j] = 0; + } + while($j < $nbLines) { + $currentState[$j] = 1; + $j++; + } + do { + $partialAddress = ""; + for ($j = 0; $j < $nbLines; $j++) { + if ($currentState[$j] == 1) { + $partialAddress .= $addressLines[$j] . " "; + } + } + $url = $baseurl . urlencode(trim($partialAddress)); + $success = geolocalizeAddress($url, $gAddress); + } while (!$success && nextCurrentState($currentState, $nbLines)); + } + if ($success) { + $extras = ""; + for ($i = 0; $i < $nbLines; $i++) { + if ($currentState[$i] == 0) { + $extras .= $addressLines[$i] . ", "; + } + } + trim($extras, ", "); + $address['extras'] = $extras; + } + } + if ($success) { + fillAddress($address, $gAddress); + formatAddress($address); + } + return $success; +} + +// }}} +// {{{ nextCurrentState(&$currentState, $nbLines) + +function nextCurrentState(&$currentState, $nbLines) +{ + $lastOne = 0; + $nbZeros = 2; + for ($i = 0; $i < $nbLines; $i++) { + if ($currentState[$i] == 1) { + $lastOne = $i; + $nbZeros = 2; + } else { + $nbZeros++; + } + } + if ($lastOne == 0) { + return false; + } elseif ($currentState[$lastOne - 1] == 0) { + $currentState[$lastOne - 1] = 1; + $currentState[$lastOne] = 0; + return true; + } else { + $lastZero = -1; + for ($j = 0; $j < $lastOne; $j++) { + if ($currentState[$j] == 0) { + $lastZero = $j; + } + } + if ($lastZero == -1) { + return false; + } else { + $currentState[$lastZero] = 1; + for ($k = $lastZero + 1; $k < $lastZero + $nbZeros; $k++) { + $currentState[$k] = 0; + } + for ($k = $lastZero + $nbZeros; $k < $nbLines; $k++) { + $currentState[$k] = 1; + } + return true; + } + } +} + +// }}} +// {{{ geolocalizeAddress ($url, &$result) + +function geolocalizeAddress ($url, &$result = array()) +{ + global $globals; + + if ($globals->debug & DEBUG_BT) { + if (!isset(PlBacktrace::$bt['Geoloc'])) { + new PlBacktrace('Geoloc'); + } + PlBacktrace::$bt['Geoloc']->start($url); + } + if ($f = file_get_contents($url, 'r')) { + $data = json_decode($f, true); + if ($globals->debug & DEBUG_BT) { + PlBacktrace::$bt['Geoloc']->stop(count($data), null, $data); + } + if ($data['Status']['code'] != 200) { + return false; + } + $nbResults = count($data['Placemark']); + $idAccuracy = 0; + if ($nbResults > 1) { + $bestAccuracy = $data['Placemark'][0]['AddressDetails']['Accuracy']; + for ($i = 1; $i < $nbResults; $i++) { + if ($data['Placemark'][$i]['AddressDetails']['Accuracy'] > $bestAccuracy) { + unset($data['Placemark'][$idAccuracy]); + $bestAccuracy = $data['Placemark'][$i]['AddressDetails']['Accuracy']; + $idAccuracy = $i; + } else { + unset($data['Placemark'][$i]); + } + } + } + $result = $data['Placemark'][$idAccuracy]; + return true; + } + if ($globals->debug & DEBUG_BT) { + PlBacktrace::$bt['Geoloc']->stop(0, "Can't fetch result."); + } + return false; +} + +// }}} +// {{{ fillAddress(array &$address, $gAddress) + +function fillAddress(array &$address, array $gAddress) +{ + // An address is Country -> AdministrativeArea -> SubAdministrativeArea -> Locality -> Thoroughfare + // with all the shortcuts possible + + // postalText + $address['geoloc'] = str_replace(", ", "\n", $gAddress['address']); + if (isset($gAddress['AddressDetails']['Accuracy'])) { + $address['accuracy'] = $gAddress['AddressDetails']['Accuracy']; + } + $currentPosition = $gAddress['AddressDetails']; + if (isset($currentPosition['Country'])) { + $currentPosition = $currentPosition['Country']; + $address['countryId'] = $currentPosition['CountryNameCode']; + $address['country'] = $currentPosition['CountryName']; + } + if (isset($currentPosition['AdministrativeArea'])) { + $currentPosition = $currentPosition['AdministrativeArea']; + $address['administrativeAreaName'] = $currentPosition['AdministrativeAreaName']; + } + if (isset($currentPosition['SubAdministrativeArea'])) { + $currentPosition = $currentPosition['SubAdministrativeArea']; + $address['subAdministrativeAreaName'] = $currentPosition['SubAdministrativeAreaName']; + } + if (isset($currentPosition['Locality'])) { + $currentPosition = $currentPosition['Locality']; + $address['localityName'] = $currentPosition['LocalityName']; + } + if (isset($currentPosition['Thoroughfare'])) { + $address['thoroughfareName'] = $currentPosition['Thoroughfare']['ThoroughfareName']; + } + if (isset($currentPosition['PostalCode'])) { + $address['postalCode'] = $currentPosition['PostalCode']['PostalCodeNumber']; + } + + // Coordinates + if (isset($gAddress['Point']['coordinates'][0])) { + $address['latitude'] = $gAddress['Point']['coordinates'][0]; + } + if (isset($gAddress['Point']['coordinates'][1])) { + $address['longitude'] = $gAddress['Point']['coordinates'][1]; + } + if (isset($gAddress['ExtendedData']['LatLonBox']['north'])) { + $address['north'] = $gAddress['ExtendedData']['LatLonBox']['north']; + } + if (isset($gAddress['ExtendedData']['LatLonBox']['south'])) { + $address['south'] = $gAddress['ExtendedData']['LatLonBox']['south']; + } + if (isset($gAddress['ExtendedData']['LatLonBox']['east'])) { + $address['east'] = $gAddress['ExtendedData']['LatLonBox']['east']; + } + if (isset($gAddress['ExtendedData']['LatLonBox']['west'])) { + $address['west'] = $gAddress['ExtendedData']['LatLonBox']['west']; + } +} + +// }}} +// {{{ formatAddress(array &$address) + +function formatAddress(array &$address) +{ + $same = true; + $text = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), + array("", "\n"), $address['text'])); + $geoloc = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), + array("", "\n"), $address['geoloc'])); + if (isset($address['extras'])) { + $extras = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), + array("", "\n"), $address['extras'])); + $geoloc = $extras . $geoloc; + unset($address['extras']); + } + + $arrayText = explode("\n", $text); + $arrayGeoloc = explode("\n", $geoloc); + $nbText = count($arrayText); + $nbGeoloc = count($arrayGeoloc); + + if ((($nbText > $nbGeoloc) || ($nbText < $nbGeoloc - 1)) + || (($nbText == $nbGeoloc - 1) && ($arrayText[$nbText - 1] == strtoupper($address['country'])))) { + $same = false; + } else { + foreach ($arrayText as $i => $lignText) { + if (levenshtein($lignText, trim($arrayGeoloc[$i])) > 3) { + $same = false; + } + } + } + if ($same) { + $address['text'] = $address['geoloc']; + unset($address['geoloc']); + } +} + +// }}} +// {{{ cleanText(&$text) + +function cleanText(&$text) +{ + $lines = explode("\n", $text); + $n = count($lines); + $text = ""; + for ($i = 0; $i < $n; $i++) { + if (trim($lines[$i])) { + $text .= trim($lines[$i]) . "\n"; + } + } + $text = trim($text); +} + +// }}} +// {{{ getAreaId(array &$address, $area) + +function getAreaId(array &$address, $area) +{ + if (isset($address[$area . 'Name'])) { + $res = XDB::query("SELECT id + FROM geoloc_" . $area . " + WHERE name = {?}", + $address[$area . 'Name']); + if ($res->numRows() == 0) { + $address[$area . 'Id'] = XDB::execute("INSERT INTO geoloc_" . $area . " (name, country) + VALUES ({?}, {?})", + $address[$area . 'Name'], $address['countryId']); + } else { + $address[$area . 'Id'] = $res->fetchOneCell(); + } + } +} + +// }}} // {{{ get_address_text($adr) /** make the text of an address that can be read by a mailman * @param $adr an array with all the usual fields @@ -236,7 +440,6 @@ function get_address_text($adr) return trim($t); } // }}} - // {{{ compare_addresses_text($a, $b) /** compares if two address matches * @param $a the raw text of an address @@ -253,7 +456,7 @@ function compare_addresses_text($a, $b) if (count($lb) > count($la) + 1) { return false; } - foreach ($la as $i=>$l) { + foreach ($la as $i => $l) { if (levenshtein(trim($l), trim($lb[$i])) > 3) { return false; } @@ -262,31 +465,28 @@ function compare_addresses_text($a, $b) } // }}} +// {{{ fixNumber($oldtext, &$new) -function empty_address() { - return Array( - "adr1" => "", - "adr2" => "", - "adr3" => "", - "cityid" => NULL, - "city" => "", - "postcode" => "", - "region" => "", - "regiontxt" => "", - "country" => "00", - "countrytxt" => "", - "precise_lat" => "", - "precise_lon" => ""); -} - -// create a simple address from a text without geoloc -function cut_address($txt) +function fixNumber($oldtext, &$new) { - $txt = str_replace("\r\n", "\n", $txt); - ereg("^([^\n]*)(\n([^\n]*)(\n(.*))?)?$", trim($txt), $a); - return array("adr1" => trim($a[1]), "adr2" => trim($a[3]), "adr3" => trim(str_replace("\n", " ", $a[5]))); + $ThoroughfareName = $new['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['Thoroughfare']['ThoroughfareName']; + $ThoroughfareName = trim(strtoupper(preg_replace(array("/[,\"'#~:;_\-]/", "/\r\n/"), + array("", "\n"), $ThoroughfareName))); + $oldarray = explode("\n", trim(strtoupper(preg_replace(array("/[,\"'#~:;_\-]/", "/\r\n/"), + array("", "\n"), $oldtext)))); + $mindist = strlen($ThoroughfareName); + $minpos = 0; + foreach ($oldarray as $i => $oldline) { + if (($l = levenshtein(trim($oldline), $ThoroughfareName)) < $mindist) { + $mindist = $l; + $minpos = $i; + } + } + $nb = explode(" ", $oldarray[$minpos]); + $new['text'] = $nb[0] . " " . $new['text']; } +// }}} // {{{ localize_addresses($uid) /* localize all the address of a user and modify the database * if the new address match with the old one @@ -325,7 +525,89 @@ function localize_addresses($uid) return $erreur; } // }}} +// {{{ get_address_infos($txt) +/** retrieve the infos on a text address + * store on the fly the info of the city concerned + * @param $txt the raw text of an address + */ + +function get_address_infos($txt) +{ + global $globals; + + $url = $globals->geoloc->webservice_url."address.php?precise=1&txt=" . urlencode($txt); + if ($globals->debug & DEBUG_BT) { + if (!isset(PlBacktrace::$bt['Geoloc'])) { + new PlBacktrace('Geoloc'); + } + PlBacktrace::$bt['Geoloc']->start($url); + } + $f = @fopen($url, 'r'); + if ($f === false) { + if ($globals->debug & DEBUG_BT) { + PlBacktrace::$bt['Geoloc']->stop(0, 'Can\'t fetch result'); + } + return false; + } + $keys = explode('|',fgets($f)); + $vals = explode('|',fgets($f)); + if ($globals->debug & DEBUG_BT) { + $data = array(); + for ($i = 0 ; $i < count($keys) ; ++$i) { + $data[] = array($keys[$i], $vals[$i]); + } + PlBacktrace::$bt['Geoloc']->stop(count($keys), null, $data); + } + $infos = empty_address(); + foreach ($keys as $i=>$key) { + if($vals[$i]) { + if ($key == 'sql') { + $infos[$key] = $vals[$i]; + } else { + $val = strtr($vals[$i], array(chr(197).chr(147) => "œ")); + $infos[$key] = $val; + } + } + } + if (empty($infos['country'])) { + $infos['country'] = '00'; + } + if (isset($infos['sql']) && $infos['sql']) { + $sql = explode(', ', trim($infos['sql'], '()')); + if (count($sql) == 16) { + for ($i = 0 ; $i < 16 ; ++$i) { + $sql[$i] = stripslashes(trim($sql[$i], ' \'')); + } + XDB::execute("REPLACE INTO geoloc_city + VALUES ({?}, {?}, {?}, {?}, {?}, {?}, + {?}, {?}, {?}, {?}, {?}, {?}, + {?}, {?}, {?}, {?})", + $sql[0], $sql[1], $sql[2], $sql[3], $sql[4], $sql[5], + $sql[6], $sql[7], $sql[8], $sql[9], $sql[10], $sql[11], + $sql[12], $sql[13], $sql[14], $sql[15]); + } + } + if (isset($infos['display']) && $infos['display']) + XDB::execute("UPDATE geoloc_pays + SET display = {?} + WHERE a2 = {?}", $infos['display'], $infos['country']); + if (isset($infos['cityid'])) { + fix_cities_not_on_map(1, $infos['cityid']); + if (floatval($infos['precise_lat']) && floatval($infos['precise_lon'])) { + $res = XDB::query("SELECT c.lat / 100000, c.lon / 100000 + FROM geoloc_city AS c + WHERE c.id = {?}", $infos['cityid']); + if ($res->numRows()) { + list($glat, $glng) = $res->fetchOneRow(); + $infos['precise_lat'] = $glat; + $infos['precise_lon'] = $glng; + } + } + } + return $infos; +} +// }}} // {{{ synchro_city($id) /** synchronise the local geoloc_city base to geoloc.org * @param $id the id of the city to synchronize @@ -343,7 +625,6 @@ function synchro_city($id) } } // }}} - // {{{ function fix_cities_not_on_map($limit) function fix_cities_not_on_map($limit=false, $cityid=false) { @@ -387,7 +668,6 @@ function set_smallest_levels() } // }}} - function geoloc_to_x($lon, $lat) { return deg2rad(1) * $lon *100; @@ -562,7 +842,6 @@ function geoloc_getData_subcountries($mapid, $sin, $minentities) return array($countries, $cities); } -// }}} // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: ?> diff --git a/modules/profile.php b/modules/profile.php index b6f5cfc..4583907 100644 --- a/modules/profile.php +++ b/modules/profile.php @@ -433,12 +433,12 @@ class ProfileModule extends PLModule $page->assign('medal_list', $mlist); } - function handler_ajax_address(&$page, $adid) + function handler_ajax_address(&$page, $id) { header('Content-Type: text/html; charset=utf-8'); $page->changeTpl('profile/adresses.address.tpl', NO_SKIN); - $page->assign('i', $adid); - $page->assign('adr', array()); + $page->assign('i', $id); + $page->assign('address', array()); } function handler_ajax_tel(&$page, $prefid, $prefname, $telid) diff --git a/modules/profile/addresses.inc.php b/modules/profile/addresses.inc.php index 6fc1f72..6eca8bd 100644 --- a/modules/profile/addresses.inc.php +++ b/modules/profile/addresses.inc.php @@ -30,27 +30,18 @@ class ProfileAddress extends ProfileGeoloc $this->pub = new ProfilePub(); } - private function cleanAddress(ProfilePage &$page, $adrid, array &$address, &$success) + private function cleanAddress(ProfilePage &$page, $addrid, array &$address) { - if (@$address['changed']) { - $address['datemaj'] = time(); - } - $success = true; if (!isset($address['tel'])) { $address['tel'] = array(); } - $profiletel = new ProfilePhones('address', $adrid); - $address['tel'] = $profiletel->value($page, 'tel', $address['tel'], $s); - $address['checked'] = $this->bool->value($page, 'checked', $address['checked'], $s); - $address['secondaire'] = $this->bool->value($page, 'secondaire', $address['secondaire'], $s); - $address['mail'] = $this->bool->value($page, 'mail', $address['mail'], $s); + $profiletel = new ProfilePhones('address', $addrid); + $address['tel'] = $profiletel->value($page, 'tel', $address['tel'], $s); + $address['current'] = $this->bool->value($page, 'current', $address['current'], $s); $address['temporary'] = $this->bool->value($page, 'temporary', $address['temporary'], $s); - $address['current'] = $this->bool->value($page, 'current', @$address['current'], $s); - $address['pub'] = $this->pub->value($page, 'pub', $address['pub'], $s); - unset($address['parsevalid']); - unset($address['changed']); - unset($address['removed']); - unset($address['display']); + $address['secondary'] = $this->bool->value($page, 'secondary', $address['secondary'], $s); + $address['mail'] = $this->bool->value($page, 'mail', $address['mail'], $s); + $address['pub'] = $this->pub->value($page, 'pub', $address['pub'], $s); } public function value(ProfilePage &$page, $field, $value, &$success) @@ -58,99 +49,101 @@ class ProfileAddress extends ProfileGeoloc $init = false; if (is_null($value)) { $value = $page->values['addresses']; - $init = true; + $init = true; } - foreach ($value as $key=>&$adr) { - if (@$adr['removed']) { + foreach ($value as $key => &$address) { + if (isset($address['removed']) && $address['removed']) { unset($value[$key]); } } $current = 0; $success = true; - foreach ($value as $key=>&$adr) { - if (@$adr['current']) { + foreach ($value as $key => &$address) { + if (isset($address['current']) && $address['current']) { $current++; } } if ($current == 0 && count($value) > 0) { - foreach ($value as $key=>&$adr) { - $adr['current'] = true; + foreach ($value as $address) { + $address['current'] = true; break; } - } else if ($current > 1) { + } elseif ($current > 1) { $success = false; } - foreach ($value as $key=>&$adr) { - $ls = true; - $this->geolocAddress($adr, $s); - $ls = ($ls && $s); - $this->cleanAddress($page, $key, $adr, $s); - $ls = ($ls && $s); - if (!trim($adr['text'])) { + foreach ($value as $key => &$address) { + if (!trim($address['text'])) { unset($value[$key]); - } else if (!$init) { - $success = ($success && $ls); + } elseif (!$init) { + $this->geolocAddress($address, $s); + $success = $success && $s; } + $this->cleanAddress($page, $key, $address); } return $value; } - private function saveTel($adrid, $telid, array &$tel) + private function saveTel($addrid, $telid, array &$tel) { XDB::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type, - search_tel, display_tel, pub) + search_tel, display_tel, pub) VALUES ({?}, 'address', {?}, {?}, {?}, {?}, {?}, {?})", - S::i('uid'), $adrid, $telid, $tel['type'], - format_phone_number($tel['tel']), $tel['tel'], $tel['pub']); + S::i('uid'), $addrid, $telid, $tel['type'], + format_phone_number($tel['tel']), $tel['tel'], $tel['pub']); } - private function saveAddress($adrid, array &$address) + private function saveAddress($addrid, array &$address) { + require_once "geoloc.inc.php"; + $flags = new PlFlagSet(); - if ($address['secondaire']) { - $flags->addFlag('res-secondaire'); - } - if ($address['mail']) { - $flags->addFlag('courrier'); + if ($address['current']) { + $flags->addFlag('current'); } if ($address['temporary']) { - $flags->addFlag('temporaire'); - } - if ($address['current']) { - $flags->addFlag('active'); + $flags->addFlag('temporary'); } - if ($address['checked']) { - $flags->addFlag('coord-checked'); + if ($address['secondary']) { + $flags->addFlag('secondary'); } - XDB::execute("INSERT INTO adresses (adr1, adr2, adr3, - postcode, city, cityid, - country, region, regiontxt, - pub, datemaj, statut, - uid, adrid, glat, glng, comment) - VALUES ({?}, {?}, {?}, - {?}, {?}, {?}, - {?}, {?}, {?}, - {?}, FROM_UNIXTIME({?}), {?}, - {?}, {?}, {?}, {?}, {?})", - $address['adr1'], $address['adr2'], $address['adr3'], - $address['postcode'], $address['city'], $address['cityid'], - $address['country'], $address['region'], $address['regiontxt'], - $address['pub'], $address['datemaj'], $flags, - S::i('uid'), $adrid, $address['precise_lat'], $address['precise_lon'], $address['comment']); + if ($address['mail']) { + $flags->addFlag('mail'); + } + if ($address['cedex'] = + (strpos(strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), + array("", "\n"), $address['text'])), 'CEDEX')) !== false) { + $flags->addFlag('cedex'); + } + getAreaId($address, "administrativeArea"); + getAreaId($address, "subAdministrativeArea"); + getAreaId($address, "locality"); + XDB::execute("INSERT INTO profile_addresses (pid, type, id, flags, accuracy, + text, postalText, postalCode, localityId, + subAdministrativeAreaId, administrativeAreaId, + countryId, latitude, longitude, updateTime, pub, comment, + north, south, east, west) + VALUES ({?}, 'home', {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, + {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?}, {?}, {?})", + S::i('uid'), $addrid, $flags, $address['accuracy'], + $address['text'], 'postalText'/*$address['postalText']*/, $address['postalCode'], $address['localityId'], + $address['subAdministrativeAreaId'], $address['administrativeAreaId'], + $address['countryId'], $address['latitude'], $address['longitude'], + $address['updateTime'], $address['pub'], $address['comment'], + $address['north'], $address['south'], $address['east'], $address['west']); } public function save(ProfilePage &$page, $field, $value) { - XDB::execute("DELETE FROM adresses - WHERE uid = {?}", + XDB::execute("DELETE FROM profile_addresses + WHERE pid = {?} AND type = 'home'", S::i('uid')); XDB::execute("DELETE FROM profile_phones WHERE uid = {?} AND link_type = 'address'", S::i('uid')); - foreach ($value as $adrid=>&$address) { - $this->saveAddress($adrid, $address); - $profiletel = new ProfilePhones('address', $adrid); + foreach ($value as $addrid => &$address) { + $this->saveAddress($addrid, $address); + $profiletel = new ProfilePhones('address', $addrid); $profiletel->saveTels('tel', $address['tel']); } } @@ -164,27 +157,23 @@ class ProfileAddresses extends ProfilePage { parent::__construct($wiz); $this->settings['addresses'] = new ProfileAddress(); - $this->watched['addresses'] = true; + $this->watched['addresses'] = true; } protected function _fetchData() { - // Build the addresses tree - $res = XDB::query("SELECT a.adrid AS id, a.adr1, a.adr2, a.adr3, - UNIX_TIMESTAMP(a.datemaj) AS datemaj, - a.postcode, a.city, a.cityid, a.region, a.regiontxt, - a.pub, a.country, gp.pays AS countrytxt, gp.display, - FIND_IN_SET('coord-checked', a.statut) AS checked, - FIND_IN_SET('res-secondaire', a.statut) AS secondaire, - FIND_IN_SET('courrier', a.statut) AS mail, - FIND_IN_SET('temporaire', a.statut) AS temporary, - FIND_IN_SET('active', a.statut) AS current, - a.glat AS precise_lat, a.glng AS precise_lon, - a.comment - FROM adresses AS a - INNER JOIN geoloc_pays AS gp ON(gp.a2 = a.country) - WHERE uid = {?} AND NOT FIND_IN_SET('pro', statut) - ORDER BY adrid", + $res = XDB::query("SELECT type, id, accuracy, text, postalText, + postalCode, localityId, subAdministrativeAreaId, administrativeAreaId, + countryId, latitude, longitude, pub, comment, updateTime, + north, south, east, west, + FIND_IN_SET('current', flags) AS current, + FIND_IN_SET('temporary', flags) AS temporary, + FIND_IN_SET('secondary', flags) AS secondary, + FIND_IN_SET('mail', flags) AS mail, + FIND_IN_SET('cedex', flags) AS cedex + FROM profile_addresses + WHERE pid = {?} AND type = 'home' + ORDER BY id", S::i('uid')); if ($res->numRows() == 0) { $this->values['addresses'] = array(); @@ -192,7 +181,7 @@ class ProfileAddresses extends ProfilePage $this->values['addresses'] = $res->fetchAllAssoc(); } - $res = XDB::iterator("SELECT link_id AS adrid, tel_type AS type, pub, display_tel AS tel, comment + $res = XDB::iterator("SELECT link_id AS addrid, tel_type AS type, pub, display_tel AS tel, comment FROM profile_phones WHERE uid = {?} AND link_type = 'address' ORDER BY link_id", @@ -200,8 +189,8 @@ class ProfileAddresses extends ProfilePage $i = 0; $adrNb = count($this->values['addresses']); while ($tel = $res->next()) { - $adrid = $tel['adrid']; - unset($tel['adrid']); + $adrid = $tel['addrid']; + unset($tel['addrid']); while ($i < $adrNb && $this->values['addresses'][$i]['id'] < $adrid) { $i++; } @@ -216,7 +205,7 @@ class ProfileAddresses extends ProfilePage $address['tel'][] = $tel; } } - foreach ($this->values['addresses'] as $id=>&$address) { + foreach ($this->values['addresses'] as $id => &$address) { if (!isset($address['tel'])) { $address['tel'] = array(); } diff --git a/modules/profile/page.inc.php b/modules/profile/page.inc.php index 45fc082..b46e23a 100644 --- a/modules/profile/page.inc.php +++ b/modules/profile/page.inc.php @@ -208,9 +208,9 @@ class ProfilePub extends ProfileNoSave if (is_null($value)) { return isset($page->values[$field]) ? $page->values[$field] : S::v($field); } - if (is_null($value) || !$value) { + if (!$value) { $value = 'private'; - } else if ($value == 'on') { // Checkbox + } elseif ($value == 'on') { // Checkbox $value = 'public'; } return $value; @@ -223,7 +223,7 @@ class ProfileBool extends ProfileNoSave { $success = true; if (is_null($value)) { - $value = @$page->values[$field]; + $value = isset($page->values[$field]) ? $page->values[$field] : null; } return $value ? "1" : ""; } @@ -260,38 +260,22 @@ abstract class ProfileGeoloc implements ProfileSetting { require_once 'geoloc.inc.php'; $success = true; - unset($address['geoloc']); - unset($address['geoloc_cityid']); - if (@$address['parsevalid'] - || (@$address['text'] && @$address['changed']) - || (@$address['text'] && !@$address['cityid'])) { - $address = array_merge($address, empty_address()); - $new = get_address_infos(@$address['text']); - if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new)) - || (@$address['parsevalid'] && @$address['cityid'])) { - $address = array_merge($address, $new); - $address['checked'] = true; - } else if (@$address['parsevalid']) { - $address = array_merge($address, cut_address(@$address['text'])); - $address['checked'] = true; - $mailer = new PlMailer('geoloc/geoloc.mail.tpl'); - $mailer->assign('text', get_address_text($address)); - $mailer->assign('geoloc', $geotxt); - $mailer->send(); - } else if (@$address['changed'] || !@$address['checked']) { + if ($address['changed'] == 1) { + cleanText($address['text']); + geolocGoogle($address); + $address['updateTime'] = time(); + // postalAddress + if (isset($address['geoloc'])) { $success = false; - $address = array_merge($address, cut_address(@$address['text'])); - $address['checked'] = false; - $address['geoloc'] = $geotxt; - $address['geoloc_cityid'] = $new['cityid']; - } else { - $address = array_merge($address, cut_address(@$address['text'])); - $address['checked'] = true; } + unset($address['changed']); + } + if (isset($address['geoloc_choice']) && $address['geoloc_choice'] == 0) { + $mailer = new PlMailer('geoloc/geoloc.mail.tpl'); + $mailer->assign('text', $address['text']); + $mailer->assign('geoloc', $address['geoloc']); + $mailer->send(); } - $address['precise_lat'] = rtrim($address['precise_lat'], '.0'); - $address['precise_lon'] = rtrim($address['precise_lon'], '.0'); - $address['text'] = get_address_text($address); } } diff --git a/templates/geoloc/form.address.tpl b/templates/geoloc/form.address.tpl index 9e543e9..0f5cc34 100644 --- a/templates/geoloc/form.address.tpl +++ b/templates/geoloc/form.address.tpl @@ -20,47 +20,54 @@ {* *} {**************************************************************************} - -{if $adr.geoloc} -
+{if $address.geoloc} +
La geolocalisation n'a pas donné un résultat certain, valide la nouvelle adresse ou modifie l'ancienne pour que ton adresse puisse être prise en compte.
- {/if} +
- -{if $adr.geoloc} - - - - - + +{if $address.geoloc} +
-
- Valider ta version + - - - - - - - - - - - - - +{if $address.geoloc} + + + +{/if} + + + + + + + + + + + + + + + + + + + + + + + + + {* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *} diff --git a/templates/profile/adresses.address.tpl b/templates/profile/adresses.address.tpl index 118b56a..3067c22 100644 --- a/templates/profile/adresses.address.tpl +++ b/templates/profile/adresses.address.tpl @@ -20,30 +20,32 @@ {* *} {**************************************************************************} -{assign var=adpref value="addresses[$i]"} -{assign var=adid value="addresses_$i"} - - - +{assign var=prefname value="addresses[$i]"} +{assign var=prefid value="addresses_$i"} + - +
@@ -51,49 +53,68 @@
- - +
Adresse n°{$i+1}
- {include file="include/flags.radio.tpl" name="`$adpref`[pub]" val=$adr.pub} + {include file="include/flags.radio.tpl" name="`$prefname`[pub]" val=$address.pub}
-
{include file="geoloc/form.address.tpl" name=$adpref id=$adid adr=$adr}
+
+ {include file="geoloc/form.address.tpl" prefname=$prefname prefid=$prefid address=$address id=$i} +
- - + +
- - + +
- - +
- - +
- {foreach from=$adr.tel key=t item=tel} -
- {include file="profile/phone.tpl" prefname="`$adpref`[tel]" prefid="`$adid`_tel" telid=$t tel=$tel} + {foreach from=$address.tel key=t item=tel} +
+ {include file="profile/phone.tpl" prefname="`$prefname`[tel]" + prefid="`$prefid`_tel" telid=$t tel=$tel}
{/foreach} - {if $adr.tel|@count eq 0} -
- {include file="profile/phone.tpl" prefname="`$adpref`[tel]" prefid="`$adid`_tel" telid=0 tel=0} + {if $address.tel|@count eq 0} +
+ {include file="profile/phone.tpl" prefname="`$prefname`[tel]" prefid="`$prefid`_tel" telid=0 tel=0}
{/if} -
- + diff --git a/templates/profile/adresses.tpl b/templates/profile/adresses.tpl index 845279d..19fa656 100644 --- a/templates/profile/adresses.tpl +++ b/templates/profile/adresses.tpl @@ -20,18 +20,18 @@ {* *} {**************************************************************************} -{foreach key=i item=adr from=$addresses} +{foreach key=i item=address from=$addresses}
-{include file="profile/adresses.address.tpl" i=$i adr=$adr} +{include file="profile/adresses.address.tpl" i=$i address=$address}
{/foreach} {if $addresses|@count eq 0}
-{include file="profile/adresses.address.tpl" i=0 adr=0} +{include file="profile/adresses.address.tpl" i=0 address=0}
{/if} -
+
{icon name=add title="Ajouter une adresse"} Ajouter une adresse diff --git a/upgrade/newdirectory-0.0.1/15_addresses.sql b/upgrade/newdirectory-0.0.1/15_addresses.sql new file mode 100644 index 0000000..e437e22 --- /dev/null +++ b/upgrade/newdirectory-0.0.1/15_addresses.sql @@ -0,0 +1,93 @@ +CREATE TABLE IF NOT EXISTS profile_addresses ( + pid INT(11) DEFAULT NULL, + jobid INT(6) UNSIGNED DEFAULT NULL, + type ENUM('home','job','hq') NOT NULL DEFAULT 'home', + id TINYINT(3) UNSIGNED NOT NULL DEFAULT 0, + flags SET('current','temporary','secondary','mail','cedex') DEFAULT NULL, + accuracy TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, + text TEXT NOT NULL, + postalText TEXT NOT NULL, + postalCode VARCHAR(255) DEFAULT NULL, + localityId INT(11) DEFAULT NULL, + subAdministrativeAreaId INT(11) DEFAULT NULL, + administrativeAreaId INT(11) DEFAULT NULL, + countryId CHAR(2) DEFAULT NULL, + latitude FLOAT(10,7) DEFAULT NULL, + longitude FLOAT(10,7) DEFAULT NULL, + north FLOAT(10,7) DEFAULT NULL, + south FLOAT(10,7) DEFAULT NULL, + east FLOAT(10,7) DEFAULT NULL, + west FLOAT(10,7) DEFAULT NULL, + updateTime DATE NOT NULL DEFAULT 0, + pub ENUM('public','ax','private') NOT NULL DEFAULT 'private', + comment VARCHAR(255) DEFAULT NULL, + PRIMARY KEY(pid, jobid, type, id), + INDEX pid (pid), + INDEX jobid (jobid), + INDEX type (type), + INDEX adrid (id), + INDEX localityId (localityId), + INDEX administrativeAreaId (administrativeAreaId), + INDEX subAdministrativeAreaId (subAdministrativeAreaId), + INDEX countryId (countryId) +) CHARSET=utf8; + +CREATE TABLE IF NOT EXISTS geoloc_countries ( + iso_3166_1 CHAR(2) NOT NULL, + iso_3166_2 CHAR(3) NOT NULL, + iso_3166_3 SMALLINT(3) UNSIGNED NOT NULL, + worldRegion CHAR(2) DEFAULT NULL, + countryFR VARCHAR(255) NOT NULL, + country VARCHAR(255) NOT NULL, + capital VARCHAR(255) NOT NULL, + nationalityFR VARCHAR(255) DEFAULT NULL, + nationality VARCHAR(255) DEFAULT NULL, + phonePrefix SMALLINT(5) UNSIGNED DEFAULT NULL, + phoneFormat VARCHAR(255) NOT NULL, + licensePlate CHAR(4) DEFAULT NULL, + belongsTo CHAR(2) DEFAULT NULL, + PRIMARY KEY(iso_3166_1), + INDEX(iso_3166_1), + INDEX(phonePrefix) +) CHARSET=utf8; + +INSERT INTO geoloc_countries (iso_3166_1, iso_3166_2, iso_3166_3, worldRegion, + countryFR, country, capital, nationalityFR, + phonePrefix, phoneFormat, licensePlate) + SELECT a2, a3, n3, worldrgn, pays, country, capital, nat, phoneprf, phoneformat, license_plate + FROM geoloc_pays; + +CREATE TABLE IF NOT EXISTS geoloc_administrativeArea ( + id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, + name VARCHAR(255) NOT NULL, + country CHAR(2) NOT NULL, + PRIMARY KEY(id), + UNIQUE KEY(id, name, country), + INDEX(id), + INDEX(name), + INDEX(country) +) CHARSET=utf8; + +CREATE TABLE IF NOT EXISTS geoloc_subAdministrativeArea ( + id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, + name VARCHAR(255) NOT NULL, + country CHAR(2) NOT NULL, + PRIMARY KEY(id), + UNIQUE KEY(id, name, country), + INDEX(id), + INDEX(name), + INDEX(country) +) CHARSET=utf8; + +CREATE TABLE IF NOT EXISTS geoloc_locality ( + id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, + name VARCHAR(255) NOT NULL, + country CHAR(2) NOT NULL, + PRIMARY KEY(id), + UNIQUE KEY(id, name, country), + INDEX(id), + INDEX(name), + INDEX(country) +) CHARSET=utf8; + +-- vim:set syntax=mysql: