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