Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 91
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
OlzTermineListItem
0.00% covered (danger)
0.00%
0 / 91
0.00% covered (danger)
0.00%
0 / 2
506
0.00% covered (danger)
0.00%
0 / 1
 getHtml
0.00% covered (danger)
0.00%
0 / 88
0.00% covered (danger)
0.00%
0 / 1
342
 getTimeText
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace Olz\Termine\Components\OlzTermineListItem;
4
5use Olz\Components\Common\OlzComponent;
6use Olz\Entity\Termine\TerminLabel;
7use Olz\Termine\Components\OlzDateCalendar\OlzDateCalendar;
8
9/** @extends OlzComponent<array<string, mixed>> */
10class OlzTermineListItem extends OlzComponent {
11    public function getHtml(mixed $args): string {
12        $db = $this->dbUtils()->getDb();
13        $code_path = $this->envUtils()->getCodePath();
14        $code_href = $this->envUtils()->getCodeHref();
15        $termine_utils = $this->termineUtils()->loadTypeOptions();
16
17        $out = '';
18        $current_filter = json_decode($_GET['filter'] ?? '{}', true);
19        $filter_arg = '';
20        if ($current_filter !== $termine_utils->getDefaultFilter()) {
21            $enc_current_filter = urlencode($_GET['filter'] ?? '{}');
22            $filter_arg = "?filter={$enc_current_filter}";
23        }
24
25        $id = $args['id'];
26        $owner_user_id = $args['owner_user_id'];
27        $start_date = $args['start_date'];
28        $start_time = $args['start_time'];
29        $end_date = $args['end_date'];
30        $end_time = $args['end_time'];
31        $title = $args['title'];
32        $text = $args['text'];
33        $labels = $args['labels'];
34        $termin_location_id = $args['location_id'];
35        $image_ids = $args['image_ids'];
36        $is_deadline = count($labels) > 0 && $labels[0]->getIdent() === 'meldeschluss';
37
38        $link = "{$code_href}termine/{$id}{$filter_arg}";
39        $type_imgs = implode('', array_map(function (TerminLabel $label) use ($code_path, $code_href) {
40            $ident = $label->getIdent();
41            // TODO: Remove fallback mechanism?
42            $fallback_path = "{$code_path}assets/icns/termine_type_{$ident}_20.svg";
43            $fallback_href = is_file($fallback_path)
44                ? "{$code_href}assets/icns/termine_type_{$ident}_20.svg" : null;
45            $icon_href = $label->getIcon() ? $label->getFileHref($label->getIcon()) : $fallback_href;
46            return $icon_href ? "<img src='{$icon_href}' alt='' class='type-icon'>" : '';
47        }, $labels));
48        $start_icon = OlzDateCalendar::render([
49            'date' => $start_date,
50            'size' => 'S',
51        ]);
52        $end_icon = ($end_date && $end_date !== $start_date)
53            ? ' &ndash; '.OlzDateCalendar::render([
54                'date' => $end_date,
55                'size' => 'S',
56            ])
57            : null;
58        $start_time_text = $this->getTimeText($start_time);
59        if ($is_deadline && $start_time_text === '23:59') {
60            $start_time_text = null;
61        }
62        $end_time_text = $this->getTimeText($end_time);
63        $time_text = $start_time_text ? (
64            $end_time_text
65                ? "{$start_time_text} &ndash; {$end_time_text}"
66                : "{$start_time_text}"
67        ) : null;
68        if ($termin_location_id) {
69            $sane_termin_location_id = intval($termin_location_id);
70            $result_location = $db->query("SELECT name FROM termin_locations WHERE id='{$sane_termin_location_id}'");
71            // @phpstan-ignore-next-line
72            $row_location = $result_location->fetch_assoc();
73            $location_name = $row_location['name'] ?? null;
74            $text = "{$location_name} {$text}";
75        }
76        $text = strip_tags($this->htmlUtils()->renderMarkdown($text));
77        $image = '';
78        if (count($image_ids ?? []) > 0) {
79            $image = $this->imageUtils()->olzImage(
80                'termine',
81                $id,
82                $image_ids[0],
83                64,
84                'image'
85            );
86        }
87
88        $user = $this->authUtils()->getCurrentUser();
89        $is_owner = $user && $owner_user_id && intval($owner_user_id) === intval($user->getId());
90        $has_all_permissions = $this->authUtils()->hasPermission('all');
91        $can_edit = $is_owner || $has_all_permissions;
92        $edit_admin = '';
93        if ($can_edit) {
94            $json_id = json_encode(intval($id));
95            $edit_admin = <<<ZZZZZZZZZZ
96                <button
97                    class='btn btn-secondary-outline btn-sm edit-termin-list-button'
98                    onclick='return olz.termineListItemEditTermin({$json_id})'
99                >
100                    <img src='{$code_href}assets/icns/edit_16.svg' class='noborder' />
101                </button>
102                ZZZZZZZZZZ;
103        }
104
105        $out .= <<<ZZZZZZZZZZ
106            <div class='olz-termine-list-item'>
107                <a class='link' href='{$link}'></a>
108                <div class='content'>
109                    <div class='date-container'>
110                        <div class='date-calendars'>{$start_icon}{$end_icon}</div>
111                        <div class='time-text'>{$time_text}</div>
112                    </div>
113                    <div class='title-text-container'>
114                        <div class='title'>{$title}{$edit_admin} {$type_imgs}</div>
115                        <div class='text'>{$text}</div>
116                    </div>
117                    <div class='image-container'>
118                        {$image}
119                    </div>
120                </div>
121            </div>
122            ZZZZZZZZZZ;
123        return $out;
124    }
125
126    protected function getTimeText(?string $iso_time): ?string {
127        if (!$iso_time || $iso_time === '00:00:00') {
128            return null;
129        }
130        return date("H:i", strtotime($iso_time) ?: 0);
131    }
132}