From a3328b1eef7871d9860be82a1d1a65b1572f6880 Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Jacob?= Date: Wed, 1 Jun 2011 18:07:05 +0200 Subject: [PATCH] Requires address validation properly, ie when addresses are saved (and fixes a few addresses related issues). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Jacob --- classes/address.php | 69 +++++++++++++++------------------- htdocs/javascript/profile.js | 21 +++++------ include/validations/address.inc.php | 39 +++++++++---------- modules/profile.php | 19 ++-------- modules/profile/addresses.inc.php | 2 +- templates/geoloc/form.address.tpl | 5 ++- templates/profile/adresses.address.tpl | 2 +- templates/profile/adresses.tpl | 2 +- 8 files changed, 67 insertions(+), 92 deletions(-) diff --git a/classes/address.php b/classes/address.php index 46fbebe..7080158 100644 --- a/classes/address.php +++ b/classes/address.php @@ -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); } } } diff --git a/htdocs/javascript/profile.js b/htdocs/javascript/profile.js index beb65ee..f117885 100644 --- a/htdocs/javascript/profile.js +++ b/htdocs/javascript/profile.js @@ -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('
'); - $('#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 diff --git a/include/validations/address.inc.php b/include/validations/address.inc.php index 349674a..b31bc9b 100644 --- a/include/validations/address.inc.php +++ b/include/validations/address.inc.php @@ -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(); } } } diff --git a/modules/profile.php b/modules/profile.php index ff7d9e4..c464e94 100644 --- a/modules/profile.php +++ b/modules/profile.php @@ -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) diff --git a/modules/profile/addresses.inc.php b/modules/profile/addresses.inc.php index 62d21ea..50f86fd 100644 --- a/modules/profile/addresses.inc.php +++ b/modules/profile/addresses.inc.php @@ -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.'); } diff --git a/templates/geoloc/form.address.tpl b/templates/geoloc/form.address.tpl index 8bde680..99e9b69 100644 --- a/templates/geoloc/form.address.tpl +++ b/templates/geoloc/form.address.tpl @@ -52,15 +52,16 @@ {/if} -
+
promoColor()}" alt="Position de l'adresse" /> {if t($geocoding_removal)}
{if !t($address.request)} - {icon name=cross title="Adresse mal localisée"} Signaler que le repère est mal placé + {else} Localisation en attente de validation. + {/if} {/if} diff --git a/templates/profile/adresses.address.tpl b/templates/profile/adresses.address.tpl index 712212c..7f32152 100644 --- a/templates/profile/adresses.address.tpl +++ b/templates/profile/adresses.address.tpl @@ -65,7 +65,7 @@ {if !$isMe} - Adress postale :
{$address.postalText|nl2br}
+ Adresse postale :
{$address.postalText|nl2br}
{else} diff --git a/templates/profile/adresses.tpl b/templates/profile/adresses.tpl index 2fd4d33..561b38b 100644 --- a/templates/profile/adresses.tpl +++ b/templates/profile/adresses.tpl @@ -34,7 +34,7 @@ {/if} -- 2.1.4