2024-09-23 15:33:48 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace kernel\modules\option\controllers;
|
|
|
|
|
|
|
|
use JetBrains\PhpStorm\NoReturn;
|
|
|
|
use kernel\AdminController;
|
|
|
|
use kernel\helpers\Debug;
|
|
|
|
use kernel\modules\option\models\forms\CreateOptionForm;
|
|
|
|
use kernel\modules\option\models\Option;
|
|
|
|
use kernel\modules\option\service\OptionService;
|
|
|
|
|
|
|
|
class OptionController extends AdminController
|
|
|
|
{
|
|
|
|
|
|
|
|
private OptionService $optionService;
|
|
|
|
|
|
|
|
public function init(): void
|
|
|
|
{
|
|
|
|
parent::init();
|
|
|
|
$this->cgView->viewPath = KERNEL_MODULES_DIR . '/option/views/';
|
|
|
|
$this->optionService = new OptionService();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionCreate(): void
|
|
|
|
{
|
|
|
|
$this->cgView->render('form.php');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[NoReturn] public function actionAdd(): void
|
|
|
|
{
|
|
|
|
$optionForm = new CreateOptionForm();
|
|
|
|
$optionForm->load($_REQUEST);
|
|
|
|
if ($optionForm->validate()) {
|
|
|
|
$option = $this->optionService->create($optionForm);
|
|
|
|
if ($option) {
|
|
|
|
$this->redirect('/admin/option');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->redirect('/admin/option/create');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionIndex($page_number = 1): void
|
|
|
|
{
|
|
|
|
$this->cgView->render('index.php', ['page_number' => $page_number]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function actionView(int $id): void
|
|
|
|
{
|
|
|
|
$option = Option::find($id);
|
|
|
|
|
|
|
|
if (!$option) {
|
|
|
|
throw new \Exception('Option not found');
|
|
|
|
}
|
|
|
|
$this->cgView->render("view.php", ['option' => $option]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function actionUpdate(int $id): void
|
|
|
|
{
|
|
|
|
$model = Option::find($id);
|
|
|
|
|
|
|
|
if (!$model) {
|
|
|
|
throw new \Exception('Option not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cgView->render("form.php", ['model' => $model]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function actionEdit(int $id): void
|
|
|
|
{
|
|
|
|
$option = Option::find($id);
|
|
|
|
if (!$option) {
|
|
|
|
throw new \Exception('Option not found');
|
|
|
|
}
|
|
|
|
$optionForm = new CreateOptionForm();
|
|
|
|
$optionForm->load($_REQUEST);
|
|
|
|
if ($optionForm->validate()) {
|
2024-09-23 17:03:42 +03:00
|
|
|
$option = $this->optionService->update($optionForm, $option);
|
2024-09-23 15:33:48 +03:00
|
|
|
if ($option) {
|
2024-09-23 17:03:42 +03:00
|
|
|
$this->redirect('/admin/option/' . $option->id);
|
2024-09-23 15:33:48 +03:00
|
|
|
}
|
|
|
|
}
|
2024-09-23 17:03:42 +03:00
|
|
|
|
|
|
|
$this->redirect('/admin/option/update/' . $id);
|
2024-09-23 15:33:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[NoReturn] public function actionDelete(int $id): void
|
|
|
|
{
|
|
|
|
Option::find($id)->delete();
|
|
|
|
$this->redirect('/admin/option');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|