2024-07-03 14:41:15 +03:00
|
|
|
<?php
|
|
|
|
|
2024-07-05 13:49:04 +03:00
|
|
|
namespace app\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-07-05 13:49:04 +03:00
|
|
|
use app\models\Post;
|
2024-07-25 16:15:18 +03:00
|
|
|
use app\models\User;
|
|
|
|
use app\services\PostService;
|
|
|
|
use app\services\UserService;
|
|
|
|
use Exception;
|
|
|
|
use JetBrains\PhpStorm\NoReturn;
|
|
|
|
use kernel\Controller;
|
2024-07-03 14:41:15 +03:00
|
|
|
|
2024-07-25 16:15:18 +03:00
|
|
|
class PostController extends Controller
|
2024-07-03 14:41:15 +03:00
|
|
|
{
|
2024-07-25 16:15:18 +03:00
|
|
|
protected function init(): void
|
2024-07-03 14:41:15 +03:00
|
|
|
{
|
2024-07-25 16:15:18 +03:00
|
|
|
$this->cgView->viewPath = ROOT_DIR . "/views/admin/";
|
|
|
|
$this->cgView->layout = "layouts/main.php";
|
2024-07-03 14:41:15 +03:00
|
|
|
}
|
2024-07-25 16:15:18 +03:00
|
|
|
public function actionCreate(): void
|
|
|
|
{
|
|
|
|
$this->cgView->render("post/form.php");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[NoReturn] public function actionAdd(): void
|
|
|
|
{
|
|
|
|
$postForm = new CreatePostForm();
|
|
|
|
$postService = new PostService();
|
|
|
|
$postForm->load($_REQUEST);
|
2024-07-26 16:09:06 +03:00
|
|
|
// Debug::dd($_REQUEST);
|
2024-07-26 12:42:44 +03:00
|
|
|
if(UserService::check($_REQUEST['user_id'])) {
|
2024-07-26 16:09:06 +03:00
|
|
|
// Debug::dd(123);
|
2024-07-25 16:15:18 +03:00
|
|
|
if ($postForm->validate()) {
|
2024-07-26 16:09:06 +03:00
|
|
|
|
|
|
|
// Debug::dd($postForm);
|
2024-07-25 16:15:18 +03:00
|
|
|
$post = $postService->create($postForm);
|
|
|
|
if ($post) {
|
|
|
|
$this->redirect("/admin/post/" . $post->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->redirect("/admin/post/create");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2024-07-05 13:49:04 +03:00
|
|
|
|
2024-07-08 16:20:25 +03:00
|
|
|
public function actionIndex(): void
|
2024-07-05 13:49:04 +03:00
|
|
|
{
|
2024-07-26 11:57:05 +03:00
|
|
|
$contents = Post::all();
|
2024-07-25 16:15:18 +03:00
|
|
|
|
2024-07-26 11:57:05 +03:00
|
|
|
$this->cgView->render("post/index.php", ['contents' => $contents]);
|
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-07-26 11:57:05 +03:00
|
|
|
$this->cgView->render("post/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");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cgView->render("post/form.php", ['model' => $model]);
|
|
|
|
}
|
|
|
|
|
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-07-26 11:57:05 +03:00
|
|
|
if((new UserService)->check($_REQUEST['user_id'])) {
|
2024-07-25 16:15:18 +03:00
|
|
|
if ($postForm->validate()) {
|
|
|
|
$post = $postService->update($postForm, $post);
|
|
|
|
if ($post) {
|
|
|
|
$this->redirect("/admin/post/" . $post->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$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
|
|
|
}
|