1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Actions\ViewService;
4 use BookStack\Entities\EntityContextManager;
5 use BookStack\Entities\Repos\EntityRepo;
6 use BookStack\Entities\SearchService;
7 use BookStack\Exceptions\NotFoundException;
8 use Illuminate\Contracts\View\Factory;
9 use Illuminate\Http\Request;
10 use Illuminate\View\View;
12 class SearchController extends Controller
14 protected $entityRepo;
15 protected $viewService;
16 protected $searchService;
17 protected $entityContextManager;
20 * SearchController constructor.
21 * @param EntityRepo $entityRepo
22 * @param ViewService $viewService
23 * @param SearchService $searchService
24 * @param EntityContextManager $entityContextManager
26 public function __construct(
27 EntityRepo $entityRepo,
28 ViewService $viewService,
29 SearchService $searchService,
30 EntityContextManager $entityContextManager
32 $this->entityRepo = $entityRepo;
33 $this->viewService = $viewService;
34 $this->searchService = $searchService;
35 $this->entityContextManager = $entityContextManager;
36 parent::__construct();
40 * Searches all entities.
41 * @param Request $request
43 * @internal param string $searchTerm
45 public function search(Request $request)
47 $searchTerm = $request->get('term');
48 $this->setPageTitle(trans('entities.search_for_term', ['term' => $searchTerm]));
50 $page = intval($request->get('page', '0')) ?: 1;
51 $nextPageLink = url('/search?term=' . urlencode($searchTerm) . '&page=' . ($page+1));
53 $results = $this->searchService->searchEntities($searchTerm, 'all', $page, 20);
55 return view('search.all', [
56 'entities' => $results['results'],
57 'totalResults' => $results['total'],
58 'searchTerm' => $searchTerm,
59 'hasNextPage' => $results['has_more'],
60 'nextPageLink' => $nextPageLink
66 * Searches all entities within a book.
67 * @param Request $request
68 * @param integer $bookId
70 * @internal param string $searchTerm
72 public function searchBook(Request $request, $bookId)
74 $term = $request->get('term', '');
75 $results = $this->searchService->searchBook($bookId, $term);
76 return view('partials.entity-list', ['entities' => $results]);
80 * Searches all entities within a chapter.
81 * @param Request $request
82 * @param integer $chapterId
84 * @internal param string $searchTerm
86 public function searchChapter(Request $request, $chapterId)
88 $term = $request->get('term', '');
89 $results = $this->searchService->searchChapter($chapterId, $term);
90 return view('partials.entity-list', ['entities' => $results]);
94 * Search for a list of entities and return a partial HTML response of matching entities.
95 * Returns the most popular entities if no search is provided.
96 * @param Request $request
99 public function searchEntitiesAjax(Request $request)
101 $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
102 $searchTerm = $request->get('term', false);
103 $permission = $request->get('permission', 'view');
105 // Search for entities otherwise show most popular
106 if ($searchTerm !== false) {
107 $searchTerm .= ' {type:'. implode('|', $entityTypes) .'}';
108 $entities = $this->searchService->searchEntities($searchTerm, 'all', 1, 20, $permission)['results'];
110 $entities = $this->viewService->getPopular(20, 0, $entityTypes, $permission);
113 return view('search.entity-ajax-list', ['entities' => $entities]);
117 * Search siblings items in the system.
118 * @param Request $request
119 * @return Factory|View|mixed
121 public function searchSiblings(Request $request)
123 $type = $request->get('entity_type', null);
124 $id = $request->get('entity_id', null);
126 $entity = $this->entityRepo->getById($type, $id);
128 return $this->jsonError(trans('errors.entity_not_found'), 404);
134 if ($entity->isA('page') && $entity->chapter) {
135 $entities = $this->entityRepo->getChapterChildren($entity->chapter);
138 // Page in book or chapter
139 if (($entity->isA('page') && !$entity->chapter) || $entity->isA('chapter')) {
140 $entities = $this->entityRepo->getBookDirectChildren($entity->book);
144 // Gets just the books in a shelf if shelf is in context
145 if ($entity->isA('book')) {
146 $contextShelf = $this->entityContextManager->getContextualShelfForBook($entity);
148 $entities = $this->entityRepo->getBookshelfChildren($contextShelf);
150 $entities = $this->entityRepo->getAll('book');
155 if ($entity->isA('bookshelf')) {
156 $entities = $this->entityRepo->getAll('bookshelf');
159 return view('partials.entity-list-basic', ['entities' => $entities, 'style' => 'compact']);