Vinoteca: wine shop on 1C-Bitrix (module, template, components, pages)
This commit is contained in:
commit
0590195880
593 files changed
+27942
No files matched your search
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Controller;
|
||||
|
||||
use Bitrix\Iblock\ElementTable;
|
||||
use Bitrix\Main\Engine\ActionFilter\Csrf;
|
||||
use Bitrix\Main\Engine\ActionFilter\HttpMethod;
|
||||
use Bitrix\Main\Engine\Controller;
|
||||
use Bitrix\Main\Error;
|
||||
use Bitrix\Main\DB\SqlQueryException;
|
||||
use Bitrix\Main\Loader;
|
||||
use Bitrix\Sale\Fuser;
|
||||
use Jinove\Vinoteca\Catalog\FavoriteTable;
|
||||
|
||||
class Favorite extends Controller
|
||||
{
|
||||
public function configureActions(): array
|
||||
{
|
||||
return [
|
||||
"state" => [
|
||||
"prefilters" => [new HttpMethod([HttpMethod::METHOD_GET])],
|
||||
],
|
||||
"set" => [
|
||||
"prefilters" => [new HttpMethod([HttpMethod::METHOD_POST]), new Csrf()],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function stateAction(): ?array
|
||||
{
|
||||
if (!$this->loadModules()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ["items" => FavoriteTable::productIds((int)Fuser::getId(true))];
|
||||
}
|
||||
|
||||
public function setAction(int $id, string $on): ?array
|
||||
{
|
||||
if (!$this->loadModules()) {
|
||||
return null;
|
||||
}
|
||||
if ($on === "Y" && !$this->isProduct($id)) {
|
||||
$this->addError(new Error("Wrong product id"));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$fuserId = (int)Fuser::getId();
|
||||
$row = FavoriteTable::getRow([
|
||||
"select" => ["ID"],
|
||||
"filter" => ["=FUSER_ID" => $fuserId, "=PRODUCT_ID" => $id],
|
||||
]);
|
||||
$result = null;
|
||||
if ($on === "Y" && !$row) {
|
||||
try {
|
||||
$result = FavoriteTable::add(["FUSER_ID" => $fuserId, "PRODUCT_ID" => $id]);
|
||||
} catch (SqlQueryException $exception) {
|
||||
$result = null;
|
||||
}
|
||||
} elseif ($on !== "Y" && $row) {
|
||||
$result = FavoriteTable::delete($row["ID"]);
|
||||
}
|
||||
if ($result !== null && !$result->isSuccess()) {
|
||||
$this->addErrors($result->getErrors());
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return ["items" => FavoriteTable::productIds($fuserId)];
|
||||
}
|
||||
|
||||
private function loadModules(): bool
|
||||
{
|
||||
if (!Loader::includeModule("sale") || !Loader::includeModule("iblock")) {
|
||||
$this->addError(new Error("Modules sale and iblock are required"));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function isProduct(int $id): bool
|
||||
{
|
||||
return $id > 0 && ElementTable::getCount([
|
||||
"=ID" => $id,
|
||||
"=IBLOCK_ID" => VINO_CATALOG_IBLOCK,
|
||||
"=ACTIVE" => "Y",
|
||||
]) > 0;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user