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