Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 5
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 / 63
0.00% covered (danger)
0.00%
0 / 5
306
0.00% covered (danger)
0.00%
0 / 1
 getTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 getHtml
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 1
156
1<?php
2
3namespace Olz\Suche\Components\OlzSuche;
4
5use Olz\Components\Common\OlzPostingListItem\OlzPostingListItem;
6use Olz\Components\Common\OlzRootComponent;
7use Olz\Components\Page\OlzFooter\OlzFooter;
8use Olz\Components\Page\OlzHeader\OlzHeader;
9use Olz\Utils\HttpParams;
10
11/** @extends HttpParams<array{anfrage: string}> */
12class OlzSucheParams extends HttpParams {
13}
14
15/** @extends OlzRootComponent<array<string, mixed>> */
16class OlzSuche extends OlzRootComponent {
17    public function getTitle(): string {
18        return "Suche";
19    }
20
21    public function getDescription(string $pretty_terms): string {
22        return "Stichwort-Suche nach \"{$pretty_terms}\" auf der Website der OL Zimmerberg.";
23    }
24
25    public string $description = "Stichwort-Suche auf der Website der OL Zimmerberg.";
26
27    public function getSearchTitle(): string {
28        return 'Suche';
29    }
30
31    public function getSearchResults(array $terms): array {
32        $code_href = $this->envUtils()->getCodeHref();
33        $content = "{$this->getTitle()} - {$this->getDescription('Suche')}";
34        return [
35            ...$this->searchUtils()->getStaticSearchResults($content, $terms, [
36                'link' => "{$code_href}suche?anfrage=Suche",
37                'title' => $this->searchUtils()->highlight('Suche', $terms) ?: '?',
38            ]),
39        ];
40    }
41
42    public function getHtml(mixed $args): string {
43        $params = $this->httpUtils()->validateGetParams(OlzSucheParams::class);
44
45        $terms = preg_split('/[\s,\.;]+/', $params['anfrage']);
46        $this->generalUtils()->checkNotFalse($terms, "Could not split search terms '{$params['anfrage']}'");
47        $pretty_terms = implode(', ', $terms);
48        $esc_pretty_terms = htmlspecialchars($pretty_terms);
49
50        $out = OlzHeader::render([
51            'title' => "\"{$pretty_terms}\" - {$this->getTitle()}",
52            'description' => $this->getDescription($pretty_terms),
53        ]);
54
55        $out .= <<<'ZZZZZZZZZZ'
56            <div class='content-right'>
57            </div>
58            <div class='content-middle olz-suche'>
59            ZZZZZZZZZZ;
60
61        $out .= "<h1>Suchresultate für \"{$esc_pretty_terms}\"</h1>";
62
63        if (($terms[0] ?? '') === '') {
64            $out .= "<p><i>Keine Resultate</i></p>";
65            $out .= OlzFooter::render();
66            return $out;
67        }
68
69        $start_time = microtime(true);
70
71        $sections = $this->searchUtils()->getSearchResults($terms);
72        $has_results = false;
73        foreach ($sections as $section) {
74            if ($section['bestScore'] === null) {
75                continue;
76            }
77            $has_results = true;
78            $pretty_best_score = $this->authUtils()->hasPermission('all') ? " (Score: {$section['bestScore']})" : '';
79            $out .= "<h2 class='bar green'>{$section['title']}{$pretty_best_score}</h2>";
80            foreach ($section['results'] as $result) {
81                $pretty_date = null;
82                if ($result['date']) {
83                    $pretty_date = $this->dateUtils()->olzDate("tt.mm.jj", $result['date']);
84                    $date_formattings = implode(' ', $this->searchUtils()->getDateFormattings($result['date']));
85                    $is_date_matching = false;
86                    foreach ($terms as $term) {
87                        $esc_term = preg_quote($term);
88                        if (preg_match("/{$esc_term}/i", $date_formattings)) {
89                            $is_date_matching = true;
90                            break;
91                        }
92                    }
93                    if ($is_date_matching) {
94                        $pretty_date = $this->searchUtils()->highlight($pretty_date, [$pretty_date]);
95                    }
96                }
97                $pretty_score = $this->authUtils()->hasPermission('all') ? " (Score: {$result['score']})" : '';
98                $out .= OlzPostingListItem::render([
99                    'link' => $result['link'],
100                    'icon' => $result['icon'],
101                    'date' => $pretty_date,
102                    'title' => $this->searchUtils()->highlight($result['title'], $terms).$pretty_score,
103                    'text' => $this->searchUtils()->highlight($result['text'] ?? '', $terms),
104                ]);
105            }
106        }
107        if (!$has_results) {
108            $out .= "<p><i>Keine Resultate</i></p>";
109        }
110
111        $duration = microtime(true) - $start_time;
112        $pretty_duration = number_format($duration, 3, '.', '\'');
113        $this->log()->info("Search for '{$pretty_terms}' took {$pretty_duration}s.");
114
115        $out .= "</div>";
116
117        $out .= OlzFooter::render();
118        return $out;
119    }
120}