aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/front/controller/visitor/DailyController.php
blob: 05b4f095970dc4dc19770d28278568561f73855e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php

declare(strict_types=1);

namespace Shaarli\Front\Controller\Visitor;

use DateTime;
use DateTimeImmutable;
use Shaarli\Bookmark\Bookmark;
use Shaarli\Render\TemplatePage;
use Slim\Http\Request;
use Slim\Http\Response;

/**
 * Class DailyController
 *
 * Slim controller used to render the daily page.
 */
class DailyController extends ShaarliVisitorController
{
    public static $DAILY_RSS_NB_DAYS = 8;

    /**
     * Controller displaying all bookmarks published in a single day.
     * It take a `day` date query parameter (format YYYYMMDD).
     */
    public function index(Request $request, Response $response): Response
    {
        $day = $request->getQueryParam('day') ?? date('Ymd');

        $availableDates = $this->container->bookmarkService->days();
        $nbAvailableDates = count($availableDates);
        $index = array_search($day, $availableDates);

        if ($index === false) {
            // no bookmarks for day, but at least one day with bookmarks
            $day = $availableDates[$nbAvailableDates - 1] ?? $day;
            $previousDay = $availableDates[$nbAvailableDates - 2] ?? '';
        } else {
            $previousDay = $availableDates[$index - 1] ?? '';
            $nextDay = $availableDates[$index + 1] ?? '';
        }

        if ($day === date('Ymd')) {
            $this->assignView('dayDesc', t('Today'));
        } elseif ($day === date('Ymd', strtotime('-1 days'))) {
            $this->assignView('dayDesc', t('Yesterday'));
        }

        try {
            $linksToDisplay = $this->container->bookmarkService->filterDay($day);
        } catch (\Exception $exc) {
            $linksToDisplay = [];
        }

        $formatter = $this->container->formatterFactory->getFormatter();
        // We pre-format some fields for proper output.
        foreach ($linksToDisplay as $key => $bookmark) {
            $linksToDisplay[$key] = $formatter->format($bookmark);
            // This page is a bit specific, we need raw description to calculate the length
            $linksToDisplay[$key]['formatedDescription'] = $linksToDisplay[$key]['description'];
            $linksToDisplay[$key]['description'] = $bookmark->getDescription();
        }

        $dayDate = DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $day.'_000000');
        $data = [
            'linksToDisplay' => $linksToDisplay,
            'day' => $dayDate->getTimestamp(),
            'dayDate' => $dayDate,
            'previousday' => $previousDay ?? '',
            'nextday' => $nextDay ?? '',
        ];

        // Hooks are called before column construction so that plugins don't have to deal with columns.
        $data = $this->executeHooks($data);

        $data['cols'] = $this->calculateColumns($data['linksToDisplay']);

        foreach ($data as $key => $value) {
            $this->assignView($key, $value);
        }

        $mainTitle = $this->container->conf->get('general.title', 'Shaarli');
        $this->assignView(
            'pagetitle',
            t('Daily') .' - '. format_date($dayDate, false) . ' - ' . $mainTitle
        );

        return $response->write($this->render(TemplatePage::DAILY));
    }

    /**
     * Daily RSS feed: 1 RSS entry per day giving all the bookmarks on that day.
     * Gives the last 7 days (which have bookmarks).
     * This RSS feed cannot be filtered and does not trigger plugins yet.
     */
    public function rss(Request $request, Response $response): Response
    {
        $response = $response->withHeader('Content-Type', 'application/rss+xml; charset=utf-8');

        $pageUrl = page_url($this->container->environment);
        $cache = $this->container->pageCacheManager->getCachePage($pageUrl);

        $cached = $cache->cachedVersion();
        if (!empty($cached)) {
            return $response->write($cached);
        }

        $days = [];
        foreach ($this->container->bookmarkService->search() as $bookmark) {
            $day = $bookmark->getCreated()->format('Ymd');

            // Stop iterating after DAILY_RSS_NB_DAYS entries
            if (count($days) === static::$DAILY_RSS_NB_DAYS && !isset($days[$day])) {
                break;
            }

            $days[$day][] = $bookmark;
        }

        // Build the RSS feed.
        $indexUrl = escape(index_url($this->container->environment));

        $formatter = $this->container->formatterFactory->getFormatter();
        $formatter->addContextData('index_url', $indexUrl);

        $dataPerDay = [];

        /** @var Bookmark[] $bookmarks */
        foreach ($days as $day => $bookmarks) {
            $dayDatetime = DateTimeImmutable::createFromFormat(Bookmark::LINK_DATE_FORMAT, $day.'_000000');
            $dataPerDay[$day] = [
                'date' => $dayDatetime,
                'date_rss' => $dayDatetime->format(DateTime::RSS),
                'date_human' => format_date($dayDatetime, false, true),
                'absolute_url' => $indexUrl . '/daily?day=' . $day,
                'links' => [],
            ];

            foreach ($bookmarks as $key => $bookmark) {
                $dataPerDay[$day]['links'][$key] = $formatter->format($bookmark);

                // Make permalink URL absolute
                if ($bookmark->isNote()) {
                    $dataPerDay[$day]['links'][$key]['url'] = $indexUrl . $bookmark->getUrl();
                }
            }
        }

        $this->assignView('title', $this->container->conf->get('general.title', 'Shaarli'));
        $this->assignView('index_url', $indexUrl);
        $this->assignView('page_url', $pageUrl);
        $this->assignView('hide_timestamps', $this->container->conf->get('privacy.hide_timestamps', false));
        $this->assignView('days', $dataPerDay);

        $rssContent = $this->render(TemplatePage::DAILY_RSS);

        $cache->cache($rssContent);

        return $response->write($rssContent);
    }

    /**
     * We need to spread the articles on 3 columns.
     * did not want to use a JavaScript lib like http://masonry.desandro.com/
     * so I manually spread entries with a simple method: I roughly evaluate the
     * height of a div according to title and description length.
     */
    protected function calculateColumns(array $links): array
    {
        // Entries to display, for each column.
        $columns = [[], [], []];
        // Rough estimate of columns fill.
        $fill = [0, 0, 0];
        foreach ($links as $link) {
            // Roughly estimate length of entry (by counting characters)
            // Title: 30 chars = 1 line. 1 line is 30 pixels height.
            // Description: 836 characters gives roughly 342 pixel height.
            // This is not perfect, but it's usually OK.
            $length = strlen($link['title'] ?? '') + (342 * strlen($link['description'] ?? '')) / 836;
            if (! empty($link['thumbnail'])) {
                $length += 100; // 1 thumbnails roughly takes 100 pixels height.
            }
            // Then put in column which is the less filled:
            $smallest = min($fill); // find smallest value in array.
            $index = array_search($smallest, $fill); // find index of this smallest value.
            array_push($columns[$index], $link); // Put entry in this column.
            $fill[$index] += $length;
        }

        return $columns;
    }

    /**
     * @param mixed[] $data Variables passed to the template engine
     *
     * @return mixed[] Template data after active plugins render_picwall hook execution.
     */
    protected function executeHooks(array $data): array
    {
        $this->container->pluginManager->executeHooks(
            'render_daily',
            $data,
            ['loggedin' => $this->container->loginManager->isLoggedIn()]
        );

        return $data;
    }
}