]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SearchController.php
Merge pull request #1646 from kostefun/patch-15
[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\Managers\EntityContext;
8 use BookStack\Entities\SearchService;
9 use Illuminate\Http\Request;
10
11 class SearchController extends Controller
12 {
13     protected $viewService;
14     protected $searchService;
15     protected $entityContextManager;
16
17     /**
18      * SearchController constructor.
19      */
20     public function __construct(
21         ViewService $viewService,
22         SearchService $searchService,
23         EntityContext $entityContextManager
24     ) {
25         $this->viewService = $viewService;
26         $this->searchService = $searchService;
27         $this->entityContextManager = $entityContextManager;
28         parent::__construct();
29     }
30
31     /**
32      * Searches all entities.
33      */
34     public function search(Request $request)
35     {
36         $searchTerm = $request->get('term');
37         $this->setPageTitle(trans('entities.search_for_term', ['term' => $searchTerm]));
38
39         $page = intval($request->get('page', '0')) ?: 1;
40         $nextPageLink = url('/search?term=' . urlencode($searchTerm) . '&page=' . ($page+1));
41
42         $results = $this->searchService->searchEntities($searchTerm, 'all', $page, 20);
43
44         return view('search.all', [
45             'entities'   => $results['results'],
46             'totalResults' => $results['total'],
47             'searchTerm' => $searchTerm,
48             'hasNextPage' => $results['has_more'],
49             'nextPageLink' => $nextPageLink
50         ]);
51     }
52
53
54     /**
55      * Searches all entities within a book.
56      */
57     public function searchBook(Request $request, int $bookId)
58     {
59         $term = $request->get('term', '');
60         $results = $this->searchService->searchBook($bookId, $term);
61         return view('partials.entity-list', ['entities' => $results]);
62     }
63
64     /**
65      * Searches all entities within a chapter.
66      */
67     public function searchChapter(Request $request, int $chapterId)
68     {
69         $term = $request->get('term', '');
70         $results = $this->searchService->searchChapter($chapterId, $term);
71         return view('partials.entity-list', ['entities' => $results]);
72     }
73
74     /**
75      * Search for a list of entities and return a partial HTML response of matching entities.
76      * Returns the most popular entities if no search is provided.
77      */
78     public function searchEntitiesAjax(Request $request)
79     {
80         $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
81         $searchTerm =  $request->get('term', false);
82         $permission = $request->get('permission', 'view');
83
84         // Search for entities otherwise show most popular
85         if ($searchTerm !== false) {
86             $searchTerm .= ' {type:'. implode('|', $entityTypes) .'}';
87             $entities = $this->searchService->searchEntities($searchTerm, 'all', 1, 20, $permission)['results'];
88         } else {
89             $entities = $this->viewService->getPopular(20, 0, $entityTypes, $permission);
90         }
91
92         return view('search.entity-ajax-list', ['entities' => $entities]);
93     }
94
95     /**
96      * Search siblings items in the system.
97      */
98     public function searchSiblings(Request $request)
99     {
100         $type = $request->get('entity_type', null);
101         $id = $request->get('entity_id', null);
102
103         $entity = Entity::getEntityInstance($type)->newQuery()->visible()->find($id);
104         if (!$entity) {
105             return $this->jsonError(trans('errors.entity_not_found'), 404);
106         }
107
108         $entities = [];
109
110         // Page in chapter
111         if ($entity->isA('page') && $entity->chapter) {
112             $entities = $entity->chapter->visiblePages();
113         }
114
115         // Page in book or chapter
116         if (($entity->isA('page') && !$entity->chapter) || $entity->isA('chapter')) {
117             $entities = $entity->book->getDirectChildren();
118         }
119
120         // Book
121         // Gets just the books in a shelf if shelf is in context
122         if ($entity->isA('book')) {
123             $contextShelf = $this->entityContextManager->getContextualShelfForBook($entity);
124             if ($contextShelf) {
125                 $entities = $contextShelf->visibleBooks()->get();
126             } else {
127                 $entities = Book::visible()->get();
128             }
129         }
130
131         // Shelve
132         if ($entity->isA('bookshelf')) {
133             $entities = Bookshelf::visible()->get();
134         }
135
136         return view('partials.entity-list-basic', ['entities' => $entities, 'style' => 'compact']);
137     }
138 }