Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 104
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
OlzTerminTemplateDetailParams
n/a
0 / 0
n/a
0 / 0
0
n/a
0 / 0
OlzTerminTemplateDetail
0.00% covered (danger)
0.00%
0 / 104
0.00% covered (danger)
0.00%
0 / 4
380
0.00% covered (danger)
0.00%
0 / 1
 hasAccess
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 searchSqlWhenHasAccess
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHtmlWhenHasAccess
0.00% covered (danger)
0.00%
0 / 97
0.00% covered (danger)
0.00%
0 / 1
272
 getTerminTemplateById
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Olz\Termine\Components\OlzTerminTemplateDetail;
4
5use Olz\Components\Common\OlzLocationMap\OlzLocationMap;
6use Olz\Components\Common\OlzRootComponent;
7use Olz\Components\Page\OlzFooter\OlzFooter;
8use Olz\Components\Page\OlzHeader\OlzHeader;
9use Olz\Entity\Termine\TerminLabel;
10use Olz\Entity\Termine\TerminTemplate;
11use Olz\Users\Components\OlzUserInfoModal\OlzUserInfoModal;
12use Olz\Utils\HttpParams;
13
14/** @extends HttpParams<array{filter?: ?string}> */
15class OlzTerminTemplateDetailParams extends HttpParams {
16}
17
18/** @extends OlzRootComponent<array<string, mixed>> */
19class OlzTerminTemplateDetail extends OlzRootComponent {
20    public function hasAccess(): bool {
21        return true;
22    }
23
24    public function searchSqlWhenHasAccess(array $terms): string|array|null {
25        return null;
26    }
27
28    public function getHtmlWhenHasAccess(mixed $args): string {
29        $this->httpUtils()->validateGetParams(OlzTerminTemplateDetailParams::class);
30
31        $code_href = $this->envUtils()->getCodeHref();
32        $code_path = $this->envUtils()->getCodePath();
33        $user = $this->authUtils()->getCurrentUser();
34        $id = $args['id'] ?? null;
35
36        $termin_template = $this->getTerminTemplateById($id);
37
38        if (!$termin_template) {
39            $this->httpUtils()->dieWithHttpError(404);
40            throw new \Exception('should already have failed');
41        }
42
43        $title = $termin_template->getTitle() ?? '';
44        $back_link = "{$code_href}termine/vorlagen";
45        $out = OlzHeader::render([
46            'back_link' => $back_link,
47            'title' => "{$title} - Vorlagen",
48            'description' => "Vorlagen, um OL Zimmerberg-Termine zu erstellen.",
49            'norobots' => true,
50        ]);
51
52        // Creation Tools
53        $has_termine_permissions = $this->authUtils()->hasPermission('termine');
54        $creation_tools = '';
55        if ($has_termine_permissions) {
56            $esc_template_id = json_encode($id);
57            $creation_tools .= <<<ZZZZZZZZZZ
58                <div>
59                    <button
60                        id='create-termin-template-button'
61                        class='btn btn-secondary'
62                        onclick='return olz.initOlzEditTerminModal(undefined, {$esc_template_id})'
63                    >
64                        <img src='{$code_href}assets/icns/new_white_16.svg' class='noborder' />
65                        Neuer Termin aus dieser Vorlage
66                    </button>
67                </div>
68                ZZZZZZZZZZ;
69        }
70
71        $out .= <<<ZZZZZZZZZZ
72            <div class='content-right'>
73                <div style='padding:4px 3px 10px 3px;'>
74                    {$creation_tools}
75                </div>
76            </div>
77            <div class='content-middle'>
78            ZZZZZZZZZZ;
79
80        $start_time = $termin_template->getStartTime() ?? '';
81        $duration_seconds = $termin_template->getDurationSeconds() ?? '';
82        $title = $termin_template->getTitle() ?? '';
83        $text = $termin_template->getText() ?? '';
84        $organizer = $termin_template->getOrganizerUser();
85        $labels = [...$termin_template->getLabels()];
86        $termin_location = $termin_template->getLocation();
87        $image_ids = $termin_template->getImageIds();
88
89        $out .= "<div class='olz-termin-template-detail'>";
90
91        // Editing Tools
92        $is_owner = $user && intval($termin_template->getOwnerUser()?->getId() ?? 0) === intval($user->getId());
93        $has_termine_permissions = $this->authUtils()->hasPermission('termine');
94        $can_edit = $is_owner || $has_termine_permissions;
95        if ($can_edit) {
96            $json_id = json_encode($id);
97            $out .= <<<ZZZZZZZZZZ
98                <div>
99                    <button
100                        id='edit-termin-template-button'
101                        class='btn btn-primary'
102                        onclick='return olz.editTerminTemplate({$json_id})'
103                    >
104                        <img src='{$code_href}assets/icns/edit_white_16.svg' class='noborder' />
105                        Bearbeiten
106                    </button>
107                </div>
108                ZZZZZZZZZZ;
109        }
110
111        $pretty_date = '(irgendwann)';
112        $duration_interval = \DateInterval::createFromDateString("+{$duration_seconds} seconds");
113        if ($start_time && $duration_interval) {
114            $end_time = (clone $start_time)->add($duration_interval);
115            $pretty_date = $duration_seconds
116                ? $start_time->format('H:i')." – ".$end_time->format('H:i')
117                : $start_time->format('H:i');
118        }
119        $label_imgs = implode('', array_map(function (TerminLabel $label) use ($code_path, $code_href) {
120            $ident = $label->getIdent();
121            // TODO: Remove fallback mechanism?
122            $fallback_path = "{$code_path}assets/icns/termine_type_{$ident}_20.svg";
123            $fallback_href = is_file($fallback_path)
124                ? "{$code_href}assets/icns/termine_type_{$ident}_20.svg" : null;
125            $icon_href = $label->getIcon() ? $label->getFileHref($label->getIcon()) : $fallback_href;
126            return $icon_href ? "<img src='{$icon_href}' alt='' class='type-icon'>" : '';
127        }, $labels));
128
129        $out .= "<h5>{$pretty_date}</h5>";
130        $out .= "<h1>{$title} {$label_imgs}</h1>";
131        if ($organizer) {
132            $pretty_organizer = OlzUserInfoModal::render(['user' => $organizer]);
133            $out .= "<div>Organisator: {$pretty_organizer}</div><br>";
134        }
135
136        if ($termin_location) {
137            $out .= OlzLocationMap::render([
138                'name' => $termin_location->getName(),
139                'latitude' => $termin_location->getLatitude(),
140                'longitude' => $termin_location->getLongitude(),
141                'zoom' => 13,
142            ]);
143        }
144
145        $text_html = $this->htmlUtils()->renderMarkdown($text);
146        $text_html = $termin_template->replaceImagePaths($text_html);
147        $text_html = $termin_template->replaceFilePaths($text_html);
148        $out .= "<div>{$text_html}</div>";
149
150        if (count($image_ids) > 0) {
151            $out .= "<h3>Bilder</h3><div class='lightgallery gallery-container'>";
152            foreach ($image_ids as $image_id) {
153                $out .= "<div class='gallery-image'>";
154                $out .= $this->imageUtils()->olzImage(
155                    'termin_templates',
156                    $id,
157                    $image_id,
158                    110,
159                    'gallery[myset]'
160                );
161                $out .= "</div>";
162            }
163            $out .= "</div>";
164        }
165
166        $out .= "</div>"; // olz-termin-location-detail
167        $out .= "</div>"; // content-middle
168
169        $out .= OlzFooter::render();
170
171        return $out;
172    }
173
174    protected function getTerminTemplateById(int $id): ?TerminTemplate {
175        $termin_template_repo = $this->entityManager()->getRepository(TerminTemplate::class);
176        return $termin_template_repo->findOneBy([
177            'id' => $id,
178            'on_off' => 1,
179        ]);
180    }
181}