]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SearchController.php
Revamped some complex queries, added favourites to home
[bookstack] / app / Http / Controllers / SearchController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Actions\ViewService;
4 use BookStack\Entities\Queries\Popular;
5 use BookStack\Entities\Tools\SearchRunner;
6 use BookStack\Entities\Tools\ShelfContext;
7 use BookStack\Entities\Tools\SearchOptions;
8 use BookStack\Entities\Tools\SiblingFetcher;
9 use Illuminate\Http\Request;
10
11 class SearchController extends Controller
12 {
13     protected $viewService;
14     protected $searchRunner;
15     protected $entityContextManager;
16
17     public function __construct(
18         ViewService $viewService,
19         SearchRunner $searchRunner,
20         ShelfContext $entityContextManager
21     ) {
22         $this->viewService = $viewService;
23         $this->searchRunner = $searchRunner;
24         $this->entityContextManager = $entityContextManager;
25     }
26
27     /**
28      * Searches all entities.
29      */
30     public function search(Request $request)
31     {
32         $searchOpts = SearchOptions::fromRequest($request);
33         $fullSearchString = $searchOpts->toString();
34         $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
35
36         $page = intval($request->get('page', '0')) ?: 1;
37         $nextPageLink = url('/search?term=' . urlencode($fullSearchString) . '&page=' . ($page+1));
38
39         $results = $this->searchRunner->searchEntities($searchOpts, 'all', $page, 20);
40
41         return view('search.all', [
42             'entities'   => $results['results'],
43             'totalResults' => $results['total'],
44             'searchTerm' => $fullSearchString,
45             'hasNextPage' => $results['has_more'],
46             'nextPageLink' => $nextPageLink,
47             'options' => $searchOpts,
48         ]);
49     }
50
51     /**
52      * Searches all entities within a book.
53      */
54     public function searchBook(Request $request, int $bookId)
55     {
56         $term = $request->get('term', '');
57         $results = $this->searchRunner->searchBook($bookId, $term);
58         return view('partials.entity-list', ['entities' => $results]);
59     }
60
61     /**
62      * Searches all entities within a chapter.
63      */
64     public function searchChapter(Request $request, int $chapterId)
65     {
66         $term = $request->get('term', '');
67         $results = $this->searchRunner->searchChapter($chapterId, $term);
68         return view('partials.entity-list', ['entities' => $results]);
69     }
70
71     /**
72      * Search for a list of entities and return a partial HTML response of matching entities.
73      * Returns the most popular entities if no search is provided.
74      */
75     public function searchEntitiesAjax(Request $request)
76     {
77         $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
78         $searchTerm =  $request->get('term', false);
79         $permission = $request->get('permission', 'view');
80
81         // Search for entities otherwise show most popular
82         if ($searchTerm !== false) {
83             $searchTerm .= ' {type:'. implode('|', $entityTypes) .'}';
84             $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20, $permission)['results'];
85         } else {
86             $entities = (new Popular)->run(20, 0, $entityTypes, $permission);
87         }
88
89         return view('search.entity-ajax-list', ['entities' => $entities]);
90     }
91
92     /**
93      * Search siblings items in the system.
94      */
95     public function searchSiblings(Request $request)
96     {
97         $type = $request->get('entity_type', null);
98         $id = $request->get('entity_id', null);
99
100         $entities = (new SiblingFetcher)->fetch($type, $id);
101         return view('partials.entity-list-basic', ['entities' => $entities, 'style' => 'compact']);
102     }
103 }