]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/front/controller/visitor/DailyController.php
Merge pull request #1697 from ArthurHoaro/feature/pagination
[github/shaarli/Shaarli.git] / application / front / controller / visitor / DailyController.php
CommitLineData
69e29ff6
A
1<?php
2
3declare(strict_types=1);
4
2899ebb5 5namespace Shaarli\Front\Controller\Visitor;
69e29ff6
A
6
7use DateTime;
8use Shaarli\Bookmark\Bookmark;
36e6d88d 9use Shaarli\Helper\DailyPageHelper;
1a8ac737 10use Shaarli\Render\TemplatePage;
69e29ff6
A
11use Slim\Http\Request;
12use Slim\Http\Response;
13
14/**
15 * Class DailyController
16 *
17 * Slim controller used to render the daily page.
69e29ff6 18 */
2899ebb5 19class DailyController extends ShaarliVisitorController
69e29ff6 20{
c4d5be53
A
21 public static $DAILY_RSS_NB_DAYS = 8;
22
69e29ff6
A
23 /**
24 * Controller displaying all bookmarks published in a single day.
25 * It take a `day` date query parameter (format YYYYMMDD).
26 */
27 public function index(Request $request, Response $response): Response
28 {
36e6d88d
A
29 $type = DailyPageHelper::extractRequestedType($request);
30 $format = DailyPageHelper::getFormatByType($type);
31 $latestBookmark = $this->container->bookmarkService->getLatest();
32 $dateTime = DailyPageHelper::extractRequestedDateTime($type, $request->getQueryParam($type), $latestBookmark);
33 $start = DailyPageHelper::getStartDateTimeByType($type, $dateTime);
34 $end = DailyPageHelper::getEndDateTimeByType($type, $dateTime);
35 $dailyDesc = DailyPageHelper::getDescriptionByType($type, $dateTime);
36
37 $linksToDisplay = $this->container->bookmarkService->findByDate(
38 $start,
39 $end,
40 $previousDay,
41 $nextDay
42 );
69e29ff6
A
43
44 $formatter = $this->container->formatterFactory->getFormatter();
301c7ab1 45 $formatter->addContextData('base_path', $this->container->basePath);
69e29ff6
A
46 // We pre-format some fields for proper output.
47 foreach ($linksToDisplay as $key => $bookmark) {
48 $linksToDisplay[$key] = $formatter->format($bookmark);
49 // This page is a bit specific, we need raw description to calculate the length
50 $linksToDisplay[$key]['formatedDescription'] = $linksToDisplay[$key]['description'];
51 $linksToDisplay[$key]['description'] = $bookmark->getDescription();
52 }
53
69e29ff6
A
54 $data = [
55 'linksToDisplay' => $linksToDisplay,
36e6d88d
A
56 'dayDate' => $start,
57 'day' => $start->getTimestamp(),
58 'previousday' => $previousDay ? $previousDay->format($format) : '',
59 'nextday' => $nextDay ? $nextDay->format($format) : '',
60 'dayDesc' => $dailyDesc,
61 'type' => $type,
62 'localizedType' => $this->translateType($type),
69e29ff6
A
63 ];
64
65 // Hooks are called before column construction so that plugins don't have to deal with columns.
9fbc4229 66 $this->executePageHooks('render_daily', $data, TemplatePage::DAILY);
69e29ff6
A
67
68 $data['cols'] = $this->calculateColumns($data['linksToDisplay']);
69
9fbc4229 70 $this->assignAllView($data);
69e29ff6
A
71
72 $mainTitle = $this->container->conf->get('general.title', 'Shaarli');
73 $this->assignView(
74 'pagetitle',
36e6d88d 75 $data['localizedType'] . ' - ' . $data['dayDesc'] . ' - ' . $mainTitle
69e29ff6
A
76 );
77
1a8ac737 78 return $response->write($this->render(TemplatePage::DAILY));
69e29ff6
A
79 }
80
c4d5be53
A
81 /**
82 * Daily RSS feed: 1 RSS entry per day giving all the bookmarks on that day.
83 * Gives the last 7 days (which have bookmarks).
84 * This RSS feed cannot be filtered and does not trigger plugins yet.
85 */
86 public function rss(Request $request, Response $response): Response
87 {
88 $response = $response->withHeader('Content-Type', 'application/rss+xml; charset=utf-8');
f00600a2
A
89 $type = DailyPageHelper::extractRequestedType($request);
90 $cacheDuration = DailyPageHelper::getCacheDatePeriodByType($type);
c4d5be53
A
91
92 $pageUrl = page_url($this->container->environment);
f00600a2 93 $cache = $this->container->pageCacheManager->getCachePage($pageUrl, $cacheDuration);
c4d5be53
A
94
95 $cached = $cache->cachedVersion();
96 if (!empty($cached)) {
97 return $response->write($cached);
98 }
99
100 $days = [];
36e6d88d
A
101 $format = DailyPageHelper::getFormatByType($type);
102 $length = DailyPageHelper::getRssLengthByType($type);
9b8c0a45 103 foreach ($this->container->bookmarkService->search()->getBookmarks() as $bookmark) {
36e6d88d 104 $day = $bookmark->getCreated()->format($format);
c4d5be53
A
105
106 // Stop iterating after DAILY_RSS_NB_DAYS entries
36e6d88d 107 if (count($days) === $length && !isset($days[$day])) {
c4d5be53
A
108 break;
109 }
110
111 $days[$day][] = $bookmark;
112 }
113
114 // Build the RSS feed.
115 $indexUrl = escape(index_url($this->container->environment));
116
117 $formatter = $this->container->formatterFactory->getFormatter();
118 $formatter->addContextData('index_url', $indexUrl);
119
120 $dataPerDay = [];
121
122 /** @var Bookmark[] $bookmarks */
123 foreach ($days as $day => $bookmarks) {
36e6d88d
A
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
c4d5be53 132 $dataPerDay[$day] = [
36e6d88d
A
133 'date' => $endDateTime,
134 'date_rss' => $endDateTime->format(DateTime::RSS),
2883c6d0 135 'date_human' => DailyPageHelper::getDescriptionByType($type, $dayDateTime, false),
53054b2b 136 'absolute_url' => $indexUrl . 'daily?' . $type . '=' . $day,
c4d5be53
A
137 'links' => [],
138 ];
139
140 foreach ($bookmarks as $key => $bookmark) {
141 $dataPerDay[$day]['links'][$key] = $formatter->format($bookmark);
142
143 // Make permalink URL absolute
144 if ($bookmark->isNote()) {
36e6d88d 145 $dataPerDay[$day]['links'][$key]['url'] = rtrim($indexUrl, '/') . $bookmark->getUrl();
c4d5be53
A
146 }
147 }
148 }
149
36e6d88d
A
150 $this->assignAllView([
151 'title' => $this->container->conf->get('general.title', 'Shaarli'),
152 'index_url' => $indexUrl,
153 'page_url' => $pageUrl,
154 'hide_timestamps' => $this->container->conf->get('privacy.hide_timestamps', false),
155 'days' => $dataPerDay,
156 'type' => $type,
157 'localizedType' => $this->translateType($type),
158 ]);
c4d5be53 159
1a8ac737 160 $rssContent = $this->render(TemplatePage::DAILY_RSS);
c4d5be53
A
161
162 $cache->cache($rssContent);
163
164 return $response->write($rssContent);
165 }
166
69e29ff6
A
167 /**
168 * We need to spread the articles on 3 columns.
169 * did not want to use a JavaScript lib like http://masonry.desandro.com/
170 * so I manually spread entries with a simple method: I roughly evaluate the
171 * height of a div according to title and description length.
172 */
173 protected function calculateColumns(array $links): array
174 {
175 // Entries to display, for each column.
176 $columns = [[], [], []];
177 // Rough estimate of columns fill.
178 $fill = [0, 0, 0];
179 foreach ($links as $link) {
180 // Roughly estimate length of entry (by counting characters)
181 // Title: 30 chars = 1 line. 1 line is 30 pixels height.
182 // Description: 836 characters gives roughly 342 pixel height.
183 // This is not perfect, but it's usually OK.
184 $length = strlen($link['title'] ?? '') + (342 * strlen($link['description'] ?? '')) / 836;
185 if (! empty($link['thumbnail'])) {
186 $length += 100; // 1 thumbnails roughly takes 100 pixels height.
187 }
188 // Then put in column which is the less filled:
189 $smallest = min($fill); // find smallest value in array.
190 $index = array_search($smallest, $fill); // find index of this smallest value.
191 array_push($columns[$index], $link); // Put entry in this column.
192 $fill[$index] += $length;
193 }
194
195 return $columns;
196 }
36e6d88d
A
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 }
69e29ff6 206}