Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
16.67% covered (danger)
16.67%
33 / 198
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
OlzNewsDetailParams
n/a
0 / 0
n/a
0 / 0
0
n/a
0 / 0
OlzNewsDetail
16.67% covered (danger)
16.67%
33 / 198
33.33% covered (danger)
33.33%
2 / 6
663.21
0.00% covered (danger)
0.00%
0 / 1
 hasAccess
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 searchSqlWhenHasAccess
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
1 / 1
1
 getPageTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPageDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHtmlWhenHasAccess
0.00% covered (danger)
0.00%
0 / 158
0.00% covered (danger)
0.00%
0 / 1
812
 getNewsEntryById
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3// =============================================================================
4// Alle Neuigkeiten rund um die OL Zimmerberg
5// =============================================================================
6
7namespace Olz\News\Components\OlzNewsDetail;
8
9use Doctrine\Common\Collections\Criteria;
10use Olz\Components\Common\OlzRootComponent;
11use Olz\Components\Page\OlzFooter\OlzFooter;
12use Olz\Components\Page\OlzHeader\OlzHeader;
13use Olz\Entity\News\NewsEntry;
14use Olz\News\Components\OlzArticleMetadata\OlzArticleMetadata;
15use Olz\News\Components\OlzAuthorBadge\OlzAuthorBadge;
16use Olz\News\Utils\NewsUtils;
17use Olz\Utils\HttpParams;
18
19/** @extends HttpParams<array{von?: ?string}> */
20class OlzNewsDetailParams extends HttpParams {
21}
22
23/** @extends OlzRootComponent<array<string, mixed>> */
24class OlzNewsDetail extends OlzRootComponent {
25    public function hasAccess(): bool {
26        return true;
27    }
28
29    public function searchSqlWhenHasAccess(array $terms): string|array|null {
30        $code_href = $this->envUtils()->getCodeHref();
31        $today_iso = $this->dateUtils()->getIsoToday();
32        $pretty_format_sql = "CASE ".implode('', array_map(function ($entry) {
33            $esc_ident = $this->generalUtils()->internalSqlEscape($entry['ident']);
34            $esc_name = $this->generalUtils()->internalSqlEscape($entry['name']);
35            return "WHEN format = '{$esc_ident}' THEN '{$esc_name}'";
36        }, NewsUtils::ALL_FORMAT_OPTIONS))." ELSE format END";
37        $where = implode(' AND ', array_map(function ($term) {
38            $date_sql = $this->searchUtils()->getDateSql('published_date', $term) ?? '0';
39            return <<<ZZZZZZZZZZ
40                (
41                    title LIKE '%{$term}%'
42                    OR teaser LIKE '%{$term}%'
43                    OR content LIKE '%{$term}%'
44                    OR {$date_sql}
45                )
46                ZZZZZZZZZZ;
47        }, $terms));
48        return [
49            'with' => [
50                <<<ZZZZZZZZZZ
51                    base_news AS (
52                        SELECT
53                            CONCAT('{$code_href}news/', id) AS link,
54                            CONCAT('{$code_href}assets/icns/entry_type_', format, '_20.svg') AS icon,
55                            published_date AS date,
56                            CONCAT('News (', {$pretty_format_sql}, '): ', title) AS title,
57                            CONCAT(IFNULL(teaser, ''), ' ', IFNULL(content, '')) AS text,
58                            DATEDIFF(published_date, '{$today_iso}') AS diffdays
59                        FROM news
60                        WHERE
61                            on_off = '1'
62                            AND {$this->newsUtils()->getIsNotArchivedSql()}
63                            AND {$where}
64                    )
65                    ZZZZZZZZZZ,
66            ],
67            'query' => <<<'ZZZZZZZZZZ'
68                    SELECT
69                        link, icon, date, title, text,
70                        CASE
71                            WHEN diffdays < -400 THEN 0.7
72                            WHEN diffdays < -100 THEN 1.0 + (diffdays + 100) * 0.3 / 300.0
73                            WHEN diffdays < 100 THEN 1.0
74                            ELSE 0.1
75                        END AS time_relevance
76                    FROM base_news
77                ZZZZZZZZZZ,
78        ];
79    }
80
81    public function getPageTitle(): string {
82        return "";
83    }
84
85    public function getPageDescription(): string {
86        return "";
87    }
88
89    public function getHtmlWhenHasAccess(mixed $args): string {
90        $this->httpUtils()->validateGetParams(OlzNewsDetailParams::class);
91        $code_href = $this->envUtils()->getCodeHref();
92        $db = $this->dbUtils()->getDb();
93        $entityManager = $this->dbUtils()->getEntityManager();
94        $user = $this->authUtils()->getCurrentUser();
95        $id = $args['id'] ?? null;
96
97        $news_repo = $entityManager->getRepository(NewsEntry::class);
98        $is_not_archived = $this->newsUtils()->getIsNotArchivedCriteria();
99        $criteria = Criteria::create()
100            ->where(Criteria::expr()->andX(
101                $is_not_archived,
102                Criteria::expr()->eq('id', $id),
103                Criteria::expr()->eq('on_off', 1),
104            ))
105            ->setFirstResult(0)
106            ->setMaxResults(1)
107        ;
108        $news_entries = $news_repo->matching($criteria);
109        $num_news_entries = $news_entries->count();
110        $is_archived = $num_news_entries !== 1;
111
112        if ($is_archived && !$this->authUtils()->hasPermission('any')) {
113            $this->httpUtils()->dieWithHttpError(404);
114            throw new \Exception('should already have failed');
115        }
116
117        $article_metadata = "";
118        try {
119            $article_metadata = OlzArticleMetadata::render(['id' => $id]);
120        } catch (\Exception $exc) {
121            $this->httpUtils()->dieWithHttpError(404);
122            throw new \Exception('should already have failed');
123        }
124
125        $news_entry = $this->getNewsEntryById($id);
126
127        if (!$news_entry) {
128            $this->httpUtils()->dieWithHttpError(404);
129            throw new \Exception('should already have failed');
130        }
131
132        $format = $news_entry->getFormat();
133        $title = $news_entry->getTitle();
134        $teaser = $news_entry->getTeaser() ?? '';
135        $content = $news_entry->getContent() ?? '';
136        $published_date = $news_entry->getPublishedDate();
137
138        // Markdown
139        // TODO: Do NOT ever allow!
140        $html_input = ($format === 'forum' || $published_date->format('Y') > '2020')
141            ? 'escape' : 'allow';
142        $teaser = $this->htmlUtils()->renderMarkdown($teaser, [
143            'html_input' => $html_input,
144        ]);
145        $content = $this->htmlUtils()->renderMarkdown($content, [
146            'html_input' => $html_input,
147        ]);
148
149        // Datei- & Bildpfade
150        $teaser = $news_entry->replaceImagePaths($teaser);
151        $teaser = $news_entry->replaceFilePaths($teaser);
152        $content = $news_entry->replaceImagePaths($content);
153        $content = $news_entry->replaceFilePaths($content);
154
155        $description = trim(strip_tags($teaser)) ?: trim(strip_tags($content));
156        $out = OlzHeader::render([
157            'back_link' => "{$code_href}news",
158            'title' => "{$title} - News",
159            'description' => $description,
160            'norobots' => $is_archived,
161            'canonical_url' => "{$code_href}news/{$id}",
162            'additional_headers' => [
163                $article_metadata,
164            ],
165        ]);
166
167        $found_entry = array_find(
168            NewsUtils::ALL_FORMAT_OPTIONS,
169            fn ($entry) => $entry['ident'] === $format,
170        );
171        $this->generalUtils()->checkNotNull($found_entry, "No such format: {$format}");
172        $name = $found_entry['name'];
173        $icon = $found_entry['icon'] ?? null;
174        $icon_html = "<img src='{$code_href}assets/icns/{$icon}' alt='' class='format-icon'>";
175        $pretty_format = "{$icon_html}{$name}";
176
177        $pretty_date = $this->dateUtils()->olzDate("tt.mm.jjjj", $news_entry->getPublishedDate());
178        $author_user = $news_entry->getAuthorUser();
179        $author_role = $news_entry->getAuthorRole();
180        $author_name = $news_entry->getAuthorName();
181        $author_email = $news_entry->getAuthorEmail();
182        $pretty_author = OlzAuthorBadge::render([
183            'news_id' => $news_entry->getId() ?: 0,
184            'user' => $author_user,
185            'role' => $author_role,
186            'name' => $author_name,
187            'email' => $author_email,
188        ]);
189        $image_ids = $news_entry->getImageIds();
190        $num_images = count($image_ids);
191        $download_all_link = $this->authUtils()->hasPermission('any')
192            ? "<a href='{$code_href}news/{$id}/all.zip'>Alle herunterladen</a>" : '';
193
194        $out .= <<<ZZZZZZZZZZ
195            <div class='content-right'>
196                <div style='padding:4px 3px 10px 3px;'>
197                    <div id='format-info'><b>Format: </b>{$pretty_format}</div>
198                    <div><b>Datum: </b>{$pretty_date}</div>
199                    <div><b>Autor: </b>{$pretty_author}</div>
200                    <div><b>Anzahl Bilder: </b>{$num_images}</div>
201                    <div class='pretty'>{$download_all_link}</div>
202                </div>
203            </div>
204            <div class='content-middle'>
205            ZZZZZZZZZZ;
206
207        $db->query("UPDATE news SET `counter`=`counter` + 1 WHERE `id`='{$id}'");
208
209        $published_date = $this->dateUtils()->olzDate("tt.mm.jj", $published_date);
210
211        $is_owner = $user && intval($news_entry->getOwnerUser()?->getId() ?? 0) === intval($user->getId());
212        $has_news_permissions = $this->authUtils()->hasPermission('news');
213        $can_edit = $is_owner || $has_news_permissions;
214        $edit_admin = '';
215        if ($can_edit) {
216            $json_id = json_encode($id);
217            $has_blog = $this->authUtils()->hasPermission('kaderblog', $user);
218            $has_roles = !empty($this->authUtils()->getAuthenticatedRoles());
219            $json_mode = htmlentities(json_encode($has_roles ? ($has_blog ? 'account_with_all' : 'account_with_aktuell') : ($has_blog ? 'account_with_blog' : 'account')) ?: '');
220            $edit_admin = <<<ZZZZZZZZZZ
221                <div>
222                    <button
223                        id='edit-news-button'
224                        class='btn btn-primary'
225                        onclick='return olz.editNews({$json_id}{$json_mode})'
226                    >
227                        <img src='{$code_href}assets/icns/edit_white_16.svg' class='noborder' />
228                        Bearbeiten
229                    </button>
230                </div>
231                ZZZZZZZZZZ;
232        }
233
234        $out .= "<h1>{$edit_admin}{$title}</h1>";
235
236        $gallery = '';
237        $num_images = count($image_ids);
238        if ($num_images > 0) {
239            $gallery .= "<div class='lightgallery gallery-container'>";
240            foreach ($image_ids as $image_id) {
241                $gallery .= "<div class='gallery-image'>";
242                $gallery .= $this->imageUtils()->olzImage(
243                    'news',
244                    $id,
245                    $image_id,
246                    128,
247                    'gallery[myset]'
248                );
249                $gallery .= "</div>";
250            }
251            $gallery .= "</div>";
252        }
253
254        if ($format === 'aktuell') {
255            $out .= "<p><b>{$teaser}</b><p>{$content}</p><br/><br/>{$gallery}\n";
256        } elseif ($format === 'kaderblog') {
257            $out .= "<p>{$content}</p><br/><br/>{$gallery}\n";
258        } elseif ($format === 'forum') {
259            $out .= "<p><b>{$teaser}</b><p>{$content}</p><br/><br/>{$gallery}\n";
260        } elseif ($format === 'galerie') {
261            $out .= "<p>{$content}</p>{$gallery}\n";
262        } elseif ($format === 'video') {
263            $youtube_url = $news_entry->getExternalUrl() ?? '';
264            $res0 = preg_match("/^https\\:\\/\\/(www\\.)?youtu\\.be\\/([a-zA-Z0-9\\-\\_]{6,})/", $youtube_url, $matches0);
265            $res1 = preg_match("/^https\\:\\/\\/(www\\.)?youtube\\.com\\/watch\\?v\\=([a-zA-Z0-9\\-\\_]{6,})/", $youtube_url, $matches1);
266            $youtube_match = null;
267            if ($res0) {
268                $youtube_match = $matches0[2];
269            }
270            if ($res1) {
271                $youtube_match = $matches1[2];
272            }
273
274            $out .= "<div class='video-container'>";
275            $out .= "<div style='background-image:url({$code_href}assets/icns/movie_dot.svg);background-repeat:repeat-x;margin:0px;padding:0px;height:24px;'></div>\n";
276            if ($youtube_match != null) {
277                $out .= "<iframe width='560' height='315' src='https://www.youtube.com/embed/{$youtube_match}' frameborder='0' allow='accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture' allowfullscreen></iframe>";
278            } else {
279                $this->log()->error("Invalid YouTube link (ID:{$id}): {$youtube_url}");
280                $out .= "Fehlerhafter YouTube-Link!";
281            }
282            $out .= "<div style='background-image:url({$code_href}assets/icns/movie_dot.svg);background-repeat:repeat-x;margin:0px;padding:0px;height:24px;'></div>";
283            $out .= "</div>";
284        } else {
285            $out .= "<div class='lightgallery'><p><b>{$teaser}</b><p>{$content}</p></div>\n";
286        }
287
288        // Reactions
289        $json_id = json_encode($id);
290        $linked_reactions = $this->htmlUtils()->getLinkedReactions("{$teaser} {$content}");
291        $json_reactions = json_encode($linked_reactions) ?: '[]';
292        $out .= "<div id='news-reactions'></div><script>olz.initNewsReactions({$json_id}{$json_reactions});</script>";
293
294        $out .= "</div>"; // content-middle
295
296        $out .= OlzFooter::render();
297
298        return $out;
299    }
300
301    protected function getNewsEntryById(int $id): ?NewsEntry {
302        $news_repo = $this->entityManager()->getRepository(NewsEntry::class);
303        return $news_repo->findOneBy([
304            'id' => $id,
305            'on_off' => 1,
306        ]);
307    }
308}