]> BookStack Code Mirror - bookstack/blob - app/Search/SearchController.php
HTML: Aligned and standardised DOMDocument usage
[bookstack] / app / Search / SearchController.php
1 <?php
2
3 namespace BookStack\Search;
4
5 use BookStack\Entities\Queries\Popular;
6 use BookStack\Entities\Tools\SiblingFetcher;
7 use BookStack\Http\Controller;
8 use Illuminate\Http\Request;
9
10 class SearchController extends Controller
11 {
12     public function __construct(
13         protected SearchRunner $searchRunner
14     ) {
15     }
16
17     /**
18      * Searches all entities.
19      */
20     public function search(Request $request, SearchResultsFormatter $formatter)
21     {
22         $searchOpts = SearchOptions::fromRequest($request);
23         $fullSearchString = $searchOpts->toString();
24         $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
25
26         $page = intval($request->get('page', '0')) ?: 1;
27         $nextPageLink = url('/search?term=' . urlencode($fullSearchString) . '&page=' . ($page + 1));
28
29         $results = $this->searchRunner->searchEntities($searchOpts, 'all', $page, 20);
30         $formatter->format($results['results']->all(), $searchOpts);
31
32         return view('search.all', [
33             'entities'     => $results['results'],
34             'totalResults' => $results['total'],
35             'searchTerm'   => $fullSearchString,
36             'hasNextPage'  => $results['has_more'],
37             'nextPageLink' => $nextPageLink,
38             'options'      => $searchOpts,
39         ]);
40     }
41
42     /**
43      * Searches all entities within a book.
44      */
45     public function searchBook(Request $request, int $bookId)
46     {
47         $term = $request->get('term', '');
48         $results = $this->searchRunner->searchBook($bookId, $term);
49
50         return view('entities.list', ['entities' => $results]);
51     }
52
53     /**
54      * Searches all entities within a chapter.
55      */
56     public function searchChapter(Request $request, int $chapterId)
57     {
58         $term = $request->get('term', '');
59         $results = $this->searchRunner->searchChapter($chapterId, $term);
60
61         return view('entities.list', ['entities' => $results]);
62     }
63
64     /**
65      * Search for a list of entities and return a partial HTML response of matching entities.
66      * Returns the most popular entities if no search is provided.
67      */
68     public function searchForSelector(Request $request)
69     {
70         $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
71         $searchTerm = $request->get('term', false);
72         $permission = $request->get('permission', 'view');
73
74         // Search for entities otherwise show most popular
75         if ($searchTerm !== false) {
76             $searchTerm .= ' {type:' . implode('|', $entityTypes) . '}';
77             $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20)['results'];
78         } else {
79             $entities = (new Popular())->run(20, 0, $entityTypes);
80         }
81
82         return view('search.parts.entity-selector-list', ['entities' => $entities, 'permission' => $permission]);
83     }
84
85     /**
86      * Search for a list of entities and return a partial HTML response of matching entities
87      * to be used as a result preview suggestion list for global system searches.
88      */
89     public function searchSuggestions(Request $request)
90     {
91         $searchTerm = $request->get('term', '');
92         $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 5)['results'];
93
94         foreach ($entities as $entity) {
95             $entity->setAttribute('preview_content', '');
96         }
97
98         return view('search.parts.entity-suggestion-list', [
99             'entities' => $entities->slice(0, 5)
100         ]);
101     }
102
103     /**
104      * Search siblings items in the system.
105      */
106     public function searchSiblings(Request $request)
107     {
108         $type = $request->get('entity_type', null);
109         $id = $request->get('entity_id', null);
110
111         $entities = (new SiblingFetcher())->fetch($type, $id);
112
113         return view('entities.list-basic', ['entities' => $entities, 'style' => 'compact']);
114     }
115 }