1 | <?php |
||
2 | |||
3 | namespace Frontend\Modules\Blog\Actions; |
||
4 | |||
5 | use DateTimeImmutable; |
||
6 | use Frontend\Core\Engine\Base\Block as FrontendBaseBlock; |
||
7 | use Frontend\Core\Engine\Model; |
||
8 | use Frontend\Core\Engine\Navigation; |
||
9 | use Frontend\Core\Language\Language as FL; |
||
10 | use Frontend\Core\Engine\Navigation as FrontendNavigation; |
||
11 | use Frontend\Modules\Blog\Engine\Model as FrontendBlogModel; |
||
0 ignored issues
–
show
|
|||
12 | use Symfony\Component\HttpFoundation\Response; |
||
13 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
14 | |||
15 | class Archive extends FrontendBaseBlock |
||
16 | { |
||
17 | /** @var array */ |
||
18 | private $articles; |
||
19 | |||
20 | /** @var DateTimeImmutable */ |
||
21 | private $startDate; |
||
22 | |||
23 | /** @var DateTimeImmutable */ |
||
24 | private $endDate; |
||
25 | |||
26 | /** @var bool */ |
||
27 | private $hasMonth; |
||
28 | |||
29 | /** @var string */ |
||
30 | private $format; |
||
31 | |||
32 | 4 | public function execute(): void |
|
33 | { |
||
34 | 4 | parent::execute(); |
|
35 | 4 | $this->loadTemplate(); |
|
36 | 4 | $this->getData(); |
|
37 | |||
38 | 2 | $this->parse(); |
|
39 | 2 | } |
|
40 | |||
41 | 2 | private function buildUrl(): string |
|
42 | { |
||
43 | 2 | return FrontendNavigation::getUrlForBlock($this->getModule(), $this->getAction()) |
|
44 | 2 | . '/' . $this->startDate->format($this->format); |
|
45 | } |
||
46 | |||
47 | 4 | private function buildPaginationConfig(): array |
|
48 | { |
||
49 | 4 | $requestedPage = $this->url->getParameter('page', 'int', 1); |
|
50 | 4 | $numberOfItems = FrontendBlogModel::getAllForDateRangeCount( |
|
51 | 4 | $this->startDate->getTimestamp(), |
|
52 | 4 | $this->endDate->getTimestamp() |
|
53 | ); |
||
54 | |||
55 | 4 | $limit = $this->get('fork.settings')->get($this->getModule(), 'overview_num_items', 10); |
|
56 | 4 | $numberOfPages = (int) ceil($numberOfItems / $limit); |
|
57 | |||
58 | // Check if the page exists |
||
59 | 4 | if ($requestedPage > $numberOfPages || $requestedPage < 1) { |
|
60 | 2 | throw new NotFoundHttpException(); |
|
61 | } |
||
62 | |||
63 | return [ |
||
64 | 2 | 'url' => $this->buildUrl(), |
|
65 | 2 | 'limit' => $limit, |
|
66 | 2 | 'offset' => ($requestedPage * $limit) - $limit, |
|
67 | 2 | 'requested_page' => $requestedPage, |
|
68 | 2 | 'num_items' => $numberOfItems, |
|
69 | 2 | 'num_pages' => $numberOfPages, |
|
70 | ]; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Complete the slug to prevent wrong months when the current day is higher than possible in the requested month |
||
75 | * |
||
76 | * @param string $slug |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | 4 | private function getStartDateSlug(string $slug):string |
|
81 | { |
||
82 | 4 | if (!$this->hasMonth) { |
|
83 | 1 | $slug .= '/01'; |
|
84 | } |
||
85 | |||
86 | 4 | return $slug . '/01 00:00:00'; |
|
87 | } |
||
88 | |||
89 | 4 | private function getSlug(): string |
|
90 | { |
||
91 | 4 | $yearIndex = $this->url->getParameter(0) === FL::act('Archive') ? 1 : 0; |
|
92 | 4 | $monthIndex = $yearIndex + 1; |
|
93 | 4 | $this->hasMonth = !empty($this->url->getParameter($monthIndex)); |
|
94 | |||
95 | 4 | if ($this->hasMonth) { |
|
96 | 3 | $this->format = 'Y/m'; |
|
97 | |||
98 | 3 | return $this->url->getParameter($yearIndex) . '/' . $this->url->getParameter($monthIndex); |
|
99 | } |
||
100 | |||
101 | 1 | $this->format = 'Y'; |
|
102 | |||
103 | 1 | return $this->url->getParameter($yearIndex); |
|
104 | } |
||
105 | |||
106 | 4 | private function setDateRange(): void |
|
107 | { |
||
108 | 4 | $slug = $this->getSlug(); |
|
109 | 4 | $this->startDate = DateTimeImmutable::createFromFormat('Y/m/d H:i:s', $this->getStartDateSlug($slug)); |
|
110 | |||
111 | 4 | if (!$this->startDate instanceof DateTimeImmutable) { |
|
112 | throw new NotFoundHttpException(); |
||
113 | } |
||
114 | |||
115 | 4 | if ($slug !== $this->startDate->format($this->format)) { |
|
116 | // redirect /2010/6 to /2010/06 to avoid duplicate content |
||
117 | $redirectUrl = Navigation::getUrlForBlock($this->getModule(), $this->getAction()) |
||
118 | . '/' . $this->startDate->format($this->format); |
||
119 | if ($this->getRequest()->getQueryString() !== null) { |
||
120 | $redirectUrl .= '?' . $this->getRequest()->getQueryString(); |
||
121 | } |
||
122 | |||
123 | $this->redirect($redirectUrl, Response::HTTP_MOVED_PERMANENTLY); |
||
124 | } |
||
125 | |||
126 | 4 | $this->endDate = $this->startDate |
|
127 | 4 | ->setDate($this->startDate->format('Y'), 12, 31) |
|
128 | 4 | ->setTime(23, 59, 59, 999999); |
|
129 | 4 | } |
|
130 | |||
131 | 4 | private function getData(): void |
|
132 | { |
||
133 | 4 | $this->setDateRange(); |
|
134 | 4 | $this->pagination = $this->buildPaginationConfig(); |
|
135 | |||
136 | 2 | $this->articles = FrontendBlogModel::getAllForDateRange( |
|
137 | 2 | $this->startDate->getTimestamp(), |
|
138 | 2 | $this->endDate->getTimestamp(), |
|
139 | 2 | $this->pagination['limit'], |
|
140 | 2 | $this->pagination['offset'] |
|
141 | ); |
||
142 | 2 | } |
|
143 | |||
144 | 2 | private function parse(): void |
|
145 | { |
||
146 | 2 | $this->addLinkToRssFeed(); |
|
147 | 2 | $this->addPageToBreadcrumb(); |
|
148 | 2 | $this->setPageTitle(); |
|
149 | 2 | $this->parsePagination(); |
|
150 | |||
151 | 2 | $this->template->assign( |
|
152 | 2 | 'archive', |
|
153 | [ |
||
154 | 2 | 'start_date' => $this->startDate->getTimestamp(), |
|
155 | 2 | 'end_date' => $this->endDate->getTimestamp(), |
|
156 | 2 | 'year' => $this->startDate->format('Y'), |
|
157 | 2 | 'month' => $this->hasMonth ? $this->startDate->format('m') : null, |
|
158 | ] |
||
159 | ); |
||
160 | 2 | $this->template->assign('items', $this->articles); |
|
161 | 2 | $this->template->assign( |
|
162 | 2 | 'allowComments', |
|
163 | 2 | $this->get('fork.settings')->get($this->getModule(), 'allow_comments') |
|
164 | ); |
||
165 | 2 | } |
|
166 | |||
167 | 2 | private function setPageTitle(): void |
|
168 | { |
||
169 | 2 | $this->header->setPageTitle(\SpoonFilter::ucfirst(FL::lbl('Archive'))); |
|
170 | 2 | $this->header->setPageTitle($this->startDate->format('Y')); |
|
171 | 2 | if ($this->hasMonth) { |
|
172 | 1 | $this->header->setPageTitle( |
|
173 | 1 | \SpoonDate::getDate('F', $this->startDate->getTimestamp(), LANGUAGE) |
|
174 | ); |
||
175 | } |
||
176 | 2 | } |
|
177 | |||
178 | 2 | private function addPageToBreadcrumb(): void |
|
179 | { |
||
180 | 2 | $this->breadcrumb->addElement(\SpoonFilter::ucfirst(FL::lbl('Archive'))); |
|
181 | 2 | $this->breadcrumb->addElement($this->startDate->format('Y')); |
|
182 | 2 | if ($this->hasMonth) { |
|
183 | 1 | $this->breadcrumb->addElement( |
|
184 | 1 | \SpoonDate::getDate( |
|
185 | 1 | 'F', |
|
186 | 1 | $this->startDate->getTimestamp(), |
|
187 | 1 | LANGUAGE |
|
188 | ) |
||
189 | ); |
||
190 | } |
||
191 | 2 | } |
|
192 | |||
193 | 2 | private function addLinkToRssFeed(): void |
|
194 | { |
||
195 | 2 | $this->header->addRssLink( |
|
196 | 2 | $this->get('fork.settings')->get($this->getModule(), 'rss_title_' . LANGUAGE, SITE_DEFAULT_TITLE), |
|
197 | 2 | FrontendNavigation::getUrlForBlock($this->getModule(), 'Rss') |
|
198 | ); |
||
199 | 2 | } |
|
200 | } |
||
201 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths