Switches geolocalization to Google Maps.
authorStéphane Jacob <jacou@melix.net>
Fri, 13 Feb 2009 00:56:24 +0000 (01:56 +0100)
committerStéphane Jacob <jacou@melix.net>
Sun, 15 Feb 2009 01:07:36 +0000 (02:07 +0100)
htdocs/javascript/profile.js
include/geoloc.inc.php
modules/profile.php
modules/profile/addresses.inc.php
modules/profile/page.inc.php
templates/geoloc/form.address.tpl
templates/profile/adresses.address.tpl
templates/profile/adresses.tpl
upgrade/newdirectory-0.0.1/15_addresses.sql [new file with mode: 0644]

index 9e58fa9..19589e4 100644 (file)
@@ -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('<div id="addresses_' + i + '_cont"></div>');
-    Ajax.update_html('addresses_' + i + '_cont', 'profile/ajax/address/' + i, checkCurrentAddress);
+    $('#add_address').before('<div id="addresses_' + i + '_cont"></div>');
+    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)
index 00e8fa9..c776e42 100644 (file)
@@ -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) => "&oelig;"));
-                $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) => "&oelig;"));
+                $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:
 ?>
index b6f5cfc..4583907 100644 (file)
@@ -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)
index 6fc1f72..6eca8bd 100644 (file)
@@ -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();
             }
index 45fc082..b46e23a 100644 (file)
@@ -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);
     }
 }
 
index 9e543e9..0f5cc34 100644 (file)
 {*                                                                        *}
 {**************************************************************************}
 
-<input type="hidden" name="{$name}[changed]" value="0"/>
-{if $adr.geoloc}
-<div class="erreur" id="{$id}_geoloc_error">
+{if $address.geoloc}
+<div class="erreur {$prefid}_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.
 </div>
-<script type="text/javascript">setTimeout("document.location += '#{$adid}'", 10);</script>
 {/if}
+
 <div>
-<textarea name="{$name}[text]" cols="30" rows="4"
-          onchange="form['{$name}[changed]'].value=1"
-          {if !$adr.cityid && $adr.datemaj}class="error"{/if}
-          >{$adr.text}</textarea>
-{if $adr.geoloc}
-<span id="{$id}_geoloc">
-<textarea cols="30" rows="4"
-          class="valid"
-          name="{$name}[geoloc]"
-          onclick="blur()"
-          >{$adr.geoloc}</textarea>
-<input type="hidden" name="{$name}[geoloc_cityid]" value="{$adr.geoloc_cityid}" />
-<input type="hidden" name="{$name}[parsevalid]" value="0" />
-</span>
+  <textarea name="{$prefname}[text]" cols="30" rows="4" onkeyup="addressChanged({$id})"
+            {if $address.geoloc}class="error"{/if}>{$address.text}</textarea>
+{if $address.geoloc}
+  <textarea cols="30" rows="4" class="valid {$prefid}_geoloc"
+            name="{$prefname}[geoloc]">{$address.geoloc}</textarea>
 </div>
-<div class="center" id="{$id}_geoloc_valid">
-  <a href="javascript:validAddress('{$id}', '{$name}')">Valider ta version</a>
+<div class="center {$prefid}_geoloc">
+  <a href="javascript:validGeoloc('{$id}', 0)">Valider ta version</a>
   &bull;
-  <a href="javascript:validGeoloc('{$id}', '{$name}')">Valider la version géolocalisée</a>
+  <a href="javascript:validGeoloc('{$id}', 1)">Valider la version géolocalisée</a>
 {/if}
 </div>
