Vinoteca: wine shop on 1C-Bitrix (module, template, components, pages)

This commit is contained in:
jimagie committed 2026-10-22 23:22:27 +05:00
commit 52795535ae
586 files changed
+27619

No files matched your search

@@ -0,0 +1,164 @@
<?php
declare(strict_types=1);
namespace Jinove\Vinoteca;
use Bitrix\Iblock\ElementTable;
use Bitrix\Main\Application;
use Bitrix\Main\Loader;
use Bitrix\Main\Type\DateTime;
use Jinove\Vinoteca\Catalog\MegaMenu;
use Jinove\Vinoteca\Catalog\Sections;
final class Sitemap
{
private const FILE = "/sitemap.xml";
private const LIMIT = 50000;
public static function refresh(): string
{
self::write(self::build());
return __METHOD__ . "();";
}
public static function build(): string
{
$pages = self::pages();
$languages = Config::languages();
$host = Config::host();
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">' . "\n";
$count = 0;
foreach ($pages as $page) {
$alternates = "";
foreach ($languages as $code => $language) {
$alternates .= ' <xhtml:link rel="alternate" hreflang="' . $code . '" href="' . self::escape($host . self::url($language["dir"], $page["path"])) . '"/>' . "\n";
}
$alternates .= ' <xhtml:link rel="alternate" hreflang="x-default" href="' . self::escape($host . self::url($languages["en"]["dir"], $page["path"])) . '"/>' . "\n";
foreach ($languages as $language) {
if (++$count > self::LIMIT) {
break 2;
}
$xml .= " <url>\n <loc>" . self::escape($host . self::url($language["dir"], $page["path"])) . "</loc>\n";
if ($page["lastmod"] !== "") {
$xml .= " <lastmod>" . $page["lastmod"] . "</lastmod>\n";
}
$xml .= $alternates . " </url>\n";
}
}
return $xml . "</urlset>\n";
}
private static function pages(): array
{
$catalogId = Config::iblockId("catalog");
$pages = [];
if ($catalogId <= 0 || !Loader::includeModule("iblock")) {
return $pages;
}
$sectionCodes = [];
foreach (Sections::all() as $code => $section) {
$sectionCodes[$section["ID"]] = $code;
}
$now = new DateTime();
$wines = [];
$latest = 0;
$rows = ElementTable::getList([
"select" => ["CODE", "IBLOCK_SECTION_ID", "TIMESTAMP_X"],
"filter" => [
"=IBLOCK_ID" => $catalogId,
"=ACTIVE" => "Y",
["LOGIC" => "OR", ["=ACTIVE_FROM" => false], ["<=ACTIVE_FROM" => $now]],
["LOGIC" => "OR", ["=ACTIVE_TO" => false], [">=ACTIVE_TO" => $now]],
],
"order" => ["ID" => "ASC"],
]);
while ($row = $rows->fetch()) {
$section = $sectionCodes[(int)$row["IBLOCK_SECTION_ID"]] ?? "";
if ($section === "" || (string)$row["CODE"] === "") {
continue;
}
$time = $row["TIMESTAMP_X"] instanceof DateTime ? $row["TIMESTAMP_X"]->getTimestamp() : 0;
$latest = max($latest, $time);
$wines[] = ["path" => "catalog/" . $section . "/" . rawurlencode((string)$row["CODE"]) . "/", "lastmod" => self::date($time)];
}
$menu = MegaMenu::collect($catalogId, 1000);
$lastmod = self::date($latest);
$pages[] = ["path" => "", "lastmod" => $lastmod];
$pages[] = ["path" => "catalog/", "lastmod" => $lastmod];
foreach (["TYPES", "COUNTRIES", "BRANDS"] as $group) {
foreach ($menu[$group] as $item) {
$pages[] = ["path" => ltrim(self::relative((string)$item["URL"]), "/"), "lastmod" => $lastmod];
}
}
$pages[] = ["path" => "brands/", "lastmod" => $lastmod];
$pages[] = ["path" => "about/", "lastmod" => $lastmod];
$pages = array_merge($pages, self::section("news", "articles/", $now));
$pages = array_merge($pages, self::section("offers", "offers/", $now, static fn(int $id): bool => Offers::summary(Offers::discountIdsOf($id))["active"]));
$pages = array_merge($pages, $wines);
$pages[] = ["path" => "privacy/", "lastmod" => self::date(Consent::timestamp(Consent::POLICY_VERSION))];
$pages[] = ["path" => "consent/", "lastmod" => self::date(Consent::timestamp(Consent::VERSION))];
return $pages;
}
private static function section(string $iblock, string $folder, DateTime $now, ?callable $keep = null): array
{
$iblockId = Config::iblockId($iblock);
if ($iblockId <= 0) {
return [];
}
$items = [];
$latest = 0;
$rows = ElementTable::getList([
"select" => ["ID", "CODE", "TIMESTAMP_X"],
"filter" => [
"=IBLOCK_ID" => $iblockId,
"=ACTIVE" => "Y",
["LOGIC" => "OR", ["=ACTIVE_FROM" => false], ["<=ACTIVE_FROM" => $now]],
["LOGIC" => "OR", ["=ACTIVE_TO" => false], [">=ACTIVE_TO" => $now]],
],
"order" => ["ACTIVE_FROM" => "DESC", "ID" => "DESC"],
]);
while ($row = $rows->fetch()) {
if ((string)$row["CODE"] === "" || ($keep !== null && !$keep((int)$row["ID"]))) {
continue;
}
$time = $row["TIMESTAMP_X"] instanceof DateTime ? $row["TIMESTAMP_X"]->getTimestamp() : 0;
$latest = max($latest, $time);
$items[] = ["path" => $folder . rawurlencode((string)$row["CODE"]) . "/", "lastmod" => self::date($time)];
}
return array_merge([["path" => $folder, "lastmod" => self::date($latest)]], $items);
}
private static function write(string $xml): void
{
$file = Application::getDocumentRoot() . self::FILE;
$temporary = $file . ".tmp";
if (file_put_contents($temporary, $xml, LOCK_EX) !== false) {
rename($temporary, $file);
}
}
private static function relative(string $url): string
{
return SITE_DIR !== "/" && str_starts_with($url, SITE_DIR) ? "/" . substr($url, strlen(SITE_DIR)) : $url;
}
private static function url(string $directory, string $path): string
{
return rtrim($directory, "/") . "/" . $path;
}
private static function date(int $timestamp): string
{
return $timestamp > 0 ? date("Y-m-d", $timestamp) : "";
}
private static function escape(string $value): string
{
return htmlspecialchars($value, ENT_XML1 | ENT_QUOTES, "UTF-8");
}
}