Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 177 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
OlzNewsDetailParams | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | n/a |
0 / 0 |
|||
OlzNewsDetail | |
0.00% |
0 / 177 |
|
0.00% |
0 / 4 |
992 | |
0.00% |
0 / 1 |
getSearchTitle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSearchResults | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
getHtml | |
0.00% |
0 / 157 |
|
0.00% |
0 / 1 |
650 | |||
getNewsEntryById | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | // ============================================================================= |
4 | // Alle Neuigkeiten rund um die OL Zimmerberg |
5 | // ============================================================================= |
6 | |
7 | namespace Olz\News\Components\OlzNewsDetail; |
8 | |
9 | use Doctrine\Common\Collections\Criteria; |
10 | use Olz\Components\Common\OlzRootComponent; |
11 | use Olz\Components\Page\OlzFooter\OlzFooter; |
12 | use Olz\Components\Page\OlzHeader\OlzHeader; |
13 | use Olz\Entity\News\NewsEntry; |
14 | use Olz\News\Components\OlzArticleMetadata\OlzArticleMetadata; |
15 | use Olz\News\Components\OlzAuthorBadge\OlzAuthorBadge; |
16 | use Olz\News\Utils\NewsUtils; |
17 | use Olz\Utils\HttpParams; |
18 | |
19 | /** @extends HttpParams<array{filter?: ?string, von?: ?string}> */ |
20 | class OlzNewsDetailParams extends HttpParams { |
21 | } |
22 | |
23 | /** @extends OlzRootComponent<array<string, mixed>> */ |
24 | class OlzNewsDetail extends OlzRootComponent { |
25 | public function getSearchTitle(): string { |
26 | return 'News'; |
27 | } |
28 | |
29 | public function getSearchResults(array $terms): array { |
30 | $results = []; |
31 | $code_href = $this->envUtils()->getCodeHref(); |
32 | $news_repo = $this->entityManager()->getRepository(NewsEntry::class); |
33 | $news = $news_repo->search($terms); |
34 | foreach ($news as $news_entry) { |
35 | $id = $news_entry->getId(); |
36 | $results[] = $this->searchUtils()->getScoredSearchResult([ |
37 | 'link' => "{$code_href}news/{$id}", |
38 | 'icon' => $this->newsUtils()->getNewsFormatIcon($news_entry) ?: null, |
39 | 'date' => $news_entry->getPublishedDate(), |
40 | 'title' => $news_entry->getTitle() ?: '?', |
41 | 'text' => $news_entry->getTeaser()." ".$news_entry->getContent(), |
42 | ], $terms); |
43 | } |
44 | return $results; |
45 | } |
46 | |
47 | public function getHtml(mixed $args): string { |
48 | $this->httpUtils()->validateGetParams(OlzNewsDetailParams::class); |
49 | $code_href = $this->envUtils()->getCodeHref(); |
50 | $db = $this->dbUtils()->getDb(); |
51 | $entityManager = $this->dbUtils()->getEntityManager(); |
52 | $user = $this->authUtils()->getCurrentUser(); |
53 | $id = $args['id'] ?? null; |
54 | |
55 | $news_utils = $this->newsUtils(); |
56 | $news_repo = $entityManager->getRepository(NewsEntry::class); |
57 | $is_not_archived = $news_utils->getIsNotArchivedCriteria(); |
58 | $criteria = Criteria::create() |
59 | ->where(Criteria::expr()->andX( |
60 | $is_not_archived, |
61 | Criteria::expr()->eq('id', $id), |
62 | Criteria::expr()->eq('on_off', 1), |
63 | )) |
64 | ->setFirstResult(0) |
65 | ->setMaxResults(1) |
66 | ; |
67 | $news_entries = $news_repo->matching($criteria); |
68 | $num_news_entries = $news_entries->count(); |
69 | $is_archived = $num_news_entries !== 1; |
70 | |
71 | if ($is_archived && !$this->authUtils()->hasPermission('any')) { |
72 | $this->httpUtils()->dieWithHttpError(404); |
73 | throw new \Exception('should already have failed'); |
74 | } |
75 | |
76 | $article_metadata = ""; |
77 | try { |
78 | $article_metadata = OlzArticleMetadata::render(['id' => $id]); |
79 | } catch (\Exception $exc) { |
80 | $this->httpUtils()->dieWithHttpError(404); |
81 | throw new \Exception('should already have failed'); |
82 | } |
83 | |
84 | $news_entry = $this->getNewsEntryById($id); |
85 | |
86 | if (!$news_entry) { |
87 | $this->httpUtils()->dieWithHttpError(404); |
88 | throw new \Exception('should already have failed'); |
89 | } |
90 | |
91 | $title = $news_entry->getTitle(); |
92 | $back_filter = urlencode($_GET['filter'] ?? '{}'); |
93 | $out = OlzHeader::render([ |
94 | 'back_link' => "{$code_href}news?filter={$back_filter}", |
95 | 'title' => "{$title} - News", |
96 | 'description' => "Aktuelle Beiträge, Berichte von Anlässen und weitere Neuigkeiten von der OL Zimmerberg.", |
97 | 'norobots' => $is_archived, |
98 | 'canonical_url' => "{$code_href}news/{$id}", |
99 | 'additional_headers' => [ |
100 | $article_metadata, |
101 | ], |
102 | ]); |
103 | |
104 | $format = $news_entry->getFormat(); |
105 | // TODO: Use array_find with PHP 8.4 |
106 | $filtered = array_filter( |
107 | NewsUtils::ALL_FORMAT_OPTIONS, |
108 | fn ($entry) => $entry['ident'] === $format |
109 | ); |
110 | // @phpstan-ignore-next-line |
111 | $found_entry = $filtered[array_keys($filtered)[0]]; |
112 | $name = $found_entry['name']; |
113 | $icon = $found_entry['icon'] ?? null; |
114 | $icon_html = "<img src='{$code_href}assets/icns/{$icon}' alt='' class='format-icon'>"; |
115 | $pretty_format = "{$icon_html}{$name}"; |
116 | |
117 | $pretty_date = $this->dateUtils()->olzDate("tt.mm.jjjj", $news_entry->getPublishedDate()); |
118 | $author_user = $news_entry->getAuthorUser(); |
119 | $author_role = $news_entry->getAuthorRole(); |
120 | $author_name = $news_entry->getAuthorName(); |
121 | $author_email = $news_entry->getAuthorEmail(); |
122 | $pretty_author = OlzAuthorBadge::render([ |
123 | 'news_id' => $news_entry->getId() ?: 0, |
124 | 'user' => $author_user, |
125 | 'role' => $author_role, |
126 | 'name' => $author_name, |
127 | 'email' => $author_email, |
128 | ]); |
129 | $image_ids = $news_entry->getImageIds(); |
130 | $num_images = count($image_ids); |
131 | $download_all_link = $this->authUtils()->hasPermission('any') |
132 | ? "<a href='{$code_href}news/{$id}/all.zip'>Alle herunterladen</a>" : ''; |
133 | |
134 | $out .= <<<ZZZZZZZZZZ |
135 | <div class='content-right'> |
136 | <div style='padding:4px 3px 10px 3px;'> |
137 | <div id='format-info'><b>Format: </b>{$pretty_format}</div> |
138 | <div><b>Datum: </b>{$pretty_date}</div> |
139 | <div><b>Autor: </b>{$pretty_author}</div> |
140 | <div><b>Anzahl Bilder: </b>{$num_images}</div> |
141 | <div class='pretty'>{$download_all_link}</div> |
142 | </div> |
143 | </div> |
144 | <div class='content-middle'> |
145 | ZZZZZZZZZZ; |
146 | |
147 | $db->query("UPDATE news SET `counter`=`counter` + 1 WHERE `id`='{$id}'"); |
148 | |
149 | $title = $news_entry->getTitle(); |
150 | $teaser = $news_entry->getTeaser() ?? ''; |
151 | $content = $news_entry->getContent() ?? ''; |
152 | $published_date = $news_entry->getPublishedDate(); |
153 | |
154 | $published_date = $this->dateUtils()->olzDate("tt.mm.jj", $published_date); |
155 | |
156 | $is_owner = $user && intval($news_entry->getOwnerUser()?->getId() ?? 0) === intval($user->getId()); |
157 | $has_all_permissions = $this->authUtils()->hasPermission('all'); |
158 | $can_edit = $is_owner || $has_all_permissions; |
159 | $edit_admin = ''; |
160 | if ($can_edit) { |
161 | $json_id = json_encode($id); |
162 | $has_blog = $this->authUtils()->hasPermission('kaderblog', $user); |
163 | $has_roles = !empty($this->authUtils()->getAuthenticatedRoles()); |
164 | $json_mode = htmlentities(json_encode($has_roles ? ($has_blog ? 'account_with_all' : 'account_with_aktuell') : ($has_blog ? 'account_with_blog' : 'account')) ?: ''); |
165 | $edit_admin = <<<ZZZZZZZZZZ |
166 | <div> |
167 | <button |
168 | id='edit-news-button' |
169 | class='btn btn-primary' |
170 | onclick='return olz.editNews({$json_id}, {$json_mode})' |
171 | > |
172 | <img src='{$code_href}assets/icns/edit_white_16.svg' class='noborder' /> |
173 | Bearbeiten |
174 | </button> |
175 | </div> |
176 | ZZZZZZZZZZ; |
177 | } |
178 | |
179 | // TODO: Temporary fix for broken Markdown |
180 | $content = str_replace("\n", "\n\n", $content); |
181 | $content = str_replace("\n\n\n\n", "\n\n", $content); |
182 | |
183 | // Markdown |
184 | $html_input = $format === 'forum' ? 'escape' : 'allow'; // TODO: Do NOT allow! |
185 | $teaser = $this->htmlUtils()->renderMarkdown($teaser, [ |
186 | 'html_input' => $html_input, |
187 | ]); |
188 | $content = $this->htmlUtils()->renderMarkdown($content, [ |
189 | 'html_input' => $html_input, |
190 | ]); |
191 | |
192 | // Datei- & Bildpfade |
193 | $teaser = $news_entry->replaceImagePaths($teaser); |
194 | $teaser = $news_entry->replaceFilePaths($teaser); |
195 | $content = $news_entry->replaceImagePaths($content); |
196 | $content = $news_entry->replaceFilePaths($content); |
197 | |
198 | $out .= "<h1>{$edit_admin}{$title}</h1>"; |
199 | |
200 | $gallery = ''; |
201 | $num_images = count($image_ids); |
202 | if ($num_images > 0) { |
203 | $gallery .= "<div class='lightgallery gallery-container'>"; |
204 | foreach ($image_ids as $image_id) { |
205 | $gallery .= "<div class='gallery-image'>"; |
206 | $gallery .= $this->imageUtils()->olzImage( |
207 | 'news', |
208 | $id, |
209 | $image_id, |
210 | 110, |
211 | 'gallery[myset]' |
212 | ); |
213 | $gallery .= "</div>"; |
214 | } |
215 | $gallery .= "</div>"; |
216 | } |
217 | |
218 | if ($format === 'aktuell') { |
219 | $out .= "<p><b>{$teaser}</b><p>{$content}</p><br/><br/>{$gallery}\n"; |
220 | } elseif ($format === 'kaderblog') { |
221 | $out .= "<p>{$content}</p><br/><br/>{$gallery}\n"; |
222 | } elseif ($format === 'forum') { |
223 | $out .= "<p><b>{$teaser}</b><p>{$content}</p><br/><br/>{$gallery}\n"; |
224 | } elseif ($format === 'galerie') { |
225 | $out .= "<p>{$content}</p>{$gallery}\n"; |
226 | } elseif ($format === 'video') { |
227 | $youtube_url = $news_entry->getExternalUrl() ?? ''; |
228 | $res0 = preg_match("/^https\\:\\/\\/(www\\.)?youtu\\.be\\/([a-zA-Z0-9\\-\\_]{6,})/", $youtube_url, $matches0); |
229 | $res1 = preg_match("/^https\\:\\/\\/(www\\.)?youtube\\.com\\/watch\\?v\\=([a-zA-Z0-9\\-\\_]{6,})/", $youtube_url, $matches1); |
230 | $youtube_match = null; |
231 | if ($res0) { |
232 | $youtube_match = $matches0[2]; |
233 | } |
234 | if ($res1) { |
235 | $youtube_match = $matches1[2]; |
236 | } |
237 | |
238 | $out .= "<div class='video-container'>"; |
239 | $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"; |
240 | if ($youtube_match != null) { |
241 | $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>"; |
242 | } else { |
243 | $this->log()->error("Invalid YouTube link (ID:{$id}): {$youtube_url}"); |
244 | $out .= "Fehlerhafter YouTube-Link!"; |
245 | } |
246 | $out .= "<div style='background-image:url({$code_href}assets/icns/movie_dot.svg);background-repeat:repeat-x;margin:0px;padding:0px;height:24px;'></div>"; |
247 | $out .= "</div>"; |
248 | } else { |
249 | $out .= "<div class='lightgallery'><p><b>{$teaser}</b><p>{$content}</p></div>\n"; |
250 | } |
251 | $out .= "</div>"; |
252 | |
253 | $out .= OlzFooter::render(); |
254 | |
255 | return $out; |
256 | } |
257 | |
258 | protected function getNewsEntryById(int $id): ?NewsEntry { |
259 | $news_repo = $this->entityManager()->getRepository(NewsEntry::class); |
260 | return $news_repo->findOneBy([ |
261 | 'id' => $id, |
262 | 'on_off' => 1, |
263 | ]); |
264 | } |
265 | } |