diff --git a/MailAlert.php b/MailAlert.php index a8e3574..90da0bf 100644 --- a/MailAlert.php +++ b/MailAlert.php @@ -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 */ @@ -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'], ], ]; @@ -68,6 +76,7 @@ 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; @@ -75,11 +84,11 @@ public static function customerHasNotification($id_customer, $id_product, $id_pr 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 . ' @@ -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; @@ -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 @@ -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); } diff --git a/controllers/front/actions.php b/controllers/front/actions.php index 6b08724..03b5114 100644 --- a/controllers/front/actions.php +++ b/controllers/front/actions.php @@ -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'); } diff --git a/ps_emailalerts.php b/ps_emailalerts.php index 8b8c2df..b307003 100644 --- a/ps_emailalerts.php +++ b/ps_emailalerts.php @@ -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)) { @@ -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); @@ -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']; } @@ -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']) . "'"; if (Db::getInstance()->execute($sql)) { return json_encode(true); } @@ -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); } diff --git a/upgrade/upgrade-2.4.2.php b/upgrade/upgrade-2.4.2.php new file mode 100755 index 0000000..4be138e --- /dev/null +++ b/upgrade/upgrade-2.4.2.php @@ -0,0 +1,32 @@ + + * @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; +}