diff options
Diffstat (limited to 'application/front/controller/visitor/DailyController.php')
-rw-r--r-- | application/front/controller/visitor/DailyController.php | 108 |
1 files changed, 61 insertions, 47 deletions
diff --git a/application/front/controller/visitor/DailyController.php b/application/front/controller/visitor/DailyController.php index 07617cf1..29492a5f 100644 --- a/application/front/controller/visitor/DailyController.php +++ b/application/front/controller/visitor/DailyController.php | |||
@@ -5,8 +5,8 @@ declare(strict_types=1); | |||
5 | namespace Shaarli\Front\Controller\Visitor; | 5 | namespace Shaarli\Front\Controller\Visitor; |
6 | 6 | ||
7 | use DateTime; | 7 | use DateTime; |
8 | use DateTimeImmutable; | ||
9 | use Shaarli\Bookmark\Bookmark; | 8 | use Shaarli\Bookmark\Bookmark; |
9 | use Shaarli\Helper\DailyPageHelper; | ||
10 | use Shaarli\Render\TemplatePage; | 10 | use Shaarli\Render\TemplatePage; |
11 | use Slim\Http\Request; | 11 | use Slim\Http\Request; |
12 | use Slim\Http\Response; | 12 | use Slim\Http\Response; |
@@ -26,32 +26,20 @@ class DailyController extends ShaarliVisitorController | |||
26 | */ | 26 | */ |
27 | public function index(Request $request, Response $response): Response | 27 | public function index(Request $request, Response $response): Response |
28 | { | 28 | { |
29 | $day = $request->getQueryParam('day') ?? date('Ymd'); | 29 | $type = DailyPageHelper::extractRequestedType($request); |
30 | 30 | $format = DailyPageHelper::getFormatByType($type); | |
31 | $availableDates = $this->container->bookmarkService->days(); | 31 | $latestBookmark = $this->container->bookmarkService->getLatest(); |
32 | $nbAvailableDates = count($availableDates); | 32 | $dateTime = DailyPageHelper::extractRequestedDateTime($type, $request->getQueryParam($type), $latestBookmark); |
33 | $index = array_search($day, $availableDates); | 33 | $start = DailyPageHelper::getStartDateTimeByType($type, $dateTime); |
34 | 34 | $end = DailyPageHelper::getEndDateTimeByType($type, $dateTime); | |
35 | if ($index === false) { | 35 | $dailyDesc = DailyPageHelper::getDescriptionByType($type, $dateTime); |
36 | // no bookmarks for day, but at least one day with bookmarks | 36 | |
37 | $day = $availableDates[$nbAvailableDates - 1] ?? $day; | 37 | $linksToDisplay = $this->container->bookmarkService->findByDate( |
38 | $previousDay = $availableDates[$nbAvailableDates - 2] ?? ''; | 38 | $start, |
39 | } else { | 39 | $end, |
40 | $previousDay = $availableDates[$index - 1] ?? ''; | 40 | $previousDay, |
41 | $nextDay = $availableDates[$index + 1] ?? ''; | 41 | $nextDay |
42 | } | 42 | ); |
43 | |||
44 | if ($day === date('Ymd')) { | ||
45 | $this->assignView('dayDesc', t('Today')); | ||
46 | } elseif ($day === date('Ymd', strtotime('-1 days'))) { | ||
47 | $this->assignView('dayDesc', t('Yesterday')); | ||
48 | } | ||
49 | |||
50 | try { | ||
51 | $linksToDisplay = $this->container->bookmarkService->filterDay($day); | ||
52 | } catch (\Exception $exc) { | ||
53 | $linksToDisplay = []; | ||
54 | } | ||
55 | 43 | ||
56 | $formatter = $this->container->formatterFactory->getFormatter(); | 44 | $formatter = $this->container->formatterFactory->getFormatter(); |
57 | $formatter->addContextData('base_path', $this->container->basePath); | 45 | $formatter->addContextData('base_path', $this->container->basePath); |
@@ -63,13 +51,15 @@ class DailyController extends ShaarliVisitorController | |||
63 | $linksToDisplay[$key]['description'] = $bookmark->getDescription(); | 51 | $linksToDisplay[$key]['description'] = $bookmark->getDescription(); |
64 | } | 52 | } |
65 | 53 | ||
66 | $dayDate = DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $day.'_000000'); | ||
67 | $data = [ | 54 | $data = [ |
68 | 'linksToDisplay' => $linksToDisplay, | 55 | 'linksToDisplay' => $linksToDisplay, |
69 | 'day' => $dayDate->getTimestamp(), | 56 | 'dayDate' => $start, |
70 | 'dayDate' => $dayDate, | 57 | 'day' => $start->getTimestamp(), |
71 | 'previousday' => $previousDay ?? '', | 58 | 'previousday' => $previousDay ? $previousDay->format($format) : '', |
72 | 'nextday' => $nextDay ?? '', | 59 | 'nextday' => $nextDay ? $nextDay->format($format) : '', |
60 | 'dayDesc' => $dailyDesc, | ||
61 | 'type' => $type, | ||
62 | 'localizedType' => $this->translateType($type), | ||
73 | ]; | 63 | ]; |
74 | 64 | ||
75 | // Hooks are called before column construction so that plugins don't have to deal with columns. | 65 | // Hooks are called before column construction so that plugins don't have to deal with columns. |
@@ -82,7 +72,7 @@ class DailyController extends ShaarliVisitorController | |||
82 | $mainTitle = $this->container->conf->get('general.title', 'Shaarli'); | 72 | $mainTitle = $this->container->conf->get('general.title', 'Shaarli'); |
83 | $this->assignView( | 73 | $this->assignView( |
84 | 'pagetitle', | 74 | 'pagetitle', |
85 | t('Daily') .' - '. format_date($dayDate, false) . ' - ' . $mainTitle | 75 | $data['localizedType'] . ' - ' . $data['dayDesc'] . ' - ' . $mainTitle |
86 | ); | 76 | ); |
87 | 77 | ||
88 | return $response->write($this->render(TemplatePage::DAILY)); | 78 | return $response->write($this->render(TemplatePage::DAILY)); |
@@ -96,9 +86,11 @@ class DailyController extends ShaarliVisitorController | |||
96 | public function rss(Request $request, Response $response): Response | 86 | public function rss(Request $request, Response $response): Response |
97 | { | 87 | { |
98 | $response = $response->withHeader('Content-Type', 'application/rss+xml; charset=utf-8'); | 88 | $response = $response->withHeader('Content-Type', 'application/rss+xml; charset=utf-8'); |
89 | $type = DailyPageHelper::extractRequestedType($request); | ||
90 | $cacheDuration = DailyPageHelper::getCacheDatePeriodByType($type); | ||
99 | 91 | ||
100 | $pageUrl = page_url($this->container->environment); | 92 | $pageUrl = page_url($this->container->environment); |
101 | $cache = $this->container->pageCacheManager->getCachePage($pageUrl); | 93 | $cache = $this->container->pageCacheManager->getCachePage($pageUrl, $cacheDuration); |
102 | 94 | ||
103 | $cached = $cache->cachedVersion(); | 95 | $cached = $cache->cachedVersion(); |
104 | if (!empty($cached)) { | 96 | if (!empty($cached)) { |
@@ -106,11 +98,13 @@ class DailyController extends ShaarliVisitorController | |||
106 | } | 98 | } |
107 | 99 | ||
108 | $days = []; | 100 | $days = []; |
101 | $format = DailyPageHelper::getFormatByType($type); | ||
102 | $length = DailyPageHelper::getRssLengthByType($type); | ||
109 | foreach ($this->container->bookmarkService->search() as $bookmark) { | 103 | foreach ($this->container->bookmarkService->search() as $bookmark) { |
110 | $day = $bookmark->getCreated()->format('Ymd'); | 104 | $day = $bookmark->getCreated()->format($format); |
111 | 105 | ||
112 | // Stop iterating after DAILY_RSS_NB_DAYS entries | 106 | // Stop iterating after DAILY_RSS_NB_DAYS entries |
113 | if (count($days) === static::$DAILY_RSS_NB_DAYS && !isset($days[$day])) { | 107 | if (count($days) === $length && !isset($days[$day])) { |
114 | break; | 108 | break; |
115 | } | 109 | } |
116 | 110 | ||
@@ -127,12 +121,19 @@ class DailyController extends ShaarliVisitorController | |||
127 | 121 | ||
128 | /** @var Bookmark[] $bookmarks */ | 122 | /** @var Bookmark[] $bookmarks */ |
129 | foreach ($days as $day => $bookmarks) { | 123 | foreach ($days as $day => $bookmarks) { |
130 | $dayDatetime = DateTimeImmutable::createFromFormat(Bookmark::LINK_DATE_FORMAT, $day.'_000000'); | 124 | $dayDateTime = DailyPageHelper::extractRequestedDateTime($type, (string) $day); |
125 | $endDateTime = DailyPageHelper::getEndDateTimeByType($type, $dayDateTime); | ||
126 | |||
127 | // We only want the RSS entry to be published when the period is over. | ||
128 | if (new DateTime() < $endDateTime) { | ||
129 | continue; | ||
130 | } | ||
131 | |||
131 | $dataPerDay[$day] = [ | 132 | $dataPerDay[$day] = [ |
132 | 'date' => $dayDatetime, | 133 | 'date' => $endDateTime, |
133 | 'date_rss' => $dayDatetime->format(DateTime::RSS), | 134 | 'date_rss' => $endDateTime->format(DateTime::RSS), |
134 | 'date_human' => format_date($dayDatetime, false, true), | 135 | 'date_human' => DailyPageHelper::getDescriptionByType($type, $dayDateTime, false), |
135 | 'absolute_url' => $indexUrl . 'daily?day=' . $day, | 136 | 'absolute_url' => $indexUrl . 'daily?' . $type . '=' . $day, |
136 | 'links' => [], | 137 | 'links' => [], |
137 | ]; | 138 | ]; |
138 | 139 | ||
@@ -141,16 +142,20 @@ class DailyController extends ShaarliVisitorController | |||
141 | 142 | ||
142 | // Make permalink URL absolute | 143 | // Make permalink URL absolute |
143 | if ($bookmark->isNote()) { | 144 | if ($bookmark->isNote()) { |
144 | $dataPerDay[$day]['links'][$key]['url'] = $indexUrl . $bookmark->getUrl(); | 145 | $dataPerDay[$day]['links'][$key]['url'] = rtrim($indexUrl, '/') . $bookmark->getUrl(); |
145 | } | 146 | } |
146 | } | 147 | } |
147 | } | 148 | } |
148 | 149 | ||
149 | $this->assignView('title', $this->container->conf->get('general.title', 'Shaarli')); | 150 | $this->assignAllView([ |
150 | $this->assignView('index_url', $indexUrl); | 151 | 'title' => $this->container->conf->get('general.title', 'Shaarli'), |
151 | $this->assignView('page_url', $pageUrl); | 152 | 'index_url' => $indexUrl, |
152 | $this->assignView('hide_timestamps', $this->container->conf->get('privacy.hide_timestamps', false)); | 153 | 'page_url' => $pageUrl, |
153 | $this->assignView('days', $dataPerDay); | 154 | 'hide_timestamps' => $this->container->conf->get('privacy.hide_timestamps', false), |
155 | 'days' => $dataPerDay, | ||
156 | 'type' => $type, | ||
157 | 'localizedType' => $this->translateType($type), | ||
158 | ]); | ||
154 | 159 | ||
155 | $rssContent = $this->render(TemplatePage::DAILY_RSS); | 160 | $rssContent = $this->render(TemplatePage::DAILY_RSS); |
156 | 161 | ||
@@ -189,4 +194,13 @@ class DailyController extends ShaarliVisitorController | |||
189 | 194 | ||
190 | return $columns; | 195 | return $columns; |
191 | } | 196 | } |
197 | |||
198 | protected function translateType($type): string | ||
199 | { | ||
200 | return [ | ||
201 | t('day') => t('Daily'), | ||
202 | t('week') => t('Weekly'), | ||
203 | t('month') => t('Monthly'), | ||
204 | ][t($type)] ?? t('Daily'); | ||
205 | } | ||
192 | } | 206 | } |