2024-07-03 14:41:15 +03:00
|
|
|
<?php
|
|
|
|
|
2024-09-06 16:53:20 +03:00
|
|
|
namespace kernel\modules\post\controllers;
|
2024-07-03 14:41:15 +03:00
|
|
|
|
2024-07-05 13:49:04 +03:00
|
|
|
|
2024-07-08 16:20:25 +03:00
|
|
|
use app\helpers\Debug;
|
2024-07-25 16:15:18 +03:00
|
|
|
use app\models\forms\CreatePostForm;
|
2024-09-06 16:53:20 +03:00
|
|
|
use kernel\AdminController;
|
|
|
|
use kernel\modules\post\models\Post;
|
|
|
|
use kernel\modules\post\service\PostService;
|
2024-07-25 16:15:18 +03:00
|
|
|
use Exception;
|
|
|
|
use JetBrains\PhpStorm\NoReturn;
|
2024-07-03 14:41:15 +03:00
|
|
|
|
2024-09-06 16:53:20 +03:00
|
|
|
class PostController extends AdminController
|
2024-07-03 14:41:15 +03:00
|
|
|
{
|
2024-09-06 16:53:20 +03:00
|
|
|
private PostService $postService;
|
|
|
|
|
2024-07-25 16:15:18 +03:00
|
|
|
protected function init(): void
|
2024-07-03 14:41:15 +03:00
|
|
|
{
|
2024-09-06 16:53:20 +03:00
|
|
|
$this->cgView->viewPath = KERNEL_MODULES_DIR . "/post/views/";
|
|
|
|
$this->postService = new PostService();
|
2024-07-03 14:41:15 +03:00
|
|
|
}
|
2024-07-25 16:15:18 +03:00
|
|
|
public function actionCreate(): void
|
|
|
|
{
|
2024-09-06 16:53:20 +03:00
|
|
|
$this->cgView->render("form.php");
|
2024-07-25 16:15:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[NoReturn] public function actionAdd(): void
|
|
|
|
{
|
|
|
|
$postForm = new CreatePostForm();
|
|
|
|
$postForm->load($_REQUEST);
|
2024-08-30 16:14:31 +03:00
|
|
|
if ($postForm->validate()) {
|
2024-09-06 16:53:20 +03:00
|
|
|
$post = $this->postService->create($postForm);
|
2024-08-30 16:14:31 +03:00
|
|
|
if ($post) {
|
|
|
|
$this->redirect("/admin/post/" . $post->id);
|
2024-07-25 16:15:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->redirect("/admin/post/create");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2024-07-05 13:49:04 +03:00
|
|
|
|
2024-08-30 16:14:31 +03:00
|
|
|
public function actionIndex($page_number = 1): void
|
2024-07-05 13:49:04 +03:00
|
|
|
{
|
2024-09-06 16:53:20 +03:00
|
|
|
$this->cgView->render("index.php", ['page_number' => $page_number]);
|
2024-07-25 16:15:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function actionView($id): void
|
|
|
|
{
|
2024-07-26 11:57:05 +03:00
|
|
|
$content = Post::find($id);
|
2024-07-25 16:15:18 +03:00
|
|
|
|
2024-07-26 11:57:05 +03:00
|
|
|
if (!$content){
|
2024-07-25 16:15:18 +03:00
|
|
|
throw new Exception(message: "The post not found");
|
2024-07-05 13:49:04 +03:00
|
|
|
}
|
2024-09-06 16:53:20 +03:00
|
|
|
$this->cgView->render("view.php", ['content' => $content]);
|
2024-07-25 16:15:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function actionUpdate($id): void
|
|
|
|
{
|
|
|
|
$model = Post::find($id);
|
|
|
|
if (!$model){
|
|
|
|
throw new Exception(message: "The post not found");
|
|
|
|
}
|
|
|
|
|
2024-09-06 16:53:20 +03:00
|
|
|
$this->cgView->render("form.php", ['model' => $model]);
|
2024-07-25 16:15:18 +03:00
|
|
|
}
|
|
|
|
|
2024-07-26 11:57:05 +03:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2024-07-25 16:15:18 +03:00
|
|
|
public function actionEdit($id): void
|
|
|
|
{
|
|
|
|
$post = Post::find($id);
|
|
|
|
if (!$post){
|
2024-07-26 11:57:05 +03:00
|
|
|
throw new Exception(message: "The post not found");
|
2024-07-25 16:15:18 +03:00
|
|
|
}
|
|
|
|
$postForm = new CreatePostForm();
|
|
|
|
$postService = new PostService();
|
|
|
|
$postForm->load($_REQUEST);
|
2024-09-06 16:53:20 +03:00
|
|
|
if ($postForm->validate()) {
|
|
|
|
$post = $postService->update($postForm, $post);
|
|
|
|
if ($post) {
|
|
|
|
$this->redirect("/admin/post/" . $post->id);
|
2024-07-25 16:15:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->redirect("/admin/post/update/" . $id);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[NoReturn] public function actionDelete($id): void
|
|
|
|
{
|
2024-07-26 16:09:06 +03:00
|
|
|
$post = Post::find($id)->first();
|
|
|
|
$post->delete();
|
2024-07-25 16:15:18 +03:00
|
|
|
$this->redirect("/admin/post/");
|
2024-07-05 13:49:04 +03:00
|
|
|
}
|
2024-07-03 14:41:15 +03:00
|
|
|
}
|