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

This commit is contained in:
jimagie committed 2026-10-22 19:35:23 +05:00
commit 0590195880
593 files changed
+27942

No files matched your search

@@ -0,0 +1,130 @@
<?php
declare(strict_types=1);
namespace Jinove\Vinoteca\Controller;
use Bitrix\Iblock\ElementTable;
use Bitrix\Main\Application;
use Bitrix\Main\Engine\ActionFilter\Authentication;
use Bitrix\Main\Engine\ActionFilter\Csrf;
use Bitrix\Main\Engine\ActionFilter\HttpMethod;
use Bitrix\Main\Engine\Controller;
use Bitrix\Main\Engine\CurrentUser;
use Bitrix\Main\Error;
use Bitrix\Main\Loader;
use Jinove\Vinoteca\Catalog\ReviewStats;
use Jinove\Vinoteca\Config;
use Jinove\Vinoteca\Consent;
use Jinove\Vinoteca\Controller\Filter\FormGuard;
class Review extends Controller
{
private const SITE_URL = "https://vinoteca.jimagie.com";
private const MIN_FILL_MS = 2500;
private const RETRY_SECONDS = 60;
private const LANGS = ["en" => "", "ru" => "/ru", "pl" => "/pl"];
public function configureActions(): array
{
return [
"add" => [
"prefilters" => [new Authentication(), new HttpMethod([HttpMethod::METHOD_POST]), new Csrf(), new FormGuard(FormGuard::TIMING_STARTED, self::MIN_FILL_MS)],
],
];
}
public function addAction(
int $productId,
int $rating,
string $text,
string $consent = "",
string $website = "",
int $started = 0,
string $altcha = "",
string $lang = "en",
string $page = ""
): ?array {
if ($website !== "" || FormGuard::startedTooEarly($started, self::MIN_FILL_MS)) {
return ["ok" => true];
}
$user = CurrentUser::get();
$author = trim((string)$user->getFullName());
if ($author === "") {
$author = trim((string)$user->getLogin());
}
$author = mb_substr($author, 0, 60);
if (!mb_check_encoding($author, "UTF-8") || !mb_check_encoding($text, "UTF-8")) {
$this->addError(new Error("Wrong encoding", "text"));
return null;
}
$author = trim((string)preg_replace('/\s+/u', " ", $author));
$text = trim($text);
$lang = isset(self::LANGS[$lang]) ? $lang : "en";
if ($rating < 1 || $rating > 5) {
$this->addError(new Error("Rating is required", "rating"));
}
if (mb_strlen($text) < 10 || mb_strlen($text) > 2000) {
$this->addError(new Error("Text is required", "text"));
}
if ($consent !== "1") {
$this->addError(new Error("Consent is required", "consent"));
}
if ($this->getErrors()) {
return null;
}
$session = Application::getInstance()->getSession();
if (time() - (int)($session->get("vino_review_last") ?? 0) < self::RETRY_SECONDS) {
$this->addError(new Error("Too often", "too_often"));
return null;
}
if (!Loader::includeModule("iblock")) {
$this->addError(new Error("Iblock module is required", "internal"));
return null;
}
$product = ElementTable::getRow([
"select" => ["ID", "NAME"],
"filter" => ["=ID" => $productId, "=IBLOCK_ID" => Config::iblockId("catalog"), "=ACTIVE" => "Y"],
]);
if ($product === null) {
$this->addError(new Error("Wine not found", "not_found"));
return null;
}
$pagePath = (string)parse_url($page, PHP_URL_PATH);
$element = new \CIBlockElement();
$id = $element->Add([
"IBLOCK_ID" => ReviewStats::iblockId(ReviewStats::REVIEWS_CODE),
"ACTIVE" => "N",
"NAME" => mb_substr($author . ": " . $product["NAME"], 0, 255),
"ACTIVE_FROM" => ConvertTimeStamp(time(), "FULL"),
"PREVIEW_TEXT" => $text,
"PREVIEW_TEXT_TYPE" => "text",
"PROPERTY_VALUES" => [
"PRODUCT" => (int)$product["ID"],
"RATING" => $rating,
"AUTHOR" => $author,
"USER_ID" => (int)$user->getId(),
"LANG" => $lang,
"PAGE" => self::SITE_URL . ($pagePath !== "" ? $pagePath : "/"),
"CONSENT" => "Y",
"CONSENT_VERSION" => Consent::VERSION,
"CONSENT_URL" => self::SITE_URL . self::LANGS[$lang] . "/consent/",
],
]);
if (!$id) {
$this->addError(new Error("Review was not saved", "internal"));
return null;
}
$session->set("vino_review_last", time());
return ["ok" => true];
}
}