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