3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller
;
9 use Shaarli\Bookmark\Bookmark
;
10 use Slim\Http\Request
;
11 use Slim\Http\Response
;
14 * Class DailyController
16 * Slim controller used to render the daily page.
18 * @package Front\Controller
20 class DailyController
extends ShaarliController
22 public static $DAILY_RSS_NB_DAYS = 8;
25 * Controller displaying all bookmarks published in a single day.
26 * It take a `day` date query parameter (format YYYYMMDD).
28 public function index(Request
$request, Response
$response): Response
30 $day = $request->getQueryParam('day') ?? date('Ymd');
32 $availableDates = $this->container
->bookmarkService
->days();
33 $nbAvailableDates = count($availableDates);
34 $index = array_search($day, $availableDates);
36 if ($index === false) {
37 // no bookmarks for day, but at least one day with bookmarks
38 $day = $availableDates[$nbAvailableDates - 1] ?? $day;
39 $previousDay = $availableDates[$nbAvailableDates - 2] ?? '';
41 $previousDay = $availableDates[$index - 1] ?? '';
42 $nextDay = $availableDates[$index +
1] ?? '';
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'));
52 $linksToDisplay = $this->container
->bookmarkService
->filterDay($day);
53 } catch (\Exception
$exc) {
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();
66 $dayDate = DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, $day.'_000000');
68 'linksToDisplay' => $linksToDisplay,
69 'day' => $dayDate->getTimestamp(),
70 'dayDate' => $dayDate,
71 'previousday' => $previousDay ?? '',
72 'nextday' => $nextDay ?? '',
75 // Hooks are called before column construction so that plugins don't have to deal with columns.
76 $this->executeHooks($data);
78 $data['cols'] = $this->calculateColumns($data['linksToDisplay']);
80 foreach ($data as $key => $value) {
81 $this->assignView($key, $value);
84 $mainTitle = $this->container
->conf
->get('general.title', 'Shaarli');
87 t('Daily') .' - '. format_date($dayDate, false) . ' - ' . $mainTitle
90 return $response->write($this->render('daily'));
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.
98 public function rss(Request
$request, Response
$response): Response
100 $response = $response->withHeader('Content-Type', 'application/rss+xml; charset=utf-8');
102 $pageUrl = page_url($this->container
->environment
);
103 $cache = $this->container
->pageCacheManager
->getCachePage($pageUrl);
105 $cached = $cache->cachedVersion();
106 if (!empty($cached)) {
107 return $response->write($cached);
111 foreach ($this->container
->bookmarkService
->search() as $bookmark) {
112 $day = $bookmark->getCreated()->format('Ymd');
114 // Stop iterating after DAILY_RSS_NB_DAYS entries
115 if (count($days) === static::$DAILY_RSS_NB_DAYS && !isset($days[$day])) {
119 $days[$day][] = $bookmark;
122 // Build the RSS feed.
123 $indexUrl = escape(index_url($this->container
->environment
));
125 $formatter = $this->container
->formatterFactory
->getFormatter();
126 $formatter->addContextData('index_url', $indexUrl);
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,
141 foreach ($bookmarks as $key => $bookmark) {
142 $dataPerDay[$day]['links'][$key] = $formatter->format($bookmark);
144 // Make permalink URL absolute
145 if ($bookmark->isNote()) {
146 $dataPerDay[$day]['links'][$key]['url'] = $indexUrl . $bookmark->getUrl();
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);
157 $rssContent = $this->render('dailyrss');
159 $cache->cache($rssContent);
161 return $response->write($rssContent);
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.
170 protected function calculateColumns(array $links): array
172 // Entries to display, for each column.
173 $columns = [[], [], []];
174 // Rough estimate of columns fill.
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.
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;
196 * @param mixed[] $data Variables passed to the template engine
198 * @return mixed[] Template data after active plugins render_picwall hook execution.
200 protected function executeHooks(array $data): array
202 $this->container
->pluginManager
->executeHooks(
205 ['loggedin' => $this->container
->loginManager
->isLoggedIn()]