igfs/app/modules/module_shop/controllers/ModuleShopController.php
2024-10-16 16:06:25 +03:00

106 lines
2.9 KiB
PHP

<?php
namespace app\modules\module_shop\controllers;
use app\modules\module_shop\models\forms\CreateModuleShopForm;
use app\modules\module_shop\models\ModuleShop;
use app\modules\module_shop\services\ModuleShopService;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\app_modules\tag\services\TagService;
use kernel\services\ModuleService;
use mysql_xdevapi\Exception;
class ModuleShopController extends AdminController
{
protected ModuleShopService $moduleShopService;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = APP_DIR . "/modules/module_shop/views/";
$this->moduleShopService = new ModuleShopService();
}
public function actionCreate(): void
{
$this->cgView->render("form.php");
}
#[NoReturn] public function actionAdd(): void
{
$moduleShopForm = new CreateModuleShopForm();
$moduleShopForm->load($_REQUEST);
if ($moduleShopForm->validate()) {
$module = $this->moduleShopService->create($moduleShopForm);
if ($module) {
$this->redirect("/admin/module_shop/" . $module->id);
}
}
$this->redirect("/admin/module_shop/create");
}
public function actionIndex(int $page_number = 1): void
{
$this->cgView->render("index.php", ['page_number' => $page_number]);
}
public function actionView(int $id): void
{
$module = ModuleShop::find($id);
if (!$module) {
throw new Exception("The module not found");
}
$this->cgView->render("view.php", ['module' => $module]);
}
public function actionUpdate(int $id): void
{
$model = ModuleShop::find($id);
if (!$model) {
throw new Exception("The module not found");
}
$this->cgView->render("form.php", ['model' => $model]);
}
public function actionEdit(int $id): void
{
$module = ModuleShop::find($id);
if (!$module) {
throw new Exception("The module not found");
}
$moduleForm = new CreateModuleShopForm();
$moduleForm->load($_REQUEST);
if ($moduleForm->validate()) {
$module = $this->moduleShopService->update($moduleForm, $module);
if ($module) {
$this->redirect("/admin/module_shop/" . $module->id);
}
}
$this->redirect("/admin/module_shop/update/" . $id);
}
public function actionDelete(int $id): void
{
$module = ModuleShop::find($id);
if (!$module) {
throw new Exception("The module not found");
}
$module->delete();
$this->redirect("/admin/module_shop");
}
public function actionPack(int $id): void
{
$module = ModuleShop::find($id);
if (!$module) {
throw new Exception("The module not found");
}
$this->moduleShopService->packModule($module);
}
}