|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Albert221\Blog\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Albert221\Blog\Pagination\PaginatorBuilder; |
|
6
|
|
|
use Albert221\Blog\Repository\CategoryRepositoryInterface; |
|
7
|
|
|
use Albert221\Blog\Repository\PostRepositoryInterface; |
|
8
|
|
|
use Albert221\Blog\Sidebar\Widget\RecentPosts; |
|
9
|
|
|
use Albert221\Blog\Sidebar\WidgetManager; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use Zend\Diactoros\Request; |
|
12
|
|
|
|
|
13
|
|
|
class PostController extends AbstractController |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var PostRepositoryInterface Posts |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $posts; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var CategoryRepositoryInterface Categories |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $categories; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var PaginatorBuilder Paginator builder |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $paginatorBuilder; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( |
|
31
|
|
|
PostRepositoryInterface $posts, |
|
32
|
|
|
CategoryRepositoryInterface $categories, |
|
33
|
|
|
PaginatorBuilder $paginatorBuilder |
|
34
|
|
|
) { |
|
35
|
|
|
$this->posts = $posts; |
|
36
|
|
|
$this->categories = $categories; |
|
37
|
|
|
$this->paginatorBuilder = $paginatorBuilder; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Route: / |
|
42
|
|
|
* |
|
43
|
|
|
* @param ServerRequestInterface $request |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function index(ServerRequestInterface $request) |
|
48
|
|
|
{ |
|
49
|
|
|
$paginator = $this->paginatorBuilder->build($request, $this->posts->count()); |
|
50
|
|
|
|
|
51
|
|
|
$posts = $this->posts->paginated($paginator->getPage(), $paginator->getPerPage()); |
|
52
|
|
|
|
|
53
|
|
|
// TODO: Move code from here |
|
54
|
|
|
|
|
55
|
|
|
$sidebarWidgetManager = new WidgetManager(); |
|
56
|
|
|
|
|
57
|
|
|
$recentPostsWidget = new RecentPosts($this->posts, $this->twig); |
|
58
|
|
|
$sidebarWidgetManager->add($recentPostsWidget); |
|
59
|
|
|
|
|
60
|
|
|
// to here to place where this should be at |
|
61
|
|
|
|
|
62
|
|
|
return $this->view('index.twig', [ |
|
63
|
|
|
'posts' => $posts, |
|
64
|
|
|
'paginator' => $paginator, |
|
65
|
|
|
'sidebarWidgets' => $sidebarWidgetManager->getWidgets() |
|
66
|
|
|
]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Route: /{slug} |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $slug |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public function post($slug) |
|
76
|
|
|
{ |
|
77
|
|
|
$post = $this->posts->bySlug($slug); |
|
78
|
|
|
|
|
79
|
|
|
return $this->view('post.twig', compact('post')); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Route: /kategoria/{slug} |
|
84
|
|
|
* |
|
85
|
|
|
* @param ServerRequestInterface $request |
|
86
|
|
|
* @param string $slug |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function category(ServerRequestInterface $request, $slug) |
|
90
|
|
|
{ |
|
91
|
|
|
$paginator = $this->paginatorBuilder->build($request, $this->categories->postsCount($slug)); |
|
92
|
|
|
|
|
93
|
|
|
$posts = $this->categories->postsPaginated($slug, $paginator->getPage(), $paginator->getPerPage()); |
|
94
|
|
|
|
|
95
|
|
|
return $this->view('index.twig', [ |
|
96
|
|
|
'posts' => $posts, |
|
97
|
|
|
'paginator' => $paginator |
|
98
|
|
|
]); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|