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 && $nbAvailableDates > 0) {
34 // no bookmarks for day, but at least one day with bookmarks
35 $index = $nbAvailableDates - 1;
36 $day = $availableDates[$index];
39 if ($day === date('Ymd')) {
40 $this->assignView('dayDesc', t('Today'));
41 } elseif ($day === date('Ymd', strtotime('-1 days'))) {
42 $this->assignView('dayDesc', t('Yesterday'));
45 if ($index !== false) {
47 $previousDay = $availableDates[$index - 1];
49 if ($index < $nbAvailableDates - 1) {
50 $nextDay = $availableDates[$index +
1];
55 $linksToDisplay = $this->container
->bookmarkService
->filterDay($day);
56 } catch (\Exception
$exc) {
60 $formatter = $this->container
->formatterFactory
->getFormatter();
61 // We pre-format some fields for proper output.
62 foreach ($linksToDisplay as $key => $bookmark) {
63 $linksToDisplay[$key] = $formatter->format($bookmark);
64 // This page is a bit specific, we need raw description to calculate the length
65 $linksToDisplay[$key]['formatedDescription'] = $linksToDisplay[$key]['description'];
66 $linksToDisplay[$key]['description'] = $bookmark->getDescription();
69 $dayDate = DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, $day.'_000000');
71 'linksToDisplay' => $linksToDisplay,
72 'day' => $dayDate->getTimestamp(),
73 'dayDate' => $dayDate,
74 'previousday' => $previousDay ?? '',
75 'nextday' => $nextDay ?? '',
78 // Hooks are called before column construction so that plugins don't have to deal with columns.
79 $this->executeHooks($data);
81 $data['cols'] = $this->calculateColumns($data['linksToDisplay']);
83 foreach ($data as $key => $value) {
84 $this->assignView($key, $value);
87 $mainTitle = $this->container
->conf
->get('general.title', 'Shaarli');
90 t('Daily') .' - '. format_date($dayDate, false) . ' - ' . $mainTitle
93 return $response->write($this->render('daily'));
97 * We need to spread the articles on 3 columns.
98 * did not want to use a JavaScript lib like http://masonry.desandro.com/
99 * so I manually spread entries with a simple method: I roughly evaluate the
100 * height of a div according to title and description length.
102 protected function calculateColumns(array $links): array
104 // Entries to display, for each column.
105 $columns = [[], [], []];
106 // Rough estimate of columns fill.
108 foreach ($links as $link) {
109 // Roughly estimate length of entry (by counting characters)
110 // Title: 30 chars = 1 line. 1 line is 30 pixels height.
111 // Description: 836 characters gives roughly 342 pixel height.
112 // This is not perfect, but it's usually OK.
113 $length = strlen($link['title'] ?? '') +
(342 * strlen($link['description'] ?? '')) / 836;
114 if (! empty($link['thumbnail'])) {
115 $length +
= 100; // 1 thumbnails roughly takes 100 pixels height.
117 // Then put in column which is the less filled:
118 $smallest = min($fill); // find smallest value in array.
119 $index = array_search($smallest, $fill); // find index of this smallest value.
120 array_push($columns[$index], $link); // Put entry in this column.
121 $fill[$index] +
= $length;
128 * @param mixed[] $data Variables passed to the template engine
130 * @return mixed[] Template data after active plugins render_picwall hook execution.
132 protected function executeHooks(array $data): array
134 $this->container
->pluginManager
->executeHooks(
137 ['loggedin' => $this->container
->loginManager
->isLoggedIn()]