1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Web\Action; |
6
|
|
|
|
7
|
|
|
use Article\Entity\ArticleType; |
8
|
|
|
use Article\Service\PostService; |
9
|
|
|
use Category\Service\CategoryService; |
10
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
11
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
12
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
13
|
|
|
use Zend\Expressive\Template\TemplateRendererInterface as Template; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class PostsAction. |
17
|
|
|
*/ |
18
|
|
|
class PostsAction |
19
|
|
|
{ |
20
|
|
|
/** @var Template */ |
21
|
|
|
private $template; |
22
|
|
|
|
23
|
|
|
/** @var PostService */ |
24
|
|
|
private $postService; |
25
|
|
|
|
26
|
|
|
/** @var CategoryService */ |
27
|
|
|
private $categoryService; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* CategoryAction constructor. |
31
|
|
|
* |
32
|
|
|
* @param Template $template |
33
|
|
|
* @param PostService $postService |
34
|
|
|
* @param CategoryService $categoryService |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
Template $template, |
38
|
|
|
PostService $postService, |
39
|
|
|
CategoryService $categoryService |
40
|
|
|
) { |
41
|
|
|
$this->template = $template; |
42
|
|
|
$this->postService = $postService; |
43
|
|
|
$this->categoryService = $categoryService; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Executed when action is invoked. |
48
|
|
|
* |
49
|
|
|
* @param Request $request |
50
|
|
|
* @param Response $response |
51
|
|
|
* @param callable|null $next |
52
|
|
|
* |
53
|
|
|
* @throws \Exception |
54
|
|
|
* |
55
|
|
|
* @return HtmlResponse |
56
|
|
|
*/ |
57
|
|
|
public function __invoke( |
58
|
|
|
Request $request, |
59
|
|
|
Response $response, |
60
|
|
|
callable $next = null |
61
|
|
|
) { |
62
|
|
|
$params = $request->getQueryParams(); |
63
|
|
|
$page = isset($params['page']) ? $params['page'] : 1; |
64
|
|
|
$urlSlug = $request->getAttribute('category'); |
65
|
|
|
$category = $this->categoryService->getCategoryBySlug($urlSlug); |
66
|
|
|
|
67
|
|
|
if (!$category) { |
68
|
|
|
if ($urlSlug !== 'all') { |
69
|
|
|
return $next($request, $response); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Default category for all posts |
73
|
|
|
$category = (object) [ |
74
|
|
|
'name' => 'Svi članci', |
75
|
|
|
'slug' => 'all', |
76
|
|
|
'title' => 'Svi članci', |
77
|
|
|
'description' => 'Svi članci PHP i ostalih tehnologija.', |
78
|
|
|
'main_img' => null, |
79
|
|
|
'type' => ArticleType::POST, |
80
|
|
|
]; |
81
|
|
|
} elseif ($category->type != ArticleType::POST) { |
82
|
|
|
return $next($request, $response); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$posts = $this->categoryService->paginateCategoryPosts($category, $page); |
86
|
|
|
$categories = $this->categoryService->getCategories(true); |
87
|
|
|
|
88
|
|
|
return new HtmlResponse($this->template->render('web::posts', [ |
89
|
|
|
'layout' => 'layout/web', |
90
|
|
|
'categories' => $categories, |
91
|
|
|
'currentCategory' => $category, |
92
|
|
|
'posts' => $posts, |
93
|
|
|
])); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|