Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 140 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
OlzNewsListItem | |
0.00% |
0 / 140 |
|
0.00% |
0 / 2 |
420 | |
0.00% |
0 / 1 |
getHtml | |
0.00% |
0 / 130 |
|
0.00% |
0 / 1 |
240 | |||
truncateText | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | namespace Olz\News\Components\OlzNewsListItem; |
4 | |
5 | use Olz\Components\Common\OlzComponent; |
6 | use Olz\Components\Common\OlzPostingListItem\OlzPostingListItem; |
7 | use Olz\News\Components\OlzAuthorBadge\OlzAuthorBadge; |
8 | use Olz\News\Utils\NewsFilterUtils; |
9 | |
10 | /** @extends OlzComponent<array<string, mixed>> */ |
11 | class OlzNewsListItem extends OlzComponent { |
12 | /** @var array<string, string> */ |
13 | protected static $iconBasenameByFormat = [ |
14 | 'aktuell' => 'entry_type_aktuell_20.svg', |
15 | 'forum' => 'entry_type_forum_20.svg', |
16 | 'galerie' => 'entry_type_gallery_20.svg', |
17 | 'kaderblog' => 'entry_type_kaderblog_20.svg', |
18 | 'video' => 'entry_type_movie_20.svg', |
19 | ]; |
20 | |
21 | public function getHtml(mixed $args): string { |
22 | $news_utils = NewsFilterUtils::fromEnv(); |
23 | $code_href = $this->envUtils()->getCodeHref(); |
24 | |
25 | $news_entry = $args['news_entry']; |
26 | $out = ''; |
27 | $current_filter = json_decode($_GET['filter'] ?? '{}', true); |
28 | $filter_arg = ''; |
29 | if ($current_filter !== $news_utils->getDefaultFilter()) { |
30 | $enc_current_filter = urlencode($_GET['filter'] ?? '{}'); |
31 | $filter_arg = "?filter={$enc_current_filter}"; |
32 | } |
33 | |
34 | $id = $news_entry->getId(); |
35 | $published_date = $news_entry->getPublishedDate(); |
36 | $format = $news_entry->getFormat(); |
37 | $icon_basename = self::$iconBasenameByFormat[$format]; |
38 | $icon = "{$code_href}assets/icns/{$icon_basename}"; |
39 | $author_user = $news_entry->getAuthorUser(); |
40 | $author_role = $news_entry->getAuthorRole(); |
41 | $author_name = $news_entry->getAuthorName(); |
42 | $author_email = $news_entry->getAuthorEmail(); |
43 | $title = $news_entry->getTitle(); |
44 | $teaser = $news_entry->getTeaser(); |
45 | $content = $news_entry->getContent(); |
46 | $link = "news/{$id}{$filter_arg}"; |
47 | |
48 | $image_ids = $news_entry->getImageIds(); |
49 | $thumb = ''; |
50 | $size = count($image_ids); |
51 | if ($size > 0) { |
52 | $thumb = $this->imageUtils()->olzImage( |
53 | 'news', |
54 | $id, |
55 | $image_ids[0] ?? null, |
56 | 110, |
57 | 'image', |
58 | ); |
59 | } |
60 | |
61 | $author_badge = OlzAuthorBadge::render([ |
62 | 'news_id' => $news_entry->getId(), |
63 | 'user' => $author_user, |
64 | 'role' => $author_role, |
65 | 'name' => $author_name, |
66 | 'email' => $author_email, |
67 | ]); |
68 | |
69 | $user = $this->authUtils()->getCurrentUser(); |
70 | $owner_user = $news_entry->getOwnerUser(); |
71 | $is_owner = $user && $owner_user && intval($owner_user->getId() ?? 0) === intval($user->getId()); |
72 | $has_all_permissions = $this->authUtils()->hasPermission('all'); |
73 | $can_edit = $is_owner || $has_all_permissions; |
74 | $edit_admin = ''; |
75 | if ($can_edit) { |
76 | $json_id = json_encode($id); |
77 | $json_mode = $args['json_mode']; |
78 | $edit_admin = <<<ZZZZZZZZZZ |
79 | <button |
80 | class='btn btn-secondary-outline btn-sm edit-news-list-button' |
81 | onclick='return olz.newsListItemEditNews({$json_id}, {$json_mode})' |
82 | > |
83 | <img src='{$code_href}assets/icns/edit_16.svg' class='noborder' /> |
84 | </button> |
85 | ZZZZZZZZZZ; |
86 | } |
87 | |
88 | if ($format === 'aktuell') { |
89 | $out .= OlzPostingListItem::render([ |
90 | 'icon' => $icon, |
91 | 'date' => $published_date, |
92 | 'author' => $author_badge, |
93 | 'title' => $title.$edit_admin, |
94 | 'text' => $thumb.strip_tags($this->htmlUtils()->renderMarkdown($teaser, [])), |
95 | 'link' => $link, |
96 | 'class' => 'has-thumb', |
97 | ]); |
98 | } elseif ($format === 'kaderblog') { |
99 | $out .= OlzPostingListItem::render([ |
100 | 'icon' => $icon, |
101 | 'date' => $published_date, |
102 | 'author' => $author_badge, |
103 | 'title' => $title.$edit_admin, |
104 | 'text' => $thumb.strip_tags($this->htmlUtils()->renderMarkdown( |
105 | self::truncateText($content), |
106 | )), |
107 | 'link' => $link, |
108 | 'class' => 'has-thumb', |
109 | ]); |
110 | } elseif ($format === 'forum') { |
111 | $out .= OlzPostingListItem::render([ |
112 | 'icon' => $icon, |
113 | 'date' => $published_date, |
114 | 'author' => $author_badge, |
115 | 'title' => $title.$edit_admin, |
116 | 'text' => $thumb.strip_tags($this->htmlUtils()->renderMarkdown( |
117 | self::truncateText($content), |
118 | )), |
119 | 'link' => $link, |
120 | 'class' => 'has-thumb', |
121 | ]); |
122 | } elseif ($format === 'galerie') { |
123 | $thumbs = ''; |
124 | $used_thumb_indexes = []; |
125 | $size = count($image_ids); |
126 | for ($i = 0; $i < (($size > 4) ? 4 : $size); $i++) { |
127 | $random_index = rand(1, $size); |
128 | while (array_search($random_index, $used_thumb_indexes) !== false) { |
129 | $random_index = rand(1, $size); |
130 | } |
131 | array_push($used_thumb_indexes, $random_index); |
132 | $thumbs .= "<td class='test-flaky'>".$this->imageUtils()->olzImage("news", $id, $image_ids[$random_index - 1], 110, 'image')."</td>"; |
133 | } |
134 | $out .= OlzPostingListItem::render([ |
135 | 'icon' => $icon, |
136 | 'date' => $published_date, |
137 | 'author' => $author_badge, |
138 | 'title' => $title.$edit_admin, |
139 | 'text' => "<table><tr class='galerie-thumbs'>{$thumbs}</tr></table>", |
140 | 'link' => $link, |
141 | ]); |
142 | } elseif ($format === 'video') { |
143 | $thumbnail = $this->imageUtils()->olzImage("news", $id, $image_ids[0] ?? null, 110, 'image'); |
144 | $content = <<<ZZZZZZZZZZ |
145 | <div href='{$link}' style='background-color:#000;padding-top:0;' class='video-thumb'>\n |
146 | <span style='display:block;background-image:url({$code_href}assets/icns/movie_dot.svg);background-repeat:repeat-x;height:24px;'></span>\n |
147 | <span style='display:block;text-align:center;'>{$thumbnail}</span>\n |
148 | <span style='display:block;background-image:url({$code_href}assets/icns/movie_dot.svg);background-repeat:repeat-x;height:24px;'></span>\n |
149 | </div> |
150 | ZZZZZZZZZZ; |
151 | $out .= OlzPostingListItem::render([ |
152 | 'icon' => $icon, |
153 | 'date' => $published_date, |
154 | 'author' => $author_badge, |
155 | 'title' => $title.$edit_admin, |
156 | 'text' => $content, |
157 | 'link' => $link, |
158 | ]); |
159 | } else { |
160 | $out .= OlzPostingListItem::render([ |
161 | 'icon' => $icon, |
162 | 'date' => $published_date, |
163 | 'author' => $author_badge, |
164 | 'title' => $title.$edit_admin, |
165 | 'link' => $link, |
166 | ]); |
167 | } |
168 | return $out; |
169 | } |
170 | |
171 | protected static function truncateText(string $text): string { |
172 | $max_length = 300; |
173 | |
174 | $text = preg_replace("/\\s*\\n\\s*/", "\n", $text) ?? $text; |
175 | $text_length = mb_strlen($text); |
176 | |
177 | if ($text_length <= $max_length) { |
178 | return $text; |
179 | } |
180 | $text = mb_substr($text, 0, $max_length - 6); |
181 | $last_space = mb_strrpos($text, " ") ?: 0; |
182 | $last_break = mb_strrpos($text, "<br>") ?: 0; |
183 | $last_whitespace = ($last_break > $last_space) ? $last_break : $last_space; |
184 | return mb_substr($text, 0, $last_whitespace).' [...]'; |
185 | } |
186 | } |