Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 80
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
OlzTerminTemplatesListItem
0.00% covered (danger)
0.00%
0 / 80
0.00% covered (danger)
0.00%
0 / 2
240
0.00% covered (danger)
0.00%
0 / 1
 getHtml
0.00% covered (danger)
0.00%
0 / 77
0.00% covered (danger)
0.00%
0 / 1
182
 getTimeText
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Olz\Termine\Components\OlzTerminTemplatesListItem;
4
5use Olz\Components\Common\OlzComponent;
6use Olz\Entity\Termine\TerminLabel;
7use Olz\Termine\Components\OlzDateCalendar\OlzDateCalendar;
8use Olz\Users\Components\OlzUserInfoModal\OlzUserInfoModal;
9
10/** @extends OlzComponent<array<string, mixed>> */
11class OlzTerminTemplatesListItem extends OlzComponent {
12    public function getHtml(mixed $args): string {
13        $db = $this->dbUtils()->getDb();
14        $code_href = $this->envUtils()->getCodeHref();
15        $code_path = $this->envUtils()->getCodePath();
16
17        $out = '';
18
19        $termin_template = $args['termin_template'];
20        $id = $termin_template->getId();
21        $start_time = $termin_template->getStartTime();
22        $duration_seconds = $termin_template->getDurationSeconds();
23        $title = $termin_template->getTitle();
24        $text = $termin_template->getText();
25        $organizer = $termin_template->getOrganizerUser();
26        $labels = [...$termin_template->getLabels()];
27        $termin_location = $termin_template->getLocation();
28
29        $link = "{$code_href}termine/vorlagen/{$id}";
30        $type_imgs = implode('', array_map(function (TerminLabel $label) use ($code_path, $code_href) {
31            $ident = $label->getIdent();
32            // TODO: Remove fallback mechanism?
33            $fallback_path = "{$code_path}assets/icns/termine_type_{$ident}_20.svg";
34            $fallback_href = is_file($fallback_path)
35                ? "{$code_href}assets/icns/termine_type_{$ident}_20.svg" : null;
36            $icon_href = $label->getIcon() ? $label->getFileHref($label->getIcon()) : $fallback_href;
37            return $icon_href ? "<img src='{$icon_href}' alt='' class='type-icon'>" : '';
38        }, $labels));
39
40        $duration_seconds_or_zero = $duration_seconds ?? 0;
41        $duration_string = "+{$duration_seconds_or_zero} seconds";
42        $duration_interval = \DateInterval::createFromDateString($duration_string);
43        $this->generalUtils()->checkNotFalse($duration_interval, "Invalid duration: {$duration_string}");
44        $start_time_or_midnight = new \DateTime($start_time?->format('Y-m-d H:i:s') ?? '00:00:00');
45        $end_time = $duration_seconds
46            ? $start_time_or_midnight->add($duration_interval) : null;
47        $end_time_text = $this->getTimeText($end_time);
48        $day_diff = ($start_time && $end_time)
49            ? intval($end_time->format('d')) - intval($start_time->format('d')) : null;
50        $start_icon = OlzDateCalendar::render([
51            'weekday' => '',
52            'day' => 'X',
53            'month' => '',
54            'size' => 'S',
55        ]);
56        $end_icon = ($day_diff > 0)
57            ? ' &ndash; '.OlzDateCalendar::render([
58                'weekday' => '',
59                'day' => "X+{$day_diff}",
60                'month' => '',
61                'size' => 'S',
62            ])
63            : null;
64        $start_time_text = $this->getTimeText($start_time);
65
66        $time_text = $start_time_text ? (
67            $end_time_text
68                ? "{$start_time_text} &ndash; {$end_time_text}"
69                : "{$start_time_text}"
70        ) : (
71            $end_time_text
72                ? "? +{$end_time_text}"
73                : ''
74        );
75        $text = $this->htmlUtils()->renderMarkdown($text ?? '');
76        $text = $termin_template->replaceImagePaths($text);
77        $text = $termin_template->replaceFilePaths($text);
78        if ($termin_location) {
79            $sane_termin_location_id = intval($termin_location->getId());
80            $result_location = $db->query("SELECT name FROM termin_locations WHERE id='{$sane_termin_location_id}'");
81            // @phpstan-ignore-next-line
82            $row_location = $result_location->fetch_assoc();
83            $location_name = $row_location['name'] ?? null;
84            $text = "<a href='{$code_href}termine/orte/{$sane_termin_location_id}' class='linkmap'>{$location_name}</a> {$text}";
85        }
86        if ($organizer) {
87            $pretty_organizer = OlzUserInfoModal::render(['user' => $organizer]);
88            $text = "{$pretty_organizer} {$text}";
89        }
90
91        $out .= <<<ZZZZZZZZZZ
92            <div class='olz-termin-templates-list-item'>
93                <a class='link' href='{$link}'></a>
94                <div class='content'>
95                    <div class='date'>
96                        <div class='date-calendars'>{$start_icon}{$end_icon}</div>
97                        <div class='time-text'>{$time_text}</div>
98                    </div>
99                    <div class='title-text-container'>
100                        <div class='title'>{$title} {$type_imgs}</div>
101                        <div class='text'>{$text}</div>
102                    </div>
103                </div>
104            </div>
105            ZZZZZZZZZZ;
106        return $out;
107    }
108
109    protected function getTimeText(?\DateTime $datetime_time): ?string {
110        if (!$datetime_time) {
111            return null;
112        }
113        return $datetime_time->format('H:i');
114    }
115}