1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Blog\Archive; |
4
|
|
|
|
5
|
|
|
use App\Blog\Tag\TagRepository; |
6
|
|
|
use App\ViewRenderer; |
7
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
9
|
|
|
use Yiisoft\Data\Paginator\OffsetPaginator; |
10
|
|
|
|
11
|
|
|
final class ArchiveController |
12
|
|
|
{ |
13
|
|
|
private const POSTS_PER_PAGE = 3; |
14
|
|
|
private const POPULAR_TAGS_COUNT = 10; |
15
|
|
|
private ViewRenderer $viewRenderer; |
16
|
|
|
|
17
|
|
|
public function __construct(ViewRenderer $viewRenderer) |
18
|
|
|
{ |
19
|
|
|
$this->viewRenderer = $viewRenderer->withControllerName('blog/archive'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function index(ArchiveRepository $archiveRepo): Response |
23
|
|
|
{ |
24
|
|
|
return $this->viewRenderer->render('index', ['archive' => $archiveRepo->getFullArchive()]); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function monthlyArchive(Request $request, TagRepository $tagRepository, ArchiveRepository $archiveRepo): Response |
28
|
|
|
{ |
29
|
|
|
$pageNum = (int)$request->getAttribute('page', 1); |
30
|
|
|
$year = $request->getAttribute('year', null); |
31
|
|
|
$month = $request->getAttribute('month', null); |
32
|
|
|
|
33
|
|
|
$dataReader = $archiveRepo->getMonthlyArchive($year, $month); |
34
|
|
|
$paginator = (new OffsetPaginator($dataReader)) |
35
|
|
|
->withPageSize(self::POSTS_PER_PAGE) |
36
|
|
|
->withCurrentPage($pageNum); |
37
|
|
|
|
38
|
|
|
$data = [ |
39
|
|
|
'year' => $year, |
40
|
|
|
'month' => $month, |
41
|
|
|
'paginator' => $paginator, |
42
|
|
|
'archive' => $archiveRepo->getFullArchive()->withLimit(12), |
43
|
|
|
'tags' => $tagRepository->getTagMentions(self::POPULAR_TAGS_COUNT), |
44
|
|
|
]; |
45
|
|
|
return $this->viewRenderer->render('monthly-archive', $data); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function yearlyArchive(Request $request, ArchiveRepository $archiveRepo): Response |
49
|
|
|
{ |
50
|
|
|
$year = $request->getAttribute('year', null); |
51
|
|
|
|
52
|
|
|
$data = [ |
53
|
|
|
'year' => $year, |
54
|
|
|
'items' => $archiveRepo->getYearlyArchive($year), |
55
|
|
|
]; |
56
|
|
|
return $this->viewRenderer->render('yearly-archive', $data); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|