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