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