Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 109
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
OlzSucheParams
n/a
0 / 0
n/a
0 / 0
0
n/a
0 / 0
OlzSuche
0.00% covered (danger)
0.00%
0 / 109
0.00% covered (danger)
0.00%
0 / 3
272
0.00% covered (danger)
0.00%
0 / 1
 getHtml
0.00% covered (danger)
0.00%
0 / 81
0.00% covered (danger)
0.00%
0 / 1
90
 cutout
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 highlight
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Olz\Suche\Components\OlzSuche;
4
5use Olz\Components\Common\OlzComponent;
6use Olz\Components\Page\OlzFooter\OlzFooter;
7use Olz\Components\Page\OlzHeader\OlzHeader;
8use Olz\Utils\HttpParams;
9
10/** @extends HttpParams<array{anfrage: string}> */
11class OlzSucheParams extends HttpParams {
12}
13
14/** @extends OlzComponent<array<string, mixed>> */
15class OlzSuche extends OlzComponent {
16    public function getHtml(mixed $args): string {
17        $params = $this->httpUtils()->validateGetParams(OlzSucheParams::class);
18        $search_key = $params['anfrage'];
19        $date_utils = $this->dateUtils();
20        $db = $this->dbUtils()->getDb();
21        $env_utils = $this->envUtils();
22        $code_href = $env_utils->getCodeHref();
23
24        $out = OlzHeader::render([
25            'title' => "Suche",
26            'description' => "Stichwort-Suche auf der Website der OL Zimmerberg.",
27            'norobots' => true,
28        ]);
29
30        $out .= <<<'ZZZZZZZZZZ'
31            <div class='content-right'>
32            </div>
33            <div class='content-middle'>
34            ZZZZZZZZZZ;
35
36        $search_key = trim(str_replace([",", ".", ";", "   ", "  "], [" ", " ", " ", " ", " "], $search_key));
37        $search_words = explode(" ", $search_key, 4);
38        $sql = "";
39
40        $sql_termine = '';
41        $sql_news = '';
42        for ($n = 0; $n < 3; $n++) {
43            $search_key = $search_words[$n] ?? '';
44            $search_key = $db->real_escape_string($search_key);
45            if ($search_key > "") {
46                $sql_termine .= <<<ZZZZZZZZZZ
47                    (
48                        (title LIKE '%{$search_key}%')
49                        OR (text LIKE '%{$search_key}%')
50                    )
51                    AND
52                    ZZZZZZZZZZ;
53            }
54            if ($search_key > "") {
55                $sql_news .= <<<ZZZZZZZZZZ
56                    (
57                        (title LIKE '%{$search_key}%')
58                        OR (teaser LIKE '%{$search_key}%')
59                        OR (content LIKE '%{$search_key}%')
60                    )
61                    AND
62                    ZZZZZZZZZZ;
63            }
64        }
65
66        $pretty_search_words = implode(', ', $search_words);
67        $out .= "<h2>Suchresultate (Suche nach: {$pretty_search_words})</h2>";
68
69        $result_termine = '';
70        $result_news = '';
71
72        // TERMINE
73        $sql = "SELECT * FROM termine WHERE {$sql_termine}(on_off = 1) ORDER BY start_date DESC";
74        $result = $db->query($sql);
75        // @phpstan-ignore-next-line
76        $num = mysqli_num_rows($result);
77        if ($num > 0) {
78            $result_termine .= "<tr><td colspan='2'><h3 class='bar green'>Termine...</h3></td></tr>";
79        }
80
81        for ($i = 0; $i < $num; $i++) {
82            // @phpstan-ignore-next-line
83            $row = mysqli_fetch_array($result);
84            // @phpstan-ignore-next-line
85            $title = strip_tags($row['title']);
86            // @phpstan-ignore-next-line
87            $text = strip_tags($row['text']);
88            // @phpstan-ignore-next-line
89            $id = $row['id'];
90            // @phpstan-ignore-next-line
91            $start_date = $date_utils->olzDate("t. MM jjjj", $row['start_date']);
92            $cutout = $this->cutout($text, $search_words);
93            $result_termine .= <<<ZZZZZZZZZZ
94                <tr>
95                    <td>
96                        <a href="{$code_href}termine/{$id}" class="linkint">
97                            <b>{$start_date}</b>
98                        </a>
99                    </td>
100                    <td>
101                        <a href="{$code_href}termine/{$id}" class="linkint">
102                            <b>{$this->highlight($title, $search_words)}</b>
103                        </a>
104                        <br>
105                        {$this->highlight($cutout, $search_words)}
106                    </td>
107                </tr>
108                ZZZZZZZZZZ;
109        }
110
111        // NEWS
112        $result = $db->query("SELECT * FROM news WHERE {$sql_news}(on_off = 1) ORDER BY published_date DESC");
113        // @phpstan-ignore-next-line
114        $num = mysqli_num_rows($result);
115        if ($num > 0) {
116            $result_news = "<tr><td colspan='2'><h3 class='bar green'>News...</h3></td></tr>";
117        }
118
119        for ($i = 0; $i < $num; $i++) {
120            // @phpstan-ignore-next-line
121            $row = mysqli_fetch_array($result);
122            // @phpstan-ignore-next-line
123            $title = strip_tags($row['title']);
124            // @phpstan-ignore-next-line
125            $text = strip_tags($row['teaser']).strip_tags($row['content']);
126            // @phpstan-ignore-next-line
127            $id = $row['id'];
128            // @phpstan-ignore-next-line
129            $published_date = $date_utils->olzDate("t. MM jjjj", $row['published_date']);
130            $cutout = $this->cutout($text, $search_words);
131            $result_news .= <<<ZZZZZZZZZZ
132                <tr>
133                    <td>
134                        <a href="{$code_href}news/{$id}" class="linkint">
135                            <b>{$published_date}</b>
136                        </a>
137                    </td>
138                    <td>
139                        <a href="{$code_href}news/{$id}" class="linkint">
140                            <b>{$this->highlight($title, $search_words)}</b>
141                        </a>
142                        <br>
143                        {$this->highlight($cutout, $search_words)}
144                    </td>
145                </tr>
146                ZZZZZZZZZZ;
147        }
148
149        $text = $result_termine.$result_news;
150
151        if ($text != '') {
152            $out .= "<table>".$text."</table>";
153        }
154
155        $out .= "</div>";
156
157        $out .= OlzFooter::render();
158        return $out;
159    }
160
161    /** @param array<string> $search_words */
162    protected function cutout(string $text, array $search_words): string {
163        $length_a = 40;
164        $length_b = 40;
165
166        for ($m = 0; $m < 3; $m++) {
167            $prefix = "...";
168            $suffix = "...";
169            $search_key = $search_words[$m] ?? '';
170            $start = strpos(strtolower($text), $search_key);
171            if ($start > 0) {
172                $m = 3;
173            }
174        }
175        if (($start - $length_a) < 0) {
176            $start = $length_a;
177            $prefix = "";
178        }
179        if (strlen($text) < ($length_a + $length_b)) {
180            $suffix = "";
181        }
182        $text = substr($text, $start - $length_a, $length_a + $length_b);
183        return "{$prefix}{$text}{$suffix}";
184    }
185
186    /** @param array<string> $search_words */
187    protected function highlight(string $text, array $search_words): string {
188        for ($n = 0; $n < 3; $n++) {
189            $search_key = $search_words[$n] ?? '';
190            $search_variants = [
191                $search_key,
192                strtoupper($search_key),
193                ucfirst($search_key), ];
194            $replace_variants = [
195                '<span style="color:red">'.$search_key.'</span>',
196                '<span style="color:red">'.strtoupper($search_key).'</span>',
197                '<span style="color:red">'.ucfirst($search_key).'</span>', ];
198            $text = str_replace($search_variants, $replace_variants, $text);
199        }
200        return $text;
201    }
202}