-<input type="hidden" name="{$name}[cityid]" value="{$adr.cityid}" />
-<input type="hidden" name="{$name}[adr1]" value="{$adr.adr1}" />
-<input type="hidden" name="{$name}[adr2]" value="{$adr.adr2}" />
-<input type="hidden" name="{$name}[adr3]" value="{$adr.adr3}" />
-<input type="hidden" name="{$name}[postcode]" value="{$adr.postcode}"/>
-<input type="hidden" name="{$name}[city]" value="{$adr.city}" />
-<input type="hidden" name="{$name}[country]" value="{$adr.country|default:'00'}" />
-<input type="hidden" name="{$name}[countrytxt]" value="{$adr.countrytxt}" />
-<input type="hidden" name="{$name}[region]" value="{$adr.region}" />
-<input type="hidden" name="{$name}[regiontxt]" value="{$adr.regiontxt}" />
-<input type="hidden" name="{$name}[checked]" value="{$adr.checked}" />
-<input type="hidden" name="{$name}[precise_lat]" value="{$adr.precise_lat}" />
-<input type="hidden" name="{$name}[precise_lon]" value="{$adr.precise_lon}" />
+{if $address.geoloc}
+<input type="hidden" name="{$prefname}[geoloc_choice]" value="1" />
+<input type="hidden" name="{$prefname}[geoloc]" value="{$address.geoloc}" />
+<input type="hidden" name="{$prefname}[updateTime]" value="{$address.updateTime}" />
+{/if}
+<input type="hidden" name="{$prefname}[type]" value="{$address.type}" />
+<input type="hidden" name="{$prefname}[accuracy]" value="{$address.accuracy}" />
+<input type="hidden" name="{$prefname}[postalAddress]" value="{$address.postalAddress}" />
+<input type="hidden" name="{$prefname}[line1]" value="{$address.line1}" />
+<input type="hidden" name="{$prefname}[line2]" value="{$address.line2}" />
+<input type="hidden" name="{$prefname}[line3]" value="{$address.line3}" />
+<input type="hidden" name="{$prefname}[postalCode]" value="{$address.postalCode}" />
+<input type="hidden" name="{$prefname}[administrativeAreaId]" value="{$address.administrativeAreaId}" />
+<input type="hidden" name="{$prefname}[subAdministrativeAreaId]" value="{$address.subAdministrativeAreaId}" />
+<input type="hidden" name="{$prefname}[locality]" value="{$address.locality}" />
+<input type="hidden" name="{$prefname}[administrativeArea]" value="{$address.administrativeArea}" />
+<input type="hidden" name="{$prefname}[subAdministrativeArea]" value="{$address.subAdministrativeArea}" />
+<input type="hidden" name="{$prefname}[localityId]" value="{$address.localityId}" />
+<input type="hidden" name="{$prefname}[countryId]" value="{$address.countryId}" />
+<input type="hidden" name="{$prefname}[latitude]" value="{$address.latitude}" />
+<input type="hidden" name="{$prefname}[longitude]" value="{$address.longitude}" />
+<input type="hidden" name="{$prefname}[north]" value="{$address.north}" />
+<input type="hidden" name="{$prefname}[south]" value="{$address.south}" />
+<input type="hidden" name="{$prefname}[east]" value="{$address.east}" />
+<input type="hidden" name="{$prefname}[west]" value="{$address.west}" />
+<input type="hidden" name="{$prefname}[cedex]" value="{$address.cedex}" />
+<input type="hidden" name="{$prefname}[updateTime]" value="{$address.updateTime}" />
+<input type="hidden" name="{$prefname}[changed]" value="0" />
+<input type="hidden" name="{$prefname}[removed]" value="0" />
+
 {* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *}
index 118b56a..3067c22 100644 (file)
 {*                                                                        *}
 {**************************************************************************}
 
-{assign var=adpref value="addresses[$i]"}
-{assign var=adid value="addresses_$i"}
-<input type="hidden" name="{$adpref}[removed]" value="0"/>
-<input type="hidden" name="{$adpref}[datemaj]" value="{$adr.datemaj}"/>
-<table class="bicol" style="display: none; margin-bottom: 1em" id="{$adid}_grayed">
+{assign var=prefname value="addresses[$i]"}
+{assign var=prefid value="addresses_$i"}
+<table class="bicol" style="display: none; margin-bottom: 1em" id="{$prefid}_grayed">
   <tr>
     <th class="grayed">
       <div style="float: right">
