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