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)); } }