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