]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/front/controller/visitor/DailyController.php
New basePath: fix officiel plugin paths and vintage template
[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 9use Shaarli\Bookmark\Bookmark;
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 {
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.
9fbc4229 75 $this->executePageHooks('render_daily', $data, TemplatePage::DAILY);
69e29ff6
A
76
77 $data['cols'] = $this->calculateColumns($data['linksToDisplay']);
78
9fbc4229 79 $this->assignAllView($data);
69e29ff6
A
80
81 $mainTitle = $this->container->conf->get('general.title', 'Shaarli');
82 $this->assignView(
83 'pagetitle',
84 t('Daily') .' - '. format_date($dayDate, false) . ' - ' . $mainTitle
85 );
86
1a8ac737 87 return $response->write($this->render(TemplatePage::DAILY));
69e29ff6
A
88 }
89
c4d5be53
A
90 /**
91 * Daily RSS feed: 1 RSS entry per day giving all the bookmarks on that day.
92 * Gives the last 7 days (which have bookmarks).
93 * This RSS feed cannot be filtered and does not trigger plugins yet.
94 */
95 public function rss(Request $request, Response $response): Response
96 {
97 $response = $response->withHeader('Content-Type', 'application/rss+xml; charset=utf-8');
98
99 $pageUrl = page_url($this->container->environment);
100 $cache = $this->container->pageCacheManager->getCachePage($pageUrl);
101
102 $cached = $cache->cachedVersion();
103 if (!empty($cached)) {
104 return $response->write($cached);
105 }
106
107 $days = [];
108 foreach ($this->container->bookmarkService->search() as $bookmark) {
109 $day = $bookmark->getCreated()->format('Ymd');
110
111 // Stop iterating after DAILY_RSS_NB_DAYS entries
112 if (count($days) === static::$DAILY_RSS_NB_DAYS && !isset($days[$day])) {
113 break;
114 }
115
116 $days[$day][] = $bookmark;
117 }
118
119 // Build the RSS feed.
120 $indexUrl = escape(index_url($this->container->environment));
121
122 $formatter = $this->container->formatterFactory->getFormatter();
123 $formatter->addContextData('index_url', $indexUrl);
124
125 $dataPerDay = [];
126
127 /** @var Bookmark[] $bookmarks */
128 foreach ($days as $day => $bookmarks) {
129 $dayDatetime = DateTimeImmutable::createFromFormat(Bookmark::LINK_DATE_FORMAT, $day.'_000000');
130 $dataPerDay[$day] = [
131 'date' => $dayDatetime,
132 'date_rss' => $dayDatetime->format(DateTime::RSS),
133 'date_human' => format_date($dayDatetime, false, true),
134 'absolute_url' => $indexUrl . '/daily?day=' . $day,
135 'links' => [],
136 ];
137
138 foreach ($bookmarks as $key => $bookmark) {
139 $dataPerDay[$day]['links'][$key] = $formatter->format($bookmark);
140
141 // Make permalink URL absolute
142 if ($bookmark->isNote()) {
143 $dataPerDay[$day]['links'][$key]['url'] = $indexUrl . $bookmark->getUrl();
144 }
145 }
146 }
147
148 $this->assignView('title', $this->container->conf->get('general.title', 'Shaarli'));
149 $this->assignView('index_url', $indexUrl);
150 $this->assignView('page_url', $pageUrl);
151 $this->assignView('hide_timestamps', $this->container->conf->get('privacy.hide_timestamps', false));
152 $this->assignView('days', $dataPerDay);
153
1a8ac737 154 $rssContent = $this->render(TemplatePage::DAILY_RSS);
c4d5be53
A
155
156 $cache->cache($rssContent);
157
158 return $response->write($rssContent);
159 }
160
69e29ff6
A
161 /**
162 * We need to spread the articles on 3 columns.
163 * did not want to use a JavaScript lib like http://masonry.desandro.com/
164 * so I manually spread entries with a simple method: I roughly evaluate the
165 * height of a div according to title and description length.
166 */
167 protected function calculateColumns(array $links): array
168 {
169 // Entries to display, for each column.
170 $columns = [[], [], []];
171 // Rough estimate of columns fill.
172 $fill = [0, 0, 0];
173 foreach ($links as $link) {
174 // Roughly estimate length of entry (by counting characters)
175 // Title: 30 chars = 1 line. 1 line is 30 pixels height.
176 // Description: 836 characters gives roughly 342 pixel height.
177 // This is not perfect, but it's usually OK.
178 $length = strlen($link['title'] ?? '') + (342 * strlen($link['description'] ?? '')) / 836;
179 if (! empty($link['thumbnail'])) {
180 $length += 100; // 1 thumbnails roughly takes 100 pixels height.
181 }
182 // Then put in column which is the less filled:
183 $smallest = min($fill); // find smallest value in array.
184 $index = array_search($smallest, $fill); // find index of this smallest value.
185 array_push($columns[$index], $link); // Put entry in this column.
186 $fill[$index] += $length;
187 }
188
189 return $columns;
190 }
69e29ff6 191}