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