MicroFrameWork/kernel/modules/menu/controllers/MenuController.php

126 lines
3.5 KiB
PHP
Raw Normal View History

2024-09-03 16:29:44 +03:00
<?php
namespace kernel\modules\menu\controllers;
use app\helpers\Debug;
use Exception;
use JetBrains\PhpStorm\NoReturn;
2024-09-06 16:53:20 +03:00
use kernel\AdminController;
2024-09-10 15:45:12 +03:00
use kernel\FileUpload;
2024-09-03 16:29:44 +03:00
use kernel\models\Menu;
2024-09-10 15:45:12 +03:00
use kernel\modules\menu\models\forms\CreateMenuForm;
use kernel\modules\menu\service\MenuService;
2024-09-03 16:29:44 +03:00
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
2024-09-06 16:53:20 +03:00
class MenuController extends AdminController
2024-09-03 16:29:44 +03:00
{
2024-09-06 16:53:20 +03:00
private MenuService $menuService;
2024-09-03 16:29:44 +03:00
protected function init(): void
{
2024-09-06 16:53:20 +03:00
parent::init();
2024-09-03 16:29:44 +03:00
$this->cgView->viewPath = KERNEL_MODULES_DIR . "/menu/views/";
2024-09-06 16:53:20 +03:00
$this->menuService = new MenuService();
2024-09-03 16:29:44 +03:00
}
public function actionCreate(): void
{
$this->cgView->render("form.php");
}
#[NoReturn] public function actionAdd(): void
{
2024-09-10 12:55:41 +03:00
$menuForm = new CreateMenuForm();
$menuForm->load($_REQUEST);
2024-09-06 16:53:20 +03:00
if (isset($_FILES['icon_file']) && $_FILES['icon_file']['error'] === UPLOAD_ERR_OK) {
2024-09-10 11:39:45 +03:00
$file = new FileUpload($_FILES['icon_file'], ['jpg', 'jpeg', 'png']);
2024-09-06 16:53:20 +03:00
$file->upload();
2024-09-10 12:55:41 +03:00
$menuForm->setItem('icon_file', $file->getUploadFile());
2024-09-06 16:53:20 +03:00
}
2024-09-03 16:29:44 +03:00
if ($menuForm->validate()){
2024-09-06 16:53:20 +03:00
$menuItem = $this->menuService->create($menuForm);
2024-09-03 16:29:44 +03:00
if ($menuItem){
2024-09-10 12:55:41 +03:00
$this->redirect("/admin/settings/menu/" . $menuItem->id, code: 302);
2024-09-03 16:29:44 +03:00
}
}
2024-09-10 12:55:41 +03:00
$this->redirect("/admin/settings/menu/create", code: 302);
2024-09-03 16:29:44 +03:00
}
/**
* @throws Exception
*/
public function actionIndex($page_number = 1): void
{
$this->cgView->render("index.php", ['page_number' => $page_number]);
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$menuItem = Menu::find($id);
if (!$menuItem){
throw new Exception(message: "The menu item not found");
}
$this->cgView->render("view.php", ['menu' => $menuItem]);
}
/**
* @throws RuntimeError
* @throws SyntaxError
* @throws LoaderError|Exception
*/
public function actionUpdate($id): void
{
$model = Menu::find($id);
if (!$model){
throw new Exception(message: "The menu item not found");
}
$this->cgView->render("form.php", ['model' => $model]);
}
/**
* @throws Exception
*/
public function actionEdit($id): void
{
2024-09-06 16:53:20 +03:00
// Debug::prn($_REQUEST);
// Debug::prn($_FILES);
2024-09-03 16:29:44 +03:00
$menuItem = Menu::find($id);
if (!$menuItem){
throw new Exception(message: "The menu item not found");
}
2024-09-10 12:55:41 +03:00
$menuForm = new CreateMenuForm();
$menuForm->load($_REQUEST);
2024-09-10 15:45:12 +03:00
// Debug::dd($_REQUEST);
2024-09-10 12:55:41 +03:00
2024-09-06 16:53:20 +03:00
if (isset($_FILES['icon_file']) && $_FILES['icon_file']['error'] === UPLOAD_ERR_OK) {
2024-09-10 15:45:12 +03:00
$file = new FileUpload($_FILES['icon_file'], ['jpg', 'jpeg', 'png'], $_REQUEST['hashing'] ?? true);
2024-09-06 16:53:20 +03:00
$file->upload();
2024-09-10 12:55:41 +03:00
$menuForm->setItem('icon_file', $file->getUploadFile());
2024-09-06 16:53:20 +03:00
}
2024-09-10 12:55:41 +03:00
2024-09-03 16:29:44 +03:00
if ($menuForm->validate()){
2024-09-06 16:53:20 +03:00
$menuItem = $this->menuService->update($menuForm, $menuItem);
2024-09-03 16:29:44 +03:00
if ($menuItem){
2024-09-10 12:55:41 +03:00
$this->redirect("/admin/settings/menu/" . $menuItem->id, code: 302);
2024-09-03 16:29:44 +03:00
}
}
2024-09-10 12:55:41 +03:00
$this->redirect("/admin/settings/menu/update/" . $id, code: 302);
2024-09-03 16:29:44 +03:00
}
#[NoReturn] public function actionDelete($id): void
{
Menu::find($id)->delete();
$this->redirect("/admin/settings/menu/");
}
}