Vinoteca: wine shop on 1C-Bitrix (module, template, components, pages)
This commit is contained in:
commit
52795535ae
586 files changed
+27619
No files matched your search
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Bitrix\Iblock\ElementPropertyTable;
|
||||
use Bitrix\Iblock\ElementTable;
|
||||
use Bitrix\Iblock\PropertyTable;
|
||||
use Bitrix\Main\Loader;
|
||||
use Bitrix\Main\Type\DateTime;
|
||||
use Jinove\Vinoteca\Config;
|
||||
use Jinove\Vinoteca\PageSchema;
|
||||
|
||||
class BrandSchema
|
||||
{
|
||||
private const CONTEXT = "https://schema.org";
|
||||
private const WINES_LIMIT = 24;
|
||||
|
||||
public static function list(array $items, string $name, string $description): array
|
||||
{
|
||||
$url = Config::host() . SITE_DIR . "brands/";
|
||||
$list = [];
|
||||
foreach (array_values($items) as $index => $item) {
|
||||
$list[] = ["@type" => "ListItem", "position" => $index + 1, "url" => $item["url"], "name" => $item["name"]];
|
||||
}
|
||||
$page = [
|
||||
"@type" => "CollectionPage",
|
||||
"@id" => $url . "#webpage",
|
||||
"url" => $url,
|
||||
"name" => $name,
|
||||
"description" => $description,
|
||||
"inLanguage" => LANGUAGE_ID,
|
||||
"about" => ["@id" => Config::host() . "/#store"],
|
||||
"mainEntity" => ["@id" => $url . "#items"],
|
||||
"workTranslation" => PageSchema::translations(SITE_DIR . "brands/", "CollectionPage"),
|
||||
];
|
||||
$itemList = ["@type" => "ItemList", "@id" => $url . "#items", "numberOfItems" => count($list), "itemListElement" => $list];
|
||||
|
||||
return ["@context" => self::CONTEXT, "@graph" => [$page, $itemList]];
|
||||
}
|
||||
|
||||
public static function detail(array $brand, array $wines, int $winesTotal): array
|
||||
{
|
||||
$url = $brand["url"];
|
||||
$node = [
|
||||
"@type" => ["Brand", "Winery"],
|
||||
"@id" => $url . "#brand",
|
||||
"name" => $brand["name"],
|
||||
"url" => $url,
|
||||
];
|
||||
if ($brand["description"] !== "") {
|
||||
$node["description"] = $brand["description"];
|
||||
}
|
||||
if ($brand["logo"] !== "") {
|
||||
$node["logo"] = PageSchema::image($url . "#logo", $brand["logo"], $brand["logoWidth"], $brand["logoHeight"]);
|
||||
}
|
||||
$address = ["@type" => "PostalAddress"];
|
||||
if ($brand["country"] !== "") {
|
||||
$address["addressCountry"] = $brand["country"];
|
||||
}
|
||||
if ($brand["region"] !== "") {
|
||||
$address["addressRegion"] = $brand["region"];
|
||||
}
|
||||
if (count($address) > 1) {
|
||||
$node["address"] = $address;
|
||||
}
|
||||
if (preg_match('/^\d{4}$/', $brand["founded"])) {
|
||||
$node["foundingDate"] = $brand["founded"];
|
||||
}
|
||||
$list = [];
|
||||
foreach (array_values($wines) as $index => $wine) {
|
||||
$list[] = ["@type" => "ListItem", "position" => $index + 1, "url" => Config::host() . $wine["url"], "name" => $wine["name"]];
|
||||
}
|
||||
$page = [
|
||||
"@type" => "CollectionPage",
|
||||
"@id" => $url . "#webpage",
|
||||
"url" => $url,
|
||||
"name" => $brand["name"],
|
||||
"description" => $brand["pageDescription"],
|
||||
"inLanguage" => LANGUAGE_ID,
|
||||
"mainEntity" => ["@id" => $url . "#brand"],
|
||||
"workTranslation" => PageSchema::translations($brand["path"], "CollectionPage"),
|
||||
];
|
||||
$graph = [$page, $node];
|
||||
if ($list) {
|
||||
$graph[] = ["@type" => "ItemList", "@id" => $url . "#items", "numberOfItems" => $winesTotal, "itemListElement" => $list];
|
||||
}
|
||||
|
||||
return ["@context" => self::CONTEXT, "@graph" => $graph];
|
||||
}
|
||||
|
||||
public static function countryCode(string $englishName): string
|
||||
{
|
||||
return [
|
||||
"Argentina" => "AR", "Australia" => "AU", "France" => "FR", "Hungary" => "HU", "Italy" => "IT",
|
||||
"New Zealand" => "NZ", "Portugal" => "PT", "Spain" => "ES", "USA" => "US",
|
||||
][trim($englishName)] ?? "";
|
||||
}
|
||||
|
||||
public static function wines(int $brandId): array
|
||||
{
|
||||
$catalogId = Config::iblockId("catalog");
|
||||
if ($brandId <= 0 || $catalogId <= 0 || !Loader::includeModule("iblock")) {
|
||||
return ["items" => [], "total" => 0];
|
||||
}
|
||||
\CIBlock::registerWithTagCache($catalogId);
|
||||
$property = PropertyTable::getRow(["select" => ["ID"], "filter" => ["=IBLOCK_ID" => $catalogId, "=CODE" => "BRAND"]]);
|
||||
if ($property === null) {
|
||||
return ["items" => [], "total" => 0];
|
||||
}
|
||||
$ids = [];
|
||||
$rows = ElementPropertyTable::getList([
|
||||
"select" => ["IBLOCK_ELEMENT_ID"],
|
||||
"filter" => ["=IBLOCK_PROPERTY_ID" => (int)$property["ID"], "=VALUE_NUM" => $brandId],
|
||||
]);
|
||||
while ($row = $rows->fetch()) {
|
||||
$ids[] = (int)$row["IBLOCK_ELEMENT_ID"];
|
||||
}
|
||||
if (!$ids) {
|
||||
return ["items" => [], "total" => 0];
|
||||
}
|
||||
$now = new DateTime();
|
||||
$ordered = ElementTable::getList([
|
||||
"select" => ["ID"],
|
||||
"filter" => [
|
||||
"=IBLOCK_ID" => $catalogId,
|
||||
"=ID" => $ids,
|
||||
"=ACTIVE" => "Y",
|
||||
["LOGIC" => "OR", ["=ACTIVE_FROM" => false], ["<=ACTIVE_FROM" => $now]],
|
||||
["LOGIC" => "OR", ["=ACTIVE_TO" => false], [">=ACTIVE_TO" => $now]],
|
||||
],
|
||||
"order" => ["SORT" => "ASC", "ID" => "DESC"],
|
||||
])->fetchAll();
|
||||
$sortedIds = array_map(static fn(array $row): int => (int)$row["ID"], $ordered);
|
||||
$summaries = Related::summariesById(array_slice($sortedIds, 0, self::WINES_LIMIT));
|
||||
|
||||
return [
|
||||
"items" => array_values(array_filter(array_map(static fn(int $id): ?array => $summaries[$id] ?? null, array_slice($sortedIds, 0, self::WINES_LIMIT)))),
|
||||
"total" => count($sortedIds),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Jinove\Vinoteca\Config;
|
||||
use Jinove\Vinoteca\Cached;
|
||||
|
||||
class Countries
|
||||
{
|
||||
private const CACHE_TTL = 86400;
|
||||
private const CACHE_DIR = "/jinove.vinoteca/countries";
|
||||
|
||||
private static ?array $names = null;
|
||||
|
||||
public static function names(): array
|
||||
{
|
||||
if (self::$names !== null) {
|
||||
return self::$names;
|
||||
}
|
||||
$catalogId = Config::iblockId("catalog");
|
||||
return self::$names = (array)Cached::remember(self::CACHE_DIR, "names-" . $catalogId, self::CACHE_TTL, ["iblock_id_" . $catalogId], static function () use ($catalogId): array {
|
||||
$names = [];
|
||||
$rows = \CIBlockPropertyEnum::GetList(["SORT" => "ASC", "VALUE" => "ASC"], ["IBLOCK_ID" => $catalogId, "CODE" => "COUNTRY"]);
|
||||
while ($row = $rows->Fetch()) {
|
||||
$names[(string)$row["XML_ID"]] = (string)$row["VALUE"];
|
||||
}
|
||||
|
||||
return $names;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Bitrix\Main\Localization\Loc;
|
||||
|
||||
Loc::loadMessages(__FILE__);
|
||||
|
||||
class EnumNames
|
||||
{
|
||||
public static function name(string $propertyCode, string $xmlId, string $fallback = "", ?string $language = null): string
|
||||
{
|
||||
$key = "VINO_ENUM_" . $propertyCode . "_" . strtoupper(str_replace("-", "_", $xmlId));
|
||||
$message = Loc::getMessage($key, null, $language);
|
||||
|
||||
return $message !== null && $message !== "" ? $message : $fallback;
|
||||
}
|
||||
|
||||
public static function fromProperty(array $property): string
|
||||
{
|
||||
$xmlIds = (array)($property["VALUE_XML_ID"] ?? []);
|
||||
$values = (array)($property["~VALUE"] ?? $property["VALUE"] ?? []);
|
||||
$names = [];
|
||||
foreach ($xmlIds as $index => $xmlId) {
|
||||
if ((string)$xmlId === "") {
|
||||
continue;
|
||||
}
|
||||
$names[] = self::name((string)$property["CODE"], (string)$xmlId, (string)($values[$index] ?? ""));
|
||||
}
|
||||
|
||||
return implode(", ", $names);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Bitrix\Iblock\ElementTable;
|
||||
use Bitrix\Main\Loader;
|
||||
use Bitrix\Main\ORM\Data\DataManager;
|
||||
use Bitrix\Sale\Fuser;
|
||||
use Jinove\Vinoteca\Config;
|
||||
use Bitrix\Main\ORM\Fields\DatetimeField;
|
||||
use Bitrix\Main\ORM\Fields\IntegerField;
|
||||
use Bitrix\Main\Type\DateTime;
|
||||
|
||||
class FavoriteTable extends DataManager
|
||||
{
|
||||
private static int $guestFuser = 0;
|
||||
|
||||
public static function getTableName(): string
|
||||
{
|
||||
return "jinove_vinoteca_favorite";
|
||||
}
|
||||
|
||||
public static function getMap(): array
|
||||
{
|
||||
return [
|
||||
(new IntegerField("ID"))->configurePrimary()->configureAutocomplete(),
|
||||
(new IntegerField("FUSER_ID"))->configureRequired(),
|
||||
(new IntegerField("PRODUCT_ID"))->configureRequired(),
|
||||
(new DatetimeField("DATE_CREATE"))->configureDefaultValue(static fn(): DateTime => new DateTime()),
|
||||
];
|
||||
}
|
||||
|
||||
public static function productIds(int $fuserId): array
|
||||
{
|
||||
if ($fuserId <= 0) {
|
||||
return [];
|
||||
}
|
||||
$ids = array_map("intval", array_column(self::getList([
|
||||
"select" => ["PRODUCT_ID"],
|
||||
"filter" => ["=FUSER_ID" => $fuserId],
|
||||
"order" => ["ID" => "ASC"],
|
||||
])->fetchAll(), "PRODUCT_ID"));
|
||||
if (!$ids || !Loader::includeModule("iblock")) {
|
||||
return [];
|
||||
}
|
||||
$active = array_flip(array_map("intval", array_column(ElementTable::getList([
|
||||
"select" => ["ID"],
|
||||
"filter" => ["=ID" => $ids, "=IBLOCK_ID" => Config::iblockId("catalog"), "=ACTIVE" => "Y"],
|
||||
])->fetchAll(), "ID")));
|
||||
|
||||
return array_values(array_filter($ids, static fn(int $id): bool => isset($active[$id])));
|
||||
}
|
||||
|
||||
public static function onElementDelete(array $fields): void
|
||||
{
|
||||
$productId = (int)($fields["ID"] ?? 0);
|
||||
if ($productId <= 0) {
|
||||
return;
|
||||
}
|
||||
$rows = self::getList(["select" => ["ID"], "filter" => ["=PRODUCT_ID" => $productId]]);
|
||||
while ($row = $rows->fetch()) {
|
||||
self::delete((int)$row["ID"]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function rememberGuest(): void
|
||||
{
|
||||
if (Loader::includeModule("sale")) {
|
||||
self::$guestFuser = (int)Fuser::getId(true);
|
||||
}
|
||||
}
|
||||
|
||||
public static function mergeGuest(): void
|
||||
{
|
||||
if (self::$guestFuser <= 0 || !Loader::includeModule("sale")) {
|
||||
return;
|
||||
}
|
||||
$userFuser = (int)Fuser::getId(true);
|
||||
if ($userFuser <= 0 || $userFuser === self::$guestFuser) {
|
||||
return;
|
||||
}
|
||||
$owned = array_flip(array_map("intval", array_column(self::getList(["select" => ["PRODUCT_ID"], "filter" => ["=FUSER_ID" => $userFuser]])->fetchAll(), "PRODUCT_ID")));
|
||||
$rows = self::getList(["select" => ["ID", "PRODUCT_ID"], "filter" => ["=FUSER_ID" => self::$guestFuser]]);
|
||||
while ($row = $rows->fetch()) {
|
||||
if (isset($owned[(int)$row["PRODUCT_ID"]])) {
|
||||
self::delete((int)$row["ID"]);
|
||||
} else {
|
||||
self::update((int)$row["ID"], ["FUSER_ID" => $userFuser]);
|
||||
}
|
||||
}
|
||||
self::$guestFuser = 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Bitrix\Main\Loader;
|
||||
use Bitrix\Main\Localization\Loc;
|
||||
use Bitrix\Main\Page\Asset;
|
||||
use Bitrix\Sale\Fuser;
|
||||
|
||||
Loc::loadMessages(__FILE__);
|
||||
|
||||
final class Favorites
|
||||
{
|
||||
public const FILTER = "vinoFavoritesFilter";
|
||||
|
||||
public static function preparePage(): void
|
||||
{
|
||||
global $APPLICATION;
|
||||
$APPLICATION->SetTitle((string)Loc::getMessage("VINO_FAVORITES_H1"));
|
||||
$APPLICATION->SetPageProperty("title", (string)Loc::getMessage("VINO_FAVORITES_H1"));
|
||||
$APPLICATION->SetPageProperty("robots", "noindex, follow");
|
||||
$APPLICATION->SetPageProperty("vino_alternates", "N");
|
||||
Asset::getInstance()->addCss(SITE_TEMPLATE_PATH . "/catalog-styles.css");
|
||||
Asset::getInstance()->addJs(SITE_TEMPLATE_PATH . "/assets/js/vino-favorites.js");
|
||||
}
|
||||
|
||||
public static function ids(): array
|
||||
{
|
||||
if (!Loader::includeModule("sale")) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return FavoriteTable::productIds((int)Fuser::getId(true));
|
||||
}
|
||||
|
||||
public static function params(array $ids): array
|
||||
{
|
||||
$GLOBALS[self::FILTER] = ["ID" => $ids ?: [0]];
|
||||
$catalog = Listing::catalogParams();
|
||||
|
||||
return [
|
||||
"IBLOCK_TYPE" => $catalog["IBLOCK_TYPE"],
|
||||
"IBLOCK_ID" => $catalog["IBLOCK_ID"],
|
||||
"SECTION_ID" => 0,
|
||||
"SHOW_ALL_WO_SECTION" => "Y",
|
||||
"FILTER_NAME" => self::FILTER,
|
||||
"PRICE_CODE" => $catalog["PRICE_CODE"],
|
||||
"BASKET_URL" => $catalog["BASKET_URL"],
|
||||
"PAGE_ELEMENT_COUNT" => "24",
|
||||
"LINE_ELEMENT_COUNT" => "4",
|
||||
"INCLUDE_SUBSECTIONS" => "Y",
|
||||
"ELEMENT_SORT_FIELD" => "SORT",
|
||||
"ELEMENT_SORT_ORDER" => "ASC",
|
||||
"ELEMENT_SORT_FIELD2" => "ID",
|
||||
"ELEMENT_SORT_ORDER2" => "DESC",
|
||||
"PROPERTY_CODE" => $catalog["LIST_PROPERTY_CODE"],
|
||||
"CACHE_TYPE" => $catalog["CACHE_TYPE"],
|
||||
"CACHE_TIME" => $catalog["CACHE_TIME"],
|
||||
"CACHE_GROUPS" => $catalog["CACHE_GROUPS"],
|
||||
"CACHE_FILTER" => "Y",
|
||||
"SET_TITLE" => "N",
|
||||
"SET_BROWSER_TITLE" => "N",
|
||||
"SET_META_KEYWORDS" => "N",
|
||||
"SET_META_DESCRIPTION" => "N",
|
||||
"SET_STATUS_404" => "N",
|
||||
"SHOW_404" => "N",
|
||||
"ADD_SECTIONS_CHAIN" => "N",
|
||||
"ACTION_VARIABLE" => $catalog["ACTION_VARIABLE"],
|
||||
"PRODUCT_ID_VARIABLE" => $catalog["PRODUCT_ID_VARIABLE"],
|
||||
"USE_PRODUCT_QUANTITY" => "Y",
|
||||
"ADD_TO_BASKET_ACTION" => "ADD",
|
||||
"SHOW_OLD_PRICE" => "Y",
|
||||
"SHOW_DISCOUNT_PERCENT" => "Y",
|
||||
"DISPLAY_TOP_PAGER" => "N",
|
||||
"DISPLAY_BOTTOM_PAGER" => "Y",
|
||||
"PAGER_TEMPLATE" => "vinoteca",
|
||||
"PAGER_SHOW_ALWAYS" => "N",
|
||||
"PAGER_TITLE" => (string)Loc::getMessage("VINO_FAVORITES_PAGER_TITLE"),
|
||||
"SECTION_URL" => $catalog["SEF_FOLDER"] . $catalog["SEF_URL_TEMPLATES"]["section"],
|
||||
"DETAIL_URL" => $catalog["SEF_FOLDER"] . $catalog["SEF_URL_TEMPLATES"]["element"],
|
||||
"HIDE_NOT_AVAILABLE" => "N",
|
||||
"VINO_TITLE_TAG" => "h2",
|
||||
"VINO_GRID_FULL" => "Y",
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
class Listing
|
||||
{
|
||||
public const SORTS = ["new", "price_asc", "price_desc", "name"];
|
||||
|
||||
public static function catalogParams(): array
|
||||
{
|
||||
return [
|
||||
"IBLOCK_TYPE" => "catalog",
|
||||
"IBLOCK_ID" => VINO_CATALOG_IBLOCK,
|
||||
"SEF_MODE" => "Y",
|
||||
"SEF_FOLDER" => SITE_DIR . "catalog/",
|
||||
"SEF_URL_TEMPLATES" => [
|
||||
"sections" => "",
|
||||
"section" => "#SECTION_CODE#/",
|
||||
"element" => "#SECTION_CODE#/#ELEMENT_CODE#/",
|
||||
"smart_filter" => "#SECTION_CODE#/filter/#SMART_FILTER_PATH#/apply/",
|
||||
],
|
||||
"PRICE_CODE" => ["BASE"],
|
||||
"BASKET_URL" => SITE_DIR . "personal/cart/",
|
||||
"ACTION_VARIABLE" => "action",
|
||||
"PRODUCT_ID_VARIABLE" => "id",
|
||||
"CACHE_TYPE" => "A",
|
||||
"CACHE_TIME" => "3600",
|
||||
"CACHE_GROUPS" => "N",
|
||||
"SET_STATUS_404" => "Y",
|
||||
"SHOW_404" => "Y",
|
||||
"MESSAGE_404" => "",
|
||||
"FILE_404" => "",
|
||||
"LIST_PROPERTY_CODE" => ["WINE_COLOR", "SUGAR", "VOLUME", "REGION", "COUNTRY"],
|
||||
"DETAIL_PROPERTY_CODE" => ["WINE_COLOR", "SUGAR", "GRAPE", "VOLUME", "ABV", "REGION", "COUNTRY", "BRAND", "ARTNUMBER", "VINTAGE", "AGING", "SERVE_TEMP", "FOOD_PAIRING", "TASTE_COLOR", "AROMA", "TASTE", "MORE_PHOTO"],
|
||||
];
|
||||
}
|
||||
|
||||
public static function sectionTilesParams(): array
|
||||
{
|
||||
$catalog = self::catalogParams();
|
||||
|
||||
return [
|
||||
"IBLOCK_TYPE" => $catalog["IBLOCK_TYPE"],
|
||||
"IBLOCK_ID" => $catalog["IBLOCK_ID"],
|
||||
"SECTION_ID" => "",
|
||||
"SECTION_CODE" => "",
|
||||
"TOP_DEPTH" => "1",
|
||||
"COUNT_ELEMENTS" => "Y",
|
||||
"COUNT_ELEMENTS_FILTER" => "CNT_ACTIVE",
|
||||
"HIDE_SECTIONS_WITH_ZERO_COUNT_ELEMENTS" => "Y",
|
||||
"SECTION_FIELDS" => ["CODE", "PICTURE"],
|
||||
"SECTION_USER_FIELDS" => [],
|
||||
"ADD_SECTIONS_CHAIN" => "N",
|
||||
"CACHE_TYPE" => $catalog["CACHE_TYPE"],
|
||||
"CACHE_TIME" => $catalog["CACHE_TIME"],
|
||||
"CACHE_GROUPS" => $catalog["CACHE_GROUPS"],
|
||||
];
|
||||
}
|
||||
|
||||
public static function prepareBestsellers(): array
|
||||
{
|
||||
$GLOBALS["bestsellersFilter"] = ["!PROPERTY_SALELEADER" => false];
|
||||
$catalog = self::catalogParams();
|
||||
|
||||
return [
|
||||
"IBLOCK_TYPE" => $catalog["IBLOCK_TYPE"],
|
||||
"IBLOCK_ID" => $catalog["IBLOCK_ID"],
|
||||
"ELEMENT_COUNT" => "12",
|
||||
"FILTER_NAME" => "bestsellersFilter",
|
||||
"ELEMENT_SORT_FIELD" => "SORT",
|
||||
"ELEMENT_SORT_ORDER" => "asc",
|
||||
"ELEMENT_SORT_FIELD2" => "PROPERTY_RATING",
|
||||
"ELEMENT_SORT_ORDER2" => "desc",
|
||||
"PROPERTY_CODE" => $catalog["LIST_PROPERTY_CODE"],
|
||||
"PRICE_CODE" => $catalog["PRICE_CODE"],
|
||||
"PRICE_VAT_INCLUDE" => "Y",
|
||||
"USE_PRICE_COUNT" => "N",
|
||||
"SHOW_PRICE_COUNT" => "1",
|
||||
"CONVERT_CURRENCY" => "N",
|
||||
"HIDE_NOT_AVAILABLE" => "N",
|
||||
"DETAIL_URL" => SITE_DIR . "catalog/#SECTION_CODE#/#ELEMENT_CODE#/",
|
||||
"SECTION_URL" => SITE_DIR . "catalog/#SECTION_CODE#/",
|
||||
"CACHE_TYPE" => $catalog["CACHE_TYPE"],
|
||||
"CACHE_TIME" => $catalog["CACHE_TIME"],
|
||||
"CACHE_GROUPS" => $catalog["CACHE_GROUPS"],
|
||||
"CACHE_FILTER" => "Y",
|
||||
];
|
||||
}
|
||||
|
||||
public static function filterParams(string $sectionCode, string $filterPath): array
|
||||
{
|
||||
$catalog = self::catalogParams();
|
||||
|
||||
return [
|
||||
"IBLOCK_TYPE" => $catalog["IBLOCK_TYPE"],
|
||||
"IBLOCK_ID" => $catalog["IBLOCK_ID"],
|
||||
"SECTION_CODE" => $sectionCode,
|
||||
"FILTER_NAME" => "wineFilter",
|
||||
"PRICE_CODE" => $catalog["PRICE_CODE"],
|
||||
"CACHE_TYPE" => $catalog["CACHE_TYPE"],
|
||||
"CACHE_TIME" => $catalog["CACHE_TIME"],
|
||||
"CACHE_GROUPS" => $catalog["CACHE_GROUPS"],
|
||||
"SAVE_IN_SESSION" => "N",
|
||||
"XML_EXPORT" => "N",
|
||||
"SEF_MODE" => "Y",
|
||||
"SEF_RULE" => $catalog["SEF_FOLDER"] . $catalog["SEF_URL_TEMPLATES"]["smart_filter"],
|
||||
"SMART_FILTER_PATH" => $filterPath,
|
||||
"INSTANT_RELOAD" => "N",
|
||||
"TEMPLATE_THEME" => "",
|
||||
"CONVERT_CURRENCY" => "N",
|
||||
];
|
||||
}
|
||||
|
||||
public static function countryCodeFromRoute(array $variables): ?string
|
||||
{
|
||||
$code = (string)($variables["ELEMENT_CODE"] ?? "");
|
||||
|
||||
return ($variables["SECTION_CODE"] ?? "") === "country" && $code !== "" ? $code : null;
|
||||
}
|
||||
|
||||
public static function countryPath(string $countryCode): string
|
||||
{
|
||||
return self::catalogParams()["SEF_FOLDER"] . "country/" . rawurlencode($countryCode) . "/";
|
||||
}
|
||||
|
||||
public static function sectionPath(string $sectionCode): string
|
||||
{
|
||||
return self::catalogParams()["SEF_FOLDER"] . $sectionCode . "/";
|
||||
}
|
||||
|
||||
public static function basePath(string $sectionCode, string $filterPath): string
|
||||
{
|
||||
$sectionPath = self::sectionPath($sectionCode);
|
||||
|
||||
return $filterPath !== "" ? $sectionPath . "filter/" . $filterPath . "/apply/" : $sectionPath;
|
||||
}
|
||||
|
||||
public static function pagePath(string $sectionCode, string $filterPath, string $sort, int $page): string
|
||||
{
|
||||
$sectionPath = self::sectionPath($sectionCode);
|
||||
if ($filterPath === "" && $sort === "" && $page > 1) {
|
||||
return $sectionPath . "?PAGEN_1=" . $page;
|
||||
}
|
||||
|
||||
return $sectionPath;
|
||||
}
|
||||
|
||||
public static function filterPathIsValid(string $sectionCode, string $filterPath): bool
|
||||
{
|
||||
if ($filterPath === "") {
|
||||
return true;
|
||||
}
|
||||
global $APPLICATION;
|
||||
ob_start();
|
||||
try {
|
||||
$result = $APPLICATION->IncludeComponent("bitrix:catalog.smart.filter", "", self::filterParams($sectionCode, $filterPath), null, [], true);
|
||||
} finally {
|
||||
ob_end_clean();
|
||||
}
|
||||
|
||||
return (string)($result["JS_FILTER_PARAMS"]["SEF_SET_FILTER_URL"] ?? "") === self::basePath($sectionCode, $filterPath);
|
||||
}
|
||||
|
||||
public static function parsePath(string $path): ?array
|
||||
{
|
||||
$catalog = self::catalogParams();
|
||||
$variables = [];
|
||||
$engine = new \CComponentEngine();
|
||||
$engine->addGreedyPart("#SMART_FILTER_PATH#");
|
||||
$page = $engine->guessComponentPath($catalog["SEF_FOLDER"], $catalog["SEF_URL_TEMPLATES"], $variables, str_ends_with($path, "/") ? $path . "index.php" : $path);
|
||||
if ($page !== "section" && $page !== "smart_filter") {
|
||||
return null;
|
||||
}
|
||||
$sectionCode = (string)($variables["SECTION_CODE"] ?? "");
|
||||
if ($sectionCode === "" || $sectionCode !== strtolower($sectionCode) || Sections::byCode($sectionCode) === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
"SECTION_CODE" => $sectionCode,
|
||||
"FILTER_PATH" => (string)($variables["SMART_FILTER_PATH"] ?? ""),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
class LocalizedText
|
||||
{
|
||||
private const BASE_LANGUAGE = "ru";
|
||||
|
||||
public static function property(array $properties, string $code): string
|
||||
{
|
||||
if (LANGUAGE_ID !== self::BASE_LANGUAGE) {
|
||||
$translated = self::value($properties[$code . "_" . strtoupper(LANGUAGE_ID)] ?? []);
|
||||
if ($translated !== "") {
|
||||
return $translated;
|
||||
}
|
||||
}
|
||||
|
||||
return self::value($properties[$code] ?? []);
|
||||
}
|
||||
|
||||
public static function detailText(array $element): string
|
||||
{
|
||||
return self::field($element, "DETAIL_TEXT");
|
||||
}
|
||||
|
||||
public static function field(array $element, string $field): string
|
||||
{
|
||||
if (LANGUAGE_ID !== self::BASE_LANGUAGE) {
|
||||
$translated = self::value($element["PROPERTIES"][$field . "_" . strtoupper(LANGUAGE_ID)] ?? []);
|
||||
if ($translated !== "") {
|
||||
return $translated;
|
||||
}
|
||||
}
|
||||
|
||||
return (string)($element["~" . $field] ?? $element[$field] ?? "");
|
||||
}
|
||||
|
||||
public static function localizeItem(array $item, array $fields, array $properties = []): array
|
||||
{
|
||||
if (LANGUAGE_ID === self::BASE_LANGUAGE) {
|
||||
return $item;
|
||||
}
|
||||
$suffix = "_" . strtoupper(LANGUAGE_ID);
|
||||
foreach ($fields as $field) {
|
||||
$value = self::value($item["PROPERTIES"][$field . $suffix] ?? []);
|
||||
if ($value !== "") {
|
||||
$item["~" . $field] = $value;
|
||||
$item[$field] = htmlspecialcharsbx($value);
|
||||
}
|
||||
}
|
||||
foreach ($properties as $code) {
|
||||
$value = self::value($item["PROPERTIES"][$code . $suffix] ?? []);
|
||||
if ($value === "" || !isset($item["PROPERTIES"][$code])) {
|
||||
continue;
|
||||
}
|
||||
$item["PROPERTIES"][$code]["~VALUE"] = $value;
|
||||
$item["PROPERTIES"][$code]["VALUE"] = htmlspecialcharsbx($value);
|
||||
if (isset($item["DISPLAY_PROPERTIES"][$code])) {
|
||||
$item["DISPLAY_PROPERTIES"][$code]["~VALUE"] = $value;
|
||||
$item["DISPLAY_PROPERTIES"][$code]["VALUE"] = htmlspecialcharsbx($value);
|
||||
$item["DISPLAY_PROPERTIES"][$code]["DISPLAY_VALUE"] = htmlspecialcharsbx($value);
|
||||
}
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
private static function value(array $property): string
|
||||
{
|
||||
$value = $property["~VALUE"] ?? $property["VALUE"] ?? "";
|
||||
if (is_array($value)) {
|
||||
$value = $value["TEXT"] ?? implode(", ", array_filter($value, "is_scalar"));
|
||||
}
|
||||
|
||||
return trim((string)$value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Bitrix\Iblock\ElementTable;
|
||||
use Bitrix\Main\Loader;
|
||||
|
||||
final class Media
|
||||
{
|
||||
public static function thumbs(array $productIds, int $width, int $height): array
|
||||
{
|
||||
$productIds = array_values(array_unique(array_filter(array_map("intval", $productIds))));
|
||||
if (!$productIds || !Loader::includeModule("iblock")) {
|
||||
return [];
|
||||
}
|
||||
$images = [];
|
||||
$rows = ElementTable::getList(["select" => ["ID", "PREVIEW_PICTURE", "DETAIL_PICTURE"], "filter" => ["=ID" => $productIds]]);
|
||||
while ($row = $rows->fetch()) {
|
||||
$fileId = (int)($row["DETAIL_PICTURE"] ?: $row["PREVIEW_PICTURE"]);
|
||||
if ($fileId > 0) {
|
||||
$resized = \CFile::ResizeImageGet($fileId, ["width" => $width, "height" => $height], BX_RESIZE_IMAGE_PROPORTIONAL, false);
|
||||
if (!empty($resized["src"])) {
|
||||
$images[(int)$row["ID"]] = (string)$resized["src"];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $images;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Bitrix\Iblock\ElementPropertyTable;
|
||||
use Bitrix\Iblock\ElementTable;
|
||||
use Bitrix\Iblock\PropertyEnumerationTable;
|
||||
use Bitrix\Iblock\PropertyTable;
|
||||
use Bitrix\Iblock\SectionElementTable;
|
||||
use Bitrix\Main\DI\ServiceLocator;
|
||||
use Bitrix\Main\Loader;
|
||||
use Bitrix\Main\ORM\Fields\ExpressionField;
|
||||
use Jinove\Vinoteca\Config;
|
||||
|
||||
final class MegaMenu
|
||||
{
|
||||
public static function tags(int $iblockId): array
|
||||
{
|
||||
return ["iblock_id_" . $iblockId, "iblock_id_" . Config::iblockId("brands")];
|
||||
}
|
||||
|
||||
public static function collect(int $iblockId, int $brandsLimit): array
|
||||
{
|
||||
$result = ["TYPES" => [], "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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Bitrix\Iblock\ElementTable;
|
||||
use Jinove\Vinoteca\Config;
|
||||
|
||||
class PhotoSort
|
||||
{
|
||||
public const WITH_PHOTO = 100;
|
||||
public const WITHOUT_PHOTO = 500;
|
||||
|
||||
private static bool $busy = false;
|
||||
|
||||
public static function onElementChange(array &$fields): void
|
||||
{
|
||||
$id = (int)($fields["ID"] ?? 0);
|
||||
$catalogId = Config::iblockId("catalog");
|
||||
if (self::$busy || $id <= 0 || $catalogId <= 0 || (int)($fields["IBLOCK_ID"] ?? 0) !== $catalogId) {
|
||||
return;
|
||||
}
|
||||
self::apply($id);
|
||||
}
|
||||
|
||||
public static function apply(int $id): void
|
||||
{
|
||||
$row = ElementTable::getRow([
|
||||
"select" => ["DETAIL_PICTURE", "SORT", "IBLOCK_ID"],
|
||||
"filter" => ["=ID" => $id],
|
||||
]);
|
||||
if (!$row) {
|
||||
return;
|
||||
}
|
||||
$sort = (int)$row["DETAIL_PICTURE"] > 0 ? self::WITH_PHOTO : self::WITHOUT_PHOTO;
|
||||
if ((int)$row["SORT"] === $sort) {
|
||||
return;
|
||||
}
|
||||
self::$busy = true;
|
||||
(new \CIBlockElement())->Update($id, ["SORT" => $sort], false, false, false);
|
||||
self::$busy = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Bitrix\Catalog\PriceTable;
|
||||
use Bitrix\Currency\CurrencyManager;
|
||||
use Bitrix\Main\Entity\ExpressionField;
|
||||
use Bitrix\Main\Loader;
|
||||
use Jinove\Vinoteca\Config;
|
||||
use Jinove\Vinoteca\Cached;
|
||||
|
||||
class PriceRange
|
||||
{
|
||||
private const CACHE_TTL = 86400;
|
||||
private const CACHE_DIR = "/jinove.vinoteca/pricerange";
|
||||
|
||||
public static function get(string $priceCode = "BASE"): string
|
||||
{
|
||||
$catalogId = Config::iblockId("catalog");
|
||||
return (string)Cached::remember(self::CACHE_DIR, "range-" . $catalogId . "-" . $priceCode, self::CACHE_TTL, ["iblock_id_" . $catalogId], static fn(): string => self::collect($priceCode));
|
||||
}
|
||||
|
||||
private static function collect(string $priceCode): string
|
||||
{
|
||||
if (!Loader::includeModule("catalog") || !Loader::includeModule("currency")) {
|
||||
return "";
|
||||
}
|
||||
$groupId = PricePresets::groupId($priceCode);
|
||||
if ($groupId === 0) {
|
||||
return "";
|
||||
}
|
||||
$row = PriceTable::getList([
|
||||
"select" => ["MIN_PRICE", "MAX_PRICE"],
|
||||
"filter" => ["=CATALOG_GROUP_ID" => $groupId, ">PRICE" => 0],
|
||||
"runtime" => [
|
||||
new ExpressionField("MIN_PRICE", "MIN(%s)", "PRICE"),
|
||||
new ExpressionField("MAX_PRICE", "MAX(%s)", "PRICE"),
|
||||
],
|
||||
])->fetch();
|
||||
if (!$row || $row["MIN_PRICE"] <= 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return (int)round((float)$row["MIN_PRICE"]) . "-" . (int)round((float)$row["MAX_PRICE"]) . " " . CurrencyManager::getBaseCurrency();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Bitrix\Main\Engine\CurrentUser;
|
||||
use Bitrix\Main\Loader;
|
||||
|
||||
final class Prices
|
||||
{
|
||||
public static function optimal(array $productIds, string $siteId = SITE_ID): array
|
||||
{
|
||||
$productIds = array_values(array_unique(array_filter(array_map("intval", $productIds))));
|
||||
if (!$productIds || !Loader::includeModule("catalog") || !Loader::includeModule("currency")) {
|
||||
return [];
|
||||
}
|
||||
$groups = CurrentUser::get()->getUserGroups();
|
||||
$prices = [];
|
||||
foreach ($productIds as $productId) {
|
||||
$optimal = \CCatalogProduct::GetOptimalPrice($productId, 1, $groups, "N", [], $siteId);
|
||||
$price = $optimal["RESULT_PRICE"] ?? null;
|
||||
if (!$price || (float)$price["DISCOUNT_PRICE"] <= 0) {
|
||||
continue;
|
||||
}
|
||||
$prices[$productId] = [
|
||||
"price" => (float)$price["DISCOUNT_PRICE"],
|
||||
"base" => max((float)$price["DISCOUNT_PRICE"], (float)$price["BASE_PRICE"]),
|
||||
"currency" => (string)$price["CURRENCY"],
|
||||
];
|
||||
}
|
||||
|
||||
return $prices;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Bitrix\Iblock\ElementPropertyTable;
|
||||
use Bitrix\Iblock\ElementTable;
|
||||
use Bitrix\Iblock\IblockTable;
|
||||
use Bitrix\Iblock\PropertyTable;
|
||||
use Bitrix\Iblock\SectionTable;
|
||||
use Bitrix\Main\Loader;
|
||||
use Jinove\Vinoteca\Config;
|
||||
use Jinove\Vinoteca\Cached;
|
||||
|
||||
class Related
|
||||
{
|
||||
private const CACHE_TTL = 86400;
|
||||
private const CACHE_DIR = "/jinove.vinoteca/related";
|
||||
|
||||
public static function ids(int $iblockId, int $elementId): array
|
||||
{
|
||||
if (!Loader::includeModule("iblock")) {
|
||||
return [];
|
||||
}
|
||||
$property = PropertyTable::getRow(["select" => ["ID"], "filter" => ["=IBLOCK_ID" => $iblockId, "=CODE" => "RECOMMEND"]]);
|
||||
if ($property === null) {
|
||||
return [];
|
||||
}
|
||||
$linked = [];
|
||||
$rows = ElementPropertyTable::getList([
|
||||
"select" => ["VALUE"],
|
||||
"filter" => ["=IBLOCK_ELEMENT_ID" => $elementId, "=IBLOCK_PROPERTY_ID" => (int)$property["ID"]],
|
||||
"order" => ["ID" => "ASC"],
|
||||
]);
|
||||
while ($row = $rows->fetch()) {
|
||||
$linked[] = (int)$row["VALUE"];
|
||||
}
|
||||
$linked = array_values(array_filter(array_unique($linked)));
|
||||
if (!$linked) {
|
||||
return [];
|
||||
}
|
||||
$active = [];
|
||||
$items = ElementTable::getList(["select" => ["ID"], "filter" => ["=ID" => $linked, "=IBLOCK_ID" => $iblockId, "=ACTIVE" => "Y"]]);
|
||||
while ($item = $items->fetch()) {
|
||||
$active[(int)$item["ID"]] = true;
|
||||
}
|
||||
|
||||
return array_values(array_filter($linked, fn(int $id): bool => isset($active[$id])));
|
||||
}
|
||||
|
||||
public static function summaries(array $ids): array
|
||||
{
|
||||
$found = self::summariesById($ids);
|
||||
|
||||
return array_values(array_filter(array_map(static fn(int $id): ?array => $found[$id] ?? null, $ids)));
|
||||
}
|
||||
|
||||
public static function summariesById(array $ids): array
|
||||
{
|
||||
$ids = array_values(array_unique(array_filter(array_map("intval", $ids))));
|
||||
if (!$ids) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_intersect_key(self::all(), array_flip($ids));
|
||||
}
|
||||
|
||||
private static function all(): array
|
||||
{
|
||||
$iblockId = Config::iblockId("catalog");
|
||||
if ($iblockId <= 0 || !Loader::includeModule("iblock")) {
|
||||
return [];
|
||||
}
|
||||
return (array)Cached::remember(self::CACHE_DIR, "summaries-" . $iblockId . "-" . SITE_DIR, self::CACHE_TTL, ["iblock_id_" . $iblockId], static fn(): array => self::collect($iblockId));
|
||||
}
|
||||
|
||||
private static function collect(int $iblockId): array
|
||||
{
|
||||
$template = (string)(IblockTable::getRowById($iblockId)["DETAIL_PAGE_URL"] ?? "");
|
||||
$elements = ElementTable::getList([
|
||||
"select" => ["ID", "NAME", "CODE", "IBLOCK_SECTION_ID"],
|
||||
"filter" => ["=IBLOCK_ID" => $iblockId, "=ACTIVE" => "Y"],
|
||||
])->fetchAll();
|
||||
$sectionIds = array_values(array_unique(array_filter(array_map(static fn(array $row): int => (int)$row["IBLOCK_SECTION_ID"], $elements))));
|
||||
$sectionCodes = [];
|
||||
if ($sectionIds) {
|
||||
$sections = SectionTable::getList(["select" => ["ID", "CODE"], "filter" => ["=ID" => $sectionIds]]);
|
||||
while ($section = $sections->fetch()) {
|
||||
$sectionCodes[(int)$section["ID"]] = (string)$section["CODE"];
|
||||
}
|
||||
}
|
||||
$found = [];
|
||||
foreach ($elements as $row) {
|
||||
$found[(int)$row["ID"]] = [
|
||||
"name" => (string)$row["NAME"],
|
||||
"url" => (string)preg_replace('#/{2,}#', "/", strtr($template, [
|
||||
"#SITE_DIR#" => SITE_DIR,
|
||||
"#SECTION_CODE#" => $sectionCodes[(int)$row["IBLOCK_SECTION_ID"]] ?? "",
|
||||
"#ELEMENT_CODE#" => (string)$row["CODE"],
|
||||
"#CODE#" => (string)$row["CODE"],
|
||||
"#ID#" => (string)$row["ID"],
|
||||
"#ELEMENT_ID#" => (string)$row["ID"],
|
||||
])),
|
||||
];
|
||||
}
|
||||
|
||||
return $found;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Bitrix\Iblock\ElementPropertyTable;
|
||||
use Bitrix\Iblock\ElementTable;
|
||||
use Bitrix\Iblock\IblockTable;
|
||||
use Bitrix\Iblock\PropertyTable;
|
||||
use Bitrix\Main\Loader;
|
||||
|
||||
class ReviewStats
|
||||
{
|
||||
public const IBLOCK_TYPE = "catalog";
|
||||
public const REVIEWS_CODE = "reviews";
|
||||
public const CATALOG_CODE = "wine";
|
||||
|
||||
private static array $iblockIds = [];
|
||||
private static array $deleting = [];
|
||||
|
||||
public static function iblockId(string $code): int
|
||||
{
|
||||
if (!isset(self::$iblockIds[$code])) {
|
||||
if (!Loader::includeModule("iblock")) {
|
||||
return 0;
|
||||
}
|
||||
$row = IblockTable::getRow([
|
||||
"select" => ["ID"],
|
||||
"filter" => ["=IBLOCK_TYPE_ID" => self::IBLOCK_TYPE, "=CODE" => $code],
|
||||
]);
|
||||
self::$iblockIds[$code] = (int)($row["ID"] ?? 0);
|
||||
}
|
||||
|
||||
return self::$iblockIds[$code];
|
||||
}
|
||||
|
||||
public static function onElementChange(array &$fields): void
|
||||
{
|
||||
$id = (int)($fields["ID"] ?? 0);
|
||||
if ($id <= 0 || (int)($fields["IBLOCK_ID"] ?? 0) !== self::iblockId(self::REVIEWS_CODE)) {
|
||||
return;
|
||||
}
|
||||
self::recalc(self::productOf($id));
|
||||
}
|
||||
|
||||
public static function onBeforeDelete(int $id): void
|
||||
{
|
||||
$row = ElementTable::getRow(["select" => ["IBLOCK_ID"], "filter" => ["=ID" => $id]]);
|
||||
if ((int)($row["IBLOCK_ID"] ?? 0) === self::iblockId(self::REVIEWS_CODE)) {
|
||||
self::$deleting[$id] = self::productOf($id);
|
||||
}
|
||||
}
|
||||
|
||||
public static function onAfterDelete(array $fields): void
|
||||
{
|
||||
$id = (int)($fields["ID"] ?? 0);
|
||||
if (isset(self::$deleting[$id])) {
|
||||
self::recalc(self::$deleting[$id]);
|
||||
unset(self::$deleting[$id]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function summary(int $productId): ?array
|
||||
{
|
||||
$catalogId = self::iblockId(self::CATALOG_CODE);
|
||||
if ($productId <= 0 || $catalogId <= 0) {
|
||||
return null;
|
||||
}
|
||||
$values = [];
|
||||
foreach (["REVIEW_COUNT", "REVIEW_RATING"] as $code) {
|
||||
$property = PropertyTable::getRow(["select" => ["ID"], "filter" => ["=IBLOCK_ID" => $catalogId, "=CODE" => $code]]);
|
||||
$row = $property === null ? null : ElementPropertyTable::getRow([
|
||||
"select" => ["VALUE"],
|
||||
"filter" => ["=IBLOCK_ELEMENT_ID" => $productId, "=IBLOCK_PROPERTY_ID" => (int)$property["ID"]],
|
||||
]);
|
||||
$values[$code] = $row === null ? 0.0 : (float)$row["VALUE"];
|
||||
}
|
||||
|
||||
return ["count" => (int)$values["REVIEW_COUNT"], "avg" => round($values["REVIEW_RATING"], 1)];
|
||||
}
|
||||
|
||||
public static function items(int $productId, int $limit = 50): array
|
||||
{
|
||||
$reviewsId = self::iblockId(self::REVIEWS_CODE);
|
||||
if ($productId <= 0 || $reviewsId <= 0) {
|
||||
return [];
|
||||
}
|
||||
$items = [];
|
||||
$rows = \CIBlockElement::GetList(
|
||||
["ACTIVE_FROM" => "DESC", "ID" => "DESC"],
|
||||
["IBLOCK_ID" => $reviewsId, "ACTIVE" => "Y", "PROPERTY_PRODUCT" => $productId],
|
||||
false,
|
||||
["nTopCount" => $limit],
|
||||
["ID", "PREVIEW_TEXT", "ACTIVE_FROM", "DATE_CREATE", "PROPERTY_AUTHOR", "PROPERTY_RATING"]
|
||||
);
|
||||
while ($row = $rows->Fetch()) {
|
||||
$rating = (int)$row["PROPERTY_RATING_VALUE"];
|
||||
$author = trim((string)$row["PROPERTY_AUTHOR_VALUE"]);
|
||||
$text = trim((string)preg_replace('/\s+/u', " ", strip_tags((string)$row["PREVIEW_TEXT"])));
|
||||
if ($rating < 1 || $rating > 5 || $author === "" || $text === "") {
|
||||
continue;
|
||||
}
|
||||
$items[] = [
|
||||
"author" => $author,
|
||||
"rating" => $rating,
|
||||
"text" => $text,
|
||||
"date" => date("Y-m-d", MakeTimeStamp((string)($row["ACTIVE_FROM"] ?: $row["DATE_CREATE"]))),
|
||||
];
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
public static function recalc(int $productId): void
|
||||
{
|
||||
$catalogId = self::iblockId(self::CATALOG_CODE);
|
||||
if ($productId <= 0 || $catalogId <= 0) {
|
||||
return;
|
||||
}
|
||||
$sum = 0;
|
||||
$count = 0;
|
||||
$rows = \CIBlockElement::GetList(
|
||||
[],
|
||||
["IBLOCK_ID" => self::iblockId(self::REVIEWS_CODE), "ACTIVE" => "Y", "PROPERTY_PRODUCT" => $productId],
|
||||
false,
|
||||
false,
|
||||
["ID", "PROPERTY_RATING"]
|
||||
);
|
||||
while ($row = $rows->Fetch()) {
|
||||
$rating = (int)$row["PROPERTY_RATING_VALUE"];
|
||||
if ($rating >= 1 && $rating <= 5) {
|
||||
$sum += $rating;
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
\CIBlockElement::SetPropertyValuesEx($productId, $catalogId, [
|
||||
"REVIEW_COUNT" => $count,
|
||||
"REVIEW_RATING" => $count > 0 ? round($sum / $count, 1) : false,
|
||||
]);
|
||||
\CIBlock::clearIblockTagCache($catalogId);
|
||||
}
|
||||
|
||||
private static function productOf(int $reviewId): int
|
||||
{
|
||||
$value = \CIBlockElement::GetProperty(self::iblockId(self::REVIEWS_CODE), $reviewId, [], ["CODE" => "PRODUCT"])->Fetch();
|
||||
|
||||
return (int)($value["VALUE"] ?? 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Bitrix\Iblock\ElementPropertyTable;
|
||||
use Bitrix\Iblock\ElementTable;
|
||||
use Bitrix\Iblock\PropertyEnumerationTable;
|
||||
use Bitrix\Iblock\PropertyTable;
|
||||
use Bitrix\Main\Loader;
|
||||
use Jinove\Vinoteca\Config;
|
||||
use Jinove\Vinoteca\Cached;
|
||||
|
||||
final class SearchMeta
|
||||
{
|
||||
private const CACHE_TTL = 86400;
|
||||
private const CACHE_DIR = "/jinove.vinoteca/search-meta-v2";
|
||||
private const PROPERTIES = ["REGION", "COUNTRY"];
|
||||
|
||||
public static function get(array $ids): array
|
||||
{
|
||||
return array_intersect_key(self::all(), array_flip(array_map("intval", $ids)));
|
||||
}
|
||||
|
||||
private static function all(): array
|
||||
{
|
||||
$catalogId = Config::iblockId("catalog");
|
||||
return (array)Cached::remember(self::CACHE_DIR, "meta-" . $catalogId . "-" . LANGUAGE_ID, self::CACHE_TTL, ["iblock_id_" . $catalogId, "iblock_id_" . Config::iblockId("brands")], static fn(): array => self::collect($catalogId));
|
||||
}
|
||||
|
||||
private static function collect(int $catalogId): array
|
||||
{
|
||||
if ($catalogId <= 0 || !Loader::includeModule("iblock")) {
|
||||
return [];
|
||||
}
|
||||
$propertyCodes = [];
|
||||
$rows = PropertyTable::getList(["select" => ["ID", "CODE"], "filter" => ["=IBLOCK_ID" => $catalogId, "=CODE" => self::PROPERTIES]]);
|
||||
while ($row = $rows->fetch()) {
|
||||
$propertyCodes[(int)$row["ID"]] = (string)$row["CODE"];
|
||||
}
|
||||
$enums = [];
|
||||
if ($propertyCodes) {
|
||||
$rows = PropertyEnumerationTable::getList(["select" => ["ID", "XML_ID", "VALUE"], "filter" => ["@PROPERTY_ID" => array_keys($propertyCodes)]]);
|
||||
while ($row = $rows->fetch()) {
|
||||
$enums[(int)$row["ID"]] = ["XML_ID" => (string)$row["XML_ID"], "VALUE" => (string)$row["VALUE"]];
|
||||
}
|
||||
}
|
||||
$parts = [];
|
||||
if ($propertyCodes) {
|
||||
$rows = ElementPropertyTable::getList([
|
||||
"select" => ["IBLOCK_ELEMENT_ID", "IBLOCK_PROPERTY_ID", "VALUE_ENUM"],
|
||||
"filter" => ["@IBLOCK_PROPERTY_ID" => array_keys($propertyCodes), ">VALUE_ENUM" => 0],
|
||||
]);
|
||||
while ($row = $rows->fetch()) {
|
||||
$enum = $enums[(int)$row["VALUE_ENUM"]] ?? null;
|
||||
if ($enum !== null) {
|
||||
$code = $propertyCodes[(int)$row["IBLOCK_PROPERTY_ID"]];
|
||||
$parts[(int)$row["IBLOCK_ELEMENT_ID"]][$code] = EnumNames::name($code, $enum["XML_ID"], $enum["VALUE"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
$brands = self::brands($catalogId);
|
||||
$meta = [];
|
||||
$rows = ElementTable::getList(["select" => ["ID", "NAME"], "filter" => ["=IBLOCK_ID" => $catalogId]]);
|
||||
while ($row = $rows->fetch()) {
|
||||
$id = (int)$row["ID"];
|
||||
$meta[$id] = [
|
||||
"NAME" => (string)$row["NAME"],
|
||||
"META" => implode(", ", array_filter([$parts[$id]["REGION"] ?? "", $parts[$id]["COUNTRY"] ?? ""])),
|
||||
"BRAND" => $brands[$id] ?? "",
|
||||
];
|
||||
}
|
||||
|
||||
return $meta;
|
||||
}
|
||||
|
||||
private static function brands(int $catalogId): array
|
||||
{
|
||||
$property = PropertyTable::getRow(["select" => ["ID"], "filter" => ["=IBLOCK_ID" => $catalogId, "=CODE" => "BRAND"]]);
|
||||
if ($property === null) {
|
||||
return [];
|
||||
}
|
||||
$links = [];
|
||||
$rows = ElementPropertyTable::getList([
|
||||
"select" => ["IBLOCK_ELEMENT_ID", "VALUE"],
|
||||
"filter" => ["=IBLOCK_PROPERTY_ID" => (int)$property["ID"]],
|
||||
]);
|
||||
while ($row = $rows->fetch()) {
|
||||
if ((int)$row["VALUE"] > 0) {
|
||||
$links[(int)$row["IBLOCK_ELEMENT_ID"]] = (int)$row["VALUE"];
|
||||
}
|
||||
}
|
||||
if (!$links) {
|
||||
return [];
|
||||
}
|
||||
$names = [];
|
||||
$rows = ElementTable::getList(["select" => ["ID", "NAME"], "filter" => ["=ID" => array_values(array_unique($links))]]);
|
||||
while ($row = $rows->fetch()) {
|
||||
$names[(int)$row["ID"]] = (string)$row["NAME"];
|
||||
}
|
||||
|
||||
return array_filter(array_map(static fn(int $brandId): string => $names[$brandId] ?? "", $links));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
class WineSchema
|
||||
{
|
||||
private const CONTEXT = "https://schema.org";
|
||||
|
||||
public static function card(array $in): array
|
||||
{
|
||||
$url = (string)$in["url"];
|
||||
$store = ["@id" => (string)$in["host"] . "/#store"];
|
||||
$images = [];
|
||||
foreach ((array)$in["images"] as $index => $image) {
|
||||
$node = [
|
||||
"@type" => "ImageObject",
|
||||
"@id" => $url . "#image-" . ($index + 1),
|
||||
"url" => $image["src"],
|
||||
"contentUrl" => $image["src"],
|
||||
"caption" => $image["alt"],
|
||||
"inLanguage" => $in["language"],
|
||||
];
|
||||
if ($image["width"] > 0 && $image["height"] > 0) {
|
||||
$node["width"] = ["@type" => "QuantitativeValue", "value" => $image["width"], "unitCode" => "E37"];
|
||||
$node["height"] = ["@type" => "QuantitativeValue", "value" => $image["height"], "unitCode" => "E37"];
|
||||
}
|
||||
$images[] = $node;
|
||||
}
|
||||
|
||||
$product = [
|
||||
"@type" => "Product",
|
||||
"@id" => $url . "#product",
|
||||
"name" => $in["name"],
|
||||
"url" => $url,
|
||||
];
|
||||
if ($in["description"] !== "") {
|
||||
$product["description"] = $in["description"];
|
||||
}
|
||||
if ($images) {
|
||||
$product["image"] = $images;
|
||||
}
|
||||
if ($in["sku"] !== "") {
|
||||
$product["sku"] = $in["sku"];
|
||||
}
|
||||
if ($in["category"] !== "") {
|
||||
$product["category"] = $in["category"];
|
||||
}
|
||||
if ($in["country"] !== "") {
|
||||
$product["countryOfOrigin"] = ["@type" => "Country", "name" => $in["country"]];
|
||||
}
|
||||
if ($in["vintage"] !== "") {
|
||||
$product["productionDate"] = $in["vintage"];
|
||||
}
|
||||
if (!empty($in["brand"]["name"])) {
|
||||
$brand = ["@type" => "Brand", "name" => $in["brand"]["name"]];
|
||||
if ($in["brand"]["url"] !== "") {
|
||||
$brand["@id"] = $in["brand"]["url"] . "#brand";
|
||||
$brand["url"] = $in["brand"]["url"];
|
||||
}
|
||||
$product["brand"] = $brand;
|
||||
}
|
||||
$properties = [];
|
||||
foreach ((array)$in["properties"] as $row) {
|
||||
$node = ["@type" => "PropertyValue", "name" => $row["name"], "value" => $row["value"]];
|
||||
if (!empty($row["unitCode"])) {
|
||||
$node["unitCode"] = $row["unitCode"];
|
||||
}
|
||||
$properties[] = $node;
|
||||
}
|
||||
if ($properties) {
|
||||
$product["additionalProperty"] = $properties;
|
||||
}
|
||||
if ($in["price"] > 0 && $in["currency"] !== "") {
|
||||
$offer = [
|
||||
"@type" => "Offer",
|
||||
"@id" => $url . "#offer",
|
||||
"url" => $url,
|
||||
"price" => $in["price"],
|
||||
"priceCurrency" => $in["currency"],
|
||||
"availability" => $in["canBuy"] ? "https://schema.org/InStock" : "https://schema.org/OutOfStock",
|
||||
"itemCondition" => "https://schema.org/NewCondition",
|
||||
"seller" => $store,
|
||||
];
|
||||
if ($in["listPrice"] > $in["price"]) {
|
||||
$offer["priceSpecification"] = [
|
||||
"@type" => "UnitPriceSpecification",
|
||||
"priceType" => "https://schema.org/ListPrice",
|
||||
"price" => $in["listPrice"],
|
||||
"priceCurrency" => $in["currency"],
|
||||
];
|
||||
}
|
||||
$product["offers"] = $offer;
|
||||
}
|
||||
if ($in["reviewCount"] > 0 && $in["reviewRating"] > 0) {
|
||||
$product["aggregateRating"] = [
|
||||
"@type" => "AggregateRating",
|
||||
"ratingValue" => $in["reviewRating"],
|
||||
"reviewCount" => $in["reviewCount"],
|
||||
"bestRating" => 5,
|
||||
"worstRating" => 1,
|
||||
];
|
||||
}
|
||||
$reviews = [];
|
||||
foreach ((array)$in["reviews"] as $review) {
|
||||
$reviews[] = [
|
||||
"@type" => "Review",
|
||||
"author" => ["@type" => "Person", "name" => $review["author"]],
|
||||
"datePublished" => $review["date"],
|
||||
"reviewRating" => ["@type" => "Rating", "ratingValue" => $review["rating"], "bestRating" => 5, "worstRating" => 1],
|
||||
"reviewBody" => $review["text"],
|
||||
"inLanguage" => $review["language"],
|
||||
];
|
||||
}
|
||||
if ($reviews) {
|
||||
$product["review"] = $reviews;
|
||||
}
|
||||
$related = [];
|
||||
foreach ((array)$in["related"] as $item) {
|
||||
$related[] = ["@type" => "Product", "@id" => $item["url"] . "#product", "name" => $item["name"], "url" => $item["url"]];
|
||||
}
|
||||
if ($related) {
|
||||
$product["isRelatedTo"] = $related;
|
||||
}
|
||||
|
||||
$page = [
|
||||
"@type" => "ItemPage",
|
||||
"@id" => $url . "#webpage",
|
||||
"url" => $url,
|
||||
"name" => $in["name"],
|
||||
"description" => $in["pageDescription"],
|
||||
"inLanguage" => $in["language"],
|
||||
"mainEntity" => ["@id" => $url . "#product"],
|
||||
"datePublished" => $in["created"],
|
||||
"dateModified" => $in["modified"],
|
||||
];
|
||||
if ($images) {
|
||||
$page["primaryImageOfPage"] = ["@id" => $images[0]["@id"]];
|
||||
}
|
||||
$translations = [];
|
||||
foreach ((array)$in["translations"] as $language => $translationUrl) {
|
||||
$translations[] = ["@type" => "ItemPage", "@id" => $translationUrl . "#webpage", "url" => $translationUrl, "inLanguage" => $language];
|
||||
}
|
||||
if ($translations) {
|
||||
$page["workTranslation"] = $translations;
|
||||
}
|
||||
|
||||
return ["@context" => self::CONTEXT, "@graph" => [$page, $product]];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Bitrix\Iblock\ElementTable;
|
||||
use Bitrix\Iblock\IblockTable;
|
||||
use Bitrix\Main\Application;
|
||||
use Bitrix\Main\Data\Cache;
|
||||
use Bitrix\Main\Loader;
|
||||
use Bitrix\Main\SystemException;
|
||||
|
||||
class WineStats
|
||||
{
|
||||
private const CACHE_TTL = 86400;
|
||||
private const CACHE_DIR = "/jinove.vinoteca/winestats";
|
||||
|
||||
private ?int $activeCount = null;
|
||||
|
||||
public function __construct(
|
||||
private readonly string $iblockType,
|
||||
private readonly string $iblockCode,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getActiveCount(): int
|
||||
{
|
||||
if ($this->activeCount !== null) {
|
||||
return $this->activeCount;
|
||||
}
|
||||
|
||||
$cache = Cache::createInstance();
|
||||
$cacheId = "active-count-" . $this->iblockType . "-" . $this->iblockCode;
|
||||
if ($cache->initCache(self::CACHE_TTL, $cacheId, self::CACHE_DIR)) {
|
||||
return $this->activeCount = (int)$cache->getVars()["count"];
|
||||
}
|
||||
|
||||
$cache->startDataCache();
|
||||
$taggedCache = Application::getInstance()->getTaggedCache();
|
||||
$taggedCache->startTagCache(self::CACHE_DIR);
|
||||
try {
|
||||
$iblockId = $this->getIblockId();
|
||||
$count = ElementTable::getCount([
|
||||
"=IBLOCK_ID" => $iblockId,
|
||||
"=ACTIVE" => "Y",
|
||||
]);
|
||||
$taggedCache->registerTag("iblock_id_" . $iblockId);
|
||||
} catch (\Throwable $e) {
|
||||
$taggedCache->abortTagCache();
|
||||
$cache->abortDataCache();
|
||||
throw $e;
|
||||
}
|
||||
$taggedCache->endTagCache();
|
||||
$cache->endDataCache(["count" => $count]);
|
||||
|
||||
return $this->activeCount = $count;
|
||||
}
|
||||
|
||||
private function getIblockId(): int
|
||||
{
|
||||
if (!Loader::includeModule("iblock")) {
|
||||
throw new SystemException("Module iblock is not installed");
|
||||
}
|
||||
$iblock = IblockTable::getRow([
|
||||
"select" => ["ID"],
|
||||
"filter" => ["=IBLOCK_TYPE_ID" => $this->iblockType, "=CODE" => $this->iblockCode],
|
||||
]);
|
||||
if ($iblock === null) {
|
||||
throw new SystemException("Iblock " . $this->iblockType . "/" . $this->iblockCode . " not found");
|
||||
}
|
||||
|
||||
return (int)$iblock["ID"];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user