]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SearchController.php
Finished off UI for 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         if (!$request->has('term')) {
65             return redirect()->back();
66         }
67         $searchTerm = $request->get('term');
68         $searchWhereTerms = [['book_id', '=', $bookId]];
69         $pages = $this->entityRepo->getBySearch('page', $searchTerm, $searchWhereTerms);
70         $chapters = $this->entityRepo->getBySearch('chapter', $searchTerm, $searchWhereTerms);
71         return view('search/book', ['pages' => $pages, 'chapters' => $chapters, 'searchTerm' => $searchTerm]);
72     }
73
74
75     /**
76      * Search for a list of entities and return a partial HTML response of matching entities.
77      * Returns the most popular entities if no search is provided.
78      * @param Request $request
79      * @return mixed
80      */
81     public function searchEntitiesAjax(Request $request)
82     {
83         $entities = collect();
84         $entityTypes = $request->has('types') ? collect(explode(',', $request->get('types'))) : collect(['page', 'chapter', 'book']);
85         $searchTerm = ($request->has('term') && trim($request->get('term')) !== '') ? $request->get('term') : false;
86
87         // Search for entities otherwise show most popular
88         if ($searchTerm !== false) {
89             foreach (['page', 'chapter', 'book'] as $entityType) {
90                 if ($entityTypes->contains($entityType)) {
91                     // TODO - Update to new system
92                     $entities = $entities->merge($this->entityRepo->getBySearch($entityType, $searchTerm)->items());
93                 }
94             }
95             $entities = $entities->sortByDesc('title_relevance');
96         } else {
97             $entityNames = $entityTypes->map(function ($type) {
98                 return 'BookStack\\' . ucfirst($type);
99             })->toArray();
100             $entities = $this->viewService->getPopular(20, 0, $entityNames);
101         }
102
103         return view('search/entity-ajax-list', ['entities' => $entities]);
104     }
105
106 }
107
108