diff options
Diffstat (limited to 'application/front/controllers')
-rw-r--r-- | application/front/controllers/DailyController.php | 142 |
1 files changed, 142 insertions, 0 deletions
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 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller; | ||
6 | |||
7 | use DateTime; | ||
8 | use Shaarli\Bookmark\Bookmark; | ||
9 | use Slim\Http\Request; | ||
10 | use Slim\Http\Response; | ||
11 | |||
12 | /** | ||
13 | * Class DailyController | ||
14 | * | ||
15 | * Slim controller used to render the daily page. | ||
16 | * | ||
17 | * @package Front\Controller | ||
18 | */ | ||
19 | class 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 | } | ||