Requires address validation properly, ie when addresses are saved (and fixes a few...
authorStéphane Jacob <sj@m4x.org>
Wed, 1 Jun 2011 16:07:05 +0000 (18:07 +0200)
committerStéphane Jacob <sj@m4x.org>
Wed, 1 Jun 2011 16:07:05 +0000 (18:07 +0200)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
classes/address.php
htdocs/javascript/profile.js
include/validations/address.inc.php
modules/profile.php
modules/profile/addresses.inc.php
templates/geoloc/form.address.tpl
templates/profile/adresses.address.tpl
templates/profile/adresses.tpl

index 46fbebe..7080158 100644 (file)
@@ -355,7 +355,7 @@ class Address
                                                                                array('', "\n"), $this->text)), 'CEDEX')) !== false);
             }
         }
-        $this->request = !is_null(AddressReq::get_request($this->pid, $this->jobid, $this->groupid, $this->type, $this->text));
+        $this->request = ($this->request || !is_null(AddressReq::get_request($this->pid, $this->jobid, $this->groupid, $this->type, $this->id)));
     }
 
     public function setId($id)
@@ -589,13 +589,13 @@ class Address
         if ($this->changed == 1) {
             $gmapsGeocoder = new GMapsGeocoder();
             $gmapsGeocoder->getGeocodedAddress($this);
-        }
 
-        $componants = array();
-        foreach ($this->components as $component) {
-            $componants[] = Geocoder::getComponentId($component);
+            $componants = array();
+            foreach ($this->components as $component) {
+                $componants[] = Geocoder::getComponentId($component);
+            }
+            $this->componentsIds = implode(',', $componants);
         }
-        $this->componentsIds = implode(',', $componants);
 
         return true;
     }
@@ -705,44 +705,35 @@ class Address
             if ($this->type == self::LINK_PROFILE) {
                 Phone::savePhones($this->phones, $this->pid, Phone::LINK_ADDRESS, $this->id);
             }
