96 lines
3.1 KiB
PHP
96 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Jinove\Vinoteca\Catalog;
|
|
|
|
use Bitrix\Iblock\ElementTable;
|
|
use Bitrix\Main\Loader;
|
|
use Bitrix\Main\ORM\Data\DataManager;
|
|
use Bitrix\Sale\Fuser;
|
|
use Jinove\Vinoteca\Config;
|
|
use Bitrix\Main\ORM\Fields\DatetimeField;
|
|
use Bitrix\Main\ORM\Fields\IntegerField;
|
|
use Bitrix\Main\Type\DateTime;
|
|
|
|
class FavoriteTable extends DataManager
|
|
{
|
|
private static int $guestFuser = 0;
|
|
|
|
public static function getTableName(): string
|
|
{
|
|
return "jinove_vinoteca_favorite";
|
|
}
|
|
|
|
public static function getMap(): array
|
|
{
|
|
return [
|
|
(new IntegerField("ID"))->configurePrimary()->configureAutocomplete(),
|
|
(new IntegerField("FUSER_ID"))->configureRequired(),
|
|
(new IntegerField("PRODUCT_ID"))->configureRequired(),
|
|
(new DatetimeField("DATE_CREATE"))->configureDefaultValue(static fn(): DateTime => new DateTime()),
|
|
];
|
|
}
|
|
|
|
public static function productIds(int $fuserId): array
|
|
{
|
|
if ($fuserId <= 0) {
|
|
return [];
|
|
}
|
|
$ids = array_map("intval", array_column(self::getList([
|
|
"select" => ["PRODUCT_ID"],
|
|
"filter" => ["=FUSER_ID" => $fuserId],
|
|
"order" => ["ID" => "ASC"],
|
|
])->fetchAll(), "PRODUCT_ID"));
|
|
if (!$ids || !Loader::includeModule("iblock")) {
|
|
return [];
|
|
}
|
|
$active = array_flip(array_map("intval", array_column(ElementTable::getList([
|
|
"select" => ["ID"],
|
|
"filter" => ["=ID" => $ids, "=IBLOCK_ID" => Config::iblockId("catalog"), "=ACTIVE" => "Y"],
|
|
])->fetchAll(), "ID")));
|
|
|
|
return array_values(array_filter($ids, static fn(int $id): bool => isset($active[$id])));
|
|
}
|
|
|
|
public static function onElementDelete(array $fields): void
|
|
{
|
|
$productId = (int)($fields["ID"] ?? 0);
|
|
if ($productId <= 0) {
|
|
return;
|
|
}
|
|
$rows = self::getList(["select" => ["ID"], "filter" => ["=PRODUCT_ID" => $productId]]);
|
|
while ($row = $rows->fetch()) {
|
|
self::delete((int)$row["ID"]);
|
|
}
|
|
}
|
|
|
|
public static function rememberGuest(): void
|
|
{
|
|
if (Loader::includeModule("sale")) {
|
|
self::$guestFuser = (int)Fuser::getId(true);
|
|
}
|
|
}
|
|
|
|
public static function mergeGuest(): void
|
|
{
|
|
if (self::$guestFuser <= 0 || !Loader::includeModule("sale")) {
|
|
return;
|
|
}
|
|
$userFuser = (int)Fuser::getId(true);
|
|
if ($userFuser <= 0 || $userFuser === self::$guestFuser) {
|
|
return;
|
|
}
|
|
$owned = array_flip(array_map("intval", array_column(self::getList(["select" => ["PRODUCT_ID"], "filter" => ["=FUSER_ID" => $userFuser]])->fetchAll(), "PRODUCT_ID")));
|
|
$rows = self::getList(["select" => ["ID", "PRODUCT_ID"], "filter" => ["=FUSER_ID" => self::$guestFuser]]);
|
|
while ($row = $rows->fetch()) {
|
|
if (isset($owned[(int)$row["PRODUCT_ID"]])) {
|
|
self::delete((int)$row["ID"]);
|
|
} else {
|
|
self::update((int)$row["ID"], ["FUSER_ID" => $userFuser]);
|
|
}
|
|
}
|
|
self::$guestFuser = 0;
|
|
}
|
|
}
|