|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
declare(strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace WebHemi\Middleware\Action\Website\Directory; |
|
15
|
|
|
|
|
16
|
|
|
use RuntimeException; |
|
17
|
|
|
use WebHemi\DateTime; |
|
18
|
|
|
use WebHemi\Data\Entity; |
|
19
|
|
|
use WebHemi\Middleware\Action\Website\IndexAction; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class ArchiveAction. |
|
23
|
|
|
*/ |
|
24
|
|
|
class ArchiveAction extends IndexAction |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Gets template map name or template file path. |
|
28
|
|
|
* |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getTemplateName() : string |
|
32
|
|
|
{ |
|
33
|
|
|
return 'website-post-list'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Gets template data. |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getTemplateData() : array |
|
42
|
|
|
{ |
|
43
|
|
|
$blogPosts = []; |
|
44
|
|
|
$parameters = $this->getRoutingParameters(); |
|
45
|
|
|
$date = $parameters['uri_parameter'] ?? null; |
|
46
|
|
|
|
|
47
|
|
|
if (!$date) { |
|
48
|
|
|
throw new RuntimeException('Forbidden', 403); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$dateParts = explode('-', $date); |
|
52
|
|
|
|
|
53
|
|
|
if (!preg_match('/^\d{4}\-\d{2}$/', $date) || !checkdate((int) ($dateParts[1] ?? 13), 1, (int) $dateParts[0])) { |
|
54
|
|
|
throw new RuntimeException('Bad Request', 400); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** @var Entity\ApplicationEntity $applicationEntity */ |
|
58
|
|
|
$applicationEntity = $this->getApplicationStorage() |
|
59
|
|
|
->getApplicationByName($this->environmentManager->getSelectedApplication()); |
|
60
|
|
|
|
|
61
|
|
|
/** @var Entity\Filesystem\FilesystemEntity[] $publications */ |
|
62
|
|
|
$publications = $this->getFilesystemStorage() |
|
63
|
|
|
->getPublishedDocuments( |
|
64
|
|
|
$applicationEntity->getApplicationId(), |
|
65
|
|
|
[ |
|
66
|
|
|
'YEAR(date_published) = ?' => (int)$dateParts[0], |
|
67
|
|
|
'MONTH(date_published) = ?' => (int)$dateParts[1] |
|
68
|
|
|
] |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
if (!$publications) { |
|
|
|
|
|
|
72
|
|
|
throw new RuntimeException('Not Found', 404); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** @var DateTime $titleDate */ |
|
76
|
|
|
$titleDate = $publications[0]->getDatePublished(); |
|
77
|
|
|
|
|
78
|
|
|
/** @var Entity\Filesystem\FilesystemEntity $filesystemEntity */ |
|
79
|
|
|
foreach ($publications as $filesystemEntity) { |
|
80
|
|
|
$blogPosts[] = $this->getBlobPostData($applicationEntity, $filesystemEntity); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return [ |
|
84
|
|
|
'page' => [ |
|
85
|
|
|
'title' => $titleDate->format('Y4B'), |
|
86
|
|
|
'type' => 'Archive', |
|
87
|
|
|
], |
|
88
|
|
|
'activeMenu' => $date, |
|
89
|
|
|
'blogPosts' => $blogPosts, |
|
90
|
|
|
]; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.