]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SearchController.php
Finished refactor of entity repos
[bookstack] / app / Http / Controllers / SearchController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Repos\EntityRepo;
4 use BookStack\Services\ViewService;
5 use Illuminate\Http\Request;
6
7 class SearchController extends Controller
8 {
9     protected $entityRepo;
10     protected $viewService;
11
12     /**
13      * SearchController constructor.
14      * @param EntityRepo $entityRepo
15      * @param ViewService $viewService
16      */
17     public function __construct(EntityRepo $entityRepo, ViewService $viewService)
18     {
19         $this->entityRepo = $entityRepo;
20         $this->viewService = $viewService;
21         parent::__construct();
22     }
23
24     /**
25      * Searches all entities.
26      * @param Request $request
27      * @return \Illuminate\View\View
28      * @internal param string $searchTerm
29      */
30     public function searchAll(Request $request)
31     {
32         if (!$request->has('term')) {
33             return redirect()->back();
34         }
35         $searchTerm = $request->get('term');
36         $paginationAppends = $request->only('term');
37         $pages = $this->entityRepo->getBySearch('page', $searchTerm, [], 20, $paginationAppends);
38         $books = $this->entityRepo->getBySearch('book', $searchTerm, [], 10, $paginationAppends);
39         $chapters = $this->entityRepo->getBySearch('chapter', $searchTerm, [], 10, $paginationAppends);
40         $this->setPageTitle(trans('entities.search_for_term', ['term' => $searchTerm]));
41         return view('search/all', [
42             'pages'      => $pages,
43             'books'      => $books,
44             'chapters'   => $chapters,
45             'searchTerm' => $searchTerm
46         ]);
47     }
48
49     /**
50      * Search only the pages in the system.
51      * @param Request $request
52      * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
53      */
54     public function searchPages(Request $request)
55     {
56         if (!$request->has('term')) return redirect()->back();
57
58         $searchTerm = $request->get('term');
59         $paginationAppends = $request->only('term');
60         $pages = $this->entityRepo->getBySearch('page', $searchTerm, [], 20, $paginationAppends);
61         $this->setPageTitle(trans('entities.search_page_for_term', ['term' => $searchTerm]));
62         return view('search/entity-search-list', [
63             'entities'   => $pages,
64             'title'      => trans('entities.search_results_page'),
65             'searchTerm' => $searchTerm
66         ]);
67     }
68
69     /**
70      * Search only the chapters in the system.
71      * @param Request $request
72      * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
73      */
74     public function searchChapters(Request $request)
75     {
76         if (!$request->has('term')) return redirect()->back();
77
78         $searchTerm = $request->get('term');
79         $paginationAppends = $request->only('term');
80         $chapters = $this->entityRepo->getBySearch('chapter', $searchTerm, [], 20, $paginationAppends);
81         $this->setPageTitle(trans('entities.search_chapter_for_term', ['term' => $searchTerm]));
82         return view('search/entity-search-list', [
83             'entities'   => $chapters,
84             'title'      => trans('entities.search_results_chapter'),
85             'searchTerm' => $searchTerm
86         ]);
87     }
88
89     /**
90      * Search only the books in the system.
91      * @param Request $request
92      * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
93      */
94     public function searchBooks(Request $request)
95     {
96         if (!$request->has('term')) return redirect()->back();
97
98         $searchTerm = $request->get('term');
99         $paginationAppends = $request->only('term');
100         $books = $this->entityRepo->getBySearch('book', $searchTerm, [], 20, $paginationAppends);
101         $this->setPageTitle(trans('entities.search_book_for_term', ['term' => $searchTerm]));
102         return view('search/entity-search-list', [
103             'entities'   => $books,
104             'title'      => trans('entities.search_results_book'),
105             'searchTerm' => $searchTerm
106         ]);
107     }
108
109     /**
110      * Searches all entities within a book.
111      * @param Request $request
112      * @param integer $bookId
113      * @return \Illuminate\View\View
114      * @internal param string $searchTerm
115      */
116     public function searchBook(Request $request, $bookId)
117     {
118         if (!$request->has('term')) {
119             return redirect()->back();
120         }
121         $searchTerm = $request->get('term');
122         $searchWhereTerms = [['book_id', '=', $bookId]];
123         $pages = $this->entityRepo->getBySearch('page', $searchTerm, $searchWhereTerms);
124         $chapters = $this->entityRepo->getBySearch('chapter', $searchTerm, $searchWhereTerms);
125         return view('search/book', ['pages' => $pages, 'chapters' => $chapters, 'searchTerm' => $searchTerm]);
126     }
127
128
129     /**
130      * Search for a list of entities and return a partial HTML response of matching entities.
131      * Returns the most popular entities if no search is provided.
132      * @param Request $request
133      * @return mixed
134      */
135     public function searchEntitiesAjax(Request $request)
136     {
137         $entities = collect();
138         $entityTypes = $request->has('types') ? collect(explode(',', $request->get('types'))) : collect(['page', 'chapter', 'book']);
139         $searchTerm = ($request->has('term') && trim($request->get('term')) !== '') ? $request->get('term') : false;
140
141         // Search for entities otherwise show most popular
142         if ($searchTerm !== false) {
143             foreach (['page', 'chapter', 'book'] as $entityType) {
144                 if ($entityTypes->contains($entityType)) {
145                     $entities = $entities->merge($this->entityRepo->getBySearch($entityType, $searchTerm)->items());
146                 }
147             }
148             $entities = $entities->sortByDesc('title_relevance');
149         } else {
150             $entityNames = $entityTypes->map(function ($type) {
151                 return 'BookStack\\' . ucfirst($type);
152             })->toArray();
153             $entities = $this->viewService->getPopular(20, 0, $entityNames);
154         }
155
156         return view('search/entity-ajax-list', ['entities' => $entities]);
157     }
158
159 }
160
161