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