Files

95 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Jinove\Vinoteca\Controller;
use Bitrix\Main\Engine\ActionFilter\HttpMethod;
use Bitrix\Main\Engine\Controller;
use Bitrix\Main\Engine\Response\Component;
use Bitrix\Main\Error;
use Jinove\Vinoteca\Catalog\Listing;
class Catalog extends Controller
{
public function configureActions(): array
{
return [
"listing" => [
"prefilters" => [new HttpMethod([HttpMethod::METHOD_GET])],
],
"filter" => [
"prefilters" => [new HttpMethod([HttpMethod::METHOD_GET])],
],
];
}
public function listingAction(string $path): ?Component
{
$route = Listing::parsePath($path);
if ($route === null) {
$this->addError(new Error("Wrong path"));
return null;
}
if (!Listing::filterPathIsValid($route["SECTION_CODE"], $route["FILTER_PATH"])) {
$this->addError(new Error("Wrong filter"));
return null;
}
return new Component(
"jinove:catalog.listing",
"",
[
"SECTION_CODE" => $route["SECTION_CODE"],
"FILTER_PATH" => $route["FILTER_PATH"],
"AJAX" => "Y",
],
[],
static function (): array {
global $APPLICATION;
return ["title" => (string)($APPLICATION->GetPageProperty("title") ?: $APPLICATION->GetTitle())];
}
);
}
public function filterAction(string $section): ?array
{
$route = Listing::parsePath(Listing::sectionPath($section));
if ($route === null) {
$this->addError(new Error("Wrong section"));
return null;
}
global $APPLICATION;
ob_start();
$result = $APPLICATION->IncludeComponent(
"bitrix:catalog.smart.filter",
"",
Listing::filterParams($route["SECTION_CODE"], ""),
null,
[],
true
);
ob_end_clean();
$disabled = [];
foreach ((array)($result["ITEMS"] ?? []) as $item) {
if (!empty($item["PRICE"])) {
continue;
}
foreach ((array)($item["VALUES"] ?? []) as $value) {
$disabled[$value["CONTROL_NAME"]] = !empty($value["DISABLED"]);
}
}
return [
"url" => (string)($result["JS_FILTER_PARAMS"]["SEF_SET_FILTER_URL"] ?? Listing::sectionPath($route["SECTION_CODE"])),
"disabled" => $disabled,
];
}
}