diff --git a/modules/ps_emailsubscription/controllers/front/unsubscribe.php b/modules/ps_emailsubscription/controllers/front/unsubscribe.php new file mode 100644 index 00000000..81da5ae3 --- /dev/null +++ b/modules/ps_emailsubscription/controllers/front/unsubscribe.php @@ -0,0 +1,34 @@ +message = $this->module->trans( + 'Invalid unsubscribe link.', + [], + 'Modules.Emailsubscription.Shop' + ); + } else { + $this->message = $this->module->unsubscribeByToken($token); + } + } + + public function initContent() + { + parent::initContent(); + + $this->context->smarty->assign('message', $this->message); + $this->setTemplate( + 'module:ps_emailsubscription/views/templates/front/unsubscribe_execution.tpl' + ); + } +} diff --git a/modules/ps_emailsubscription/mails/pl/newsletter_conf.html b/modules/ps_emailsubscription/mails/pl/newsletter_conf.html index 69b9e1dd..8dbccc3c 100644 --- a/modules/ps_emailsubscription/mails/pl/newsletter_conf.html +++ b/modules/ps_emailsubscription/mails/pl/newsletter_conf.html @@ -292,6 +292,12 @@ 🎁 Oto Twój kod rabatowy na pierwsze zakupy: {discount}

Wykorzystaj go na lulandia.pl i ciesz się komfortowym snem! +
+
+

