]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SearchController.php
Merge pull request #340 from BookStackApp/search_system
[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 = $request->has('page') && is_int(intval($request->get('page'))) ? intval($request->get('page')) : 1;
40         $nextPageLink = baseUrl('/search?term=' . urlencode($searchTerm) . '&page=' . ($page+1));
41
42         $results = $this->searchService->searchEntities($searchTerm, 'all', $page, 20);
43         $hasNextPage = $this->searchService->searchEntities($searchTerm, 'all', $page+1, 20)['count'] > 0;
44
45         return view('search/all', [
46             'entities'   => $results['results'],
47             'totalResults' => $results['total'],
48             'searchTerm' => $searchTerm,
49             'hasNextPage' => $hasNextPage,
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->has('types') ? collect(explode(',', $request->get('types'))) : collect(['page', 'chapter', 'book']);
92         $searchTerm = ($request->has('term') && trim($request->get('term')) !== '') ? $request->get('term') : false;
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)['results'];
98         } else {
99             $entityNames = $entityTypes->map(function ($type) {
100                 return 'BookStack\\' . ucfirst($type);
101             })->toArray();
102             $entities = $this->viewService->getPopular(20, 0, $entityNames);
103         }
104
105         return view('search/entity-ajax-list', ['entities' => $entities]);
106     }
107
108 }
109
110