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