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