Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 228
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
OlzTerminDetailParams
n/a
0 / 0
n/a
0 / 0
0
n/a
0 / 0
OlzTerminDetail
0.00% covered (danger)
0.00%
0 / 228
0.00% covered (danger)
0.00%
0 / 5
3080
0.00% covered (danger)
0.00%
0 / 1
 getSearchTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSearchResults
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
 getHtml
0.00% covered (danger)
0.00%
0 / 205
0.00% covered (danger)
0.00%
0 / 1
2162
 getTerminById
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getTimeText
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Olz\Termine\Components\OlzTerminDetail;
4
5use Doctrine\Common\Collections\Criteria;
6use Olz\Components\Common\OlzLocationMap\OlzLocationMap;
7use Olz\Components\Common\OlzRootComponent;
8use Olz\Components\Page\OlzFooter\OlzFooter;
9use Olz\Components\Page\OlzHeader\OlzHeader;
10use Olz\Components\Schema\OlzEventData\OlzEventData;
11use Olz\Entity\Termine\Termin;
12use Olz\Entity\Termine\TerminLabel;
13use Olz\Termine\Components\OlzDateCalendar\OlzDateCalendar;
14use Olz\Utils\HttpParams;
15
16/** @extends HttpParams<array{von?: ?string}> */
17class OlzTerminDetailParams extends HttpParams {
18}
19
20/** @extends OlzRootComponent<array<string, mixed>> */
21class OlzTerminDetail extends OlzRootComponent {
22    public function getSearchTitle(): string {
23        return 'Termine';
24    }
25
26    public function getSearchResults(array $terms): array {
27        $results = [];
28        $code_href = $this->envUtils()->getCodeHref();
29        $termin_repo = $this->entityManager()->getRepository(Termin::class);
30        $termine = $termin_repo->search($terms);
31        foreach ($termine as $termin) {
32            $id = $termin->getId();
33            $results[] = $this->searchUtils()->getScoredSearchResult([
34                'link' => "{$code_href}termine/{$id}",
35                'icon' => "{$code_href}assets/icns/termine_type_all_20.svg",
36                'date' => $termin->getStartDate(),
37                'title' => $termin->getTitle() ?: '?',
38                'text' => $termin->getText() ?: null,
39            ], $terms);
40        }
41        return $results;
42    }
43
44    public function getHtml(mixed $args): string {
45        $this->httpUtils()->validateGetParams(OlzTerminDetailParams::class);
46
47        $code_href = $this->envUtils()->getCodeHref();
48        $code_path = $this->envUtils()->getCodePath();
49        $data_path = $this->envUtils()->getDataPath();
50        $date_utils = $this->dateUtils();
51        $today = $date_utils->getIsoToday();
52        $entityManager = $this->dbUtils()->getEntityManager();
53        $user = $this->authUtils()->getCurrentUser();
54        $id = $args['id'] ?? null;
55
56        $termin_repo = $entityManager->getRepository(Termin::class);
57        $is_not_archived = $this->termineUtils()->getIsNotArchivedCriteria();
58        $criteria = Criteria::create()
59            ->where(Criteria::expr()->andX(
60                $is_not_archived,
61                Criteria::expr()->eq('id', $id),
62                Criteria::expr()->eq('on_off', 1),
63            ))
64            ->setFirstResult(0)
65            ->setMaxResults(1)
66        ;
67        $termine = $termin_repo->matching($criteria);
68        $num_termine = $termine->count();
69        $is_archived = $num_termine !== 1;
70
71        if ($is_archived && !$this->authUtils()->hasPermission('any')) {
72            $this->httpUtils()->dieWithHttpError(404);
73            throw new \Exception('should already have failed');
74        }
75
76        $termin = $this->getTerminById($id);
77
78        if (!$termin) {
79            $this->httpUtils()->dieWithHttpError(404);
80            throw new \Exception('should already have failed');
81        }
82
83        $title = $termin->getTitle() ?? '';
84        $termin_year = $termin->getStartDate()->format('Y');
85        $this_year = $this->dateUtils()->getCurrentDateInFormat('Y');
86        $maybe_date = ($termin_year !== $this_year) ? " {$termin_year}" : '';
87        $title = "{$title}{$maybe_date}";
88        $out = OlzHeader::render([
89            'back_link' => "{$code_href}termine",
90            'title' => "{$title} - Termine",
91            'description' => "Orientierungslauf-Wettkämpfe, OL-Wochen, OL-Weekends, Trainings und Vereinsanlässe der OL Zimmerberg.",
92            'norobots' => $is_archived,
93            'canonical_url' => "{$code_href}termine/{$id}",
94        ]);
95
96        $out .= <<<'ZZZZZZZZZZ'
97            <div class='content-right optional'>
98                <div style='padding:4px 3px 10px 3px;'>
99                </div>
100            </div>
101            <div class='content-middle'>
102            ZZZZZZZZZZ;
103
104        $start_date = $termin->getStartDate();
105        $end_date = $termin->getEndDate() ?? null;
106        $start_time = $termin->getStartTime() ?? null;
107        $end_time = $termin->getEndTime() ?? null;
108        $text = $termin->getText() ?? '';
109        $labels = [...$termin->getLabels()];
110        $xkoord = $termin->getCoordinateX() ?? 0;
111        $ykoord = $termin->getCoordinateY() ?? 0;
112        $solv_uid = $termin->getSolvId();
113        $termin_location = $termin->getLocation();
114        $has_olz_location = ($xkoord > 0 && $ykoord > 0);
115        $has_termin_location = (
116            $termin_location
117            && $termin_location->getLatitude() > 0
118            && $termin_location->getLongitude() > 0
119        );
120        $lat = null;
121        $lng = null;
122        $location_name = null;
123        if ($has_termin_location) {
124            $lat = $termin_location->getLatitude();
125            $lng = $termin_location->getLongitude();
126            $location_name = $termin_location->getName();
127        }
128        if ($has_olz_location) {
129            $lat = $this->mapUtils()->CHtoWGSlat($xkoord, $ykoord);
130            $lng = $this->mapUtils()->CHtoWGSlng($xkoord, $ykoord);
131            $location_name = null;
132        }
133        $has_location = $has_olz_location || $has_termin_location;
134        $image_ids = $termin->getImageIds();
135
136        $out .= OlzEventData::render([
137            'name' => $title,
138            'start_date' => $date_utils->olzDate('jjjj-mm-tt', $start_date),
139            'end_date' => $end_date ? $date_utils->olzDate('jjjj-mm-tt', $end_date) : null,
140            'location' => $has_location ? [
141                'lat' => $lat,
142                'lng' => $lng,
143                'name' => $location_name,
144            ] : null,
145        ]);
146
147        $out .= "<div class='olz-termin-detail'>";
148
149        $out .= "<div class='preview'>";
150        // Bild anzeigen
151        if (count($image_ids) > 0) {
152            $out .= $this->imageUtils()->olzImage(
153                'termine',
154                $id,
155                $image_ids[0],
156                840
157            );
158        // Karte zeigen
159        } elseif ($has_location) {
160            $out .= OlzLocationMap::render([
161                'latitude' => $lat,
162                'longitude' => $lng,
163                'zoom' => 13,
164            ]);
165        }
166        // Date Calendar Icon
167        $out .= "<div class='date-calendar-container'>";
168        $out .= "<div class='date-calendars'>";
169        $out .= "<div class='date-calendar'>";
170        $out .= OlzDateCalendar::render(['date' => $start_date]);
171        $out .= $this->getTimeText($start_time) ?? '';
172        $out .= ($end_time && (!$end_date || $end_date === $start_date))
173            ? ' &ndash; '.$this->getTimeText($end_time)
174            : '';
175        $out .= "</div>";
176        $out .= "<div class='date-calendar'>";
177        $out .= ($end_date && $end_date !== $start_date)
178            ? OlzDateCalendar::render(['date' => $end_date])
179            : '';
180        $out .= ($end_time && $end_date && $end_date !== $start_date)
181            ? $this->getTimeText($end_time)
182            : '';
183        $out .= "</div>";
184        $out .= "</div>";
185        $out .= "</div>";
186
187        $out .= "</div>";
188
189        // Editing Tools
190        $is_owner = $user && intval($termin->getOwnerUser()?->getId() ?? 0) === intval($user->getId());
191        $has_termine_permissions = $this->authUtils()->hasPermission('termine');
192        $can_edit = $is_owner || $has_termine_permissions;
193        if ($can_edit) {
194            $json_id = json_encode($id);
195            $out .= <<<ZZZZZZZZZZ
196                <div>
197                    <button
198                        id='edit-termin-button'
199                        class='btn btn-primary'
200                        onclick='return olz.editTermin({$json_id})'
201                    >
202                        <img src='{$code_href}assets/icns/edit_white_16.svg' class='noborder' />
203                        Bearbeiten
204                    </button>
205                </div>
206                ZZZZZZZZZZ;
207        }
208
209        // Date & Title
210        $pretty_date = $this->dateUtils()->formatDateTimeRange(
211            $start_date->format('Y-m-d'),
212            $start_time?->format('H:i:s'),
213            $end_date?->format('Y-m-d'),
214            $end_time?->format('H:i:s'),
215            $format = 'long',
216        );
217        $maybe_solv_link = '';
218        if ($solv_uid) {
219            // SOLV-Übersicht-Link zeigen
220            $maybe_solv_link .= "<a href='https://www.o-l.ch/cgi-bin/fixtures?&mode=show&unique_id={$solv_uid}' target='_blank' class='linkol' style='margin-left: 20px; font-weight: normal;'>O-L.ch</a>\n";
221        }
222        $label_imgs = implode('', array_map(function (TerminLabel $label) use ($code_path, $code_href) {
223            $ident = $label->getIdent();
224            // TODO: Remove fallback mechanism?
225            $fallback_path = "{$code_path}assets/icns/termine_type_{$ident}_20.svg";
226            $fallback_href = is_file($fallback_path)
227                ? "{$code_href}assets/icns/termine_type_{$ident}_20.svg" : null;
228            $icon_href = $label->getIcon() ? $label->getFileHref($label->getIcon()) : $fallback_href;
229            return $icon_href ? "<img src='{$icon_href}' alt='' class='type-icon'>" : '';
230        }, $labels));
231        $out .= "<h5>{$pretty_date}{$maybe_solv_link}</h5>";
232        $out .= "<h1>{$title} {$label_imgs}</h1>";
233
234        // Text
235        // TODO: Temporary fix for broken Markdown
236        $text = str_replace("\n", "\n\n", $text);
237        $text = str_replace("\n\n\n\n", "\n\n", $text);
238        $text_html = $this->htmlUtils()->renderMarkdown($text, [
239            'html_input' => 'allow', // TODO: Do NOT allow!
240        ]);
241        $text_html = $termin->replaceImagePaths($text_html);
242        $text_html = $termin->replaceFilePaths($text_html);
243        if ($termin->getDeadline() && $termin->getDeadline() != "0000-00-00") {
244            $text_html .= ($text_html == "" ? "" : "<br />")."Meldeschluss: ".$date_utils->olzDate("t. MM ", $termin->getDeadline());
245        }
246        $out .= "<div>".$text_html."</div>";
247
248        // Link
249        $link = '';
250        if ($solv_uid && $start_date <= $today && !preg_match('/(Rangliste|Resultat)/', $link)) {
251            // SOLV Ranglisten-Link zeigen
252            $link .= "<div><a href='http://www.o-l.ch/cgi-bin/results?unique_id={$solv_uid}&club=zimmerberg' target='_blank' class='linkol'>Rangliste</a></div>\n";
253        }
254        $result_filename = "{$termin_year}-termine-{$id}.xml";
255        if (is_file("{$data_path}results/{$result_filename}")) {
256            // OLZ Ranglisten-Link zeigen
257            $link .= "<div><a href='{$code_href}apps/resultate?file={$result_filename}' target='_blank' class='linkext'>Ranglisten</a></div>\n";
258        } elseif ($can_edit) {
259            // OLZ Rangliste-hochladen-Link zeigen
260            $link .= "<div><a href='{$code_href}apps/resultate?file={$result_filename}' target='_blank' class='linkext'>Rangliste hochladen</a></div>\n";
261        }
262        if ($link == "") {
263            $link = "&nbsp;";
264        } else {
265            $link = str_replace("&", "&amp;", str_replace("&amp;", "&", $link));
266        }
267        $link = str_replace("www.solv.ch", "www.o-l.ch", $link);
268        $out .= "<div class='links'>".$link."</div>";
269
270        // Karte zeigen
271        if ($has_location) {
272            if ($location_name !== null) {
273                $location_maybe_link = $location_name;
274                if ($has_termin_location && $this->authUtils()->hasPermission('termine')) {
275                    $location_maybe_link = "<a href='{$code_href}termine/orte/{$termin_location->getId()}' class='linkmap'>{$location_name}</a>";
276                }
277                $out .= "<h3>Ort: {$location_maybe_link}</h3>";
278            } else {
279                $out .= "<h3>Ort</h3>";
280            }
281            $out .= OlzLocationMap::render([
282                'name' => $location_name ?? '',
283                'latitude' => $lat,
284                'longitude' => $lng,
285                'zoom' => 13,
286            ]);
287            if ($has_termin_location) {
288                $details = $termin_location->getDetails() ?? '';
289                $details_html = $this->htmlUtils()->renderMarkdown($details);
290                $details_html = $termin_location->replaceImagePaths($details_html);
291                $details_html = $termin_location->replaceFilePaths($details_html);
292                $out .= $details_html;
293
294                $location_image_ids = $termin_location->getImageIds();
295                if (count($location_image_ids) > 0) {
296                    $out .= "<div class='lightgallery gallery-container'>";
297                    foreach ($location_image_ids as $image_id) {
298                        $out .= "<div class='gallery-image'>";
299                        $out .= $this->imageUtils()->olzImage(
300                            'termin_locations',
301                            $termin_location->getId(),
302                            $image_id,
303                            110,
304                            'gallery[myset]'
305                        );
306                        $out .= "</div>";
307                    }
308                    $out .= "</div>";
309                }
310            }
311        }
312
313        $out .= "</div>"; // olz-termin-detail
314        $out .= "</div>"; // content-middle
315
316        $out .= OlzFooter::render();
317
318        return $out;
319    }
320
321    protected function getTerminById(int $id): ?Termin {
322        $termin_repo = $this->entityManager()->getRepository(Termin::class);
323        return $termin_repo->findOneBy([
324            'id' => $id,
325            'on_off' => 1,
326        ]);
327    }
328
329    protected function getTimeText(?\DateTime $time): ?string {
330        if (!$time || $time->format('H:i:s') === '00:00:00') {
331            return null;
332        }
333        return $time->format('H:i');
334    }
335}