]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/front/controllers/DailyController.php
Slim daily: minor bugfix with empty data
[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;
8use Shaarli\Bookmark\Bookmark;
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12/**
13 * Class DailyController
14 *
15 * Slim controller used to render the daily page.
16 *
17 * @package Front\Controller
18 */
19class DailyController extends ShaarliController
20{
21 /**
22 * Controller displaying all bookmarks published in a single day.
23 * It take a `day` date query parameter (format YYYYMMDD).
24 */
25 public function index(Request $request, Response $response): Response
26 {
27 $day = $request->getQueryParam('day') ?? date('Ymd');
28
29 $availableDates = $this->container->bookmarkService->days();
30 $nbAvailableDates = count($availableDates);
31 $index = array_search($day, $availableDates);
32
e3d28be9 33 if ($index === false) {
69e29ff6 34 // no bookmarks for day, but at least one day with bookmarks
e3d28be9
A
35 $day = $availableDates[$nbAvailableDates - 1] ?? $day;
36 $previousDay = $availableDates[$nbAvailableDates - 2] ?? '';
37 } else {
38 $previousDay = $availableDates[$index - 1] ?? '';
39 $nextDay = $availableDates[$index + 1] ?? '';
69e29ff6
A
40 }
41
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'));
46 }
47
69e29ff6
A
48 try {
49 $linksToDisplay = $this->container->bookmarkService->filterDay($day);
50 } catch (\Exception $exc) {
51 $linksToDisplay = [];
52 }
53
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();
61 }
62
63 $dayDate = DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $day.'_000000');
64 $data = [
65 'linksToDisplay' => $linksToDisplay,
66 'day' => $dayDate->getTimestamp(),
67 'dayDate' => $dayDate,
68 'previousday' => $previousDay ?? '',
69 'nextday' => $nextDay ?? '',
70 ];
71
72 // Hooks are called before column construction so that plugins don't have to deal with columns.
73 $this->executeHooks($data);
74
75 $data['cols'] = $this->calculateColumns($data['linksToDisplay']);
76
77 foreach ($data as $key => $value) {
78 $this->assignView($key, $value);
79 }
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
87 return $response->write($this->render('daily'));
88 }
89
90 /**
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.
95 */
96 protected function calculateColumns(array $links): array
97 {
98 // Entries to display, for each column.
99 $columns = [[], [], []];
100 // Rough estimate of columns fill.
101 $fill = [0, 0, 0];
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.
110 }
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;
116 }
117
118 return $columns;
119 }
120
121 /**
122 * @param mixed[] $data Variables passed to the template engine
123 *
124 * @return mixed[] Template data after active plugins render_picwall hook execution.
125 */
126 protected function executeHooks(array $data): array
127 {
128 $this->container->pluginManager->executeHooks(
129 'render_daily',
130 $data,
131 ['loggedin' => $this->container->loginManager->isLoggedIn()]
132 );
133
134 return $data;
135 }
136}