66 lines
1.9 KiB
PHP
66 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Jinove\Vinoteca\Catalog;
|
|
|
|
use Bitrix\Catalog\GroupTable;
|
|
use Bitrix\Main\Loader;
|
|
use Jinove\Vinoteca\Cached;
|
|
|
|
class PricePresets
|
|
{
|
|
private const CACHE_TTL = 86400;
|
|
private const CACHE_DIR = "/jinove.vinoteca/pricepresets";
|
|
private const LIMITS = [3000, 5000, 10000];
|
|
|
|
public static function limits(int $iblockId, string $sectionCode, string $priceCode): array
|
|
{
|
|
return (array)Cached::remember(self::CACHE_DIR, "limits-" . $iblockId . "-" . $sectionCode . "-" . $priceCode, self::CACHE_TTL, ["iblock_id_" . $iblockId], static fn(): array => self::collect($iblockId, $sectionCode, $priceCode));
|
|
}
|
|
|
|
public static function groupId(string $priceCode): int
|
|
{
|
|
if (!Loader::includeModule("catalog")) {
|
|
return 0;
|
|
}
|
|
$group = GroupTable::getRow([
|
|
"select" => ["ID"],
|
|
"filter" => ["=NAME" => $priceCode],
|
|
"cache" => ["ttl" => self::CACHE_TTL],
|
|
]);
|
|
|
|
return $group === null ? 0 : (int)$group["ID"];
|
|
}
|
|
|
|
private static function collect(int $iblockId, string $sectionCode, string $priceCode): array
|
|
{
|
|
if (!Loader::includeModule("iblock")) {
|
|
return [];
|
|
}
|
|
$groupId = self::groupId($priceCode);
|
|
if ($groupId === 0) {
|
|
return [];
|
|
}
|
|
|
|
$filter = [
|
|
"IBLOCK_ID" => $iblockId,
|
|
"ACTIVE" => "Y",
|
|
"SECTION_CODE" => $sectionCode,
|
|
"INCLUDE_SUBSECTIONS" => "Y",
|
|
];
|
|
$total = (int)\CIBlockElement::GetList([], $filter, []);
|
|
$limits = [];
|
|
$previous = 0;
|
|
foreach (self::LIMITS as $limit) {
|
|
$count = (int)\CIBlockElement::GetList([], $filter + ["<=CATALOG_PRICE_" . $groupId => $limit], []);
|
|
if ($count > $previous && $count < $total) {
|
|
$limits[] = $limit;
|
|
$previous = $count;
|
|
}
|
|
}
|
|
|
|
return $limits;
|
|
}
|
|
}
|