119 lines
3.8 KiB
PHP
119 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Jinove\Vinoteca\Controller;
|
|
|
|
use Bitrix\Iblock\ElementTable;
|
|
use Bitrix\Iblock\SectionElementTable;
|
|
use Bitrix\Main\Engine\ActionFilter\Csrf;
|
|
use Bitrix\Main\Engine\ActionFilter\HttpMethod;
|
|
use Bitrix\Main\Engine\Controller;
|
|
use Bitrix\Main\Error;
|
|
use Bitrix\Main\Loader;
|
|
use Jinove\Vinoteca\Catalog\Comparison;
|
|
use Jinove\Vinoteca\Config;
|
|
|
|
class Compare extends Controller
|
|
{
|
|
public function configureActions(): array
|
|
{
|
|
return [
|
|
"state" => [
|
|
"prefilters" => [new HttpMethod([HttpMethod::METHOD_GET])],
|
|
],
|
|
"set" => [
|
|
"prefilters" => [new HttpMethod([HttpMethod::METHOD_POST]), new Csrf()],
|
|
],
|
|
"clear" => [
|
|
"prefilters" => [new HttpMethod([HttpMethod::METHOD_POST]), new Csrf()],
|
|
],
|
|
];
|
|
}
|
|
|
|
public function stateAction(): array
|
|
{
|
|
return $this->describe(Comparison::prune(), false);
|
|
}
|
|
|
|
public function clearAction(): array
|
|
{
|
|
Comparison::write([]);
|
|
|
|
return $this->describe([], false);
|
|
}
|
|
|
|
public function setAction(int $id, string $on): ?array
|
|
{
|
|
$items = Comparison::read();
|
|
if ($on !== "Y") {
|
|
unset($items[$id]);
|
|
Comparison::write($items);
|
|
|
|
return $this->describe($items, false);
|
|
}
|
|
if (isset($items[$id])) {
|
|
return $this->describe($items, false);
|
|
}
|
|
if (count($items) >= Comparison::MAX) {
|
|
return $this->describe($items, true);
|
|
}
|
|
|
|
$item = $this->loadItem($id);
|
|
if ($item === null) {
|
|
$this->addError(new Error("Wrong product id"));
|
|
|
|
return null;
|
|
}
|
|
$items[$id] = $item;
|
|
Comparison::write($items);
|
|
|
|
return $this->describe($items, false);
|
|
}
|
|
|
|
private function describe(array $items, bool $full): array
|
|
{
|
|
return ["items" => array_map("intval", array_keys($items)), "full" => $full];
|
|
}
|
|
|
|
private function loadItem(int $id): ?array
|
|
{
|
|
if ($id <= 0 || !Loader::includeModule("iblock")) {
|
|
return null;
|
|
}
|
|
$element = ElementTable::getRow([
|
|
"select" => ["ID", "IBLOCK_ID", "IBLOCK_SECTION_ID", "NAME", "CODE", "SECTION_CODE" => "IBLOCK_SECTION.CODE", "DETAIL_PAGE_URL" => "IBLOCK.DETAIL_PAGE_URL"],
|
|
"filter" => ["=ID" => $id, "=IBLOCK_ID" => Config::iblockId("catalog"), "=ACTIVE" => "Y"],
|
|
]);
|
|
if ($element === null) {
|
|
return null;
|
|
}
|
|
$url = \CIBlock::ReplaceDetailUrl((string)$element["DETAIL_PAGE_URL"], [
|
|
"ID" => $element["ID"],
|
|
"CODE" => $element["CODE"],
|
|
"IBLOCK_ID" => $element["IBLOCK_ID"],
|
|
"IBLOCK_SECTION_ID" => $element["IBLOCK_SECTION_ID"],
|
|
"SECTION_CODE" => $element["SECTION_CODE"],
|
|
], false, "E");
|
|
$sections = array_map("intval", array_column(SectionElementTable::getList([
|
|
"select" => ["IBLOCK_SECTION_ID"],
|
|
"filter" => ["=IBLOCK_ELEMENT_ID" => $id, "=ADDITIONAL_PROPERTY_ID" => null],
|
|
])->fetchAll(), "IBLOCK_SECTION_ID"));
|
|
|
|
return [
|
|
"ID" => (int)$element["ID"],
|
|
"~ID" => (int)$element["ID"],
|
|
"IBLOCK_ID" => (int)$element["IBLOCK_ID"],
|
|
"~IBLOCK_ID" => (int)$element["IBLOCK_ID"],
|
|
"IBLOCK_SECTION_ID" => (int)$element["IBLOCK_SECTION_ID"],
|
|
"~IBLOCK_SECTION_ID" => (int)$element["IBLOCK_SECTION_ID"],
|
|
"NAME" => htmlspecialcharsbx($element["NAME"]),
|
|
"~NAME" => $element["NAME"],
|
|
"DETAIL_PAGE_URL" => htmlspecialcharsbx($url),
|
|
"~DETAIL_PAGE_URL" => $url,
|
|
"SECTIONS_LIST" => array_combine($sections, $sections),
|
|
"PARENT_ID" => (int)$element["ID"],
|
|
];
|
|
}
|
|
}
|