]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SearchController.php
Added tests to cover ldap group mapping
[bookstack] / app / Http / Controllers / SearchController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Repos\EntityRepo;
4 use BookStack\Services\SearchService;
5 use BookStack\Services\ViewService;
6 use Illuminate\Http\Request;
7
8 class SearchController extends Controller
9 {
10     protected $entityRepo;
11     protected $viewService;
12     protected $searchService;
13
14     /**
15      * SearchController constructor.
16      * @param EntityRepo $entityRepo
17      * @param ViewService $viewService
18      * @param SearchService $searchService
19      */
20     public function __construct(EntityRepo $entityRepo, ViewService $viewService, SearchService $searchService)
21     {
22         $this->entityRepo = $entityRepo;
23         $this->viewService = $viewService;
24         $this->searchService = $searchService;
25         parent::__construct();
26     }
27
28     /**
29      * Searches all entities.
30      * @param Request $request
31      * @return \Illuminate\View\View
32      * @internal param string $searchTerm
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 = baseUrl('/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      * @param Request $request
57      * @param integer $bookId
58      * @return \Illuminate\View\View
59      * @internal param string $searchTerm
60      */
61     public function searchBook(Request $request, $bookId)
62     {
63         $term = $request->get('term', '');
64         $results = $this->searchService->searchBook($bookId, $term);
65         return view('partials/entity-list', ['entities' => $results]);
66     }
67
68     /**
69      * Searches all entities within a chapter.
70      * @param Request $request
71      * @param integer $chapterId
72      * @return \Illuminate\View\View
73      * @internal param string $searchTerm
74      */
75     public function searchChapter(Request $request, $chapterId)
76     {
77         $term = $request->get('term', '');
78         $results = $this->searchService->searchChapter($chapterId, $term);
79         return view('partials/entity-list', ['entities' => $results]);
80     }
81
82     /**
83      * Search for a list of entities and return a partial HTML response of matching entities.
84      * Returns the most popular entities if no search is provided.
85      * @param Request $request
86      * @return mixed
87      */
88     public function searchEntitiesAjax(Request $request)
89     {
90         $entityTypes = $request->filled('types') ? collect(explode(',', $request->get('types'))) : collect(['page', 'chapter', 'book']);
91         $searchTerm =  $request->get('term', false);
92         $permission = $request->get('permission', 'view');
93
94         // Search for entities otherwise show most popular
95         if ($searchTerm !== false) {
96             $searchTerm .= ' {type:'. implode('|', $entityTypes->toArray()) .'}';
97             $entities = $this->searchService->searchEntities($searchTerm, 'all', 1, 20, $permission)['results'];
98         } else {
99             $entityNames = $entityTypes->map(function ($type) {
100                 return 'BookStack\\' . ucfirst($type);
101             })->toArray();
102             $entities = $this->viewService->getPopular(20, 0, $entityNames, $permission);
103         }
104
105         return view('search/entity-ajax-list', ['entities' => $entities]);
106     }
107 }