+ Nie chcemy zakłócać Twojego spokoju ????
+ Jeśli wolisz, by nasze wiadomości Cię nie budziły,Wypisz mnie. +
diff --git a/modules/ps_emailsubscription/ps_emailsubscription.php b/modules/ps_emailsubscription/ps_emailsubscription.php index e4403226..1cb7c5e9 100644 --- a/modules/ps_emailsubscription/ps_emailsubscription.php +++ b/modules/ps_emailsubscription/ps_emailsubscription.php @@ -819,16 +819,29 @@ class Ps_Emailsubscription extends Module implements WidgetInterface * * @return bool */ - protected function sendConfirmationEmail($email, $code) + function sendConfirmationEmail($email, $code) { $language = new Language($this->context->language->id); + $unsubscribeToken = $this->getUnsubscribeTokenByEmail($email); + $unsubscribeUrl = ''; + + if ($unsubscribeToken) { + $unsubscribeUrl = Context::getContext()->link->getModuleLink( + 'ps_emailsubscription', + 'unsubscribe', + ['token' => $unsubscribeToken], + true + ); + } + return Mail::send( $this->context->language->id, 'newsletter_conf', 'Potwierdzenie zapisu do newslettera', [ - '{discount}' => $code, + '{discount}' => $code, + '{unsubscribe_url}' => $unsubscribeUrl, ], pSQL($email), null, @@ -842,6 +855,7 @@ class Ps_Emailsubscription extends Module implements WidgetInterface ); } + /** * Send a verification email. * @@ -1470,4 +1484,105 @@ class Ps_Emailsubscription extends Module implements WidgetInterface return json_encode($this->trans('Newsletter subscription: no email to export, this customer has not registered.', [], 'Modules.Emailsubscription.Admin')); } } + + protected function getGuestEmailByUnsubscribeToken($token) + { + $sql = 'SELECT `email` + FROM `' . _DB_PREFIX_ . 'emailsubscription` + WHERE MD5(CONCAT(`email`, `newsletter_date_add`, \'' . pSQL(Configuration::get('NW_SALT')) . '\')) + = \'' . pSQL($token) . '\' + AND `active` = 1'; + + return Db::getInstance()->getValue($sql); + } + + /** + * Customer email for unsubscribe token. + */ + protected function getUserEmailByUnsubscribeToken($token) + { + $sql = 'SELECT `email` + FROM `' . _DB_PREFIX_ . 'customer` + WHERE MD5(CONCAT(`email`, `date_add`, \'' . pSQL(Configuration::get('NW_SALT')) . '\')) + = \'' . pSQL($token) . '\' + AND `newsletter` = 1'; + + return Db::getInstance()->getValue($sql); + } + /** + * Unsubscribe by token from email. + * + * @param string $token + * + * @return string message + */ + public function unsubscribeByToken($token) + { + $email = null; + $isGuest = false; + + if ($email = $this->getGuestEmailByUnsubscribeToken($token)) { + $isGuest = true; + } elseif ($email = $this->getUserEmailByUnsubscribeToken($token)) { + $isGuest = false; + } else { + return $this->trans( + 'Nieprawidłowy lub wygasły link do rezygnacji z subskrypcji.', + [], + 'Modules.Emailsubscription.Shop' + ); + } + + if ($isGuest) { + $sql = 'DELETE FROM `' . _DB_PREFIX_ . 'emailsubscription` + WHERE `email` = \'' . pSQL($email) . '\' + AND `id_shop` = ' . (int) $this->context->shop->id; + } else { + $sql = 'UPDATE `' . _DB_PREFIX_ . 'customer` + SET `newsletter` = 0 + WHERE `email` = \'' . pSQL($email) . '\' + AND `id_shop` = ' . (int) $this->context->shop->id; + } + + if (!Db::getInstance()->execute($sql)) { + return $this->trans( + 'Wystąpił błąd podczas próby rezygnacji z subskrypcji.', + [], + 'Modules.Emailsubscription.Shop' + ); + } + + return $this->trans( + 'Wypisałeś się z newslettera.', + [], + 'Modules.Emailsubscription.Shop' + ); + } + /** + * Token used in unsubscribe link (for active subscribers). + */ + protected function getUnsubscribeTokenByEmail($email) + { + // guest subscription + $sql = 'SELECT MD5(CONCAT(`email`, `newsletter_date_add`, \'' . pSQL(Configuration::get('NW_SALT')) . '\')) AS token + FROM `' . _DB_PREFIX_ . 'emailsubscription` + WHERE `active` = 1 + AND `email` = \'' . pSQL($email) . '\' + AND `id_shop` = ' . (int) $this->context->shop->id; + + $token = Db::getInstance()->getValue($sql); + if ($token) { + return $token; + } + + // customer subscription + $sql = 'SELECT MD5(CONCAT(`email`, `date_add`, \'' . pSQL(Configuration::get('NW_SALT')) . '\')) AS token + FROM `' . _DB_PREFIX_ . 'customer` + WHERE `newsletter` = 1 + AND `email` = \'' . pSQL($email) . '\' + AND `id_shop` = ' . (int) $this->context->shop->id; + + return Db::getInstance()->getValue($sql); + } + } diff --git a/modules/ps_emailsubscription/views/templates/front/unsubscribe_execution.tpl b/modules/ps_emailsubscription/views/templates/front/unsubscribe_execution.tpl new file mode 100644 index 00000000..3c99e3ee --- /dev/null +++ b/modules/ps_emailsubscription/views/templates/front/unsubscribe_execution.tpl @@ -0,0 +1,31 @@ +{extends file='page.tpl'} + +{block name="page_title"} + {l s='Newsletter' d='Modules.Emailsubscription.Shop'} +{/block} + +{block name="page_content"} +
+ + +

+ {$message|escape:'htmlall':'UTF-8'} +

+ +

+ {l s='Nie będziesz już otrzymywać od nas wiadomości newsletterowych na ten adres e-mail.' d='Modules.Emailsubscription.Shop'} +

+ +

+ {l s='Jeśli wypisałeś(-aś) się przez pomyłkę, możesz w każdej chwili ponownie zapisać się do newslettera.' d='Modules.Emailsubscription.Shop'} +

+ + + {l s='Przejdź do strony głównej' d='Modules.Emailsubscription.Shop'} + +
+{/block}