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-09-10 15:45:12 +03:00
|
|
|
use Exception;
|
|
|
|
use JetBrains\PhpStorm\NoReturn;
|
2024-09-06 16:53:20 +03:00
|
|
|
use kernel\AdminController;
|
2024-11-21 13:00:21 +03:00
|
|
|
use kernel\EntityRelation;
|
|
|
|
use kernel\helpers\Debug;
|
2024-09-10 15:45:12 +03:00
|
|
|
use kernel\modules\post\models\forms\CreatePostForm;
|
2024-09-06 16:53:20 +03:00
|
|
|
use kernel\modules\post\models\Post;
|
|
|
|
use kernel\modules\post\service\PostService;
|
2024-11-21 13:00:21 +03:00
|
|
|
use kernel\Request;
|
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-10 12:55:41 +03:00
|
|
|
parent::init();
|
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-11-21 13:00:21 +03:00
|
|
|
|
|
|
|
$entityRelation = new EntityRelation();
|
2024-11-25 15:17:41 +03:00
|
|
|
$entityRelation->saveEntityRelation(entity: "post", model: $post, request: new Request());
|
2024-11-21 13:00:21 +03:00
|
|
|
|
2024-08-30 16:14:31 +03:00
|
|
|
if ($post) {
|
2024-10-31 16:12:02 +03:00
|
|
|
$this->redirect("/admin/post/view/" . $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-10-17 14:55:00 +03:00
|
|
|
public function actionIndex(int $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
|
|
|
|
*/
|
2024-10-17 14:55:00 +03:00
|
|
|
public function actionView(int $id): void
|
2024-07-25 16:15:18 +03:00
|
|
|
{
|
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
|
|
|
|
*/
|
2024-10-17 14:55:00 +03:00
|
|
|
public function actionUpdate(int $id): void
|
2024-07-25 16:15:18 +03:00
|
|
|
{
|
|
|
|
$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-10-17 14:55:00 +03:00
|
|
|
public function actionEdit(int $id): void
|
2024-07-25 16:15:18 +03:00
|
|
|
{
|
|
|
|
$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();
|
|
|
|
$postForm->load($_REQUEST);
|
2024-09-06 16:53:20 +03:00
|
|
|
if ($postForm->validate()) {
|
2024-10-17 14:55:00 +03:00
|
|
|
$post = $this->postService->update($postForm, $post);
|
2024-11-28 15:34:57 +03:00
|
|
|
|
|
|
|
$entityRelation = new EntityRelation();
|
|
|
|
$entityRelation->saveEntityRelation(entity: "post", model: $post, request: new Request());
|
|
|
|
|
2024-09-06 16:53:20 +03:00
|
|
|
if ($post) {
|
2024-10-31 16:12:02 +03:00
|
|
|
$this->redirect("/admin/post/view/" . $post->id);
|
2024-07-25 16:15:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->redirect("/admin/post/update/" . $id);
|
|
|
|
}
|
|
|
|
|
2024-10-17 14:55:00 +03:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
#[NoReturn] public function actionDelete(int $id): void
|
2024-07-25 16:15:18 +03:00
|
|
|
{
|
2024-07-26 16:09:06 +03:00
|
|
|
$post = Post::find($id)->first();
|
2024-10-17 14:55:00 +03:00
|
|
|
if (!$post){
|
|
|
|
throw new Exception(message: "The post not found");
|
|
|
|
}
|
2024-11-28 15:34:57 +03:00
|
|
|
|
|
|
|
$entityRelation = new EntityRelation();
|
|
|
|
$entityRelation->deleteEntityRelation(entity: "post", model: $post);
|
|
|
|
|
2024-07-26 16:09:06 +03:00
|
|
|
$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
|
|
|
}
|