1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Actions\ViewService;
4 use BookStack\Entities\Repos\EntityRepo;
5 use BookStack\Entities\SearchService;
6 use BookStack\Exceptions\NotFoundException;
7 use Illuminate\Http\Request;
9 class SearchController extends Controller
11 protected $entityRepo;
12 protected $viewService;
13 protected $searchService;
16 * SearchController constructor.
17 * @param \BookStack\Entities\Repos\EntityRepo $entityRepo
18 * @param ViewService $viewService
19 * @param SearchService $searchService
21 public function __construct(EntityRepo $entityRepo, ViewService $viewService, SearchService $searchService)
23 $this->entityRepo = $entityRepo;
24 $this->viewService = $viewService;
25 $this->searchService = $searchService;
26 parent::__construct();
30 * Searches all entities.
31 * @param Request $request
32 * @return \Illuminate\View\View
33 * @internal param string $searchTerm
35 public function search(Request $request)
37 $searchTerm = $request->get('term');
38 $this->setPageTitle(trans('entities.search_for_term', ['term' => $searchTerm]));
40 $page = intval($request->get('page', '0')) ?: 1;
41 $nextPageLink = baseUrl('/search?term=' . urlencode($searchTerm) . '&page=' . ($page+1));
43 $results = $this->searchService->searchEntities($searchTerm, 'all', $page, 20);
45 return view('search.all', [
46 'entities' => $results['results'],
47 'totalResults' => $results['total'],
48 'searchTerm' => $searchTerm,
49 'hasNextPage' => $results['has_more'],
50 'nextPageLink' => $nextPageLink
56 * Searches all entities within a book.
57 * @param Request $request
58 * @param integer $bookId
59 * @return \Illuminate\View\View
60 * @internal param string $searchTerm
62 public function searchBook(Request $request, $bookId)
64 $term = $request->get('term', '');
65 $results = $this->searchService->searchBook($bookId, $term);
66 return view('partials.entity-list', ['entities' => $results]);
70 * Searches all entities within a chapter.
71 * @param Request $request
72 * @param integer $chapterId
73 * @return \Illuminate\View\View
74 * @internal param string $searchTerm
76 public function searchChapter(Request $request, $chapterId)
78 $term = $request->get('term', '');
79 $results = $this->searchService->searchChapter($chapterId, $term);
80 return view('partials.entity-list', ['entities' => $results]);
84 * Search for a list of entities and return a partial HTML response of matching entities.
85 * Returns the most popular entities if no search is provided.
86 * @param Request $request
89 public function searchEntitiesAjax(Request $request)
91 $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
92 $searchTerm = $request->get('term', false);
93 $permission = $request->get('permission', 'view');
95 // Search for entities otherwise show most popular
96 if ($searchTerm !== false) {
97 $searchTerm .= ' {type:'. implode('|', $entityTypes) .'}';
98 $entities = $this->searchService->searchEntities($searchTerm, 'all', 1, 20, $permission)['results'];
100 $entities = $this->viewService->getPopular(20, 0, $entityTypes, $permission);
103 return view('search.entity-ajax-list', ['entities' => $entities]);
107 * Search siblings items in the system.
108 * @param Request $request
109 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
111 public function searchSiblings(Request $request)
113 $type = $request->get('entity_type', null);
114 $id = $request->get('entity_id', null);
116 $entity = $this->entityRepo->getById($type, $id);
118 return $this->jsonError(trans('errors.entity_not_found'), 404);
124 if ($entity->isA('page') && $entity->chapter) {
125 $entities = $this->entityRepo->getChapterChildren($entity->chapter);
128 // Page in book or chapter
129 if (($entity->isA('page') && !$entity->chapter) || $entity->isA('chapter')) {
130 $entities = $this->entityRepo->getBookDirectChildren($entity->book);
134 // TODO - When shelve tracking added, Update below if criteria
137 if ($entity->isA('book')) {
138 $entities = $this->entityRepo->getAll('book');
142 // TODO - When shelve tracking added
144 return view('partials.entity-list-basic', ['entities' => $entities, 'style' => 'compact']);