+
+            if ($this->request) {
+                $req = new AddressReq(S::user(), $this->toFormArray(), $this->pid, $this->jobid, $this->groupid, $this->type, $this->id);
+                $req->submit();
+            }
         }
     }
 
     public function updateGeocoding($text)
     {
-        $id = null;
-        $texts = XDB::fetchAllAssoc('id', 'SELECT  id, text
-                                   FROM  profile_addresses
-                                  WHERE  pid = {?} AND jobid = {?} AND groupid = {?} AND type = {?}',
-                                $this->pid, $this->jobid, $this->groupid, $this->type);
-        $text = preg_replace('/\s+/', ' ', $text);
-        foreach ($texts as $key => $value) {
-            if (strcmp($text, preg_replace('/\s+/', ' ', $value)) == 0) {
-                $id = $key;
-                break;
-            }
-        }
-        if (!is_null($id)) {
-            XDB::execute('UPDATE  profile_addresses
-                             SET  text = {?}, postalText = {?}, types = {?}, formatted_address = {?},
-                                  location_type = {?}, partial_match = {?}, latitude = {?}, longitude = {?},
-                                  southwest_latitude = {?}, southwest_longitude = {?}, northeast_latitude = {?}, northeast_longitude = {?},
-                                  geocoding_date = {?}, geocoding_calls = NOW()
-                           WHERE  pid = {?} AND jobid = {?} AND groupid = {?} AND type = {?} AND id = {?}',
-                         $this->text, $this->postalText, $this->types, $this->formatted_address,
-                         $this->location_type, $this->partial_match, $this->latitude, $this->longitude,
-                         $this->southwest_latitude, $this->southwest_longitude, $this->northeast_latitude, $this->northeast_longitude,
-                         $this->pid, $this->jobid, $this->groupid, $this->type, $id, $this->geocoding_calls);
-
-            XDB::execute('DELETE FROM  profile_addresses_components
-                                WHERE  pid = {?} AND jobid = {?} AND groupid = {?} AND type = {?} AND id = {?}',
-                         $this->pid, $this->jobid, $this->groupid, $this->type, $id);
-            if ($this->componentsIds) {
-                foreach (explode(',', $this->componentsIds) as $component_id) {
-                    XDB::execute('INSERT IGNORE INTO  profile_addresses_components (pid, jobid, groupid, type, id, component_id)
-                                              VALUES  ({?}, {?}, {?}, {?}, {?}, {?})',
-                                 $this->pid, $this->jobid, $this->groupid, $this->type, $id, $component_id);
-                }
+        XDB::execute('UPDATE  profile_addresses
+                         SET  text = {?}, postalText = {?}, types = {?}, formatted_address = {?},
+                              location_type = {?}, partial_match = {?}, latitude = {?}, longitude = {?},
+                              southwest_latitude = {?}, southwest_longitude = {?}, northeast_latitude = {?}, northeast_longitude = {?},
+                              geocoding_date = NOW(), geocoding_calls = {?}
+                       WHERE  pid = {?} AND jobid = {?} AND groupid = {?} AND type = {?} AND id = {?}',
+                     $this->text, $this->postalText, $this->types, $this->formatted_address,
+                     $this->location_type, $this->partial_match, $this->latitude, $this->longitude,
+                     $this->southwest_latitude, $this->southwest_longitude, $this->northeast_latitude, $this->northeast_longitude, $this->geocoding_calls,
+                     $this->pid, $this->jobid, $this->groupid, $this->type, $this->id);
+
+        XDB::execute('DELETE FROM  profile_addresses_components
+                            WHERE  pid = {?} AND jobid = {?} AND groupid = {?} AND type = {?} AND id = {?}',
+                     $this->pid, $this->jobid, $this->groupid, $this->type, $this->id);
+        if ($this->componentsIds) {
+            foreach (explode(',', $this->componentsIds) as $component_id) {
+                XDB::execute('INSERT IGNORE INTO  profile_addresses_components (pid, jobid, groupid, type, id, component_id)
+                                          VALUES  ({?}, {?}, {?}, {?}, {?}, {?})',
+                             $this->pid, $this->jobid, $this->groupid, $this->type, $this->id, $component_id);
             }
         }
     }
index beb65ee..f117885 100644 (file)
@@ -317,14 +317,14 @@ function checkCurrentAddress(id)
     }
 }
 
-function addAddress()
+function addAddress(pid)
 {
     var i = 0;
     while ($('#addresses_' + i + '_cont').length != 0) {
         i++;
     }
     $('#add_address').before('<div id="addresses_' + i + '_cont"></div>');
-    $('#addresses_' + i + '_cont').updateHtml('profile/ajax/address/' + i,
+    $('#addresses_' + i + '_cont').updateHtml('profile/ajax/address/' + i + '/' + pid,
                                               checkCurrentAddress());
 }
 
@@ -333,13 +333,18 @@ function addressChanged(prefid, color)
     var text = $('#' + prefid + '_cont').find("[name*='[text]']").val();
     $('#' + prefid + '_cont').find('[name*=changed]').val("1");
     $.xpost('map_url/', { text:text, color:color }, function(data) {
-        $('.static_map_url').find('img').attr('src', data);
+        $('#' + prefid + '_static_map_url').show();
+        $('#' + prefid + '_static_map_url').find('img').attr('src', data);
     });
 }
 
