Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 112
0.00% covered (danger)
0.00%
0 / 6
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 / 112
0.00% covered (danger)
0.00%
0 / 6
272
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 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 getPageTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPageDescription
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 / 89
0.00% covered (danger)
0.00%
0 / 1
132
 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 $this->authUtils()->hasPermission('termine');
22    }
23
24    public function searchSqlWhenHasAccess(array $terms): string|array|null {
25        $code_href = $this->envUtils()->getCodeHref();
26        $where = implode(' AND ', array_map(function ($term) {
27            return <<<ZZZZZZZZZZ
28                (
29                    tt.title LIKE '%{$term}%'
30                    OR tt.text LIKE '%{$term}%'
31                    OR tl.name LIKE '%{$term}%'
32                )
33                ZZZZZZZZZZ;
34        }, $terms));
35        return <<<ZZZZZZZZZZ
36            SELECT
37                CONCAT('{$code_href}termine/vorlagen/', tt.id) AS link,
38                '{$code_href}assets/icns/termine_type_all_20.svg' AS icon,
39                NULL AS date,
40                CONCAT('Termin-Vorlage: ', tt.title) AS title,
41                CONCAT(IFNULL(tl.name, ''), ' ', IFNULL(tt.text, ''), ' Typ: ', (
42                    SELECT GROUP_CONCAT(l.name ORDER BY l.position ASC SEPARATOR ', ')
43                    FROM
44                        termin_template_label_map ttlm
45                        JOIN termin_labels l ON (l.id = ttlm.label_id)
46                    WHERE ttlm.termin_template_id = tt.id
47                    GROUP BY tt.id
48                )) AS text,
49                1.0 AS time_relevance
50            FROM termin_templates tt LEFT JOIN termin_locations tl ON (tt.location_id = tl.id)
51            WHERE
52                tt.on_off = '1'
53                AND {$where}
54            ZZZZZZZZZZ;
55    }
56
57    public function getPageTitle(): string {
58        return "";
59    }
60
61    public function getPageDescription(): string {
62        return "";
63    }
64
65    public function getHtmlWhenHasAccess(mixed $args): string {
66        $this->httpUtils()->validateGetParams(OlzTerminTemplateDetailParams::class);
67
68        $code_href = $this->envUtils()->getCodeHref();
69        $code_path = $this->envUtils()->getCodePath();
70        $id = $args['id'] ?? null;
71
72        $termin_template = $this->getTerminTemplateById($id);
73
74        if (!$termin_template) {
75            $this->httpUtils()->dieWithHttpError(404);
76            throw new \Exception('should already have failed');
77        }
78
79        $title = $termin_template->getTitle() ?? '';
80        $back_link = "{$code_href}termine/vorlagen";
81        $out = OlzHeader::render([
82            'back_link' => $back_link,
83            'title' => "{$title} - Vorlagen",
84            'description' => "Vorlagen, um OL Zimmerberg-Termine zu erstellen.",
85            'norobots' => true,
86        ]);
87
88        // Creation Tools
89        $esc_template_id = json_encode($id);
90        $creation_tools = <<<ZZZZZZZZZZ
91            <div>
92                <button
93                    id='create-termin-template-button'
94                    class='btn btn-secondary'
95                    onclick='return olz.initOlzEditTerminModal(undefined, {$esc_template_id})'
96                >
97                    <img src='{$code_href}assets/icns/new_white_16.svg' class='noborder' />
98                    Neuer Termin aus dieser Vorlage
99                </button>
100            </div>
101            ZZZZZZZZZZ;
102
103        $out .= <<<ZZZZZZZZZZ
104            <div class='content-right'>
105                <div style='padding:4px 3px 10px 3px;'>
106                    {$creation_tools}
107                </div>
108            </div>
109            <div class='content-middle'>
110            ZZZZZZZZZZ;
111
112        $start_time = $termin_template->getStartTime() ?? '';
113        $duration_seconds = $termin_template->getDurationSeconds() ?? '';
114        $title = $termin_template->getTitle() ?? '';
115        $text = $termin_template->getText() ?? '';
116        $organizer = $termin_template->getOrganizerUser();
117        $labels = [...$termin_template->getLabels()];
118        $termin_location = $termin_template->getLocation();
119        $image_ids = $termin_template->getImageIds();
120
121        $out .= "<div class='olz-termin-template-detail'>";
122
123        // Editing Tools
124        $json_id = json_encode($id);
125        $out .= <<<ZZZZZZZZZZ
126            <div>
127                <button
128                    id='edit-termin-template-button'
129                    class='btn btn-primary'
130                    onclick='return olz.editTerminTemplate({$json_id})'
131                >
132                    <img src='{$code_href}assets/icns/edit_white_16.svg' class='noborder' />
133                    Bearbeiten
134                </button>
135            </div>
136            ZZZZZZZZZZ;
137
138        $pretty_date = '(irgendwann)';
139        $duration_interval = \DateInterval::createFromDateString("+{$duration_seconds} seconds");
140        if ($start_time) {
141            $end_time = (clone $start_time)->add($duration_interval);
142            $pretty_date = $duration_seconds
143                ? $start_time->format('H:i')." – ".$end_time->format('H:i')
144                : $start_time->format('H:i');
145        }
146        $label_imgs = implode('', array_map(function (TerminLabel $label) use ($code_path, $code_href) {
147            $ident = $label->getIdent();
148            // TODO: Remove fallback mechanism?
149            $fallback_path = "{$code_path}assets/icns/termine_type_{$ident}_20.svg";
150            $fallback_href = is_file($fallback_path)
151                ? "{$code_href}assets/icns/termine_type_{$ident}_20.svg" : null;
152            $icon_href = $label->getIcon() ? $label->getFileHref($label->getIcon()) : $fallback_href;
153            return $icon_href ? "<img src='{$icon_href}' alt='' class='type-icon'>" : '';
154        }, $labels));
155
156        $out .= "<h5>{$pretty_date}</h5>";
157        $out .= "<h1>{$title} {$label_imgs}</h1>";
158        if ($organizer) {
159            $pretty_organizer = OlzUserInfoModal::render(['user' => $organizer]);
160            $out .= "<div>Organisator: {$pretty_organizer}</div><br>";
161        }
162
163        if ($termin_location) {
164            $out .= OlzLocationMap::render([
165                'name' => $termin_location->getName(),
166                'latitude' => $termin_location->getLatitude(),
167                'longitude' => $termin_location->getLongitude(),
168                'zoom' => 13,
169            ]);
170        }
171
172        $text_html = $this->htmlUtils()->renderMarkdown($text);
173        $text_html = $termin_template->replaceImagePaths($text_html);
174        $text_html = $termin_template->replaceFilePaths($text_html);
175        $out .= "<div>{$text_html}</div>";
176
177        if (count($image_ids) > 0) {
178            $out .= "<h3>Bilder</h3><div class='lightgallery gallery-container'>";
179            foreach ($image_ids as $image_id) {
180                $out .= "<div class='gallery-image'>";
181                $out .= $this->imageUtils()->olzImage(
182                    'termin_templates',
183                    $id,
184                    $image_id,
185                    128,
186                    'gallery[myset]'
187                );
188                $out .= "</div>";
189            }
190            $out .= "</div>";
191        }
192
193        $out .= "</div>"; // olz-termin-location-detail
194        $out .= "</div>"; // content-middle
195
196        $out .= OlzFooter::render();
197
198        return $out;
199    }
200
201    protected function getTerminTemplateById(int $id): ?TerminTemplate {
202        $termin_template_repo = $this->entityManager()->getRepository(TerminTemplate::class);
203        return $termin_template_repo->findOneBy([
204            'id' => $id,
205            'on_off' => 1,
206        ]);
207    }
208}