]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SearchController.php
Start user invite system
[bookstack] / app / Http / Controllers / SearchController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Actions\ViewService;
4 use BookStack\Entities\EntityContextManager;
5 use BookStack\Entities\Repos\EntityRepo;
6 use BookStack\Entities\SearchService;
7 use BookStack\Exceptions\NotFoundException;
8 use Illuminate\Contracts\View\Factory;
9 use Illuminate\Http\Request;
10 use Illuminate\View\View;
11
12 class SearchController extends Controller
13 {
14     protected $entityRepo;
15     protected $viewService;
16     protected $searchService;
17     protected $entityContextManager;
18
19     /**
20      * SearchController constructor.
21      * @param EntityRepo $entityRepo
22      * @param ViewService $viewService
23      * @param SearchService $searchService
24      * @param EntityContextManager $entityContextManager
25      */
26     public function __construct(
27         EntityRepo $entityRepo,
28         ViewService $viewService,
29         SearchService $searchService,
30         EntityContextManager $entityContextManager
31     ) {
32         $this->entityRepo = $entityRepo;
33         $this->viewService = $viewService;
34         $this->searchService = $searchService;
35         $this->entityContextManager = $entityContextManager;
36         parent::__construct();
37     }
38
39     /**
40      * Searches all entities.
41      * @param Request $request
42      * @return View
43      * @internal param string $searchTerm
44      */
45     public function search(Request $request)
46     {
47         $searchTerm = $request->get('term');
48         $this->setPageTitle(trans('entities.search_for_term', ['term' => $searchTerm]));
49
50         $page = intval($request->get('page', '0')) ?: 1;
51         $nextPageLink = url('/search?term=' . urlencode($searchTerm) . '&page=' . ($page+1));
52
53         $results = $this->searchService->searchEntities($searchTerm, 'all', $page, 20);
54
55         return view('search.all', [
56             'entities'   => $results['results'],
57             'totalResults' => $results['total'],
58             'searchTerm' => $searchTerm,
59             'hasNextPage' => $results['has_more'],
60             'nextPageLink' => $nextPageLink
61         ]);
62     }
63
64
65     /**
66      * Searches all entities within a book.
67      * @param Request $request
68      * @param integer $bookId
69      * @return View
70      * @internal param string $searchTerm
71      */
72     public function searchBook(Request $request, $bookId)
73     {
74         $term = $request->get('term', '');
75         $results = $this->searchService->searchBook($bookId, $term);
76         return view('partials.entity-list', ['entities' => $results]);
77     }
78
79     /**
80      * Searches all entities within a chapter.
81      * @param Request $request
82      * @param integer $chapterId
83      * @return View
84      * @internal param string $searchTerm
85      */
86     public function searchChapter(Request $request, $chapterId)
87     {
88         $term = $request->get('term', '');
89         $results = $this->searchService->searchChapter($chapterId, $term);
90         return view('partials.entity-list', ['entities' => $results]);
91     }
92
93     /**
94      * Search for a list of entities and return a partial HTML response of matching entities.
95      * Returns the most popular entities if no search is provided.
96      * @param Request $request
97      * @return mixed
98      */
99     public function searchEntitiesAjax(Request $request)
100     {
101         $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
102         $searchTerm =  $request->get('term', false);
103         $permission = $request->get('permission', 'view');
104
105         // Search for entities otherwise show most popular
106         if ($searchTerm !== false) {
107             $searchTerm .= ' {type:'. implode('|', $entityTypes) .'}';
108             $entities = $this->searchService->searchEntities($searchTerm, 'all', 1, 20, $permission)['results'];
109         } else {
110             $entities = $this->viewService->getPopular(20, 0, $entityTypes, $permission);
111         }
112
113         return view('search.entity-ajax-list', ['entities' => $entities]);
114     }
115
116     /**
117      * Search siblings items in the system.
118      * @param Request $request
119      * @return Factory|View|mixed
120      */
121     public function searchSiblings(Request $request)
122     {
123         $type = $request->get('entity_type', null);
124         $id = $request->get('entity_id', null);
125
126         $entity = $this->entityRepo->getById($type, $id);
127         if (!$entity) {
128             return $this->jsonError(trans('errors.entity_not_found'), 404);
129         }
130
131         $entities = [];
132
133         // Page in chapter
134         if ($entity->isA('page') && $entity->chapter) {
135             $entities = $this->entityRepo->getChapterChildren($entity->chapter);
136         }
137
138         // Page in book or chapter
139         if (($entity->isA('page') && !$entity->chapter) || $entity->isA('chapter')) {
140             $entities = $this->entityRepo->getBookDirectChildren($entity->book);
141         }
142
143         // Book
144         // Gets just the books in a shelf if shelf is in context
145         if ($entity->isA('book')) {
146             $contextShelf = $this->entityContextManager->getContextualShelfForBook($entity);
147             if ($contextShelf) {
148                 $entities = $this->entityRepo->getBookshelfChildren($contextShelf);
149             } else {
150                 $entities = $this->entityRepo->getAll('book');
151             }
152         }
153
154         // Shelve
155         if ($entity->isA('bookshelf')) {
156             $entities = $this->entityRepo->getAll('bookshelf');
157         }
158
159         return view('partials.entity-list-basic', ['entities' => $entities, 'style' => 'compact']);
160     }
161 }