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 App\Stream\Value\DataResponseProvider; |
9
|
|
|
use Cycle\ORM\ORMInterface; |
10
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
11
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
12
|
|
|
use Yiisoft\Data\Paginator\OffsetPaginator; |
13
|
|
|
|
14
|
|
|
final class ArchiveController extends Controller |
15
|
|
|
{ |
16
|
|
|
private const POSTS_PER_PAGE = 3; |
17
|
|
|
private const POPULAR_TAGS_COUNT = 10; |
18
|
|
|
|
19
|
|
|
protected function getId(): string |
20
|
|
|
{ |
21
|
|
|
return 'blog/archive'; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function index(ArchiveRepository $archiveRepo) |
25
|
|
|
{ |
26
|
|
|
return (new DataResponseProvider(['archive' => $archiveRepo->getFullArchive()->read()])) |
27
|
|
|
->setHeaders(['X-Action' => __METHOD__]) |
28
|
|
|
->setFormat(null, [ |
29
|
|
|
'viewPath' => $this->getViewPath(), |
30
|
|
|
'view' => 'index', |
31
|
|
|
'layout' => '@views/layout/main.php', |
32
|
|
|
]); |
33
|
|
|
// return $this->render('index', ['archive' => $archiveRepo->getFullArchive()]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function monthlyArchive(Request $request, ORMInterface $orm, ArchiveRepository $archiveRepo): Response |
37
|
|
|
{ |
38
|
|
|
/** @var TagRepository $postRepo */ |
39
|
|
|
$tagRepo = $orm->getRepository(Tag::class); |
40
|
|
|
|
41
|
|
|
$pageNum = (int)$request->getAttribute('page', 1); |
42
|
|
|
$year = $request->getAttribute('year', null); |
43
|
|
|
$month = $request->getAttribute('month', null); |
44
|
|
|
|
45
|
|
|
$dataReader = $archiveRepo->getMonthlyArchive($year, $month); |
46
|
|
|
$paginator = (new OffsetPaginator($dataReader)) |
47
|
|
|
->withPageSize(self::POSTS_PER_PAGE) |
48
|
|
|
->withCurrentPage($pageNum); |
49
|
|
|
|
50
|
|
|
$data = [ |
51
|
|
|
'year' => $year, |
52
|
|
|
'month' => $month, |
53
|
|
|
'paginator' => $paginator, |
54
|
|
|
'archive' => $archiveRepo->getFullArchive()->withLimit(12), |
55
|
|
|
'tags' => $tagRepo->getTagMentions(self::POPULAR_TAGS_COUNT), |
|
|
|
|
56
|
|
|
]; |
57
|
|
|
return $this->render('monthly-archive', $data); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function yearlyArchive(Request $request, ArchiveRepository $archiveRepo): Response |
61
|
|
|
{ |
62
|
|
|
$year = $request->getAttribute('year', null); |
63
|
|
|
|
64
|
|
|
$data = [ |
65
|
|
|
'year' => $year, |
66
|
|
|
'items' => $archiveRepo->getYearlyArchive($year), |
67
|
|
|
]; |
68
|
|
|
return $this->render('yearly-archive', $data); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function custom() |
72
|
|
|
{ |
73
|
|
|
return (new DataResponseProvider(['hello', 'world'])) |
74
|
|
|
->setHeaders(['My-Name' => 'Trololosha']) |
75
|
|
|
->setCode(500); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|