Dodanie możliwości wypisania się z newslettera

This commit is contained in:
Roman Pyrih
2025-11-14 11:34:40 +01:00
parent 99ee04f426
commit 4a1e6a4424
4 changed files with 187 additions and 2 deletions

View File

@@ -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);
}
}