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