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