-function deleteGeocoding(prefid, hrpid)
+function deleteGeocoding(prefid)
 {
-    var confirmation = confirm(
+    if($('#' + prefid + '_geocoding_removal').find('[name*=request]:checkbox:checked').length == 0) {
+        return true;
+    }
+
+    return confirm(
         "La localisation de l'adresse sert à deux choses : te placer dans "
         + "le planisphère et te faire apparaître dans la recherche avancée par "
         + "pays, région, département, ville... La supprimer t'en fera disparaître. "
@@ -347,12 +352,6 @@ function deleteGeocoding(prefid, hrpid)
         + "est réellement erronée. Avant de supprimer cette localisation, l'équipe de "
         + "Polytechnique.org tentera de la réparer.\n\nConfirmes-tu ta "
         + "demande de suppression de cette localisation ?");
-
-    if (confirmation) {
-        var address = $('#' + prefid).find("[name*='[text]']").val();
-        $.xpost('profile/ajax/address/del/' + hrpid, { address:address });
-        $('#' + prefid + '_geocoding_removal').html('Localisation en attente de validation.');
-    }
 }
 
 // {{{1 Phones
index 349674a..b31bc9b 100644 (file)
@@ -26,10 +26,11 @@ class AddressReq extends ProfileValidate
     // {{{ properties
 
     // Address primary field that are not in its formatted array.
-    public $pid;
-    public $jobid;
-    public $groupid;
-    public $type;
+    public $key_pid;
+    public $key_jobid;
+    public $key_groupid;
+    public $key_type;
+    public $key_id;
 
     // We need the text given by the user, and the toy version to try to improve
     // the geocoding.
@@ -43,17 +44,17 @@ class AddressReq extends ProfileValidate
     // }}}
     // {{{ constructor
 
-    public function __construct(User $_user, Profile $_profile, $_text, $_pid, $_jobid, $_groupid, $_type, $_stamp = 0)
+    public function __construct(User $_user, array $_address, $_pid, $_jobid, $_groupid, $_type, $_id, $_stamp = 0)
     {
+        $_profile = Profile::get($_pid);
         parent::__construct($_user, $_profile, false, 'address', $_stamp);
         $this->key_pid = $_pid;
         $this->key_jobid = $_jobid;
         $this->key_groupid = $_groupid;
         $this->key_type = $_type;
-        $this->given_text = $_text;
-        $address = new Address(array('changed' => 1, 'text' => $_text));
-        $address->format();
-        $this->address = $address->toFormArray();
+        $this->key_id = $_id;
+        $this->given_text = $_address['text'];
+        $this->address = $_address;
     }
 
     // }}}
@@ -115,10 +116,11 @@ class AddressReq extends ProfileValidate
     public function commit()
     {
         $this->address = array_merge($this->address, array(
-            'pid' => $this->key_pid,
-            'jobid' => $this->key_jobid,
+            'pid'     => $this->key_pid,
+            'jobid'   => $this->key_jobid,
             'groupid' => $this->key_groupid,
-            'type' => $this->key_type
+            'type'    => $this->key_type,
+            'id'      => $this->key_id
         ));
         $this->address['text'] = ($this->modified ? $this->toy_text : $this->given_text);;
         $this->address['changed'] = 0;
@@ -132,12 +134,12 @@ class AddressReq extends ProfileValidate
     // }}}
     // {{{ function get_request()
 
-    static public function get_request($pid, $jobid, $groupid, $type, $address)
+    static public function get_request($pid, $jobid, $groupid, $type, $id)
     {
         $reqs = parent::get_typed_requests($pid, 'address');
         foreach ($reqs as &$req) {
             if ($req->key_pid == $pid && $req->key_jobid == $jobid && $req->key_groupid == $groupid
-                && $req->key_type == $type && $req->address['text'] == $address) {
+                && $req->key_type == $type && $req->key_id == $id) {
                 return $req;
             }
         }
@@ -153,14 +155,7 @@ class AddressReq extends ProfileValidate
         $requests = parent::get_all_typed_requests('address');
         foreach ($requests as &$req) {
             if ($req->key_pid == $pid && $req->key_jobid == $jobid && $req->key_groupid == $groupid && $req->key_type == $type) {
-                $count = XDB::fetchOneCell('SELECT  COUNT(*)
-                                              FROM  profile_addresses
-                                             WHERE  pid = {?} AND jobid = {?} AND groupid = {?}
-                                                    AND type = {?} AND text = {?}',
-                                           $pid, $jobid, $groupid, $type, $req->address['text']);
-                if ($count == 0) {
-                    $req->clean();
-                }
+                $req->clean();
             }
         }
     }
index ff7d9e4..c464e94 100644 (file)
@@ -396,26 +396,15 @@ class ProfileModule extends PLModule
         $page->assign('medal_list', $mlist);
     }
 
-    function handler_ajax_address($page, $id)
+    function handler_ajax_address($page, $id, $pid)
     {
         pl_content_headers("text/html");
         $page->changeTpl('profile/adresses.address.tpl', NO_SKIN);
         $page->assign('i', $id);
         $page->assign('address', array());
-    }
-
-    function handler_ajax_address_del($page, $hrpid)
-    {
-        if ($profile = Profile::get($hrpid)) {
-            if (S::user()->canEdit($profile)) {
-                $address = Post::t('address');
-                if (is_null(AddressReq::get_request($profile->id(), 0, 0, Address::LINK_PROFILE, $address))) {
-                    $req = new AddressReq(S::user(), $profile, $address, $profile->id(), 0, 0, Address::LINK_PROFILE);
-                    $req->submit();
-                }
-            }
-        }
-        exit();
+        $page->assign('profile', Profile::get($pid));
+        $page->assign('isMe', true);
+        $page->assign('geocoding_removal', true);
     }
 
     function handler_ajax_tel($page, $prefid, $prefname, $telid, $subField, $mainField, $mainId)
index 62d21ea..50f86fd 100644 (file)
@@ -47,8 +47,8 @@ class ProfileSettingAddresses implements ProfileSetting
 
         Phone::deletePhones($page->pid(), Phone::LINK_ADDRESS, null, $deletePrivate);
         Address::deleteAddresses($page->pid(), Address::LINK_PROFILE, null, null, $deletePrivate);
-        Address::saveFromArray($value, $page->pid(), Address::LINK_PROFILE, null, $deletePrivate);
         AddressReq::purge_requests($page->pid(), 0, 0, Address::LINK_PROFILE);
+        Address::saveFromArray($value, $page->pid(), Address::LINK_PROFILE, null, $deletePrivate);
         if (S::user()->isMe($page->owner) && count($value) > 1) {
             Platal::page()->trigWarning('Attention, tu as plusieurs adresses sur ton profil. Pense à supprimer celles qui sont obsolètes.');
         }
index 8bde680..99e9b69 100644 (file)
   </td>
   <td>
 {/if}
-  <div class="static_map_url" {if !t($address.latitude)}style="display: none"{/if}>
+  <div id="{$prefid}_static_map_url" {if !t($address.latitude)}style="display: none"{/if}>
     <img src="{insert name="getStaticMapURL" latitude=$address.latitude longitude=$address.longitude color=$profile->promoColor()}" alt="Position de l'adresse" />
     {if t($geocoding_removal)}
     <br />
     <small id="{$prefid}_geocoding_removal">
     {if !t($address.request)}
-      <a href="javascript:deleteGeocoding('{$prefid}', '{$hrpid}')">{icon name=cross title="Adresse mal localisée"} Signaler que le repère est mal placé</a>
+      <label><input type="checkbox" name="{$prefname}[request]" onclick="return deleteGeocoding('{$prefid}')" /> Signaler que le repère est mal placé</label>
     {else}
     Localisation en attente de validation.
+    <input type="hidden" name="{$prefname}[request]" value="{$address.request}" />
     {/if}
     </small>
     {/if}
index 712212c..7f32152 100644 (file)
@@ -65,7 +65,7 @@
   <tr {if $hiddenaddr}style="display: none"{/if}>
   {if !$isMe}
     <td>
-      <small><strong>Adress postale&nbsp;:</strong><br />{$address.postalText|nl2br}</small>
+      <small><strong>Adresse postale&nbsp;:</strong><br />{$address.postalText|nl2br}</small>
     </td>
     <td>
   {else}
index 2fd4d33..561b38b 100644 (file)
@@ -34,7 +34,7 @@
 {/if}
 
 <div id="add_address" class="center">
-  <a href="javascript:addAddress()">
+  <a href="javascript:addAddress({$profile->id()})">
     {icon name=add title="Ajouter une adresse"} Ajouter une adresse
   </a>
 </div>