3 namespace BookStack\Search;
5 use BookStack\Entities\Queries\PageQueries;
6 use BookStack\Entities\Queries\QueryPopular;
7 use BookStack\Entities\Tools\SiblingFetcher;
8 use BookStack\Http\Controller;
9 use Illuminate\Http\Request;
10 use Illuminate\Pagination\LengthAwarePaginator;
12 class SearchController extends Controller
14 public function __construct(
15 protected SearchRunner $searchRunner,
16 protected PageQueries $pageQueries,
21 * Searches all entities.
23 public function search(Request $request, SearchResultsFormatter $formatter)
25 $searchOpts = SearchOptions::fromRequest($request);
26 $fullSearchString = $searchOpts->toString();
27 $page = intval($request->get('page', '0')) ?: 1;
29 $results = $this->searchRunner->searchEntities($searchOpts, 'all', $page, 20);
30 $formatter->format($results['results']->all(), $searchOpts);
31 $paginator = new LengthAwarePaginator($results['results'], $results['total'], 20, $page);
32 $paginator->setPath('/search');
33 $paginator->appends($request->except('page'));
35 $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
37 return view('search.all', [
38 'entities' => $results['results'],
39 'totalResults' => $results['total'],
40 'paginator' => $paginator,
41 'searchTerm' => $fullSearchString,
42 'options' => $searchOpts,
47 * Searches all entities within a book.
49 public function searchBook(Request $request, int $bookId)
51 $term = $request->get('term', '');
52 $results = $this->searchRunner->searchBook($bookId, $term);
54 return view('entities.list', ['entities' => $results]);
58 * Searches all entities within a chapter.
60 public function searchChapter(Request $request, int $chapterId)
62 $term = $request->get('term', '');
63 $results = $this->searchRunner->searchChapter($chapterId, $term);
65 return view('entities.list', ['entities' => $results]);
69 * Search for a list of entities and return a partial HTML response of matching entities.
70 * Returns the most popular entities if no search is provided.
72 public function searchForSelector(Request $request, QueryPopular $queryPopular)
74 $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
75 $searchTerm = $request->get('term', false);
76 $permission = $request->get('permission', 'view');
78 // Search for entities otherwise show most popular
79 if ($searchTerm !== false) {
80 $searchTerm .= ' {type:' . implode('|', $entityTypes) . '}';
81 $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20)['results'];
83 $entities = $queryPopular->run(20, 0, $entityTypes);
86 return view('search.parts.entity-selector-list', ['entities' => $entities, 'permission' => $permission]);
90 * Search for a list of templates to choose from.
92 public function templatesForSelector(Request $request)
94 $searchTerm = $request->get('term', false);
96 if ($searchTerm !== false) {
97 $searchOptions = SearchOptions::fromString($searchTerm);
98 $searchOptions->setFilter('is_template');
99 $entities = $this->searchRunner->searchEntities($searchOptions, 'page', 1, 20)['results'];
101 $entities = $this->pageQueries->visibleTemplates()
102 ->where('draft', '=', false)
103 ->orderBy('updated_at', 'desc')
108 return view('search.parts.entity-selector-list', [
109 'entities' => $entities,
110 'permission' => 'view'
115 * Search for a list of entities and return a partial HTML response of matching entities
116 * to be used as a result preview suggestion list for global system searches.
118 public function searchSuggestions(Request $request)
120 $searchTerm = $request->get('term', '');
121 $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 5)['results'];
123 foreach ($entities as $entity) {
124 $entity->setAttribute('preview_content', '');
127 return view('search.parts.entity-suggestion-list', [
128 'entities' => $entities->slice(0, 5)
133 * Search sibling items in the system.
135 public function searchSiblings(Request $request, SiblingFetcher $siblingFetcher)
137 $type = $request->get('entity_type', null);
138 $id = $request->get('entity_id', null);
140 $entities = $siblingFetcher->fetch($type, $id);
142 return view('entities.list-basic', ['entities' => $entities, 'style' => 'compact']);