3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller
;
8 use Shaarli\Bookmark\Bookmark
;
10 use Slim\Http\Response
;
13 * Class DailyController
15 * Slim controller used to render the daily page.
17 * @package Front\Controller
19 class DailyController
extends ShaarliController
22 * Controller displaying all bookmarks published in a single day.
23 * It take a `day` date query parameter (format YYYYMMDD).
25 public function index(Request
$request, Response
$response): Response
27 $day = $request->getQueryParam('day') ?? date('Ymd');
29 $availableDates = $this->container
->bookmarkService
->days();
30 $nbAvailableDates = count($availableDates);
31 $index = array_search($day, $availableDates);
33 if ($index === false) {
34 // no bookmarks for day, but at least one day with bookmarks
35 $day = $availableDates[$nbAvailableDates - 1] ?? $day;
36 $previousDay = $availableDates[$nbAvailableDates - 2] ?? '';
38 $previousDay = $availableDates[$index - 1] ?? '';
39 $nextDay = $availableDates[$index +
1] ?? '';
42 if ($day === date('Ymd')) {
43 $this->assignView('dayDesc', t('Today'));
44 } elseif ($day === date('Ymd', strtotime('-1 days'))) {
45 $this->assignView('dayDesc', t('Yesterday'));
49 $linksToDisplay = $this->container
->bookmarkService
->filterDay($day);
50 } catch (\Exception
$exc) {
54 $formatter = $this->container
->formatterFactory
->getFormatter();
55 // We pre-format some fields for proper output.
56 foreach ($linksToDisplay as $key => $bookmark) {
57 $linksToDisplay[$key] = $formatter->format($bookmark);
58 // This page is a bit specific, we need raw description to calculate the length
59 $linksToDisplay[$key]['formatedDescription'] = $linksToDisplay[$key]['description'];
60 $linksToDisplay[$key]['description'] = $bookmark->getDescription();
63 $dayDate = DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, $day.'_000000');
65 'linksToDisplay' => $linksToDisplay,
66 'day' => $dayDate->getTimestamp(),
67 'dayDate' => $dayDate,
68 'previousday' => $previousDay ?? '',
69 'nextday' => $nextDay ?? '',
72 // Hooks are called before column construction so that plugins don't have to deal with columns.
73 $this->executeHooks($data);
75 $data['cols'] = $this->calculateColumns($data['linksToDisplay']);
77 foreach ($data as $key => $value) {
78 $this->assignView($key, $value);
81 $mainTitle = $this->container
->conf
->get('general.title', 'Shaarli');
84 t('Daily') .' - '. format_date($dayDate, false) . ' - ' . $mainTitle
87 return $response->write($this->render('daily'));
91 * We need to spread the articles on 3 columns.
92 * did not want to use a JavaScript lib like http://masonry.desandro.com/
93 * so I manually spread entries with a simple method: I roughly evaluate the
94 * height of a div according to title and description length.
96 protected function calculateColumns(array $links): array
98 // Entries to display, for each column.
99 $columns = [[], [], []];
100 // Rough estimate of columns fill.
102 foreach ($links as $link) {
103 // Roughly estimate length of entry (by counting characters)
104 // Title: 30 chars = 1 line. 1 line is 30 pixels height.
105 // Description: 836 characters gives roughly 342 pixel height.
106 // This is not perfect, but it's usually OK.
107 $length = strlen($link['title'] ?? '') +
(342 * strlen($link['description'] ?? '')) / 836;
108 if (! empty($link['thumbnail'])) {
109 $length +
= 100; // 1 thumbnails roughly takes 100 pixels height.
111 // Then put in column which is the less filled:
112 $smallest = min($fill); // find smallest value in array.
113 $index = array_search($smallest, $fill); // find index of this smallest value.
114 array_push($columns[$index], $link); // Put entry in this column.
115 $fill[$index] +
= $length;
122 * @param mixed[] $data Variables passed to the template engine
124 * @return mixed[] Template data after active plugins render_picwall hook execution.
126 protected function executeHooks(array $data): array
128 $this->container
->pluginManager
->executeHooks(
131 ['loggedin' => $this->container
->loginManager
->isLoggedIn()]