| 
 | 1 | +<?php  | 
 | 2 | + | 
 | 3 | +namespace App\Search\Listeners;  | 
 | 4 | + | 
 | 5 | +use Stillat\DocumentationSearch\Events\SearchEntriesCreated;  | 
 | 6 | + | 
 | 7 | +class SearchEntriesCreatedListener  | 
 | 8 | +{  | 
 | 9 | +    protected function getParentHeadings($headers, $target)  | 
 | 10 | +    {  | 
 | 11 | +        $hierarchy = [];  | 
 | 12 | +        $levels = [];  | 
 | 13 | + | 
 | 14 | +        $currentLevel = $target->level;  | 
 | 15 | + | 
 | 16 | +        for ($i = array_search($target, $headers) - 1; $i >= 0; $i--) {  | 
 | 17 | +            $header = $headers[$i];  | 
 | 18 | + | 
 | 19 | +            if ($header->level < $currentLevel) {  | 
 | 20 | +                $hierarchy[] = $header;  | 
 | 21 | +                $currentLevel = $header->level;  | 
 | 22 | +            }  | 
 | 23 | +        }  | 
 | 24 | + | 
 | 25 | +        foreach (array_reverse($hierarchy) as $level) {  | 
 | 26 | +            $levels[$level->level] = $level->text;  | 
 | 27 | +        }  | 
 | 28 | + | 
 | 29 | +        return $levels;  | 
 | 30 | +    }  | 
 | 31 | + | 
 | 32 | +    /**  | 
 | 33 | +     * Handle the event.  | 
 | 34 | +     */  | 
 | 35 | +    public function handle(SearchEntriesCreated $event): void  | 
 | 36 | +    {  | 
 | 37 | +        $collection = $event->entry->collection()->title;  | 
 | 38 | + | 
 | 39 | +        $headers = [];  | 
 | 40 | + | 
 | 41 | +        foreach ($event->sections as $section) {  | 
 | 42 | +            if ($section->fragment->headerDetails == null) {  | 
 | 43 | +                continue;  | 
 | 44 | +            }  | 
 | 45 | + | 
 | 46 | +            $headers[] = $section->fragment->headerDetails;  | 
 | 47 | +        }  | 
 | 48 | + | 
 | 49 | +        foreach ($event->sections as $section) {  | 
 | 50 | +            $data = $section->searchEntry->data();  | 
 | 51 | +            $category = $collection.' » '.$data['origin_title'];  | 
 | 52 | + | 
 | 53 | +            $parentHeadings = null;  | 
 | 54 | + | 
 | 55 | +            if (  | 
 | 56 | +                $section->fragment->headerDetails != null &&  | 
 | 57 | +                $section->fragment->headerDetails->level >= 3  | 
 | 58 | +            ) {  | 
 | 59 | +                $header = $section->fragment->headerDetails;  | 
 | 60 | + | 
 | 61 | +                $parentHeadings = $this->getParentHeadings(  | 
 | 62 | +                    $headers,  | 
 | 63 | +                    $header  | 
 | 64 | +                );  | 
 | 65 | +                $parentHeadings[$header->level] = $header->text;  | 
 | 66 | +            }  | 
 | 67 | + | 
 | 68 | +            if ($parentHeadings === null) {  | 
 | 69 | +                $parentHeadings = [];  | 
 | 70 | + | 
 | 71 | +                if ($data['search_title'] != null && $data['origin_title'] != $data['search_title']) {  | 
 | 72 | +                    $parentHeadings[1] = $data['search_title'];  | 
 | 73 | +                }  | 
 | 74 | +            }  | 
 | 75 | + | 
 | 76 | +            if (count($parentHeadings) > 2) {  | 
 | 77 | +                array_shift($parentHeadings);  | 
 | 78 | +            }  | 
 | 79 | + | 
 | 80 | +            $data['hierarchy_lvl0'] = $category;  | 
 | 81 | +            $data['hierarchy_lvl1'] = implode(' » ', $parentHeadings);  | 
 | 82 | + | 
 | 83 | +            if ($data['is_root']) {  | 
 | 84 | +                $data['content'] = $event->entry->intro ?? $event->entry->description ?? $data['search_content'];  | 
 | 85 | +            } else {  | 
 | 86 | +                $data['content'] = $data['search_content'] ?? '';  | 
 | 87 | +            }  | 
 | 88 | + | 
 | 89 | +            $data['url'] = $data['search_url'];  | 
 | 90 | + | 
 | 91 | +            $section->searchEntry->data($data);  | 
 | 92 | +        }  | 
 | 93 | +    }  | 
 | 94 | +}  | 
0 commit comments