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