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\Managers\EntityContext;
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 EntityContext $entityContextManager
26 $this->viewService = $viewService;
27 $this->searchService = $searchService;
28 $this->entityContextManager = $entityContextManager;
29 parent::__construct();
33 * Searches all entities.
35 public function search(Request $request)
37 $searchOpts = SearchOptions::fromRequest($request);
38 $fullSearchString = $searchOpts->toString();
39 $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
41 $page = intval($request->get('page', '0')) ?: 1;
42 $nextPageLink = url('/search?term=' . urlencode($fullSearchString) . '&page=' . ($page+1));
44 $results = $this->searchService->searchEntities($searchOpts, 'all', $page, 20);
46 return view('search.all', [
47 'entities' => $results['results'],
48 'totalResults' => $results['total'],
49 'searchTerm' => $fullSearchString,
50 'hasNextPage' => $results['has_more'],
51 'nextPageLink' => $nextPageLink,
52 'options' => $searchOpts,
58 * Searches all entities within a book.
60 public function searchBook(Request $request, int $bookId)
62 $term = $request->get('term', '');
63 $results = $this->searchService->searchBook($bookId, $term);
64 return view('partials.entity-list', ['entities' => $results]);
68 * Searches all entities within a chapter.
70 public function searchChapter(Request $request, int $chapterId)
72 $term = $request->get('term', '');
73 $results = $this->searchService->searchChapter($chapterId, $term);
74 return view('partials.entity-list', ['entities' => $results]);
78 * Search for a list of entities and return a partial HTML response of matching entities.
79 * Returns the most popular entities if no search is provided.
81 public function searchEntitiesAjax(Request $request)
83 $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
84 $searchTerm = $request->get('term', false);
85 $permission = $request->get('permission', 'view');
87 // Search for entities otherwise show most popular
88 if ($searchTerm !== false) {
89 $searchTerm .= ' {type:'. implode('|', $entityTypes) .'}';
90 $entities = $this->searchService->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20, $permission)['results'];
92 $entities = $this->viewService->getPopular(20, 0, $entityTypes, $permission);
95 return view('search.entity-ajax-list', ['entities' => $entities]);
99 * Search siblings items in the system.
101 public function searchSiblings(Request $request)
103 $type = $request->get('entity_type', null);
104 $id = $request->get('entity_id', null);
106 $entity = Entity::getEntityInstance($type)->newQuery()->visible()->find($id);
108 return $this->jsonError(trans('errors.entity_not_found'), 404);
114 if ($entity->isA('page') && $entity->chapter) {
115 $entities = $entity->chapter->getVisiblePages();
118 // Page in book or chapter
119 if (($entity->isA('page') && !$entity->chapter) || $entity->isA('chapter')) {
120 $entities = $entity->book->getDirectChildren();
124 // Gets just the books in a shelf if shelf is in context
125 if ($entity->isA('book')) {
126 $contextShelf = $this->entityContextManager->getContextualShelfForBook($entity);
128 $entities = $contextShelf->visibleBooks()->get();
130 $entities = Book::visible()->get();
135 if ($entity->isA('bookshelf')) {
136 $entities = Bookshelf::visible()->get();
139 return view('partials.entity-list-basic', ['entities' => $entities, 'style' => 'compact']);