111 lines
3.9 KiB
PHP
111 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Jinove\Vinoteca\Catalog;
|
|
|
|
use Bitrix\Iblock\ElementPropertyTable;
|
|
use Bitrix\Iblock\ElementTable;
|
|
use Bitrix\Iblock\IblockTable;
|
|
use Bitrix\Iblock\PropertyTable;
|
|
use Bitrix\Iblock\SectionTable;
|
|
use Bitrix\Main\Loader;
|
|
use Jinove\Vinoteca\Config;
|
|
use Jinove\Vinoteca\Cached;
|
|
|
|
class Related
|
|
{
|
|
private const CACHE_TTL = 86400;
|
|
private const CACHE_DIR = "/jinove.vinoteca/related";
|
|
|
|
public static function ids(int $iblockId, int $elementId): array
|
|
{
|
|
if (!Loader::includeModule("iblock")) {
|
|
return [];
|
|
}
|
|
$property = PropertyTable::getRow(["select" => ["ID"], "filter" => ["=IBLOCK_ID" => $iblockId, "=CODE" => "RECOMMEND"]]);
|
|
if ($property === null) {
|
|
return [];
|
|
}
|
|
$linked = [];
|
|
$rows = ElementPropertyTable::getList([
|
|
"select" => ["VALUE"],
|
|
"filter" => ["=IBLOCK_ELEMENT_ID" => $elementId, "=IBLOCK_PROPERTY_ID" => (int)$property["ID"]],
|
|
"order" => ["ID" => "ASC"],
|
|
]);
|
|
while ($row = $rows->fetch()) {
|
|
$linked[] = (int)$row["VALUE"];
|
|
}
|
|
$linked = array_values(array_filter(array_unique($linked)));
|
|
if (!$linked) {
|
|
return [];
|
|
}
|
|
$active = [];
|
|
$items = ElementTable::getList(["select" => ["ID"], "filter" => ["=ID" => $linked, "=IBLOCK_ID" => $iblockId, "=ACTIVE" => "Y"]]);
|
|
while ($item = $items->fetch()) {
|
|
$active[(int)$item["ID"]] = true;
|
|
}
|
|
|
|
return array_values(array_filter($linked, fn(int $id): bool => isset($active[$id])));
|
|
}
|
|
|
|
public static function summaries(array $ids): array
|
|
{
|
|
$found = self::summariesById($ids);
|
|
|
|
return array_values(array_filter(array_map(static fn(int $id): ?array => $found[$id] ?? null, $ids)));
|
|
}
|
|
|
|
public static function summariesById(array $ids): array
|
|
{
|
|
$ids = array_values(array_unique(array_filter(array_map("intval", $ids))));
|
|
if (!$ids) {
|
|
return [];
|
|
}
|
|
|
|
return array_intersect_key(self::all(), array_flip($ids));
|
|
}
|
|
|
|
private static function all(): array
|
|
{
|
|
$iblockId = Config::iblockId("catalog");
|
|
if ($iblockId <= 0 || !Loader::includeModule("iblock")) {
|
|
return [];
|
|
}
|
|
return (array)Cached::remember(self::CACHE_DIR, "summaries-" . $iblockId . "-" . SITE_DIR, self::CACHE_TTL, ["iblock_id_" . $iblockId], static fn(): array => self::collect($iblockId));
|
|
}
|
|
|
|
private static function collect(int $iblockId): array
|
|
{
|
|
$template = (string)(IblockTable::getRowById($iblockId)["DETAIL_PAGE_URL"] ?? "");
|
|
$elements = ElementTable::getList([
|
|
"select" => ["ID", "NAME", "CODE", "IBLOCK_SECTION_ID"],
|
|
"filter" => ["=IBLOCK_ID" => $iblockId, "=ACTIVE" => "Y"],
|
|
])->fetchAll();
|
|
$sectionIds = array_values(array_unique(array_filter(array_map(static fn(array $row): int => (int)$row["IBLOCK_SECTION_ID"], $elements))));
|
|
$sectionCodes = [];
|
|
if ($sectionIds) {
|
|
$sections = SectionTable::getList(["select" => ["ID", "CODE"], "filter" => ["=ID" => $sectionIds]]);
|
|
while ($section = $sections->fetch()) {
|
|
$sectionCodes[(int)$section["ID"]] = (string)$section["CODE"];
|
|
}
|
|
}
|
|
$found = [];
|
|
foreach ($elements as $row) {
|
|
$found[(int)$row["ID"]] = [
|
|
"name" => (string)$row["NAME"],
|
|
"url" => (string)preg_replace('#/{2,}#', "/", strtr($template, [
|
|
"#SITE_DIR#" => SITE_DIR,
|
|
"#SECTION_CODE#" => $sectionCodes[(int)$row["IBLOCK_SECTION_ID"]] ?? "",
|
|
"#ELEMENT_CODE#" => (string)$row["CODE"],
|
|
"#CODE#" => (string)$row["CODE"],
|
|
"#ID#" => (string)$row["ID"],
|
|
"#ELEMENT_ID#" => (string)$row["ID"],
|
|
])),
|
|
];
|
|
}
|
|
|
|
return $found;
|
|
}
|
|
}
|