]>
Commit | Line | Data |
---|---|---|
69e29ff6 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
2899ebb5 | 5 | namespace Shaarli\Front\Controller\Visitor; |
69e29ff6 A |
6 | |
7 | use DateTime; | |
c4d5be53 | 8 | use DateTimeImmutable; |
69e29ff6 | 9 | use Shaarli\Bookmark\Bookmark; |
1a8ac737 | 10 | use Shaarli\Render\TemplatePage; |
69e29ff6 A |
11 | use Slim\Http\Request; |
12 | use Slim\Http\Response; | |
13 | ||
14 | /** | |
15 | * Class DailyController | |
16 | * | |
17 | * Slim controller used to render the daily page. | |
69e29ff6 | 18 | */ |
2899ebb5 | 19 | class 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 | { | |
29 | $day = $request->getQueryParam('day') ?? date('Ymd'); | |
30 | ||
31 | $availableDates = $this->container->bookmarkService->days(); | |
32 | $nbAvailableDates = count($availableDates); | |
33 | $index = array_search($day, $availableDates); | |
34 | ||
e3d28be9 | 35 | if ($index === false) { |
69e29ff6 | 36 | // no bookmarks for day, but at least one day with bookmarks |
e3d28be9 A |
37 | $day = $availableDates[$nbAvailableDates - 1] ?? $day; |
38 | $previousDay = $availableDates[$nbAvailableDates - 2] ?? ''; | |
39 | } else { | |
40 | $previousDay = $availableDates[$index - 1] ?? ''; | |
41 | $nextDay = $availableDates[$index + 1] ?? ''; | |
69e29ff6 A |
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 | ||
69e29ff6 A |
50 | try { |
51 | $linksToDisplay = $this->container->bookmarkService->filterDay($day); | |
52 | } catch (\Exception $exc) { | |
53 | $linksToDisplay = []; | |
54 | } | |
55 | ||
56 | $formatter = $this->container->formatterFactory->getFormatter(); | |
57 | // We pre-format some fields for proper output. | |
58 | foreach ($linksToDisplay as $key => $bookmark) { | |
59 | $linksToDisplay[$key] = $formatter->format($bookmark); | |
60 | // This page is a bit specific, we need raw description to calculate the length | |
61 | $linksToDisplay[$key]['formatedDescription'] = $linksToDisplay[$key]['description']; | |
62 | $linksToDisplay[$key]['description'] = $bookmark->getDescription(); | |
63 | } | |
64 | ||
65 | $dayDate = DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $day.'_000000'); | |
66 | $data = [ | |
67 | 'linksToDisplay' => $linksToDisplay, | |
68 | 'day' => $dayDate->getTimestamp(), | |
69 | 'dayDate' => $dayDate, | |
70 | 'previousday' => $previousDay ?? '', | |
71 | 'nextday' => $nextDay ?? '', | |
72 | ]; | |
73 | ||
74 | // Hooks are called before column construction so that plugins don't have to deal with columns. | |
c22fa57a | 75 | $data = $this->executeHooks($data); |
69e29ff6 A |
76 | |
77 | $data['cols'] = $this->calculateColumns($data['linksToDisplay']); | |
78 | ||
79 | foreach ($data as $key => $value) { | |
80 | $this->assignView($key, $value); | |
81 | } | |
82 | ||
83 | $mainTitle = $this->container->conf->get('general.title', 'Shaarli'); | |
84 | $this->assignView( | |
85 | 'pagetitle', | |
86 | t('Daily') .' - '. format_date($dayDate, false) . ' - ' . $mainTitle | |
87 | ); | |
88 | ||
1a8ac737 | 89 | return $response->write($this->render(TemplatePage::DAILY)); |
69e29ff6 A |
90 | } |
91 | ||
c4d5be53 A |
92 | /** |
93 | * Daily RSS feed: 1 RSS entry per day giving all the bookmarks on that day. | |
94 | * Gives the last 7 days (which have bookmarks). | |
95 | * This RSS feed cannot be filtered and does not trigger plugins yet. | |
96 | */ | |
97 | public function rss(Request $request, Response $response): Response | |
98 | { | |
99 | $response = $response->withHeader('Content-Type', 'application/rss+xml; charset=utf-8'); | |
100 | ||
101 | $pageUrl = page_url($this->container->environment); | |
102 | $cache = $this->container->pageCacheManager->getCachePage($pageUrl); | |
103 | ||
104 | $cached = $cache->cachedVersion(); | |
105 | if (!empty($cached)) { | |
106 | return $response->write($cached); | |
107 | } | |
108 | ||
109 | $days = []; | |
110 | foreach ($this->container->bookmarkService->search() as $bookmark) { | |
111 | $day = $bookmark->getCreated()->format('Ymd'); | |
112 | ||
113 | // Stop iterating after DAILY_RSS_NB_DAYS entries | |
114 | if (count($days) === static::$DAILY_RSS_NB_DAYS && !isset($days[$day])) { | |
115 | break; | |
116 | } | |
117 | ||
118 | $days[$day][] = $bookmark; | |
119 | } | |
120 | ||
121 | // Build the RSS feed. | |
122 | $indexUrl = escape(index_url($this->container->environment)); | |
123 | ||
124 | $formatter = $this->container->formatterFactory->getFormatter(); | |
125 | $formatter->addContextData('index_url', $indexUrl); | |
126 | ||
127 | $dataPerDay = []; | |
128 | ||
129 | /** @var Bookmark[] $bookmarks */ | |
130 | foreach ($days as $day => $bookmarks) { | |
131 | $dayDatetime = DateTimeImmutable::createFromFormat(Bookmark::LINK_DATE_FORMAT, $day.'_000000'); | |
132 | $dataPerDay[$day] = [ | |
133 | 'date' => $dayDatetime, | |
134 | 'date_rss' => $dayDatetime->format(DateTime::RSS), | |
135 | 'date_human' => format_date($dayDatetime, false, true), | |
136 | 'absolute_url' => $indexUrl . '/daily?day=' . $day, | |
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()) { | |
145 | $dataPerDay[$day]['links'][$key]['url'] = $indexUrl . $bookmark->getUrl(); | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | $this->assignView('title', $this->container->conf->get('general.title', 'Shaarli')); | |
151 | $this->assignView('index_url', $indexUrl); | |
152 | $this->assignView('page_url', $pageUrl); | |
153 | $this->assignView('hide_timestamps', $this->container->conf->get('privacy.hide_timestamps', false)); | |
154 | $this->assignView('days', $dataPerDay); | |
155 | ||
1a8ac737 | 156 | $rssContent = $this->render(TemplatePage::DAILY_RSS); |
c4d5be53 A |
157 | |
158 | $cache->cache($rssContent); | |
159 | ||
160 | return $response->write($rssContent); | |
161 | } | |
162 | ||
69e29ff6 A |
163 | /** |
164 | * We need to spread the articles on 3 columns. | |
165 | * did not want to use a JavaScript lib like http://masonry.desandro.com/ | |
166 | * so I manually spread entries with a simple method: I roughly evaluate the | |
167 | * height of a div according to title and description length. | |
168 | */ | |
169 | protected function calculateColumns(array $links): array | |
170 | { | |
171 | // Entries to display, for each column. | |
172 | $columns = [[], [], []]; | |
173 | // Rough estimate of columns fill. | |
174 | $fill = [0, 0, 0]; | |
175 | foreach ($links as $link) { | |
176 | // Roughly estimate length of entry (by counting characters) | |
177 | // Title: 30 chars = 1 line. 1 line is 30 pixels height. | |
178 | // Description: 836 characters gives roughly 342 pixel height. | |
179 | // This is not perfect, but it's usually OK. | |
180 | $length = strlen($link['title'] ?? '') + (342 * strlen($link['description'] ?? '')) / 836; | |
181 | if (! empty($link['thumbnail'])) { | |
182 | $length += 100; // 1 thumbnails roughly takes 100 pixels height. | |
183 | } | |
184 | // Then put in column which is the less filled: | |
185 | $smallest = min($fill); // find smallest value in array. | |
186 | $index = array_search($smallest, $fill); // find index of this smallest value. | |
187 | array_push($columns[$index], $link); // Put entry in this column. | |
188 | $fill[$index] += $length; | |
189 | } | |
190 | ||
191 | return $columns; | |
192 | } | |
193 | ||
194 | /** | |
195 | * @param mixed[] $data Variables passed to the template engine | |
196 | * | |
197 | * @return mixed[] Template data after active plugins render_picwall hook execution. | |
198 | */ | |
199 | protected function executeHooks(array $data): array | |
200 | { | |
201 | $this->container->pluginManager->executeHooks( | |
202 | 'render_daily', | |
203 | $data, | |
204 | ['loggedin' => $this->container->loginManager->isLoggedIn()] | |
205 | ); | |
206 | ||
207 | return $data; | |
208 | } | |
209 | } |