Vinoteca: модуль, шаблон и компоненты интернет-магазина вина на 1С-Битрикс
This commit is contained in:
commit
bb7d8af290
469 files changed
+26426
No files matched your search
@@ -0,0 +1,240 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Bitrix\Catalog\ProductTable;
|
||||
use Bitrix\Main\Application;
|
||||
use Bitrix\Main\Loader;
|
||||
use Bitrix\Main\Localization\Loc;
|
||||
use Jinove\Vinoteca\Config;
|
||||
use Jinove\Vinoteca\Money;
|
||||
|
||||
Loc::loadMessages(__FILE__);
|
||||
|
||||
final class Comparison
|
||||
{
|
||||
public const SESSION_KEY = "CATALOG_COMPARE_LIST";
|
||||
public const MAX = 4;
|
||||
private const ROWS = ["WINE_COLOR", "COUNTRY", "REGION", "BRAND", "GRAPE", "SUGAR", "ABV", "VOLUME", "VINTAGE", "AGING", "SERVE_TEMP"];
|
||||
|
||||
public static function folder(): string
|
||||
{
|
||||
return SITE_DIR . "compare/";
|
||||
}
|
||||
|
||||
public static function preparePage(): void
|
||||
{
|
||||
global $APPLICATION;
|
||||
self::prune();
|
||||
$APPLICATION->SetTitle((string)Loc::getMessage("VINO_COMPARE_H1"));
|
||||
$APPLICATION->SetPageProperty("title", (string)Loc::getMessage("VINO_COMPARE_H1"));
|
||||
$APPLICATION->SetPageProperty("robots", "noindex, follow");
|
||||
$APPLICATION->SetPageProperty("vino_alternates", "N");
|
||||
}
|
||||
|
||||
public static function isEmpty(): bool
|
||||
{
|
||||
return !self::read();
|
||||
}
|
||||
|
||||
public static function read(): array
|
||||
{
|
||||
$list = Application::getInstance()->getSession()[self::SESSION_KEY] ?? [];
|
||||
|
||||
return (array)($list[Config::iblockId("catalog")]["ITEMS"] ?? []);
|
||||
}
|
||||
|
||||
public static function write(array $items): void
|
||||
{
|
||||
$session = Application::getInstance()->getSession();
|
||||
$list = (array)($session[self::SESSION_KEY] ?? []);
|
||||
$list[Config::iblockId("catalog")]["ITEMS"] = $items;
|
||||
$session[self::SESSION_KEY] = $list;
|
||||
}
|
||||
|
||||
public static function prune(): array
|
||||
{
|
||||
$items = self::read();
|
||||
if (!$items || !Loader::includeModule("iblock")) {
|
||||
return $items;
|
||||
}
|
||||
$active = array_flip(array_map("intval", array_column(\Bitrix\Iblock\ElementTable::getList([
|
||||
"select" => ["ID"],
|
||||
"filter" => ["=ID" => array_map("intval", array_keys($items)), "=IBLOCK_ID" => Config::iblockId("catalog"), "=ACTIVE" => "Y"],
|
||||
])->fetchAll(), "ID")));
|
||||
$kept = array_intersect_key($items, $active);
|
||||
if (count($kept) !== count($items)) {
|
||||
self::write($kept);
|
||||
}
|
||||
|
||||
return $kept;
|
||||
}
|
||||
|
||||
public static function params(): array
|
||||
{
|
||||
$catalog = Listing::catalogParams();
|
||||
|
||||
return [
|
||||
"NAME" => self::SESSION_KEY,
|
||||
"IBLOCK_TYPE" => $catalog["IBLOCK_TYPE"],
|
||||
"IBLOCK_ID" => $catalog["IBLOCK_ID"],
|
||||
"FIELD_CODE" => [],
|
||||
"PROPERTY_CODE" => [],
|
||||
"OFFERS_FIELD_CODE" => [],
|
||||
"OFFERS_PROPERTY_CODE" => [],
|
||||
"PRICE_CODE" => $catalog["PRICE_CODE"],
|
||||
"USE_PRICE_COUNT" => "N",
|
||||
"SHOW_PRICE_COUNT" => "1",
|
||||
"PRICE_VAT_INCLUDE" => "Y",
|
||||
"CONVERT_CURRENCY" => "N",
|
||||
"BASKET_URL" => $catalog["BASKET_URL"],
|
||||
"ACTION_VARIABLE" => $catalog["ACTION_VARIABLE"],
|
||||
"PRODUCT_ID_VARIABLE" => $catalog["PRODUCT_ID_VARIABLE"],
|
||||
"SECTION_ID_VARIABLE" => "SECTION_ID",
|
||||
"DETAIL_URL" => $catalog["SEF_FOLDER"] . $catalog["SEF_URL_TEMPLATES"]["element"],
|
||||
"DISPLAY_ELEMENT_SELECT_BOX" => "N",
|
||||
"ELEMENT_SORT_FIELD" => "sort",
|
||||
"ELEMENT_SORT_ORDER" => "asc",
|
||||
"HIDE_NOT_AVAILABLE" => "N",
|
||||
"TEMPLATE_THEME" => "",
|
||||
];
|
||||
}
|
||||
|
||||
public static function build(array $compareItems): array
|
||||
{
|
||||
$ids = [];
|
||||
foreach ($compareItems as $item) {
|
||||
$ids[] = (int)($item["ID"] ?? 0);
|
||||
}
|
||||
$ids = array_values(array_filter($ids));
|
||||
if (!$ids || !Loader::includeModule("iblock")) {
|
||||
return ["ITEMS" => [], "ROWS" => []];
|
||||
}
|
||||
$summaries = Related::summariesById($ids);
|
||||
$prices = Prices::optimal($ids);
|
||||
$available = self::availability($ids);
|
||||
$catalogId = Config::iblockId("catalog");
|
||||
$elements = [];
|
||||
$rows = \CIBlockElement::GetList([], ["ID" => $ids, "IBLOCK_ID" => $catalogId], false, false, ["ID", "IBLOCK_ID", "NAME", "PREVIEW_PICTURE", "DETAIL_PICTURE"]);
|
||||
while ($row = $rows->Fetch()) {
|
||||
$row["PROPERTIES"] = [];
|
||||
$elements[(int)$row["ID"]] = $row;
|
||||
}
|
||||
if ($elements) {
|
||||
$properties = array_fill_keys(array_keys($elements), []);
|
||||
\CIBlockElement::GetPropertyValuesArray($properties, $catalogId, ["ID" => array_keys($elements)]);
|
||||
foreach ($properties as $id => $values) {
|
||||
$elements[$id]["PROPERTIES"] = $values;
|
||||
}
|
||||
}
|
||||
$brands = self::brandNames($elements);
|
||||
$items = [];
|
||||
$values = [];
|
||||
foreach ($ids as $id) {
|
||||
$element = $elements[$id] ?? null;
|
||||
$summary = $summaries[$id] ?? null;
|
||||
if ($element === null || $summary === null) {
|
||||
continue;
|
||||
}
|
||||
$items[] = self::item($element, $summary, $prices[$id] ?? null, $available[$id] ?? false);
|
||||
foreach (self::ROWS as $code) {
|
||||
$values[$code][] = self::value($code, $element["PROPERTIES"], $brands);
|
||||
}
|
||||
}
|
||||
$table = [];
|
||||
foreach (self::ROWS as $code) {
|
||||
$cells = $values[$code] ?? [];
|
||||
if (!array_filter($cells, static fn(string $cell): bool => $cell !== "")) {
|
||||
continue;
|
||||
}
|
||||
$same = count($cells) > 1 && count(array_unique($cells)) === 1;
|
||||
$table[] = ["LABEL" => (string)Loc::getMessage("VINO_COMPARE_ROW_" . $code), "VALUES" => $cells, "SAME" => $same];
|
||||
}
|
||||
|
||||
return ["ITEMS" => $items, "ROWS" => $table];
|
||||
}
|
||||
|
||||
private static function item(array $element, array $summary, ?array $price, bool $available): array
|
||||
{
|
||||
$pictureId = (int)($element["PREVIEW_PICTURE"] ?: $element["DETAIL_PICTURE"]);
|
||||
$prices = [];
|
||||
if ($price) {
|
||||
$prices[] = [
|
||||
"RATIO_PRICE" => $price["price"],
|
||||
"PRINT_RATIO_PRICE" => htmlspecialcharsbx(Money::format($price["price"], $price["currency"])),
|
||||
"RATIO_BASE_PRICE" => $price["base"],
|
||||
"PRINT_RATIO_BASE_PRICE" => htmlspecialcharsbx(Money::format($price["base"], $price["currency"])),
|
||||
"PERCENT" => $price["base"] > 0 ? round(100 - $price["price"] / $price["base"] * 100) : 0,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
"ID" => (int)$element["ID"],
|
||||
"NAME" => htmlspecialcharsbx((string)$summary["name"]),
|
||||
"~NAME" => (string)$summary["name"],
|
||||
"DETAIL_PAGE_URL" => htmlspecialcharsbx((string)$summary["url"]),
|
||||
"~DETAIL_PAGE_URL" => (string)$summary["url"],
|
||||
"PREVIEW_PICTURE" => $pictureId > 0 ? ["ID" => $pictureId] : null,
|
||||
"DETAIL_PICTURE" => null,
|
||||
"PROPERTIES" => $element["PROPERTIES"],
|
||||
"ITEM_PRICES" => $prices,
|
||||
"ITEM_PRICE_SELECTED" => 0,
|
||||
"CAN_BUY" => $available && $prices !== [],
|
||||
];
|
||||
}
|
||||
|
||||
private static function value(string $code, array $properties, array $brands): string
|
||||
{
|
||||
$raw = $properties[$code]["~VALUE"] ?? $properties[$code]["VALUE"] ?? "";
|
||||
if (is_array($raw) && !isset($raw["TEXT"])) {
|
||||
$raw = implode(", ", array_filter($raw));
|
||||
}
|
||||
$plain = trim(is_array($raw) ? (string)($raw["TEXT"] ?? "") : (string)$raw);
|
||||
$number = static fn(string $value): string => LANGUAGE_ID === "en" ? $value : str_replace(".", ",", $value);
|
||||
|
||||
return match ($code) {
|
||||
"WINE_COLOR", "COUNTRY", "REGION", "GRAPE", "SUGAR" => EnumNames::fromProperty($properties[$code] ?? []),
|
||||
"BRAND" => $brands[(int)$plain] ?? "",
|
||||
"ABV" => $plain !== "" ? $number((string)(float)$plain) . "%" : "",
|
||||
"VOLUME" => $plain !== "" ? (string)Loc::getMessage("VINO_COMPARE_VOLUME", ["#VALUE#" => $number($plain)]) : "",
|
||||
"AGING" => LocalizedText::property($properties, $code),
|
||||
default => $plain,
|
||||
};
|
||||
}
|
||||
|
||||
private static function brandNames(array $elements): array
|
||||
{
|
||||
$ids = [];
|
||||
foreach ($elements as $element) {
|
||||
$value = $element["PROPERTIES"]["BRAND"]["VALUE"] ?? 0;
|
||||
$ids[] = (int)(is_array($value) ? reset($value) : $value);
|
||||
}
|
||||
$ids = array_values(array_unique(array_filter($ids)));
|
||||
if (!$ids) {
|
||||
return [];
|
||||
}
|
||||
$names = [];
|
||||
$rows = \Bitrix\Iblock\ElementTable::getList(["select" => ["ID", "NAME"], "filter" => ["=ID" => $ids]]);
|
||||
while ($row = $rows->fetch()) {
|
||||
$names[(int)$row["ID"]] = (string)$row["NAME"];
|
||||
}
|
||||
|
||||
return $names;
|
||||
}
|
||||
|
||||
private static function availability(array $ids): array
|
||||
{
|
||||
if (!Loader::includeModule("catalog")) {
|
||||
return [];
|
||||
}
|
||||
$available = [];
|
||||
$rows = ProductTable::getList(["select" => ["ID", "AVAILABLE"], "filter" => ["=ID" => $ids]]);
|
||||
while ($row = $rows->fetch()) {
|
||||
$available[(int)$row["ID"]] = $row["AVAILABLE"] === "Y";
|
||||
}
|
||||
|
||||
return $available;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user