1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Actions\ViewService;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Bookshelf;
6 use BookStack\Entities\Entity;
7 use BookStack\Entities\Tools\ShelfContext;
8 use BookStack\Entities\SearchService;
9 use BookStack\Entities\SearchOptions;
10 use Illuminate\Http\Request;
12 class SearchController extends Controller
14 protected $viewService;
15 protected $searchService;
16 protected $entityContextManager;
19 * SearchController constructor.
21 public function __construct(
22 ViewService $viewService,
23 SearchService $searchService,
24 ShelfContext $entityContextManager
26 $this->viewService = $viewService;
27 $this->searchService = $searchService;
28 $this->entityContextManager = $entityContextManager;
32 * Searches all entities.
34 public function search(Request $request)
36 $searchOpts = SearchOptions::fromRequest($request);
37 $fullSearchString = $searchOpts->toString();
38 $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
40 $page = intval($request->get('page', '0')) ?: 1;
41 $nextPageLink = url('/search?term=' . urlencode($fullSearchString) . '&page=' . ($page+1));
43 $results = $this->searchService->searchEntities($searchOpts, 'all', $page, 20);
45 return view('search.all', [
46 'entities' => $results['results'],
47 'totalResults' => $results['total'],
48 'searchTerm' => $fullSearchString,
49 'hasNextPage' => $results['has_more'],
50 'nextPageLink' => $nextPageLink,
51 'options' => $searchOpts,
57 * Searches all entities within a book.
59 public function searchBook(Request $request, int $bookId)
61 $term = $request->get('term', '');
62 $results = $this->searchService->searchBook($bookId, $term);
63 return view('partials.entity-list', ['entities' => $results]);
67 * Searches all entities within a chapter.
69 public function searchChapter(Request $request, int $chapterId)
71 $term = $request->get('term', '');
72 $results = $this->searchService->searchChapter($chapterId, $term);
73 return view('partials.entity-list', ['entities' => $results]);
77 * Search for a list of entities and return a partial HTML response of matching entities.
78 * Returns the most popular entities if no search is provided.
80 public function searchEntitiesAjax(Request $request)
82 $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
83 $searchTerm = $request->get('term', false);
84 $permission = $request->get('permission', 'view');
86 // Search for entities otherwise show most popular
87 if ($searchTerm !== false) {
88 $searchTerm .= ' {type:'. implode('|', $entityTypes) .'}';
89 $entities = $this->searchService->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20, $permission)['results'];
91 $entities = $this->viewService->getPopular(20, 0, $entityTypes, $permission);
94 return view('search.entity-ajax-list', ['entities' => $entities]);
98 * Search siblings items in the system.
100 public function searchSiblings(Request $request)
102 $type = $request->get('entity_type', null);
103 $id = $request->get('entity_id', null);
105 $entity = Entity::getEntityInstance($type)->newQuery()->visible()->find($id);
107 return $this->jsonError(trans('errors.entity_not_found'), 404);
113 if ($entity->isA('page') && $entity->chapter) {
114 $entities = $entity->chapter->getVisiblePages();
117 // Page in book or chapter
118 if (($entity->isA('page') && !$entity->chapter) || $entity->isA('chapter')) {
119 $entities = $entity->book->getDirectChildren();
123 // Gets just the books in a shelf if shelf is in context
124 if ($entity->isA('book')) {
125 $contextShelf = $this->entityContextManager->getContextualShelfForBook($entity);
127 $entities = $contextShelf->visibleBooks()->get();
129 $entities = Book::visible()->get();
134 if ($entity->isA('bookshelf')) {
135 $entities = Bookshelf::visible()->get();
138 return view('partials.entity-list-basic', ['entities' => $entities, 'style' => 'compact']);