Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m24s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m26s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m46s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m31s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m16s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m18s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m14s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
53 lines
2 KiB
PHP
53 lines
2 KiB
PHP
<?php
|
||
|
||
namespace Inhaus\Plugins\Pages;
|
||
|
||
use App\Models\User;
|
||
use App\Search\GlobalSearch;
|
||
use Inhaus\Plugins\Pages\Models\Page;
|
||
|
||
/** Suchquelle für die globale Suche: veröffentlichte, sichtbare Seiten */
|
||
class PageSearch
|
||
{
|
||
public function __construct(private readonly PageTree $tree) {}
|
||
|
||
/**
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
public function __invoke(User $user, string $search, int $limit): array
|
||
{
|
||
$query = Page::query()->whereNotNull('published_at');
|
||
// Inhalt liegt als JSON vor (Umlaute als ü) – daher zusätzlich die JSON-Schreibweise suchen
|
||
$encoded = trim((string) json_encode($search), '"');
|
||
$query->where(function ($q) use ($search, $encoded) {
|
||
GlobalSearch::whereTerms($q, ['title', 'content'], $search);
|
||
|
||
if ($encoded !== $search) {
|
||
$q->orWhere(fn ($w) => GlobalSearch::whereTerms($w, ['content'], $encoded));
|
||
}
|
||
});
|
||
|
||
return $query->orderBy('title')->limit($limit * 3)->get()
|
||
->filter(fn (Page $page) => $this->tree->isVisible($page->id, $user))
|
||
->take($limit)
|
||
->map(fn (Page $page) => [
|
||
'title' => $page->title,
|
||
'url' => $page->url(),
|
||
'excerpt' => GlobalSearch::excerpt($this->text($page->content), $search),
|
||
'meta' => collect($this->tree->ancestors($page->id))->pluck('title')->filter()->implode(' › ') ?: 'Seite',
|
||
])->values()->all();
|
||
}
|
||
|
||
/** Alle Texte aus den Pagebuilder-Blöcken */
|
||
private function text(mixed $content): string
|
||
{
|
||
$parts = [];
|
||
array_walk_recursive($content, function ($value, $key) use (&$parts) {
|
||
if (is_string($value) && ! in_array($key, ['type', 'id', 'url', 'image', 'icon', 'layout', 'variant'], true) && ! preg_match('#^(https?:|/|[a-z0-9_.-]+$)#i', $value)) {
|
||
$parts[] = $value;
|
||
}
|
||
});
|
||
|
||
return implode(' ', $parts);
|
||
}
|
||
}
|