106 lines
4.0 KiB
PHP
106 lines
4.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Jinove\Vinoteca\Catalog;
|
|
|
|
use Bitrix\Iblock\ElementPropertyTable;
|
|
use Bitrix\Iblock\ElementTable;
|
|
use Bitrix\Iblock\PropertyEnumerationTable;
|
|
use Bitrix\Iblock\PropertyTable;
|
|
use Bitrix\Main\Loader;
|
|
use Jinove\Vinoteca\Config;
|
|
use Jinove\Vinoteca\Cached;
|
|
|
|
final class SearchMeta
|
|
{
|
|
private const CACHE_TTL = 86400;
|
|
private const CACHE_DIR = "/jinove.vinoteca/search-meta-v2";
|
|
private const PROPERTIES = ["REGION", "COUNTRY"];
|
|
|
|
public static function get(array $ids): array
|
|
{
|
|
return array_intersect_key(self::all(), array_flip(array_map("intval", $ids)));
|
|
}
|
|
|
|
private static function all(): array
|
|
{
|
|
$catalogId = Config::iblockId("catalog");
|
|
return (array)Cached::remember(self::CACHE_DIR, "meta-" . $catalogId . "-" . LANGUAGE_ID, self::CACHE_TTL, ["iblock_id_" . $catalogId, "iblock_id_" . Config::iblockId("brands")], static fn(): array => self::collect($catalogId));
|
|
}
|
|
|
|
private static function collect(int $catalogId): array
|
|
{
|
|
if ($catalogId <= 0 || !Loader::includeModule("iblock")) {
|
|
return [];
|
|
}
|
|
$propertyCodes = [];
|
|
$rows = PropertyTable::getList(["select" => ["ID", "CODE"], "filter" => ["=IBLOCK_ID" => $catalogId, "=CODE" => self::PROPERTIES]]);
|
|
while ($row = $rows->fetch()) {
|
|
$propertyCodes[(int)$row["ID"]] = (string)$row["CODE"];
|
|
}
|
|
$enums = [];
|
|
if ($propertyCodes) {
|
|
$rows = PropertyEnumerationTable::getList(["select" => ["ID", "XML_ID", "VALUE"], "filter" => ["@PROPERTY_ID" => array_keys($propertyCodes)]]);
|
|
while ($row = $rows->fetch()) {
|
|
$enums[(int)$row["ID"]] = ["XML_ID" => (string)$row["XML_ID"], "VALUE" => (string)$row["VALUE"]];
|
|
}
|
|
}
|
|
$parts = [];
|
|
if ($propertyCodes) {
|
|
$rows = ElementPropertyTable::getList([
|
|
"select" => ["IBLOCK_ELEMENT_ID", "IBLOCK_PROPERTY_ID", "VALUE_ENUM"],
|
|
"filter" => ["@IBLOCK_PROPERTY_ID" => array_keys($propertyCodes), ">VALUE_ENUM" => 0],
|
|
]);
|
|
while ($row = $rows->fetch()) {
|
|
$enum = $enums[(int)$row["VALUE_ENUM"]] ?? null;
|
|
if ($enum !== null) {
|
|
$code = $propertyCodes[(int)$row["IBLOCK_PROPERTY_ID"]];
|
|
$parts[(int)$row["IBLOCK_ELEMENT_ID"]][$code] = EnumNames::name($code, $enum["XML_ID"], $enum["VALUE"]);
|
|
}
|
|
}
|
|
}
|
|
$brands = self::brands($catalogId);
|
|
$meta = [];
|
|
$rows = ElementTable::getList(["select" => ["ID", "NAME"], "filter" => ["=IBLOCK_ID" => $catalogId]]);
|
|
while ($row = $rows->fetch()) {
|
|
$id = (int)$row["ID"];
|
|
$meta[$id] = [
|
|
"NAME" => (string)$row["NAME"],
|
|
"META" => implode(", ", array_filter([$parts[$id]["REGION"] ?? "", $parts[$id]["COUNTRY"] ?? ""])),
|
|
"BRAND" => $brands[$id] ?? "",
|
|
];
|
|
}
|
|
|
|
return $meta;
|
|
}
|
|
|
|
private static function brands(int $catalogId): array
|
|
{
|
|
$property = PropertyTable::getRow(["select" => ["ID"], "filter" => ["=IBLOCK_ID" => $catalogId, "=CODE" => "BRAND"]]);
|
|
if ($property === null) {
|
|
return [];
|
|
}
|
|
$links = [];
|
|
$rows = ElementPropertyTable::getList([
|
|
"select" => ["IBLOCK_ELEMENT_ID", "VALUE"],
|
|
"filter" => ["=IBLOCK_PROPERTY_ID" => (int)$property["ID"]],
|
|
]);
|
|
while ($row = $rows->fetch()) {
|
|
if ((int)$row["VALUE"] > 0) {
|
|
$links[(int)$row["IBLOCK_ELEMENT_ID"]] = (int)$row["VALUE"];
|
|
}
|
|
}
|
|
if (!$links) {
|
|
return [];
|
|
}
|
|
$names = [];
|
|
$rows = ElementTable::getList(["select" => ["ID", "NAME"], "filter" => ["=ID" => array_values(array_unique($links))]]);
|
|
while ($row = $rows->fetch()) {
|
|
$names[(int)$row["ID"]] = (string)$row["NAME"];
|
|
}
|
|
|
|
return array_filter(array_map(static fn(int $brandId): string => $names[$brandId] ?? "", $links));
|
|
}
|
|
}
|