Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 209
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 / 209
0.00% covered (danger)
0.00%
0 / 5
2652
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 / 186
0.00% covered (danger)
0.00%
0 / 1
1806
 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{filter?: ?string, 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        $params = $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        $back_filter = urlencode($params['filter'] ?? '{}');
89        $out = OlzHeader::render([
90            'back_link' => "{$code_href}termine?filter={$back_filter}",
91            'title' => "{$title} - Termine",
92            'description' => "Orientierungslauf-Wettkämpfe, OL-Wochen, OL-Weekends, Trainings und Vereinsanlässe der OL Zimmerberg.",
93            'norobots' => $is_archived,
94            'canonical_url' => "{$code_href}termine/{$id}",
95        ]);
96
97        $out .= <<<'ZZZZZZZZZZ'
98            <div class='content-right optional'>
99                <div style='padding:4px 3px 10px 3px;'>
100                </div>
101            </div>
102            <div class='content-middle'>
103            ZZZZZZZZZZ;
104
105        $start_date = $termin->getStartDate();
106        $end_date = $termin->getEndDate() ?? null;
107        $start_time = $termin->getStartTime() ?? null;
108        $end_time = $termin->getEndTime() ?? null;
109        $text = $termin->getText() ?? '';
110        $labels = [...$termin->getLabels()];
111        $xkoord = $termin->getCoordinateX() ?? 0;
112        $ykoord = $termin->getCoordinateY() ?? 0;
113        $solv_uid = $termin->getSolvId();
114        $termin_location = $termin->getLocation();
115        $has_olz_location = ($xkoord > 0 && $ykoord > 0);
116        $has_termin_location = (
117            $termin_location
118            && $termin_location->getLatitude() > 0
119            && $termin_location->getLongitude() > 0
120        );
121        $lat = null;
122        $lng = null;
123        $location_name = null;
124        if ($has_termin_location) {
125            $lat = $termin_location->getLatitude();
126            $lng = $termin_location->getLongitude();
127            $location_name = $termin_location->getName();
128        }
129        if ($has_olz_location) {
130            $lat = $this->mapUtils()->CHtoWGSlat($xkoord, $ykoord);
131            $lng = $this->mapUtils()->CHtoWGSlng($xkoord, $ykoord);
132            $location_name = null;
133        }
134        $has_location = $has_olz_location || $has_termin_location;
135        $image_ids = $termin->getImageIds();
136
137        $out .= OlzEventData::render([
138            'name' => $title,
139            'start_date' => $date_utils->olzDate('jjjj-mm-tt', $start_date),
140            'end_date' => $end_date ? $date_utils->olzDate('jjjj-mm-tt', $end_date) : null,
141            'location' => $has_location ? [
142                'lat' => $lat,
143                'lng' => $lng,
144                'name' => $location_name,
145            ] : null,
146        ]);
147
148        $out .= "<div class='olz-termin-detail'>";
149
150        $out .= "<div class='preview'>";
151        // Bild anzeigen
152        if (count($image_ids) > 0) {
153            $out .= $this->imageUtils()->olzImage(
154                'termine',
155                $id,
156                $image_ids[0],
157                840
158            );
159        // Karte zeigen
160        } elseif ($has_location) {
161            $out .= OlzLocationMap::render([
162                'latitude' => $lat,
163                'longitude' => $lng,
164                'zoom' => 13,
165            ]);
166        }
167        // Date Calendar Icon
168        $out .= "<div class='date-calendar-container'>";
169        $out .= "<div class='date-calendars'>";
170        $out .= "<div class='date-calendar'>";
171        $out .= OlzDateCalendar::render(['date' => $start_date]);
172        $out .= $this->getTimeText($start_time) ?? '';
173        $out .= ($end_time && (!$end_date || $end_date === $start_date))
174            ? ' &ndash; '.$this->getTimeText($end_time)
175            : '';
176        $out .= "</div>";
177        $out .= "<div class='date-calendar'>";
178        $out .= ($end_date && $end_date !== $start_date)
179            ? OlzDateCalendar::render(['date' => $end_date])
180            : '';
181        $out .= ($end_time && $end_date && $end_date !== $start_date)
182            ? $this->getTimeText($end_time)
183            : '';
184        $out .= "</div>";
185        $out .= "</div>";
186        $out .= "</div>";
187
188        $out .= "</div>";
189
190        // Editing Tools
191        $is_owner = $user && intval($termin->getOwnerUser()?->getId() ?? 0) === intval($user->getId());
192        $has_termine_permissions = $this->authUtils()->hasPermission('termine');
193        $can_edit = $is_owner || $has_termine_permissions;
194        if ($can_edit) {
195            $json_id = json_encode($id);
196            $out .= <<<ZZZZZZZZZZ
197                <div>
198                    <button
199                        id='edit-termin-button'
200                        class='btn btn-primary'
201                        onclick='return olz.editTermin({$json_id})'
202                    >
203                        <img src='{$code_href}assets/icns/edit_white_16.svg' class='noborder' />
204                        Bearbeiten
205                    </button>
206                </div>
207                ZZZZZZZZZZ;
208        }
209
210        // Date & Title
211        $pretty_date = $this->dateUtils()->formatDateTimeRange(
212            $start_date->format('Y-m-d'),
213            $start_time?->format('H:i:s'),
214            $end_date?->format('Y-m-d'),
215            $end_time?->format('H:i:s'),
216            $format = 'long',
217        );
218        $maybe_solv_link = '';
219        if ($solv_uid) {
220            // SOLV-Übersicht-Link zeigen
221            $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";
222        }
223        $label_imgs = implode('', array_map(function (TerminLabel $label) use ($code_path, $code_href) {
224            $ident = $label->getIdent();
225            // TODO: Remove fallback mechanism?
226            $fallback_path = "{$code_path}assets/icns/termine_type_{$ident}_20.svg";
227            $fallback_href = is_file($fallback_path)
228                ? "{$code_href}assets/icns/termine_type_{$ident}_20.svg" : null;
229            $icon_href = $label->getIcon() ? $label->getFileHref($label->getIcon()) : $fallback_href;
230            return $icon_href ? "<img src='{$icon_href}' alt='' class='type-icon'>" : '';
231        }, $labels));
232        $out .= "<h5>{$pretty_date}{$maybe_solv_link}</h5>";
233        $out .= "<h1>{$title} {$label_imgs}</h1>";
234
235        // Text
236        // TODO: Temporary fix for broken Markdown
237        $text = str_replace("\n", "\n\n", $text);
238        $text = str_replace("\n\n\n\n", "\n\n", $text);
239        $text_html = $this->htmlUtils()->renderMarkdown($text, [
240            'html_input' => 'allow', // TODO: Do NOT allow!
241        ]);
242        $text_html = $termin->replaceImagePaths($text_html);
243        $text_html = $termin->replaceFilePaths($text_html);
244        if ($termin->getDeadline() && $termin->getDeadline() != "0000-00-00") {
245            $text_html .= ($text_html == "" ? "" : "<br />")."Meldeschluss: ".$date_utils->olzDate("t. MM ", $termin->getDeadline());
246        }
247        $out .= "<div>".$text_html."</div>";
248
249        // Link
250        $link = '';
251        if ($solv_uid && $start_date <= $today && !preg_match('/(Rangliste|Resultat)/', $link)) {
252            // SOLV Ranglisten-Link zeigen
253            $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";
254        }
255        $result_filename = "{$termin_year}-termine-{$id}.xml";
256        if (is_file("{$data_path}results/{$result_filename}")) {
257            // OLZ Ranglisten-Link zeigen
258            $link .= "<div><a href='{$code_href}apps/resultate?file={$result_filename}' target='_blank' class='linkext'>Ranglisten</a></div>\n";
259        } elseif ($can_edit) {
260            // OLZ Rangliste-hochladen-Link zeigen
261            $link .= "<div><a href='{$code_href}apps/resultate?file={$result_filename}' target='_blank' class='linkext'>Rangliste hochladen</a></div>\n";
262        }
263        if ($link == "") {
264            $link = "&nbsp;";
265        } else {
266            $link = str_replace("&", "&amp;", str_replace("&amp;", "&", $link));
267        }
268        $link = str_replace("www.solv.ch", "www.o-l.ch", $link);
269        $out .= "<div class='links'>".$link."</div>";
270
271        // Karte zeigen
272        if ($has_location) {
273            if ($location_name !== null) {
274                $location_maybe_link = $location_name;
275                if ($has_termin_location) {
276                    $location_maybe_link = "<a href='{$code_href}termine/orte/{$termin_location->getId()}?filter={$back_filter}&id={$id}' class='linkmap'>{$location_name}</a>";
277                }
278                $out .= "<h3>Ort: {$location_maybe_link}</h3>";
279            } else {
280                $out .= "<h3>Ort</h3>";
281            }
282            $out .= OlzLocationMap::render([
283                'name' => $location_name ?? '',
284                'latitude' => $lat,
285                'longitude' => $lng,
286                'zoom' => 13,
287            ]);
288        }
289
290        $out .= "</div>"; // olz-termin-detail
291        $out .= "</div>"; // content-middle
292
293        $out .= OlzFooter::render();
294
295        return $out;
296    }
297
298    protected function getTerminById(int $id): ?Termin {
299        $termin_repo = $this->entityManager()->getRepository(Termin::class);
300        return $termin_repo->findOneBy([
301            'id' => $id,
302            'on_off' => 1,
303        ]);
304    }
305
306    protected function getTimeText(?\DateTime $time): ?string {
307        if (!$time || $time->format('H:i:s') === '00:00:00') {
308            return null;
309        }
310        return $time->format('H:i');
311    }
312}