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