|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Blog\Post; |
|
6
|
|
|
|
|
7
|
|
|
use App\Blog\Entity\Post; |
|
8
|
|
|
use App\Service\UserService; |
|
9
|
|
|
use App\Service\WebControllerService; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
11
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
12
|
|
|
use Yiisoft\Http\Method; |
|
13
|
|
|
use Yiisoft\Yii\View\ViewRenderer; |
|
14
|
|
|
|
|
15
|
|
|
final class PostController |
|
16
|
|
|
{ |
|
17
|
|
|
private ViewRenderer $viewRenderer; |
|
18
|
|
|
private WebControllerService $webService; |
|
19
|
|
|
private PostService $postService; |
|
20
|
|
|
private UserService $userService; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct( |
|
23
|
|
|
ViewRenderer $viewRenderer, |
|
24
|
|
|
WebControllerService $webService, |
|
25
|
|
|
PostService $postService, |
|
26
|
|
|
UserService $userService |
|
27
|
|
|
) { |
|
28
|
|
|
$this->viewRenderer = $viewRenderer->withControllerName('blog/post'); |
|
29
|
|
|
$this->webService = $webService; |
|
30
|
|
|
$this->postService = $postService; |
|
31
|
|
|
$this->userService = $userService; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function index(Request $request, PostRepository $postRepository): Response |
|
35
|
|
|
{ |
|
36
|
|
|
$canEdit = $this->userService->hasPermission('editPost'); |
|
37
|
|
|
$slug = $request->getAttribute('slug', null); |
|
38
|
|
|
$item = $postRepository->fullPostPage($slug); |
|
39
|
|
|
if ($item === null) { |
|
40
|
|
|
return $this->webService->getNotFoundResponse(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $this->viewRenderer->render('index', ['item' => $item, 'canEdit' => $canEdit, 'slug' => $slug]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function add(Request $request, PostForm $form): Response |
|
47
|
|
|
{ |
|
48
|
|
|
$parameters = [ |
|
49
|
|
|
'title' => 'Add post', |
|
50
|
|
|
'action' => ['blog/add'], |
|
51
|
|
|
'errors' => [], |
|
52
|
|
|
'body' => $request->getParsedBody(), |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
if ($request->getMethod() === Method::POST) { |
|
56
|
|
|
$form->load($parameters['body']); |
|
|
|
|
|
|
57
|
|
|
if ($form->validate()) { |
|
58
|
|
|
$this->postService->savePost($this->userService->getUser(), new Post(), $form); |
|
59
|
|
|
return $this->webService->getRedirectResponse('blog/index'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$parameters['errors'] = $form->firstErrors(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->viewRenderer->withCsrf()->render('__form', $parameters); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function edit(Request $request, PostForm $form, PostRepository $postRepository): Response |
|
69
|
|
|
{ |
|
70
|
|
|
$slug = $request->getAttribute('slug', null); |
|
71
|
|
|
$post = $postRepository->fullPostPage($slug); |
|
72
|
|
|
if ($post === null) { |
|
73
|
|
|
return $this->webService->getNotFoundResponse(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$parameters = [ |
|
77
|
|
|
'title' => 'Edit post', |
|
78
|
|
|
'action' => ['blog/edit', ['slug' => $slug]], |
|
79
|
|
|
'errors' => [], |
|
80
|
|
|
'body' => [ |
|
81
|
|
|
'title' => $post->getTitle(), |
|
82
|
|
|
'content' => $post->getContent(), |
|
83
|
|
|
'tags' => $this->postService->getPostTags($post) |
|
84
|
|
|
] |
|
85
|
|
|
]; |
|
86
|
|
|
|
|
87
|
|
|
if ($request->getMethod() === Method::POST) { |
|
88
|
|
|
$body = $request->getParsedBody(); |
|
89
|
|
|
$form->load($body); |
|
|
|
|
|
|
90
|
|
|
if ($form->validate()) { |
|
91
|
|
|
$this->postService->savePost($this->userService->getUser(), $post, $form); |
|
92
|
|
|
return $this->webService->getRedirectResponse('blog/index'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$parameters['body'] = $body; |
|
96
|
|
|
$parameters['errors'] = $form->firstErrors(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->viewRenderer->withCsrf()->render('__form', $parameters); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|