1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Repos\EntityRepo;
4 use BookStack\Services\ViewService;
5 use Illuminate\Http\Request;
7 class SearchController extends Controller
10 protected $viewService;
13 * SearchController constructor.
14 * @param EntityRepo $entityRepo
15 * @param ViewService $viewService
17 public function __construct(EntityRepo $entityRepo, ViewService $viewService)
19 $this->entityRepo = $entityRepo;
20 $this->viewService = $viewService;
21 parent::__construct();
25 * Searches all entities.
26 * @param Request $request
27 * @return \Illuminate\View\View
28 * @internal param string $searchTerm
30 public function searchAll(Request $request)
32 if (!$request->has('term')) {
33 return redirect()->back();
35 $searchTerm = $request->get('term');
36 $paginationAppends = $request->only('term');
37 $pages = $this->entityRepo->getBySearch('page', $searchTerm, [], 20, $paginationAppends);
38 $books = $this->entityRepo->getBySearch('book', $searchTerm, [], 10, $paginationAppends);
39 $chapters = $this->entityRepo->getBySearch('chapter', $searchTerm, [], 10, $paginationAppends);
40 $this->setPageTitle(trans('entities.search_for_term', ['term' => $searchTerm]));
41 return view('search/all', [
44 'chapters' => $chapters,
45 'searchTerm' => $searchTerm
50 * Search only the pages in the system.
51 * @param Request $request
52 * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
54 public function searchPages(Request $request)
56 if (!$request->has('term')) return redirect()->back();
58 $searchTerm = $request->get('term');
59 $paginationAppends = $request->only('term');
60 $pages = $this->entityRepo->getBySearch('page', $searchTerm, [], 20, $paginationAppends);
61 $this->setPageTitle(trans('entities.search_page_for_term', ['term' => $searchTerm]));
62 return view('search/entity-search-list', [
64 'title' => trans('entities.search_results_page'),
65 'searchTerm' => $searchTerm
70 * Search only the chapters in the system.
71 * @param Request $request
72 * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
74 public function searchChapters(Request $request)
76 if (!$request->has('term')) return redirect()->back();
78 $searchTerm = $request->get('term');
79 $paginationAppends = $request->only('term');
80 $chapters = $this->entityRepo->getBySearch('chapter', $searchTerm, [], 20, $paginationAppends);
81 $this->setPageTitle(trans('entities.search_chapter_for_term', ['term' => $searchTerm]));
82 return view('search/entity-search-list', [
83 'entities' => $chapters,
84 'title' => trans('entities.search_results_chapter'),
85 'searchTerm' => $searchTerm
90 * Search only the books in the system.
91 * @param Request $request
92 * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
94 public function searchBooks(Request $request)
96 if (!$request->has('term')) return redirect()->back();
98 $searchTerm = $request->get('term');
99 $paginationAppends = $request->only('term');
100 $books = $this->entityRepo->getBySearch('book', $searchTerm, [], 20, $paginationAppends);
101 $this->setPageTitle(trans('entities.search_book_for_term', ['term' => $searchTerm]));
102 return view('search/entity-search-list', [
103 'entities' => $books,
104 'title' => trans('entities.search_results_book'),
105 'searchTerm' => $searchTerm
110 * Searches all entities within a book.
111 * @param Request $request
112 * @param integer $bookId
113 * @return \Illuminate\View\View
114 * @internal param string $searchTerm
116 public function searchBook(Request $request, $bookId)
118 if (!$request->has('term')) {
119 return redirect()->back();
121 $searchTerm = $request->get('term');
122 $searchWhereTerms = [['book_id', '=', $bookId]];
123 $pages = $this->entityRepo->getBySearch('page', $searchTerm, $searchWhereTerms);
124 $chapters = $this->entityRepo->getBySearch('chapter', $searchTerm, $searchWhereTerms);
125 return view('search/book', ['pages' => $pages, 'chapters' => $chapters, 'searchTerm' => $searchTerm]);
130 * Search for a list of entities and return a partial HTML response of matching entities.
131 * Returns the most popular entities if no search is provided.
132 * @param Request $request
135 public function searchEntitiesAjax(Request $request)
137 $entities = collect();
138 $entityTypes = $request->has('types') ? collect(explode(',', $request->get('types'))) : collect(['page', 'chapter', 'book']);
139 $searchTerm = ($request->has('term') && trim($request->get('term')) !== '') ? $request->get('term') : false;
141 // Search for entities otherwise show most popular
142 if ($searchTerm !== false) {
143 foreach (['page', 'chapter', 'book'] as $entityType) {
144 if ($entityTypes->contains($entityType)) {
145 $entities = $entities->merge($this->entityRepo->getBySearch($entityType, $searchTerm)->items());
148 $entities = $entities->sortByDesc('title_relevance');
150 $entityNames = $entityTypes->map(function ($type) {
151 return 'BookStack\\' . ucfirst($type);
153 $entities = $this->viewService->getPopular(20, 0, $entityNames);
156 return view('search/entity-ajax-list', ['entities' => $entities]);