Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.87% covered (warning)
78.87%
56 / 71
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
OlzMenu
78.87% covered (warning)
78.87%
56 / 71
66.67% covered (warning)
66.67%
2 / 3
19.73
0.00% covered (danger)
0.00%
0 / 1
 getHtml
70.59% covered (warning)
70.59%
36 / 51
0.00% covered (danger)
0.00%
0 / 1
11.06
 getMenu
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getMenuItem
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3// =============================================================================
4// Das Navigationsmenu der Website.
5// =============================================================================
6
7namespace Olz\Components\Page\OlzMenu;
8
9use Olz\Components\Common\OlzComponent;
10
11/**
12 * @phpstan-type MenuItem array{name: string, ident: string, href: string}
13 *
14 * @extends OlzComponent<array<string, mixed>>
15 */
16class OlzMenu extends OlzComponent {
17    public function getHtml(mixed $args): string {
18        $out = '';
19
20        $code_href = $this->envUtils()->getCodeHref();
21        $data_path = $this->envUtils()->getDataPath();
22
23        $news_utils = $this->newsUtils();
24        $enc_news_filter = $news_utils->serialize($news_utils->getDefaultFilter());
25        $termine_utils = $this->termineUtils();
26        $enc_termine_filter = $termine_utils->serialize($termine_utils->getDefaultFilter());
27
28        $main_menu = [
29            ['name' => "Startseite", 'ident' => 'startseite', 'href' => ''],
30            null,
31            ['name' => "News", 'ident' => 'news', 'href' => "news?filter={$enc_news_filter}&seite=1"],
32            ['name' => "Termine", 'ident' => 'termine', 'href' => "termine?filter={$enc_termine_filter}"],
33            null,
34            ['name' => "Angebot", 'ident' => 'angebot', 'href' => 'angebot'],
35            ['name' => "Karten", 'ident' => 'karten', 'href' => 'karten'],
36            null,
37            ['name' => "Service", 'ident' => 'service', 'href' => 'service'],
38            ['name' => "Verein", 'ident' => 'verein', 'href' => 'verein'],
39        ];
40
41        // BACK-BUTTON
42        $back_menu_out = '';
43        $back_link = $args['back_link'] ?? null;
44        if ($back_link !== null) {
45            $back_menu_out = <<<ZZZZZZZZZZ
46                <a href='{$back_link}' class='menu-link' id='back-link'>
47                    <div class='menutag'>
48                        <img src='{$code_href}assets/icns/back_16.svg' alt='&lt;' class='back-icon'>
49                        Zurück
50                    </div>
51                </a>
52                ZZZZZZZZZZ;
53        }
54
55        // LIVE-RESULTATE
56        $live_menu_out = '';
57        $live_json_path = "{$data_path}results/_live.json";
58        if (is_file($live_json_path)) {
59            $content = file_get_contents($live_json_path);
60            if ($content) {
61                $live = json_decode($content, true);
62                $last_updated_at = strtotime($live['last_updated_at']) ?: 0;
63                $now = strtotime($this->dateUtils()->getIsoNow()) ?: 0;
64                if ($live && $last_updated_at > $now - 3600) {
65                    $live_file = $live['file'];
66                    $style = preg_match('/test/', $live_file) ? " style='display:none;'" : "";
67                    $live_menu_out = <<<ZZZZZZZZZZ
68                        <a href='{$code_href}apps/resultate/?file={$live_file}'{$style} class='menu-link' id='live-results-link'>
69                            <div class='menutag'>
70                                Live-Resultate
71                            </div>
72                        </a>
73                        ZZZZZZZZZZ;
74                }
75            }
76        }
77
78        $main_menu_out = $this->getMenu($main_menu);
79
80        $out .= <<<ZZZZZZZZZZ
81            <div id='menu' class='menu'>
82                <div class='back-menu'>{$back_menu_out}</div>
83                <div class='live-menu'>{$live_menu_out}</div>
84                <div class='main-menu'>{$main_menu_out}</div>
85                <div class='feedback-mail'>
86                    <script type='text/javascript'>
87                        olz.MailTo("website", "olzimmerberg.ch", "Feedback geben", "Homepage%20OL%20Zimmerberg");
88                    </script>
89                </div>
90                <div class='platform-links'>
91                    <a
92                        href='https://github.com/olzimmerberg/olz-website'
93                        target='_blank'
94                        rel='noreferrer noopener'
95                        title='OL Zimmerberg auf GitHub'
96                        class='platform-link'
97                    >
98                        <img src='{$code_href}assets/icns/github_16.svg' alt='g' class='noborder' />
99                    </a>
100                    <a
101                        href='https://www.youtube.com/channel/UCMhMdPRJOqdXHlmB9kEpmXQ'
102                        target='_blank'
103                        rel='noreferrer noopener'
104                        title='OL Zimmerberg auf YouTube'
105                        class='platform-link'
106                    >
107                        <img src='{$code_href}assets/icns/youtube_16.svg' alt='Y' class='noborder' />
108                    </a>
109                    <a
110                        href='https://www.strava.com/clubs/olzimmerberg'
111                        target='_blank'
112                        rel='noreferrer noopener'
113                        title='OL Zimmerberg auf Strava'
114                        class='platform-link'
115                    >
116                        <img src='{$code_href}assets/icns/strava_16.svg' alt='s' class='noborder' />
117                    </a>
118                </div>
119            </div>
120            ZZZZZZZZZZ;
121
122        return $out;
123    }
124
125    /** @param array<?MenuItem> $menu */
126    protected function getMenu(array $menu): string {
127        $out = '';
128        for ($i = 0; $i < count($menu); $i++) {
129            $menupunkt = $menu[$i];
130            $out .= $this->getMenuItem($menupunkt);
131        }
132        return $out;
133    }
134
135    /**
136     * @param ?MenuItem $menu_item
137     */
138    protected function getMenuItem(?array $menu_item): string {
139        if ($menu_item === null) {
140            return <<<'ZZZZZZZZZZ'
141                <div class='separator'></div>
142                ZZZZZZZZZZ;
143        }
144        $code_href = $this->envUtils()->getCodeHref();
145        $href = $menu_item['href'];
146        $href_path = substr($href, 0, strpos($href, '?') ?: strlen($href));
147        $request_uri = $_SERVER['REQUEST_URI'] ?? '';
148        $is_active = (
149            preg_match("/^\\/{$href_path}(\\/|\\?|#|$)/", $request_uri)
150            || ($href === '' && $request_uri === '')
151        );
152        $active_class = $is_active ? ' active' : '';
153        return <<<ZZZZZZZZZZ
154            <a href='{$code_href}{$href}' id='menu_a_page_{$menu_item['ident']}' class='menu-link'>
155                <div class='menutag{$active_class}'>
156                    {$menu_item['name']}
157                </div>
158            </a>
159            ZZZZZZZZZZ;
160    }
161}