]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SearchController.php
Started search interface, Added in vue and moved fonts
[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 //        $paginationAppends = $request->only('term'); TODO - Check pagination
38         $this->setPageTitle(trans('entities.search_for_term', ['term' => $searchTerm]));
39
40         $entities = $this->searchService->searchEntities($searchTerm);
41
42         return view('search/all', [
43             'entities'   => $entities,
44             'searchTerm' => $searchTerm
45         ]);
46     }
47
48
49     /**
50      * Searches all entities within a book.
51      * @param Request $request
52      * @param integer $bookId
53      * @return \Illuminate\View\View
54      * @internal param string $searchTerm
55      */
56     public function searchBook(Request $request, $bookId)
57     {
58         if (!$request->has('term')) {
59             return redirect()->back();
60         }
61         $searchTerm = $request->get('term');
62         $searchWhereTerms = [['book_id', '=', $bookId]];
63         $pages = $this->entityRepo->getBySearch('page', $searchTerm, $searchWhereTerms);
64         $chapters = $this->entityRepo->getBySearch('chapter', $searchTerm, $searchWhereTerms);
65         return view('search/book', ['pages' => $pages, 'chapters' => $chapters, 'searchTerm' => $searchTerm]);
66     }
67
68
69     /**
70      * Search for a list of entities and return a partial HTML response of matching entities.
71      * Returns the most popular entities if no search is provided.
72      * @param Request $request
73      * @return mixed
74      */
75     public function searchEntitiesAjax(Request $request)
76     {
77         $entities = collect();
78         $entityTypes = $request->has('types') ? collect(explode(',', $request->get('types'))) : collect(['page', 'chapter', 'book']);
79         $searchTerm = ($request->has('term') && trim($request->get('term')) !== '') ? $request->get('term') : false;
80
81         // Search for entities otherwise show most popular
82         if ($searchTerm !== false) {
83             foreach (['page', 'chapter', 'book'] as $entityType) {
84                 if ($entityTypes->contains($entityType)) {
85                     // TODO - Update to new system
86                     $entities = $entities->merge($this->entityRepo->getBySearch($entityType, $searchTerm)->items());
87                 }
88             }
89             $entities = $entities->sortByDesc('title_relevance');
90         } else {
91             $entityNames = $entityTypes->map(function ($type) {
92                 return 'BookStack\\' . ucfirst($type);
93             })->toArray();
94             $entities = $this->viewService->getPopular(20, 0, $entityNames);
95         }
96
97         return view('search/entity-ajax-list', ['entities' => $entities]);
98     }
99
100 }
101
102