Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 107
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 / 107
0.00% covered (danger)
0.00%
0 / 2
756
0.00% covered (danger)
0.00%
0 / 1
 getHtml
0.00% covered (danger)
0.00%
0 / 104
0.00% covered (danger)
0.00%
0 / 1
552
 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\Common\Components\OlzComponent;
6use Olz\Entity\Termine\Termin;
7use Olz\Entity\Users\User;
8use Olz\Termine\Components\OlzDateCalendar\OlzDateCalendar;
9use Olz\Users\Components\OlzUserInfoModal\OlzUserInfoModal;
10
11/** @extends OlzComponent<array<string, mixed>> */
12class OlzTermineListItem extends OlzComponent {
13    public function getHtml(mixed $args): string {
14        $db = $this->dbUtils()->getDb();
15        $code_path = $this->envUtils()->getCodePath();
16        $code_href = $this->envUtils()->getCodeHref();
17
18        $out = '';
19
20        $id = $args['id'];
21        $owner_user_id = $args['owner_user_id'];
22        $start_date = $args['start_date'];
23        $start_time = $args['start_time'];
24        $end_date = $args['end_date'];
25        $end_time = $args['end_time'];
26        $organizer_user_id = $args['organizer_user_id'];
27        $title = $args['title'];
28        $text = $args['text'];
29        $labels = $args['labels'];
30        $termin_location_id = $args['location_id'];
31        $image_ids = $args['image_ids'];
32        $is_deadline = count($labels) > 0 && $labels[0]->getIdent() === 'meldeschluss';
33
34        $link = "{$code_href}termine/{$id}";
35        $start_icon = OlzDateCalendar::render([
36            'date' => $start_date,
37            'size' => 'S',
38        ]);
39        $end_icon = ($end_date && $end_date !== $start_date)
40            ? ' &ndash; '.OlzDateCalendar::render([
41                'date' => $end_date,
42                'size' => 'S',
43            ])
44            : null;
45        $start_time_text = $this->getTimeText($start_time);
46        if ($is_deadline && $start_time_text === '23:59') {
47            $start_time_text = null;
48        }
49        $end_time_text = $this->getTimeText($end_time);
50        $time_text = $start_time_text ? (
51            $end_time_text
52                ? "{$start_time_text} &ndash; {$end_time_text}"
53                : "{$start_time_text}"
54        ) : null;
55        if ($termin_location_id) {
56            $sane_termin_location_id = intval($termin_location_id);
57            $result_location = $db->query("SELECT name FROM termin_locations WHERE id='{$sane_termin_location_id}'");
58            // @phpstan-ignore-next-line
59            $row_location = $result_location->fetch_assoc();
60            $location_name = $row_location['name'] ?? null;
61            $text = "{$location_name} {$text}";
62        }
63        $text = strip_tags($this->htmlUtils()->renderMarkdown($text));
64        $organizer_prefix = '';
65        if ($organizer_user_id) {
66            $user_repo = $this->entityManager()->getRepository(User::class);
67            $organizer = $user_repo->findOneBy(['id' => $organizer_user_id]);
68            $pretty_organizer = OlzUserInfoModal::render(['user' => $organizer]);
69            $organizer_prefix = "{$pretty_organizer} ";
70        }
71        $image = '';
72        if (count($image_ids ?? []) > 0) {
73            $image = $this->imageUtils()->olzImage(
74                'termine',
75                $id,
76                $image_ids[0],
77                64,
78                'image'
79            );
80        }
81        $sub_out = '';
82        if (!$is_deadline) {
83            $termine_repo = $this->entityManager()->getRepository(Termin::class);
84            $sub_termine = [...$termine_repo->findBy(
85                ['parent_termin' => $id, 'deleted_at' => null],
86                ['start_date' => 'ASC', 'start_time' => 'ASC'],
87            )];
88            $num_sub_termine = count($sub_termine);
89            if ($num_sub_termine > 0) {
90                $sub_out .= "<div class='sub-termine'>";
91                for ($i = 0; $i < $num_sub_termine && $i < 3; $i++) {
92                    $sub_termin = $sub_termine[$i];
93                    $sub_out .= <<<ZZZZZZZZZZ
94                        <div class='sub-termin'>
95                            <img src="{$code_href}assets/icns/termine_type_all_20.svg" alt="Termine" class="text-icon">
96                            {$sub_termin->getTitle()}
97                        </div>
98                        ZZZZZZZZZZ;
99                }
100                $num_more = $num_sub_termine - 3;
101                if ($num_more > 0) {
102                    $sub_out .= "<div class='sub-termin more'>+{$num_more}</div>";
103                }
104                $sub_out .= "</div>";
105            }
106        }
107
108        $user = $this->authUtils()->getCurrentUser();
109        $is_owner = $user && $owner_user_id && intval($owner_user_id) === intval($user->getId());
110        $is_organizer = $user && $organizer_user_id && intval($organizer_user_id) === intval($user->getId());
111        $has_all_permissions = $this->authUtils()->hasPermission('all');
112        $can_edit = $is_owner || $is_organizer || $has_all_permissions;
113        $edit_admin = '';
114        if ($can_edit) {
115            $json_id = json_encode(intval($id));
116            $edit_admin = <<<ZZZZZZZZZZ
117                <button
118                    class='btn btn-outline-auth btn-sm edit-termin-list-button'
119                    onclick='return olz.termineListItemEditTermin({$json_id})'
120                >
121                    <img src='{$code_href}assets/icns/edit_16.svg' class='noborder' />
122                </button>
123                ZZZZZZZZZZ;
124        }
125
126        $out .= <<<ZZZZZZZZZZ
127            <div class='olz-termine-list-item'>
128                <a class='link' href='{$link}'></a>
129                <div class='content'>
130                    <div class='date-container'>
131                        <div class='date-calendars'>{$start_icon}{$end_icon}</div>
132                        <div class='time-text'>{$time_text}</div>
133                    </div>
134                    <div class='title-text-container'>
135                        <div class='title'>{$title}{$edit_admin}</div>
136                        <div class='text'>{$organizer_prefix}{$text}</div>
137                    </div>
138                    <div class='image-container'>
139                        {$image}
140                    </div>
141                </div>
142                <div class='sub'>
143                    {$sub_out}
144                </div>
145            </div>
146            ZZZZZZZZZZ;
147        return $out;
148    }
149
150    protected function getTimeText(?string $iso_time): ?string {
151        if (!$iso_time || $iso_time === '00:00:00') {
152            return null;
153        }
154        return date("H:i", strtotime($iso_time) ?: 0);
155    }
156}