1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Actions\ViewService;
4 use BookStack\Entities\Models\Book;
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Entities\Tools\SearchRunner;
8 use BookStack\Entities\Tools\ShelfContext;
9 use BookStack\Entities\Tools\SearchOptions;
10 use Illuminate\Http\Request;
12 class SearchController extends Controller
14 protected $viewService;
15 protected $searchRunner;
16 protected $entityContextManager;
18 public function __construct(
19 ViewService $viewService,
20 SearchRunner $searchRunner,
21 ShelfContext $entityContextManager
23 $this->viewService = $viewService;
24 $this->searchRunner = $searchRunner;
25 $this->entityContextManager = $entityContextManager;
29 * Searches all entities.
31 public function search(Request $request)
33 $searchOpts = SearchOptions::fromRequest($request);
34 $fullSearchString = $searchOpts->toString();
35 $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
37 $page = intval($request->get('page', '0')) ?: 1;
38 $nextPageLink = url('/search?term=' . urlencode($fullSearchString) . '&page=' . ($page+1));
40 $results = $this->searchRunner->searchEntities($searchOpts, 'all', $page, 20);
42 return view('search.all', [
43 'entities' => $results['results'],
44 'totalResults' => $results['total'],
45 'searchTerm' => $fullSearchString,
46 'hasNextPage' => $results['has_more'],
47 'nextPageLink' => $nextPageLink,
48 'options' => $searchOpts,
53 * Searches all entities within a book.
55 public function searchBook(Request $request, int $bookId)
57 $term = $request->get('term', '');
58 $results = $this->searchRunner->searchBook($bookId, $term);
59 return view('partials.entity-list', ['entities' => $results]);
63 * Searches all entities within a chapter.
65 public function searchChapter(Request $request, int $chapterId)
67 $term = $request->get('term', '');
68 $results = $this->searchRunner->searchChapter($chapterId, $term);
69 return view('partials.entity-list', ['entities' => $results]);
73 * Search for a list of entities and return a partial HTML response of matching entities.
74 * Returns the most popular entities if no search is provided.
76 public function searchEntitiesAjax(Request $request)
78 $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
79 $searchTerm = $request->get('term', false);
80 $permission = $request->get('permission', 'view');
82 // Search for entities otherwise show most popular
83 if ($searchTerm !== false) {
84 $searchTerm .= ' {type:'. implode('|', $entityTypes) .'}';
85 $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20, $permission)['results'];
87 $entities = $this->viewService->getPopular(20, 0, $entityTypes, $permission);
90 return view('search.entity-ajax-list', ['entities' => $entities]);
94 * Search siblings items in the system.
96 public function searchSiblings(Request $request)
98 $type = $request->get('entity_type', null);
99 $id = $request->get('entity_id', null);
101 $entity = Entity::getEntityInstance($type)->newQuery()->visible()->find($id);
103 return $this->jsonError(trans('errors.entity_not_found'), 404);
109 if ($entity->isA('page') && $entity->chapter) {
110 $entities = $entity->chapter->getVisiblePages();
113 // Page in book or chapter
114 if (($entity->isA('page') && !$entity->chapter) || $entity->isA('chapter')) {
115 $entities = $entity->book->getDirectChildren();
119 // Gets just the books in a shelf if shelf is in context
120 if ($entity->isA('book')) {
121 $contextShelf = $this->entityContextManager->getContextualShelfForBook($entity);
123 $entities = $contextShelf->visibleBooks()->get();
125 $entities = Book::visible()->get();
130 if ($entity->isA('bookshelf')) {
131 $entities = Bookshelf::visible()->get();
134 return view('partials.entity-list-basic', ['entities' => $entities, 'style' => 'compact']);