la_bot_back/kernel/app_modules/tgbot/controllers/TgbotNotificationController.php

114 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2025-01-27 20:03:58 +03:00
<?php
namespace kernel\app_modules\tgbot\controllers;
use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\app_modules\tgbot\models\forms\CreateTgbotNotificationForm;
use kernel\app_modules\tgbot\models\TgbotNotification;
use kernel\app_modules\tgbot\services\TgbotNotificationService;
use kernel\Flash;
class TgbotNotificationController extends AdminController
{
protected TgbotNotificationService $notificationService;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tgbot/views/notification/";
$this->notificationService = new TgbotNotificationService();
}
public function actionCreate(): void
{
$this->cgView->render("form.php");
}
public function actionIndex($page_number = 1): void
{
$this->cgView->render("index.php", ['page_number' => $page_number]);
}
#[NoReturn] public function actionAdd(): void
{
$tgForm = new CreateTgbotNotificationForm();
$tgForm->load($_REQUEST);
if ($tgForm->validate()) {
$tg = $this->notificationService->create($tgForm);
if ($tg) {
$this->redirect("/admin/tg-bot-notification/view/" . $tg->id);
}
}
Flash::setMessage("error", $tgForm->getErrorsStr());
$this->redirect("/admin/tg-bot-notification/create");
}
/**
* @throws Exception
*/
public function actionUpdate($id): void
{
$model = TgbotNotification::find($id);
if (!$model) {
throw new Exception(message: "The notification not found");
}
$this->cgView->render("form.php", ['model' => $model]);
}
/**
* @throws Exception
*/
public function actionEdit($id): void
{
$tg = TgbotNotification::find($id);
if (!$tg) {
throw new Exception(message: "The tag not found");
}
$tgForm = new CreateTgbotNotificationForm();
$tgService = new TgbotNotificationService();
$tgForm->load($_REQUEST);
if ($tgForm->validate()) {
$tg = $tgService->update($tgForm, $tg);
if ($tg) {
$this->redirect("/admin/tg-bot-notification/view/" . $tg->id);
}
}
Flash::setMessage("error", $tgForm->getErrorsStr());
$this->redirect("/admin/tg-bot-notification/update/" . $id);
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$tg = TgbotNotification::find($id);
if (!$tg) {
throw new Exception(message: "The notification not found");
}
$this->cgView->render("view.php", ['tg' => $tg]);
}
/**
* @throws Exception
*/
#[NoReturn] public function actionDelete(int $id): void
{
$post = TgbotNotification::find($id)->first();
if (!$post){
throw new Exception(message: "The tg notification not found");
}
Flash::setMessage("success", "Notification deleted");
$post->delete();
$this->redirect("/admin/tg-bot-notification/");
}
}