90 lines
3.1 KiB
PHP
90 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Jinove\Vinoteca\Catalog;
|
|
|
|
use Bitrix\Iblock\InheritedProperty\SectionValues;
|
|
use Bitrix\Main\Loader;
|
|
use Jinove\Vinoteca\Config;
|
|
use Jinove\Vinoteca\Cached;
|
|
|
|
class Sections
|
|
{
|
|
private const CACHE_TTL = 86400;
|
|
private const CACHE_DIR = "/jinove.vinoteca/sections";
|
|
private const BASE_LANGUAGE = "ru";
|
|
private const FIELDS = ["NAME", "DESCRIPTION", "META_TITLE", "META_DESCRIPTION", "PAGE_TITLE"];
|
|
|
|
public static function byCode(string $code): array
|
|
{
|
|
return self::all()[$code] ?? [];
|
|
}
|
|
|
|
public static function name(string $code, string $fallback = ""): string
|
|
{
|
|
return (string)(self::byCode($code)["NAME"] ?? "") ?: $fallback;
|
|
}
|
|
|
|
public static function menuLinks(): array
|
|
{
|
|
$links = [];
|
|
foreach (self::all() as $code => $section) {
|
|
if (!$section["ACTIVE"] || $section["DEPTH_LEVEL"] !== 1) {
|
|
continue;
|
|
}
|
|
$links[] = [$section["NAME"], SITE_DIR . "catalog/" . $code . "/", [], [], ""];
|
|
}
|
|
|
|
return $links;
|
|
}
|
|
|
|
public static function all(): array
|
|
{
|
|
$catalogId = Config::iblockId("catalog");
|
|
$language = LANGUAGE_ID;
|
|
return (array)Cached::remember(self::CACHE_DIR, "sections-" . $catalogId . "-" . $language, self::CACHE_TTL, ["iblock_id_" . $catalogId], static fn(): array => self::collect($catalogId, $language));
|
|
}
|
|
|
|
private static function collect(int $catalogId, string $language): array
|
|
{
|
|
if (!Loader::includeModule("iblock")) {
|
|
return [];
|
|
}
|
|
$suffix = "_" . strtoupper($language);
|
|
$select = ["ID", "CODE", "NAME", "DESCRIPTION", "ACTIVE", "DEPTH_LEVEL"];
|
|
if ($language !== self::BASE_LANGUAGE) {
|
|
foreach (self::FIELDS as $field) {
|
|
$select[] = "UF_" . $field . $suffix;
|
|
}
|
|
}
|
|
$sections = [];
|
|
$rows = \CIBlockSection::GetList(["SORT" => "ASC"], ["IBLOCK_ID" => $catalogId], false, $select);
|
|
while ($row = $rows->Fetch()) {
|
|
$seo = (new SectionValues($catalogId, (int)$row["ID"]))->getValues();
|
|
$base = [
|
|
"NAME" => (string)$row["NAME"],
|
|
"DESCRIPTION" => (string)$row["DESCRIPTION"],
|
|
"META_TITLE" => (string)($seo["SECTION_META_TITLE"] ?? ""),
|
|
"META_DESCRIPTION" => (string)($seo["SECTION_META_DESCRIPTION"] ?? ""),
|
|
"PAGE_TITLE" => (string)($seo["SECTION_PAGE_TITLE"] ?? ""),
|
|
];
|
|
if ($language !== self::BASE_LANGUAGE) {
|
|
foreach (self::FIELDS as $field) {
|
|
$value = trim((string)($row["UF_" . $field . $suffix] ?? ""));
|
|
if ($value !== "") {
|
|
$base[$field] = $value;
|
|
}
|
|
}
|
|
}
|
|
$sections[(string)$row["CODE"]] = [
|
|
"ID" => (int)$row["ID"],
|
|
"ACTIVE" => $row["ACTIVE"] === "Y",
|
|
"DEPTH_LEVEL" => (int)$row["DEPTH_LEVEL"],
|
|
] + $base;
|
|
}
|
|
|
|
return $sections;
|
|
}
|
|
}
|