[], "COUNTRIES" => [], "BRANDS" => [], "TOTAL" => 0]; if (!Loader::includeModule("iblock")) { return $result; } $result["TYPES"] = self::types($iblockId); $result["COUNTRIES"] = self::countries($iblockId); $result["BRANDS"] = array_slice(self::brands($iblockId), 0, $brandsLimit); $result["TOTAL"] = ServiceLocator::getInstance()->get("jinove.vinoteca.wineStats")->getActiveCount(); return $result; } public static function types(int $iblockId): array { $counts = []; $rows = SectionElementTable::getList([ "select" => ["IBLOCK_SECTION_ID", "CNT"], "runtime" => [new ExpressionField("CNT", "COUNT(DISTINCT %s)", "IBLOCK_ELEMENT_ID")], "filter" => ["=IBLOCK_ELEMENT.IBLOCK_ID" => $iblockId, "=IBLOCK_ELEMENT.ACTIVE" => "Y"], "group" => ["IBLOCK_SECTION_ID"], ]); while ($row = $rows->fetch()) { $counts[(int)$row["IBLOCK_SECTION_ID"]] = (int)$row["CNT"]; } $types = []; foreach (Sections::all() as $code => $section) { $count = $counts[$section["ID"]] ?? 0; if ($section["ACTIVE"] && $section["DEPTH_LEVEL"] === 1 && $count > 0) { $types[] = ["NAME" => $section["NAME"], "URL" => SITE_DIR . "catalog/" . $code . "/", "COUNT" => $count]; } } return $types; } private static function countries(int $iblockId): array { $propertyId = self::propertyId($iblockId, "COUNTRY"); if ($propertyId <= 0) { return []; } $xmlIds = []; $rows = PropertyEnumerationTable::getList(["select" => ["ID", "XML_ID"], "filter" => ["=PROPERTY_ID" => $propertyId]]); while ($row = $rows->fetch()) { $xmlIds[(int)$row["ID"]] = (string)$row["XML_ID"]; } $names = Countries::names(); $countries = []; foreach (self::counts($iblockId, $propertyId, "VALUE_ENUM") as $enumId => $count) { $code = $xmlIds[$enumId] ?? ""; if ($code !== "" && isset($names[$code])) { $countries[] = ["NAME" => EnumNames::name("COUNTRY", $code, $names[$code]), "URL" => Listing::countryPath($code), "COUNT" => $count]; } } usort($countries, static fn(array $a, array $b): int => $b["COUNT"] <=> $a["COUNT"]); return $countries; } private static function brands(int $iblockId): array { $propertyId = self::propertyId($iblockId, "BRAND"); if ($propertyId <= 0) { return []; } $counts = self::counts($iblockId, $propertyId, "VALUE_NUM"); if (!$counts) { return []; } $brands = []; $rows = ElementTable::getList([ "select" => ["ID", "NAME", "CODE"], "filter" => ["=ID" => array_keys($counts), "=ACTIVE" => "Y"], ]); while ($row = $rows->fetch()) { $brands[] = ["NAME" => (string)$row["NAME"], "URL" => SITE_DIR . "brands/" . $row["CODE"] . "/", "COUNT" => $counts[(int)$row["ID"]]]; } usort($brands, static fn(array $a, array $b): int => $b["COUNT"] <=> $a["COUNT"]); return $brands; } private static function counts(int $iblockId, int $propertyId, string $valueField): array { $counts = []; $rows = ElementPropertyTable::getList([ "select" => [$valueField, "CNT"], "runtime" => [new ExpressionField("CNT", "COUNT(DISTINCT %s)", "IBLOCK_ELEMENT_ID")], "filter" => ["=IBLOCK_PROPERTY_ID" => $propertyId, "=ELEMENT.IBLOCK_ID" => $iblockId, "=ELEMENT.ACTIVE" => "Y", ">" . $valueField => 0], "group" => [$valueField], ]); while ($row = $rows->fetch()) { $counts[(int)$row[$valueField]] = (int)$row["CNT"]; } return $counts; } private static function propertyId(int $iblockId, string $code): int { $row = PropertyTable::getRow(["select" => ["ID"], "filter" => ["=IBLOCK_ID" => $iblockId, "=CODE" => $code]]); return (int)($row["ID"] ?? 0); } }