aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-05-17 11:06:39 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commit69e29ff65ef56b886748c58ba5b037cf217c4a1d (patch)
treeab27ad9100bc72cb3c9417b623776bc8cfa6d421 /application
parent60ae241251b753fc052e50ebd95277dfcb074cb0 (diff)
downloadShaarli-69e29ff65ef56b886748c58ba5b037cf217c4a1d.tar.gz
Shaarli-69e29ff65ef56b886748c58ba5b037cf217c4a1d.tar.zst
Shaarli-69e29ff65ef56b886748c58ba5b037cf217c4a1d.zip
Process daily page through Slim controller
Diffstat (limited to 'application')
-rw-r--r--application/Utils.php8
-rw-r--r--application/bookmark/BookmarkFilter.php2
-rw-r--r--application/front/controllers/DailyController.php142
3 files changed, 147 insertions, 5 deletions
diff --git a/application/Utils.php b/application/Utils.php
index 4e97cdda..72c90049 100644
--- a/application/Utils.php
+++ b/application/Utils.php
@@ -294,15 +294,15 @@ function normalize_spaces($string)
294 * Requires php-intl to display international datetimes, 294 * Requires php-intl to display international datetimes,
295 * otherwise default format '%c' will be returned. 295 * otherwise default format '%c' will be returned.
296 * 296 *
297 * @param DateTime $date to format. 297 * @param DateTimeInterface $date to format.
298 * @param bool $time Displays time if true. 298 * @param bool $time Displays time if true.
299 * @param bool $intl Use international format if true. 299 * @param bool $intl Use international format if true.
300 * 300 *
301 * @return bool|string Formatted date, or false if the input is invalid. 301 * @return bool|string Formatted date, or false if the input is invalid.
302 */ 302 */
303function format_date($date, $time = true, $intl = true) 303function format_date($date, $time = true, $intl = true)
304{ 304{
305 if (! $date instanceof DateTime) { 305 if (! $date instanceof DateTimeInterface) {
306 return false; 306 return false;
307 } 307 }
308 308
diff --git a/application/bookmark/BookmarkFilter.php b/application/bookmark/BookmarkFilter.php
index fd556679..797a36b8 100644
--- a/application/bookmark/BookmarkFilter.php
+++ b/application/bookmark/BookmarkFilter.php
@@ -436,7 +436,7 @@ class BookmarkFilter
436 throw new Exception('Invalid date format'); 436 throw new Exception('Invalid date format');
437 } 437 }
438 438
439 $filtered = array(); 439 $filtered = [];
440 foreach ($this->bookmarks as $key => $l) { 440 foreach ($this->bookmarks as $key => $l) {
441 if ($l->getCreated()->format('Ymd') == $day) { 441 if ($l->getCreated()->format('Ymd') == $day) {
442 $filtered[$key] = $l; 442 $filtered[$key] = $l;
diff --git a/application/front/controllers/DailyController.php b/application/front/controllers/DailyController.php
new file mode 100644
index 00000000..c2fdaa55
--- /dev/null
+++ b/application/front/controllers/DailyController.php
@@ -0,0 +1,142 @@
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
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];
37 }
38
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'));
43 }
44
45 if ($index !== false) {
46 if ($index >= 1) {
47 $previousDay = $availableDates[$index - 1];
48 }
49 if ($index < $nbAvailableDates - 1) {
50 $nextDay = $availableDates[$index + 1];
51 }
52 }
53
54 try {
55 $linksToDisplay = $this->container->bookmarkService->filterDay($day);
56 } catch (\Exception $exc) {
57 $linksToDisplay = [];
58 }
59
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();
67 }
68
69 $dayDate = DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $day.'_000000');
70 $data = [
71 'linksToDisplay' => $linksToDisplay,
72 'day' => $dayDate->getTimestamp(),
73 'dayDate' => $dayDate,
74 'previousday' => $previousDay ?? '',
75 'nextday' => $nextDay ?? '',
76 ];
77
78 // Hooks are called before column construction so that plugins don't have to deal with columns.
79 $this->executeHooks($data);
80
81 $data['cols'] = $this->calculateColumns($data['linksToDisplay']);
82
83 foreach ($data as $key => $value) {
84 $this->assignView($key, $value);
85 }
86
87 $mainTitle = $this->container->conf->get('general.title', 'Shaarli');
88 $this->assignView(
89 'pagetitle',
90 t('Daily') .' - '. format_date($dayDate, false) . ' - ' . $mainTitle
91 );
92
93 return $response->write($this->render('daily'));
94 }
95
96 /**
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.
101 */
102 protected function calculateColumns(array $links): array
103 {
104 // Entries to display, for each column.
105 $columns = [[], [], []];
106 // Rough estimate of columns fill.
107 $fill = [0, 0, 0];
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.
116 }
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;
122 }
123
124 return $columns;
125 }
126
127 /**
128 * @param mixed[] $data Variables passed to the template engine
129 *
130 * @return mixed[] Template data after active plugins render_picwall hook execution.
131 */
132 protected function executeHooks(array $data): array
133 {
134 $this->container->pluginManager->executeHooks(
135 'render_daily',
136 $data,
137 ['loggedin' => $this->container->loginManager->isLoggedIn()]
138 );
139
140 return $data;
141 }
142}