Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add date_add, date_upd, soft delete and notification send time #120

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions MailAlert.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class MailAlert extends ObjectModel

public $id_lang;

/** @var string Object creation date */
public $date_add;

/** @var string Object last modification date */
public $date_upd;

/**
* @see ObjectModel::$definition
*/
Expand All @@ -44,6 +50,8 @@ class MailAlert extends ObjectModel
'id_product_attribute' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true],
'id_shop' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true],
'id_lang' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true],
'date_add' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
'date_upd' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
],
];

Expand All @@ -68,18 +76,19 @@ public static function customerHasNotification($id_customer, $id_product, $id_pr
SELECT *
FROM `' . _DB_PREFIX_ . self::$definition['table'] . '`
WHERE ' . $where . '
AND deleted = 0
AND `id_product` = ' . (int) $id_product . '
AND `id_product_attribute` = ' . (int) $id_product_attribute . '
AND `id_shop` = ' . (int) $id_shop;

return count(Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS($sql));
}

public static function deleteAlert($id_customer, $customer_email, $id_product, $id_product_attribute, $id_shop = null)
public static function deleteAlert($id_customer, $customer_email, $id_product, $id_product_attribute, $id_shop = null, $notification_sent = true)
{
$sql = '
DELETE FROM `' . _DB_PREFIX_ . self::$definition['table'] . '`
WHERE ' . (($id_customer > 0) ? '(`customer_email` = \'' . pSQL($customer_email) . '\' OR `id_customer` = ' . (int) $id_customer . ')' :
$sql = 'UPDATE `' . _DB_PREFIX_ . self::$definition['table'] . '` set `deleted` = 1, date_upd = NOW()' .
($notification_sent ? ', `notification_sent` = NOW()' : '') .
' WHERE ' . (($id_customer > 0) ? '(`customer_email` = \'' . pSQL($customer_email) . '\' OR `id_customer` = ' . (int) $id_customer . ')' :
'`customer_email` = \'' . pSQL($customer_email) . '\'') .
' AND `id_product` = ' . (int) $id_product . '
AND `id_product_attribute` = ' . (int) $id_product_attribute . '
Expand Down Expand Up @@ -242,11 +251,12 @@ public static function sendCustomerAlert($id_product, $id_product_attribute)
);

self::deleteAlert(
$customer_id,
$customer_email,
$id_product,
$id_product_attribute,
$id_shop
(int) $customer_id,
(string) $customer_email,
(int) $id_product,
(int) $id_product_attribute,
$id_shop,
true
);
}
$context->shop->id = $current_shop;
Expand All @@ -270,7 +280,7 @@ public static function getProducts($customer, $id_lang)
$sql = '
SELECT ma.`id_product`, p.`quantity` AS product_quantity, pl.`name`, ma.`id_product_attribute`
FROM `' . _DB_PREFIX_ . self::$definition['table'] . '` ma
JOIN `' . _DB_PREFIX_ . 'product` p ON (p.`id_product` = ma.`id_product`)
JOIN `' . _DB_PREFIX_ . 'product` p ON p.`id_product` = ma.`id_product` and ma.`deleted` = 0
' . Shop::addSqlAssociation('product', 'p') . '
LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (pl.`id_product` = p.`id_product` AND pl.id_shop IN (' . implode(', ', $list_shop_ids) . '))
WHERE product_shop.`active` = 1
Expand Down Expand Up @@ -311,7 +321,7 @@ public static function getCustomers($id_product, $id_product_attribute, $id_shop
INNER JOIN `' . _DB_PREFIX_ . 'shop` s on s.id_shop = mc.id_shop
INNER JOIN `' . _DB_PREFIX_ . 'shop_group` sg on s.id_shop_group = sg.id_shop_group and (s.id_shop = ' . (int) $id_shop . ' or sg.share_stock = 1)
INNER JOIN `' . _DB_PREFIX_ . 'shop` s2 on s2.id_shop = mc.id_shop and s2.id_shop = ' . (int) $id_shop . '
WHERE mc.`id_product` = ' . (int) $id_product . ' AND mc.`id_product_attribute` = ' . (int) $id_product_attribute;
WHERE mc.`id_product` = ' . (int) $id_product . ' AND mc.`id_product_attribute` = ' . (int) $id_product_attribute . ' AND `deleted` = 0';

return Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS($sql);
}
Expand Down
3 changes: 2 additions & 1 deletion controllers/front/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public function processRemove()
(string) $context->customer->email,
(int) $product->id,
(int) $this->id_product_attribute,
(int) $context->shop->id
(int) $context->shop->id,
false
)) {
exit('0');
}
Expand Down
20 changes: 14 additions & 6 deletions ps_emailalerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ public function install($delete_params = true)
`id_product_attribute` int(10) unsigned NOT NULL,
`id_shop` int(10) unsigned NOT NULL,
`id_lang` int(10) unsigned NOT NULL,
PRIMARY KEY (`id_customer`,`customer_email`,`id_product`,`id_product_attribute`,`id_shop`)
`date_add` DATETIME NOT NULL,
`date_upd` DATETIME NOT NULL,
`deleted` TINYINT NOT NULL DEFAULT 0,
`notification_sent` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id_customer`,`customer_email`,`id_product`,`id_product_attribute`,`id_shop`),
INDEX (`deleted`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';

if (!Db::getInstance()->execute($sql)) {
Expand Down Expand Up @@ -753,7 +758,8 @@ public function hookDisplayMyAccountBlock($params)
public function hookActionProductDelete($params)
{
$sql = '
DELETE FROM `' . _DB_PREFIX_ . MailAlert::$definition['table'] . '`
UPDATE `' . _DB_PREFIX_ . MailAlert::$definition['table'] . '`
SET `deleted` = 1, date_upd = NOW()
WHERE `id_product` = ' . (int) $params['product']->id;

Db::getInstance()->execute($sql);
Expand All @@ -763,11 +769,13 @@ public function hookActionProductAttributeDelete($params)
{
if ($params['deleteAllAttributes']) {
$sql = '
DELETE FROM `' . _DB_PREFIX_ . MailAlert::$definition['table'] . '`
UPDATE `' . _DB_PREFIX_ . MailAlert::$definition['table'] . '`
SET `deleted` = 1, date_upd = NOW()
WHERE `id_product` = ' . (int) $params['id_product'];
} else {
$sql = '
DELETE FROM `' . _DB_PREFIX_ . MailAlert::$definition['table'] . '`
UPDATE `' . _DB_PREFIX_ . MailAlert::$definition['table'] . '`
SET `deleted` = 1, date_upd = NOW()
WHERE `id_product_attribute` = ' . (int) $params['id_product_attribute'] . '
AND `id_product` = ' . (int) $params['id_product'];
}
Expand Down Expand Up @@ -1284,7 +1292,7 @@ public function renderForm()
public function hookActionDeleteGDPRCustomer($customer)
{
if (!empty($customer['email']) && Validate::isEmail($customer['email'])) {
$sql = 'DELETE FROM ' . _DB_PREFIX_ . "mailalert_customer_oos WHERE customer_email = '" . pSQL($customer['email']) . "'";
$sql = 'UPDATE ' . _DB_PREFIX_ . "mailalert_customer_oos SET deleted = 1, customer_email = 'deleted', date_upd = NOW() WHERE customer_email = '" . pSQL($customer['email']) . "'";
marsaldev marked this conversation as resolved.
Show resolved Hide resolved
if (Db::getInstance()->execute($sql)) {
return json_encode(true);
}
Expand All @@ -1296,7 +1304,7 @@ public function hookActionDeleteGDPRCustomer($customer)
public function hookActionExportGDPRData($customer)
{
if (!Tools::isEmpty($customer['email']) && Validate::isEmail($customer['email'])) {
$sql = 'SELECT * FROM ' . _DB_PREFIX_ . "mailalert_customer_oos WHERE customer_email = '" . pSQL($customer['email']) . "'";
$sql = 'SELECT * FROM ' . _DB_PREFIX_ . "mailalert_customer_oos WHERE deleted = 0 and customer_email = '" . pSQL($customer['email']) . "'";
if ($res = Db::getInstance()->ExecuteS($sql)) {
return json_encode($res);
}
Expand Down
32 changes: 32 additions & 0 deletions upgrade/upgrade-2.4.2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* 2007-2022 PrestaShop.
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2022 PrestaShop SA
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
function upgrade_module_2_4_2()
{
$result = Db::getInstance()->execute('ALTER TABLE `' . _DB_PREFIX_ . 'mailalert_customer_oos` ADD `date_add` DATETIME NOT NULL, ADD `date_upd` DATETIME NOT NULL;');
$result = $result && Db::getInstance()->execute('ALTER TABLE `' . _DB_PREFIX_ . 'mailalert_customer_oos` ADD `deleted` TINYINT NOT NULL DEFAULT 0, ADD `notification_sent` DATETIME NULL DEFAULT NULL, ADD INDEX (`deleted`)');

return $result;
}