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,339 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca;
|
||||
|
||||
use Bitrix\Iblock\Iblock;
|
||||
use Bitrix\Main\Application;
|
||||
use Bitrix\Main\Data\Cache;
|
||||
use Bitrix\Main\Loader;
|
||||
use Bitrix\Main\Localization\Loc;
|
||||
use Bitrix\Main\Type\DateTime;
|
||||
use Bitrix\Sale\Internals\DiscountCouponTable;
|
||||
use Bitrix\Sale\Internals\DiscountGroupTable;
|
||||
use Bitrix\Sale\Internals\DiscountTable;
|
||||
use Jinove\Vinoteca\Catalog\Listing;
|
||||
|
||||
Loc::loadMessages(__FILE__);
|
||||
|
||||
final class Offers
|
||||
{
|
||||
public const FILTER = "vinoOffersFilter";
|
||||
public const CACHE_TAG = "jinove_offers_discounts";
|
||||
private const CACHE_DIR = "/jinove/vinoteca/offers";
|
||||
|
||||
public static function folder(): string
|
||||
{
|
||||
return SITE_DIR . "offers/";
|
||||
}
|
||||
|
||||
public static function iblockId(): int
|
||||
{
|
||||
return Config::iblockId("offers");
|
||||
}
|
||||
|
||||
public static function discounts(): array
|
||||
{
|
||||
if (!Loader::includeModule("sale")) {
|
||||
return [];
|
||||
}
|
||||
$cache = Cache::createInstance();
|
||||
$cacheId = "discounts_" . Config::basketSiteId();
|
||||
if ($cache->initCache(3600, $cacheId, self::CACHE_DIR)) {
|
||||
$vars = (array)$cache->getVars();
|
||||
if ((int)($vars["expires"] ?? 0) === 0 || (int)$vars["expires"] > time()) {
|
||||
return (array)($vars["rules"] ?? []);
|
||||
}
|
||||
$cache->clean($cacheId, self::CACHE_DIR);
|
||||
}
|
||||
self::clearCache();
|
||||
$cache->startDataCache();
|
||||
$taggedCache = Application::getInstance()->getTaggedCache();
|
||||
$taggedCache->startTagCache(self::CACHE_DIR);
|
||||
try {
|
||||
$taggedCache->registerTag(self::CACHE_TAG);
|
||||
$vars = self::loadDiscounts();
|
||||
} catch (\Throwable $exception) {
|
||||
$taggedCache->abortTagCache();
|
||||
$cache->abortDataCache();
|
||||
throw $exception;
|
||||
}
|
||||
$taggedCache->endTagCache();
|
||||
$cache->endDataCache($vars);
|
||||
|
||||
return $vars["rules"];
|
||||
}
|
||||
|
||||
private static function loadDiscounts(): array
|
||||
{
|
||||
$time = time();
|
||||
$expires = 0;
|
||||
$boundary = static function ($date) use ($time, &$expires): void {
|
||||
if ($date instanceof DateTime && $date->getTimestamp() > $time) {
|
||||
$expires = $expires === 0 ? $date->getTimestamp() : min($expires, $date->getTimestamp());
|
||||
}
|
||||
};
|
||||
$current = static function ($from, $to) use ($time): bool {
|
||||
return !($from instanceof DateTime && $from->getTimestamp() > $time) && !($to instanceof DateTime && $to->getTimestamp() < $time);
|
||||
};
|
||||
$public = [];
|
||||
$groups = DiscountGroupTable::getList([
|
||||
"select" => ["DISCOUNT_ID"],
|
||||
"filter" => ["=GROUP_ID" => 2, "=ACTIVE" => "Y"],
|
||||
]);
|
||||
while ($group = $groups->fetch()) {
|
||||
$public[(int)$group["DISCOUNT_ID"]] = true;
|
||||
}
|
||||
$rules = [];
|
||||
$rows = DiscountTable::getList([
|
||||
"select" => ["ID", "USE_COUPONS", "ACTIVE_FROM", "ACTIVE_TO", "CONDITIONS_LIST", "ACTIONS_LIST"],
|
||||
"filter" => ["=ACTIVE" => "Y", "=LID" => Config::basketSiteId()],
|
||||
]);
|
||||
while ($row = $rows->fetch()) {
|
||||
$boundary($row["ACTIVE_FROM"]);
|
||||
$boundary($row["ACTIVE_TO"]);
|
||||
if (!$current($row["ACTIVE_FROM"], $row["ACTIVE_TO"]) || !isset($public[(int)$row["ID"]]) || !empty($row["CONDITIONS_LIST"]["CHILDREN"])) {
|
||||
continue;
|
||||
}
|
||||
$rule = self::parseActions((array)$row["ACTIONS_LIST"]);
|
||||
if ($rule === null) {
|
||||
continue;
|
||||
}
|
||||
if ($row["USE_COUPONS"] === "Y") {
|
||||
$coupons = DiscountCouponTable::getList([
|
||||
"select" => ["COUPON", "ACTIVE_FROM", "ACTIVE_TO"],
|
||||
"filter" => [
|
||||
"=DISCOUNT_ID" => (int)$row["ID"],
|
||||
"=ACTIVE" => "Y",
|
||||
"=TYPE" => DiscountCouponTable::TYPE_MULTI_ORDER,
|
||||
"=MAX_USE" => 0,
|
||||
],
|
||||
]);
|
||||
while ($coupon = $coupons->fetch()) {
|
||||
$boundary($coupon["ACTIVE_FROM"]);
|
||||
$boundary($coupon["ACTIVE_TO"]);
|
||||
if ($current($coupon["ACTIVE_FROM"], $coupon["ACTIVE_TO"])) {
|
||||
$rule["coupons"][] = (string)$coupon["COUPON"];
|
||||
}
|
||||
}
|
||||
if (!$rule["coupons"]) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
$rules[(int)$row["ID"]] = $rule;
|
||||
}
|
||||
|
||||
return ["rules" => $rules, "expires" => $expires];
|
||||
}
|
||||
|
||||
private static function parseActions(array $actions): ?array
|
||||
{
|
||||
$rule = ["percent" => 0, "products" => [], "sections" => [], "coupons" => []];
|
||||
foreach ((array)($actions["CHILDREN"] ?? []) as $action) {
|
||||
$data = (array)($action["DATA"] ?? []);
|
||||
if (($action["CLASS_ID"] ?? "") !== "ActSaleBsktGrp" || ($data["Type"] ?? "") !== "Discount" || ($data["Unit"] ?? "") !== "Perc" || ($data["True"] ?? "True") !== "True") {
|
||||
return null;
|
||||
}
|
||||
$conditions = (array)($action["CHILDREN"] ?? []);
|
||||
if (!$conditions || (count($conditions) > 1 && ($data["All"] ?? "") !== "OR")) {
|
||||
return null;
|
||||
}
|
||||
foreach ($conditions as $condition) {
|
||||
$class = (string)($condition["CLASS_ID"] ?? "");
|
||||
if (!in_array($class, ["CondIBElement", "CondIBSection"], true) || ($condition["DATA"]["logic"] ?? "") !== "Equal" || !empty($condition["CHILDREN"])) {
|
||||
return null;
|
||||
}
|
||||
$key = $class === "CondIBElement" ? "products" : "sections";
|
||||
$rule[$key] = array_merge($rule[$key], array_map("intval", (array)($condition["DATA"]["value"] ?? [])));
|
||||
}
|
||||
$rule["percent"] = max($rule["percent"], (int)$data["Value"]);
|
||||
}
|
||||
|
||||
return $rule["percent"] > 0 && ($rule["products"] || $rule["sections"]) ? $rule : null;
|
||||
}
|
||||
|
||||
public static function summary(array $discountIds): array
|
||||
{
|
||||
$rules = array_intersect_key(self::discounts(), array_flip(array_map("intval", $discountIds)));
|
||||
$percents = array_column($rules, "percent");
|
||||
$coupons = array_merge([], ...array_column($rules, "coupons"));
|
||||
$products = array_values(array_unique(array_merge([], ...array_column($rules, "products"))));
|
||||
$sections = array_values(array_unique(array_merge([], ...array_column($rules, "sections"))));
|
||||
$badge = "";
|
||||
if ($percents) {
|
||||
$max = max($percents);
|
||||
$badge = (string)Loc::getMessage(count(array_unique($percents)) > 1 ? "VINO_OFFERS_BADGE_UPTO" : "VINO_OFFERS_BADGE", ["#PERCENT#" => $max]);
|
||||
}
|
||||
|
||||
return [
|
||||
"active" => (bool)$rules,
|
||||
"badge" => $badge,
|
||||
"coupons" => array_values(array_unique($coupons)),
|
||||
"products" => $products,
|
||||
"sections" => $sections,
|
||||
];
|
||||
}
|
||||
|
||||
public static function automatic(): array
|
||||
{
|
||||
$products = [];
|
||||
$sections = [];
|
||||
foreach (self::discounts() as $rule) {
|
||||
if (!$rule["coupons"]) {
|
||||
$products = array_merge($products, $rule["products"]);
|
||||
$sections = array_merge($sections, $rule["sections"]);
|
||||
}
|
||||
}
|
||||
|
||||
return ["products" => array_values(array_unique($products)), "sections" => array_values(array_unique($sections))];
|
||||
}
|
||||
|
||||
public static function wineParams(array $products, array $sections, string $baseLink, string $listId): array
|
||||
{
|
||||
$filter = [];
|
||||
if ($products && $sections) {
|
||||
$filter[] = ["LOGIC" => "OR", ["ID" => $products], ["SECTION_ID" => $sections, "INCLUDE_SUBSECTIONS" => "Y"]];
|
||||
} elseif ($products) {
|
||||
$filter["ID"] = $products;
|
||||
} elseif ($sections) {
|
||||
$filter["SECTION_ID"] = $sections;
|
||||
$filter["INCLUDE_SUBSECTIONS"] = "Y";
|
||||
} else {
|
||||
$filter["ID"] = [0];
|
||||
}
|
||||
$GLOBALS[self::FILTER] = $filter;
|
||||
$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" => "12",
|
||||
"LINE_ELEMENT_COUNT" => "3",
|
||||
"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_OFFERS_PAGER_TITLE"),
|
||||
"PAGER_BASE_LINK_ENABLE" => "Y",
|
||||
"PAGER_BASE_LINK" => $baseLink,
|
||||
"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" => "h3",
|
||||
"VINO_GRID_FULL" => "Y",
|
||||
"VINO_LIST_ID" => VINO_HOST . $listId . "#items",
|
||||
];
|
||||
}
|
||||
|
||||
public static function card(array $item): array
|
||||
{
|
||||
$item = News::card($item, [480, 800, 1200]);
|
||||
$ids = array_map("intval", (array)($item["PROPERTIES"]["DISCOUNT_ID"]["VALUE"] ?? []));
|
||||
$item["VINO_OFFER"] = self::summary($ids);
|
||||
Application::getInstance()->getTaggedCache()->registerTag(self::CACHE_TAG);
|
||||
$until = (string)($item["DATE_ACTIVE_TO"] ?? $item["ACTIVE_TO"] ?? "");
|
||||
$item["VINO_UNTIL"] = $until !== "" ? (string)Loc::getMessage("VINO_OFFERS_UNTIL", ["#DATE#" => DateFormat::long(DateTime::createFromUserTime($until)->getTimestamp(), LANGUAGE_ID)]) : (string)Loc::getMessage("VINO_OFFERS_ALWAYS");
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
public static function listParams(): array
|
||||
{
|
||||
return [
|
||||
"IBLOCK_TYPE" => "content",
|
||||
"IBLOCK_ID" => self::iblockId(),
|
||||
"NEWS_COUNT" => "20",
|
||||
"SORT_BY1" => "SORT",
|
||||
"SORT_ORDER1" => "ASC",
|
||||
"SORT_BY2" => "ACTIVE_FROM",
|
||||
"SORT_ORDER2" => "DESC",
|
||||
"FIELD_CODE" => ["PREVIEW_PICTURE", "PREVIEW_TEXT", "ACTIVE_FROM", "DATE_ACTIVE_TO"],
|
||||
"PROPERTY_CODE" => ["DISCOUNT_ID", "NAME_EN", "NAME_PL", "PREVIEW_TEXT_EN", "PREVIEW_TEXT_PL"],
|
||||
"DETAIL_URL" => self::folder() . "#ELEMENT_CODE#/",
|
||||
"CHECK_DATES" => "Y",
|
||||
"CACHE_TYPE" => "A",
|
||||
"CACHE_TIME" => "3600",
|
||||
"CACHE_GROUPS" => "N",
|
||||
"SET_TITLE" => "N",
|
||||
"SET_BROWSER_TITLE" => "N",
|
||||
"SET_META_KEYWORDS" => "N",
|
||||
"SET_META_DESCRIPTION" => "N",
|
||||
"SET_LAST_MODIFIED" => "N",
|
||||
"SET_STATUS_404" => "N",
|
||||
"SHOW_404" => "N",
|
||||
"INCLUDE_IBLOCK_INTO_CHAIN" => "N",
|
||||
"ADD_SECTIONS_CHAIN" => "N",
|
||||
"DISPLAY_TOP_PAGER" => "N",
|
||||
"DISPLAY_BOTTOM_PAGER" => "N",
|
||||
];
|
||||
}
|
||||
|
||||
public static function discountIdsOf(int $elementId): array
|
||||
{
|
||||
$iblockId = self::iblockId();
|
||||
if ($elementId <= 0 || $iblockId <= 0 || !Loader::includeModule("iblock")) {
|
||||
return [];
|
||||
}
|
||||
return (array)Cached::remember(self::CACHE_DIR, "discount_ids_" . $elementId, 86400, ["iblock_id_" . $iblockId], static function () use ($iblockId, $elementId): array {
|
||||
$ids = [];
|
||||
$element = Iblock::wakeUp($iblockId)->getEntityDataClass()::getByPrimary($elementId, ["select" => ["ID", "DISCOUNT_ID"]])->fetchObject();
|
||||
if ($element) {
|
||||
foreach ($element->getDiscountId()->getAll() as $value) {
|
||||
if ((int)$value->getValue() > 0) {
|
||||
$ids[] = (int)$value->getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $ids;
|
||||
});
|
||||
}
|
||||
|
||||
public static function refresh(): void
|
||||
{
|
||||
self::discounts();
|
||||
}
|
||||
|
||||
public static function canonical(string $path): void
|
||||
{
|
||||
$navNum = (int)($GLOBALS["NavNum"] ?? 0);
|
||||
$page = $navNum > 0 ? Application::getInstance()->getContext()->getRequest()->getQuery("PAGEN_" . $navNum) : null;
|
||||
if ($page !== null && (int)$page > 1) {
|
||||
$GLOBALS["APPLICATION"]->SetPageProperty("vino_canonical", $path . "?PAGEN_" . $navNum . "=" . (int)$page);
|
||||
}
|
||||
}
|
||||
|
||||
public static function clearCache(): void
|
||||
{
|
||||
Application::getInstance()->getTaggedCache()->clearByTag(self::CACHE_TAG);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user