]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SearchController.php
Moved models to folder, renamed managers to tools
[bookstack] / app / Http / Controllers / SearchController.php
1 <?php namespace BookStack\Http\Controllers;
2
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;
11
12 class SearchController extends Controller
13 {
14     protected $viewService;
15     protected $searchService;
16     protected $entityContextManager;
17
18     /**
19      * SearchController constructor.
20      */
21     public function __construct(
22         ViewService $viewService,
23         SearchService $searchService,
24         ShelfContext $entityContextManager
25     ) {
26         $this->viewService = $viewService;
27         $this->searchService = $searchService;
28         $this->entityContextManager = $entityContextManager;
29     }
30
31     /**
32      * Searches all entities.
33      */
34     public function search(Request $request)
35     {
36         $searchOpts = SearchOptions::fromRequest($request);
37         $fullSearchString = $searchOpts->toString();
38         $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
39
40         $page = intval($request->get('page', '0')) ?: 1;
41         $nextPageLink = url('/search?term=' . urlencode($fullSearchString) . '&page=' . ($page+1));
42
43         $results = $this->searchService->searchEntities($searchOpts, 'all', $page, 20);
44
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,
52         ]);
53     }
54
55
56     /**
57      * Searches all entities within a book.
58      */
59     public function searchBook(Request $request, int $bookId)
60     {
61         $term = $request->get('term', '');
62         $results = $this->searchService->searchBook($bookId, $term);
63         return view('partials.entity-list', ['entities' => $results]);
64     }
65
66     /**
67      * Searches all entities within a chapter.
68      */
69     public function searchChapter(Request $request, int $chapterId)
70     {
71         $term = $request->get('term', '');
72         $results = $this->searchService->searchChapter($chapterId, $term);
73         return view('partials.entity-list', ['entities' => $results]);
74     }
75
76     /**
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.
79      */
80     public function searchEntitiesAjax(Request $request)
81     {
82         $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
83         $searchTerm =  $request->get('term', false);
84         $permission = $request->get('permission', 'view');
85
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'];
90         } else {
91             $entities = $this->viewService->getPopular(20, 0, $entityTypes, $permission);
92         }
93
94         return view('search.entity-ajax-list', ['entities' => $entities]);
95     }
96
97     /**
98      * Search siblings items in the system.
99      */
100     public function searchSiblings(Request $request)
101     {
102         $type = $request->get('entity_type', null);
103         $id = $request->get('entity_id', null);
104
105         $entity = Entity::getEntityInstance($type)->newQuery()->visible()->find($id);
106         if (!$entity) {
107             return $this->jsonError(trans('errors.entity_not_found'), 404);
108         }
109
110         $entities = [];
111
112         // Page in chapter
113         if ($entity->isA('page') && $entity->chapter) {
114             $entities = $entity->chapter->getVisiblePages();
115         }
116
117         // Page in book or chapter
118         if (($entity->isA('page') && !$entity->chapter) || $entity->isA('chapter')) {
119             $entities = $entity->book->getDirectChildren();
120         }
121
122         // Book
123         // Gets just the books in a shelf if shelf is in context
124         if ($entity->isA('book')) {
125             $contextShelf = $this->entityContextManager->getContextualShelfForBook($entity);
126             if ($contextShelf) {
127                 $entities = $contextShelf->visibleBooks()->get();
128             } else {
129                 $entities = Book::visible()->get();
130             }
131         }
132
133         // Shelve
134         if ($entity->isA('bookshelf')) {
135             $entities = Bookshelf::visible()->get();
136         }
137
138         return view('partials.entity-list-basic', ['entities' => $entities, 'style' => 'compact']);
139     }
140 }