-        <a href="javascript:restoreAddress('{$adid}', '{$adpref}')">{icon name=arrow_refresh title="Restaurer l'adresse"}</a>
+        <a href="javascript:toggleAddress('{$i}', 0)">{icon name=arrow_refresh title="Restaurer l'adresse"}</a>
       </div>
       Restaurer l'adresse n°{$i+1}
     </th>
   </tr>
 </table>
-<table class="bicol" style="margin-bottom: 1em" id="{$adid}">
+<table class="bicol" style="margin-bottom: 1em" id="{$prefid}">
   <tr>
     <th>
       <div style="float: left">
-        <input name="{$adpref}[current]" type="radio" value="1" {if $adr.current}checked="checked"{/if}
-               id="{$adid}_current" onchange="checkCurrentAddress(this); return true" />
-        <label for="{$adid}_current" class="smaller" style="font-weight: normal">actuelle</label>
+        <label>
+          <input name="{$prefname}[current]" type="radio" {if $address.current}checked="checked"{/if}
+                      onchange="checkCurrentAddress({$i})" />
+          <span class="smaller" style="font-weight: normal">actuelle</span>
+        </label>
       </div>
       <div style="float: right">
-        <a href="javascript:removeAddress('{$adid}', '{$adpref}')">{icon name=cross title="Supprimer l'adresse"}</a>
+        <a href="javascript:toggleAddress('{$i}', 1)">
+          {icon name=cross title="Supprimer l'adresse"}
+        </a>
       </div>
       Adresse n°{$i+1}
     </th>
   <tr>
     <td>
       <div style="margin-bottom: 0.2em" class="flags">
-        {include file="include/flags.radio.tpl" name="`$adpref`[pub]" val=$adr.pub}
+        {include file="include/flags.radio.tpl" name="`$prefname`[pub]" val=$address.pub}
       </div>
       <div style="clear: both"></div>
-      <div style="float: left">{include file="geoloc/form.address.tpl" name=$adpref id=$adid adr=$adr}</div>
+      <div style="float: left">
+      {include file="geoloc/form.address.tpl" prefname=$prefname prefid=$prefid address=$address id=$i}
+      </div>
       <div style="float: left">
         <div>
-          <input type="radio" name="{$adpref}[temporary]" id="{$adid}_temp_0" value="0"
-                 {if !$adr.temporary}checked="checked"{/if} /><label for="{$adid}_temp_0">permanente</label>
-          <input type="radio" name="{$adpref}[temporary]" id="{$adid}_temp_1" value="1"
-                 {if $adr.temporary}checked="checked"{/if} /><label for="{$adid}_temp_1">temporaire</label>
+          <label>
+            <input type="radio" name="{$prefname}[temporary]" value="0"
+                   {if !$address.temporary}checked="checked"{/if} />
+            permanente
+          </label>
+          <label>
+            <input type="radio" name="{$prefname}[temporary]" value="1"
+                   {if $address.temporary}checked="checked"{/if} />
+            temporaire
+          </label>
         </div>
         <div>
-          <input type="radio" name="{$adpref}[secondaire]" id="{$adid}_sec_0" value="0"
-                 {if !$adr.secondaire}checked="checked"{/if} /><label for="{$adid}_sec_0">ma résidence principale</label>
-          <input type="radio" name="{$adpref}[secondaire]" id="{$adid}_sec_1" value="1"
-                 {if $adr.secondaire}checked="checked"{/if} /><label for="{$adid}_sec_1">une résidence secondaire</label>
+          <label>
+            <input type="radio" name="{$prefname}[secondary]" value="0"
+                   {if !$address.secondary}checked="checked"{/if} />
+            ma résidence principale
+          </label>
+          <label>
+            <input type="radio" name="{$prefname}[secondary]" value="1"
+                   {if $address.secondary}checked="checked"{/if} />
+            une résidence secondaire
+          </label>
         </div>
         <div>
