163 lines
5.7 KiB
PHP
163 lines
5.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Jinove\Vinoteca;
|
|
|
|
use Bitrix\Iblock\ElementTable;
|
|
use Bitrix\Main\Localization\Loc;
|
|
use Bitrix\Main\Type\DateTime;
|
|
use Jinove\Vinoteca\Catalog\LocalizedText;
|
|
|
|
Loc::loadMessages(__FILE__);
|
|
|
|
final class News
|
|
{
|
|
public const PAGE_SIZE = 8;
|
|
public const LIST_TEMPLATE = "vinoteca-news-list";
|
|
public const FILTER_OTHER = "vinoNewsOther";
|
|
private const LIST_FIELDS = ["PREVIEW_PICTURE", "PREVIEW_TEXT", "ACTIVE_FROM"];
|
|
private const LIST_PROPERTIES = ["NAME_EN", "NAME_PL", "PREVIEW_TEXT_EN", "PREVIEW_TEXT_PL"];
|
|
|
|
public static function folder(): string
|
|
{
|
|
return SITE_DIR . "articles/";
|
|
}
|
|
|
|
public static function iblockId(): int
|
|
{
|
|
return Config::iblockId("news");
|
|
}
|
|
|
|
public static function listParams(bool $ajax = false): array
|
|
{
|
|
return array_merge(self::baseParams(self::PAGE_SIZE), [
|
|
"DISPLAY_BOTTOM_PAGER" => "Y",
|
|
"PAGER_TEMPLATE" => "vinoteca",
|
|
"PAGER_TITLE" => (string)Loc::getMessage("VINO_NEWS_PAGER_TITLE"),
|
|
"PAGER_SHOW_ALWAYS" => "N",
|
|
"PAGER_SHOW_ALL" => "N",
|
|
"PAGER_DESC_NUMBERING" => "N",
|
|
"PAGER_BASE_LINK_ENABLE" => "Y",
|
|
"PAGER_BASE_LINK" => self::folder(),
|
|
"VINO_AJAX" => $ajax ? "Y" : "N",
|
|
]);
|
|
}
|
|
|
|
public static function homeParams(): array
|
|
{
|
|
return self::baseParams(3);
|
|
}
|
|
|
|
public static function otherParams(int $excludeId): array
|
|
{
|
|
$GLOBALS[self::FILTER_OTHER] = ["!ID" => $excludeId];
|
|
|
|
return array_merge(self::baseParams(3), [
|
|
"FILTER_NAME" => self::FILTER_OTHER,
|
|
"CACHE_FILTER" => "Y",
|
|
]);
|
|
}
|
|
|
|
public static function pageCount(): int
|
|
{
|
|
$iblockId = self::iblockId();
|
|
if ($iblockId <= 0) {
|
|
return 1;
|
|
}
|
|
return (int)Cached::remember("/jinove/vinoteca/news", "pages_" . $iblockId, 3600, ["iblock_id_" . $iblockId], static function () use ($iblockId): int {
|
|
$now = new DateTime();
|
|
$count = ElementTable::getCount([
|
|
"=IBLOCK_ID" => $iblockId,
|
|
"=ACTIVE" => "Y",
|
|
["LOGIC" => "OR", ["=ACTIVE_FROM" => false], ["<=ACTIVE_FROM" => $now]],
|
|
["LOGIC" => "OR", ["=ACTIVE_TO" => false], [">=ACTIVE_TO" => $now]],
|
|
]);
|
|
|
|
return max(1, (int)ceil($count / self::PAGE_SIZE));
|
|
});
|
|
}
|
|
|
|
public static function pageMeta(int $page): array
|
|
{
|
|
if ($page > 1) {
|
|
return [
|
|
"title" => (string)Loc::getMessage("VINO_NEWS_PAGE_TITLE_N", ["#PAGE#" => $page]),
|
|
"description" => (string)Loc::getMessage("VINO_NEWS_DESCRIPTION_N", ["#PAGE#" => $page]),
|
|
"canonical" => self::folder() . "?PAGEN_1=" . $page,
|
|
];
|
|
}
|
|
|
|
return [
|
|
"title" => (string)Loc::getMessage("VINO_NEWS_PAGE_TITLE"),
|
|
"description" => (string)Loc::getMessage("VINO_NEWS_DESCRIPTION"),
|
|
"canonical" => self::folder(),
|
|
];
|
|
}
|
|
|
|
public static function card(array $item, array $widths = [480, 800]): array
|
|
{
|
|
$item = LocalizedText::localizeItem($item, ["NAME", "PREVIEW_TEXT", "DETAIL_TEXT"], []);
|
|
$picture = $item["DETAIL_PICTURE"] ?? null;
|
|
$picture = $item["PREVIEW_PICTURE"] ?: $picture;
|
|
$item["VINO_PICTURE"] = [];
|
|
if (!empty($picture["ID"])) {
|
|
$srcset = [];
|
|
foreach ($widths as $width) {
|
|
if ((int)$picture["WIDTH"] > $width) {
|
|
$resized = \CFile::ResizeImageGet((int)$picture["ID"], ["width" => $width, "height" => 9999], BX_RESIZE_IMAGE_PROPORTIONAL, false);
|
|
if (!empty($resized["src"])) {
|
|
$srcset[] = $resized["src"] . " " . $width . "w";
|
|
}
|
|
}
|
|
}
|
|
$srcset[] = $picture["SRC"] . " " . (int)$picture["WIDTH"] . "w";
|
|
$item["VINO_PICTURE"] = [
|
|
"ID" => (int)$picture["ID"],
|
|
"SRC" => (string)$picture["SRC"],
|
|
"SRCSET" => implode(", ", $srcset),
|
|
"WIDTH" => (int)$picture["WIDTH"],
|
|
"HEIGHT" => (int)$picture["HEIGHT"],
|
|
"ALT" => (string)$item["~NAME"],
|
|
];
|
|
}
|
|
$published = $item["ACTIVE_FROM"] ? DateTime::createFromUserTime((string)$item["ACTIVE_FROM"])->getTimestamp() : 0;
|
|
$item["VINO_TIMESTAMP"] = $published;
|
|
$item["VINO_DATE"] = $published ? ["ISO" => date("Y-m-d", $published), "TEXT" => DateFormat::long($published, LANGUAGE_ID)] : [];
|
|
|
|
return $item;
|
|
}
|
|
|
|
private static function baseParams(int $count): array
|
|
{
|
|
return [
|
|
"IBLOCK_TYPE" => "news",
|
|
"IBLOCK_ID" => self::iblockId(),
|
|
"NEWS_COUNT" => (string)$count,
|
|
"SORT_BY1" => "ACTIVE_FROM",
|
|
"SORT_ORDER1" => "DESC",
|
|
"SORT_BY2" => "ID",
|
|
"SORT_ORDER2" => "DESC",
|
|
"FIELD_CODE" => self::LIST_FIELDS,
|
|
"PROPERTY_CODE" => self::LIST_PROPERTIES,
|
|
"DETAIL_URL" => self::folder() . "#ELEMENT_CODE#/",
|
|
"CHECK_DATES" => "Y",
|
|
"CACHE_TYPE" => "A",
|
|
"CACHE_TIME" => "36000000",
|
|
"CACHE_GROUPS" => "N",
|
|
"CACHE_FILTER" => "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",
|
|
];
|
|
}
|
|
}
|