[ "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; } }