-          <input type="checkbox" name="{$adpref}[mail]" id="{$adid}_mail"
-                 {if $adr.mail}checked="checked"{/if} />
-          <label for="{$adid}_mail">on peut m'y envoyer du courrier par la poste</label>
+          <label>
+            <input type="checkbox" name="{$prefname}[mail]" {if $address.mail}checked="checked"{/if} />
+            on peut m'y envoyer du courrier par la poste
+          </label>
         </div>
         <div>
-          <label for="{$adpref}[comment]">Commentaire : </label>
-          <input type="text" size="35" maxlength="100" name="{$adpref}[comment]" id="{$adpref}_comment" value="{$adr.comment}" />
+          <label>
+            Commentaire&nbsp;:
+            <input type="text" size="35" maxlength="100"
+                   name="{$prefname}[comment]" value="{$address.comment}" />
+          </label>
         </div>
       </div>
     </td>
   </tr>
   <tr class="pair">
     <td>
-      {foreach from=$adr.tel key=t item=tel}
-        <div id="{"`$adid`_tel_`$t`"}" style="clear: both">
-          {include file="profile/phone.tpl" prefname="`$adpref`[tel]" prefid="`$adid`_tel" telid=$t tel=$tel}
+      {foreach from=$address.tel key=t item=tel}
+        <div id="{"`$prefid`_tel_`$t`"}" style="clear: both">
+          {include file="profile/phone.tpl" prefname="`$prefname`[tel]"
+                   prefid="`$prefid`_tel" telid=$t tel=$tel}
         </div>
       {/foreach}
-      {if $adr.tel|@count eq 0}
-        <div id="{"`$adid`_tel_0"}" style="clear: both">
-          {include file="profile/phone.tpl" prefname="`$adpref`[tel]" prefid="`$adid`_tel" telid=0 tel=0}
+      {if $address.tel|@count eq 0}
+        <div id="{"`$prefid`_tel_0"}" style="clear: both">
+          {include file="profile/phone.tpl" prefname="`$prefname`[tel]" prefid="`$prefid`_tel" telid=0 tel=0}
         </div>
       {/if}
-      <div id="{$adid}_tel_add" class="center" style="clear: both; padding-top: 4px">
-        <a href="javascript:addTel('{$adid}_tel', '{$adpref}[tel]')">
+      <div id="{$prefid}_tel_add" class="center" style="clear: both; padding-top: 4px">
+        <a href="javascript:addTel('{$prefid}_tel', '{$prefname}[tel]')">
           {icon name=add title="Ajouter un numéro de téléphone"} Ajouter un numéro de téléphone
         </a>
       </div>
index 845279d..19fa656 100644 (file)
 {*                                                                        *}
 {**************************************************************************}
 
-{foreach key=i item=adr from=$addresses}
+{foreach key=i item=address from=$addresses}
 <div id="{"addresses_`$i`_cont"}">
-{include file="profile/adresses.address.tpl" i=$i adr=$adr}
+{include file="profile/adresses.address.tpl" i=$i address=$address}
 </div>
 {/foreach}
 {if $addresses|@count eq 0}
 <div id="addresses_0_cont">
-{include file="profile/adresses.address.tpl" i=0 adr=0}
+{include file="profile/adresses.address.tpl" i=0 address=0}
 </div>
 {/if}
 
-<div id="add_adr" class="center">
+<div id="add_address" class="center">
   <a href="javascript:addAddress()">
     {icon name=add title="Ajouter une adresse"} Ajouter une adresse
   </a>
diff --git a/upgrade/newdirectory-0.0.1/15_addresses.sql b/upgrade/newdirectory-0.0.1/15_addresses.sql
new file mode 100644 (file)
index 0000000..e437e22
--- /dev/null
@@ -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: