Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 76
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 / 76
0.00% covered (danger)
0.00%
0 / 6
210
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
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPageTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPageDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 searchSqlWhenHasAccess
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
2
 getHtmlWhenHasAccess
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 1
90
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 getDescription(string $pretty_terms): string {
22        return "Stichwort-Suche nach \"{$pretty_terms}\" auf der Website der OL Zimmerberg.";
23    }
24
25    public function getPageTitle(): string {
26        return "Suche";
27    }
28
29    public function getPageDescription(): string {
30        return "Stichwort-Suche auf der Website der OL Zimmerberg.";
31    }
32
33    public function searchSqlWhenHasAccess(array $terms): string|array|null {
34        $code_href = $this->envUtils()->getCodeHref();
35        $esc_title = $this->generalUtils()->internalSqlEscape($this->getPageTitle());
36        $esc_content = $this->generalUtils()->internalSqlEscape($this->getDescription('Suche'));
37        $where = implode(' AND ', array_map(function ($term) {
38            return <<<ZZZZZZZZZZ
39                (
40                    title LIKE '%{$term}%'
41                    OR text LIKE '%{$term}%'
42                )
43                ZZZZZZZZZZ;
44        }, $terms));
45        return [
46            'with' => [
47                <<<ZZZZZZZZZZ
48                    base_suche AS (
49                        SELECT
50                            '{$code_href}suche?anfrage=Suche' AS link,
51                            '{$code_href}assets/icns/magnifier_16.svg' AS icon,
52                            NULL AS date,
53                            '{$esc_title}' AS title,
54                            '{$esc_content}' AS text
55                    )
56                    ZZZZZZZZZZ,
57            ],
58            'query' => <<<ZZZZZZZZZZ
59                    SELECT
60                        link, icon, date, title, text,
61                        0.9 AS time_relevance
62                    FROM base_suche
63                    WHERE {$where}
64                ZZZZZZZZZZ,
65        ];
66    }
67
68    public function getHtmlWhenHasAccess(mixed $args): string {
69        $params = $this->httpUtils()->validateGetParams(OlzSucheParams::class);
70
71        $terms = preg_split('/[\s,\.;]+/', $params['anfrage']);
72        $this->generalUtils()->checkNotFalse($terms, "Could not split search terms '{$params['anfrage']}'");
73        $pretty_terms = implode(', ', $terms);
74        $esc_pretty_terms = htmlspecialchars($pretty_terms);
75
76        $out = OlzHeader::render([
77            'title' => "\"{$pretty_terms}\" - {$this->getPageTitle()}",
78            'description' => $this->getDescription($pretty_terms),
79        ]);
80
81        $out .= <<<'ZZZZZZZZZZ'
82            <div class='content-right'>
83            </div>
84            <div class='content-middle olz-suche'>
85            ZZZZZZZZZZ;
86
87        $out .= "<h1>Suchresultate für \"{$esc_pretty_terms}\"</h1>";
88
89        if (($terms[0] ?? '') === '') {
90            $out .= "<p><i>Keine Resultate</i></p>";
91            $out .= OlzFooter::render();
92            return $out;
93        }
94
95        $start_time = microtime(true);
96
97        $results = $this->searchUtils()->getSearchResults($terms);
98        foreach ($results as $result) {
99            $pretty_date = null;
100            if ($result['date']) {
101                $pretty_date = $this->dateUtils()->olzDate("tt.mm.jj", $result['date']);
102                $date_formattings = implode(' ', $this->searchUtils()->getDateFormattings($result['date']));
103                $is_date_matching = false;
104                foreach ($terms as $term) {
105                    $esc_term = preg_quote($term);
106                    if (preg_match("/{$esc_term}/i", $date_formattings)) {
107                        $is_date_matching = true;
108                        break;
109                    }
110                }
111                if ($is_date_matching) {
112                    $pretty_date = $this->searchUtils()->highlight($pretty_date, [$pretty_date]);
113                }
114            }
115            $pretty_debug = $this->authUtils()->hasPermission('all') ? "<pre>{$result['debug']}</pre>" : '';
116            $out .= OlzPostingListItem::render([
117                'link' => $result['link'],
118                'icon' => $result['icon'],
119                'date' => $pretty_date,
120                'title' => $this->searchUtils()->highlight($result['title'], $terms),
121                'text' => $pretty_debug.$this->searchUtils()->highlight($result['text'] ?? '', $terms),
122            ]);
123        }
124        if (count($results) === 0) {
125            $out .= "<p><i>Keine Resultate</i></p>";
126        }
127
128        $duration = microtime(true) - $start_time;
129        $pretty_duration = number_format($duration, 3, '.', '\'');
130        $this->log()->info("Search for '{$pretty_terms}' took {$pretty_duration}s.");
131
132        $out .= "</div>";
133
134        $out .= OlzFooter::render();
135        return $out;
136    }
137}