From dbc365317e6c38d6cff05e783e1f8bf36641dfcb Mon Sep 17 00:00:00 2001 From: Pascal Corpet Date: Mon, 13 Jun 2005 09:05:03 +0000 Subject: [PATCH] interface geoloc git-archimport-id: opensource@polytechnique.org--2005/platal--mainline--0.9--patch-711 --- include/geoloc.inc.php | 39 +++++++++++++++++++--------- include/profil/get_adresses.inc.php | 5 ---- include/profil/verif_adresses.inc.php | 48 +++++++++++++++-------------------- templates/geoloc/form.address.tpl | 36 ++++++++------------------ 4 files changed, 58 insertions(+), 70 deletions(-) diff --git a/include/geoloc.inc.php b/include/geoloc.inc.php index defe993..216d786 100644 --- a/include/geoloc.inc.php +++ b/include/geoloc.inc.php @@ -110,13 +110,15 @@ function get_address_text($adr) { // compares if two address matches // $b should be a complete valid address -function compare_addresses($a, $b) { - if ($a['country'] != '00' && $b['country'] != $a['country']) return false; - if ($a['postcode'] && $a['postcode'] != $b['postcode']) return false; - if ($a['city'] && strtoupper($a['city']) != strtoupper($b['city'])) return false; - if (trim($a['adr1']) != trim($b['adr1'])) return false; - if (trim($a['adr2']) != trim($b['adr2'])) return false; - if (trim($a['adr3']) != trim($b['adr3']))return false; +function compare_addresses_text($a, $b) { + $ta = strtoupper(preg_replace(array("/[,\"'#~:;_\-]/", "/\r\n/"), array(" ", "\n"), $a)); + $tb = strtoupper(preg_replace(array("/[,\"'#~:;_\-]/", "/\r\n/"), array(" ", "\n"), $b)); + + $la = explode("\n", $ta); + $lb = explode("\n", $tb); + + if (count($lb) > count($la) + 1) return false; + foreach ($la as $i=>$l) if (levenshtein($l, $lb[$i]) > 3) return false; return true; } @@ -125,11 +127,19 @@ function empty_address() { "adr1" => "", "adr2" => "", "adr3" => "", - "city_id" => NULL, + "cityid" => NULL, "city" => "", "postcode" => "", "region" => "", - "country" => "00"); + "country" => "00", + "countrytxt" => ""); +} + +// create a simple address from a text without geoloc +function cut_address($txt) { + $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]))); } // localize all the address of a user and modify the database @@ -140,8 +150,8 @@ function localize_addresses($uid) { $erreur = Array(); while ($a = $res->next()) { - $new = get_address_infos(get_address_text($a)); - if ($new['cityid'] && compare_addresses($a, $new)) { + $new = get_address_infos($ta = get_address_text($a)); + if (compare_addresses_text($ta, get_address_text($new))) { $globals->xdb->execute("UPDATE adresses SET adr1 = {?}, adr2 = {?}, adr3 = {?}, cityid = {?}, city = {?}, postcode = {?}, @@ -151,7 +161,12 @@ function localize_addresses($uid) { $new['cityid'], $new['city'], $new['postcode'], $new['region'], $new['country'], $uid, $a['adrid']); - } else $erreur[$a['adrid']] = $new; + $new['store'] = true; + if (!$new['cityid']) return $erreur[$a['adrid']] = $new; + } else { + $new['store'] = false; + $erreur[$a['adrid']] = $new; + } } return $erreur; diff --git a/include/profil/get_adresses.inc.php b/include/profil/get_adresses.inc.php index 417bf9e..c2201a2 100644 --- a/include/profil/get_adresses.inc.php +++ b/include/profil/get_adresses.inc.php @@ -61,8 +61,6 @@ $sql_order = ''; $res = $globals->xdb->query("SELECT adrid FROM adresses WHERE uid = {?} AND NOT FIND_IN_SET('pro', statut) ".$sql_order, Session::getInt('uid', -1)); $adrids = $res->fetchColumn(); -$non_geoloc = localize_addresses(Session::getInt('uid', -1)); - //recuperation des donnees de la bd $res = $globals->xdb->iterRow( "SELECT @@ -92,9 +90,6 @@ for ($i = 0; $i < $nb_adr; $i++) { $adresses[$adrid]['numero_formulaire'] = -1; require_once('geoloc.inc.php'); $adresses[$adrid]['txt'] = get_address_text($adresses[$adrid]); - - if (isset($non_geoloc[$adrid])) - $adresses[$adrid]['geoloc'] = get_address_text($non_geoloc[$adrid]); } ?> diff --git a/include/profil/verif_adresses.inc.php b/include/profil/verif_adresses.inc.php index b6e56cc..93ee5cb 100644 --- a/include/profil/verif_adresses.inc.php +++ b/include/profil/verif_adresses.inc.php @@ -65,7 +65,7 @@ function set_flag_adr($varname,$i){ function replace_address($i){ - global $page, $adresses; + global $adresses; if(!isset($adresses[$i])){ $adresses[$i]['nouvelle'] = 'ajout'; $adresses[$i]['adrid'] = $i; @@ -91,47 +91,41 @@ function replace_address($i){ replace_ifset_adr('pub', $i); replace_ifset_adr('tel_pub', $i); if (!get_adr_arg('parsevalid', $i)) replace_ifset_adr('txt', $i); + $tab = Env::getMixed('numero_formulaire', Array()); + if($tab[$i]) + $adresses[$i]['numero_formulaire'] = $tab[$i]; + else + $adresses[$i]['numero_formulaire'] = -1; +} + +function geoloc_adresse($i) { + global $adresses; $change = Env::get('change'.$i); - if (Env::get('parseretry'.$i)) { - $adresses[$i]['txt'] = get_adr_arg('retrytxt', $i); - $change = true; - } - if (get_adr_arg('parsevalid', $i) || ($adresses[$i]['txt'] && $change)) { + if (get_adr_arg('parsevalid', $i) || ($adresses[$i]['txt'] && $change) || (!$adresses[$i]['cityid'])) { require_once('geoloc.inc.php'); + // erases the previous address (but not the phone or pub) + $adresses[$i] = array_merge($adresses[$i], empty_address()); + // localize new address $new = get_address_infos($adresses[$i]['txt']); - // if we found a localisation, erase old address - if ($new['sql'] || get_adr_arg('parsevalid', $i)) { - $adresses[$i]['adr1'] = ''; - $adresses[$i]['adr2'] = ''; - $adresses[$i]['adr3'] = ''; - $adresses[$i]['postcode'] = ''; - $adresses[$i]['city'] = ''; - unset($adresses[$i]['cityid']); - $adresses[$i]['country'] = '00'; - $adresses[$i]['countrytxt'] = ''; - $adresses[$i]['region'] = ''; + if (compare_addresses_text($adresses[$i]['txt'], $geotxt = get_address_text($new)) || get_adr_arg('parsevalid', $i)) $adresses[$i] = array_merge($adresses[$i], $new); - unset($adresses[$i]['geoloc']); - }else { - $page->trig("L'adresse n'a pas été reconnue par la geoloc"); - $adresses[$i]['old_txt'] = $adresses[$i]['txt']; + else { + $adresses[$i] = array_merge($adresses[$i], cut_address($adresses[$i]['txt'])); + $adresses[$i]['geoloc'] = $geotxt; + $adresses[$i]['geoloc_cityid'] = $new['cityid']; } } $adresses[$i]['txt'] = get_address_text($adresses[$i]); - $tab = Env::getMixed('numero_formulaire', Array()); - if($tab[$i]) - $adresses[$i]['numero_formulaire'] = $tab[$i]; - else - $adresses[$i]['numero_formulaire'] = -1; } - //remplace par les eventuelles nouvelles valeurs : for ($adrid = 1; $adrid <= $nb_adr_max; $adrid++) { $tab = Env::getMixed('adrid', Array()); if(isset($tab[$adrid])){ //cet adrid etait donc present dans le formulaire replace_address($adrid); } + if (isset($adresses[$adrid]['txt'])) + geoloc_adresse($adrid); } if(Env::get('old_tab', '') == 'adresses' && Env::has('modifier')){ // on ne valide que qd on vient du formulaire diff --git a/templates/geoloc/form.address.tpl b/templates/geoloc/form.address.tpl index 1587560..6ede47b 100644 --- a/templates/geoloc/form.address.tpl +++ b/templates/geoloc/form.address.tpl @@ -1,34 +1,18 @@ - {if $adr.old_txt} - - - - La geolocalisation n'a pas marché pour ta nouvelle adresse.
-
-
- -
-
-
- -
- - - {elseif $adr.geoloc && $adr.geoloc neq $adr.txt} + {if $adr.geoloc} - La geolocalisation n'a pas donné un résultat certain, vérifie la nouvelle adresse ou modifie l'ancienne pour que ton adresse puisse être prise en compte.
+ La geolocalisation n'a pas donné un résultat certain, vérifie la nouvelle adresse ou modifie l'ancienne pour que ton adresse puisse être prise en compte.

- + {if !$adr.cityid}style="background:#FAA"{/if} + >{$adr.txt}
-
-
- +
+
{$adr.geoloc}
+ [Valider]
@@ -38,7 +22,7 @@
+ >{$adr.txt} {/if} -- 2.1.4