From e90089e9dc5a97b9534628c128213d9c4be591a2 Mon Sep 17 00:00:00 2001 From: left23 Date: Tue, 7 Jun 2022 11:48:16 +0200 Subject: [PATCH 1/3] RW-573 remove unsubscribe form rules from rw-user component in favour of placing them in rw-form which is part of the global library thus available without needing to attach a library to a template --- .../components/rw-form/rw-form.css | 14 ++++++++++++++ .../components/rw-user/rw-user.css | 7 +------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/html/themes/custom/common_design_subtheme/components/rw-form/rw-form.css b/html/themes/custom/common_design_subtheme/components/rw-form/rw-form.css index 8301cf0d4..be1d91867 100644 --- a/html/themes/custom/common_design_subtheme/components/rw-form/rw-form.css +++ b/html/themes/custom/common_design_subtheme/components/rw-form/rw-form.css @@ -541,3 +541,17 @@ legend.form-required::after { form:not(.rw-entity-form-altered) .form-actions.form-item { margin-top: var(--cd-flow-space, 2.5rem); } + +/** + * Unsubscribe form. + * + * These rules are place here because this CSS is loaded globally. + */ +.unsubscribe-form .form-checkboxes .form-type-checkbox { + display: block; +} + +/* Cancel button on unsubscribe form */ +.unsubscribe-form > .button + a { + margin-left: 1rem; +} diff --git a/html/themes/custom/common_design_subtheme/components/rw-user/rw-user.css b/html/themes/custom/common_design_subtheme/components/rw-user/rw-user.css index 7a271b950..573570694 100644 --- a/html/themes/custom/common_design_subtheme/components/rw-user/rw-user.css +++ b/html/themes/custom/common_design_subtheme/components/rw-user/rw-user.css @@ -4,8 +4,7 @@ * Style the form in the page where users can subscribe to ReliefWeb * notifications. */ -.subscription-form .form-checkboxes .form-type-checkbox, -.unsubscribe-form .form-checkboxes .form-type-checkbox { +.subscription-form .form-checkboxes .form-type-checkbox { display: block; } @@ -34,10 +33,6 @@ .subscription-form .form-actions.form-item { margin-top: 24px; } -/* Cancel button on unsubscribe form */ -.unsubscribe-form > .button + a { - margin-left: 1rem; -} /** * User dashboard. From 6a4dce2c64d8f52a0c11b68ed34cb358aa381387 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 7 Jun 2022 17:01:19 +0200 Subject: [PATCH 2/3] bug: Unsubscribe countries Refs: #RW-582 --- .../src/Form/SubscriptionForm.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/html/modules/custom/reliefweb_subscriptions/src/Form/SubscriptionForm.php b/html/modules/custom/reliefweb_subscriptions/src/Form/SubscriptionForm.php index 718166ed3..f77bab991 100644 --- a/html/modules/custom/reliefweb_subscriptions/src/Form/SubscriptionForm.php +++ b/html/modules/custom/reliefweb_subscriptions/src/Form/SubscriptionForm.php @@ -169,6 +169,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { } } + $active_subscriptions = []; $subscriptions = $form_state->getValue('country_updates'); foreach ($subscriptions as $sid => $value) { if ($sid === '_none') { @@ -179,9 +180,14 @@ public function submitForm(array &$form, FormStateInterface $form_state) { } else { $this->subscribe($form_state->getValue('uid'), $sid); + $active_subscriptions[] = $sid; } } + if (!empty($active_subscriptions)) { + $this->unsubscribeOtherCountries($form_state->getValue('uid'), $active_subscriptions); + } + // Show the user a message. $this->messenger()->addStatus($this->t('Subscriptions successfully updated.')); } @@ -235,4 +241,20 @@ public function unsubscribe($uid, $sid) { ->execute(); } + /** + * Remove a user subscription. + * + * @param int $uid + * User id. + * @param array $sids + * Subscription id. + */ + public function unsubscribeOtherCountries($uid, array $sids) { + $this->database->delete('reliefweb_subscriptions_subscriptions') + ->condition('sid', 'country_updates_%', 'LIKE') + ->condition('sid', $sids, 'NOT IN') + ->condition('uid', $uid) + ->execute(); + } + } From 83db9ed09c412a7eb476dba8af33923e612d2a11 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Wed, 8 Jun 2022 09:22:23 +0200 Subject: [PATCH 3/3] bug: Unsubscribe last country as well Refs: #RW-582 --- .../src/Form/SubscriptionForm.php | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/html/modules/custom/reliefweb_subscriptions/src/Form/SubscriptionForm.php b/html/modules/custom/reliefweb_subscriptions/src/Form/SubscriptionForm.php index f77bab991..8a5535ff2 100644 --- a/html/modules/custom/reliefweb_subscriptions/src/Form/SubscriptionForm.php +++ b/html/modules/custom/reliefweb_subscriptions/src/Form/SubscriptionForm.php @@ -184,9 +184,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { } } - if (!empty($active_subscriptions)) { - $this->unsubscribeOtherCountries($form_state->getValue('uid'), $active_subscriptions); - } + $this->unsubscribeOtherCountries($form_state->getValue('uid'), $active_subscriptions); // Show the user a message. $this->messenger()->addStatus($this->t('Subscriptions successfully updated.')); @@ -250,11 +248,19 @@ public function unsubscribe($uid, $sid) { * Subscription id. */ public function unsubscribeOtherCountries($uid, array $sids) { - $this->database->delete('reliefweb_subscriptions_subscriptions') - ->condition('sid', 'country_updates_%', 'LIKE') - ->condition('sid', $sids, 'NOT IN') - ->condition('uid', $uid) - ->execute(); + if (empty($sids)) { + $this->database->delete('reliefweb_subscriptions_subscriptions') + ->condition('sid', 'country_updates_%', 'LIKE') + ->condition('uid', $uid) + ->execute(); + } + else { + $this->database->delete('reliefweb_subscriptions_subscriptions') + ->condition('sid', 'country_updates_%', 'LIKE') + ->condition('sid', $sids, 'NOT IN') + ->condition('uid', $uid) + ->execute(); + } } }