]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SearchController.php
Merge branch 'v21.05.x'
[bookstack] / app / Http / Controllers / SearchController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Entities\Queries\Popular;
6 use BookStack\Entities\Tools\SearchOptions;
7 use BookStack\Entities\Tools\SearchRunner;
8 use BookStack\Entities\Tools\ShelfContext;
9 use BookStack\Entities\Tools\SiblingFetcher;
10 use Illuminate\Http\Request;
11
12 class SearchController extends Controller
13 {
14     protected $searchRunner;
15     protected $entityContextManager;
16
17     public function __construct(
18         SearchRunner $searchRunner,
19         ShelfContext $entityContextManager
20     ) {
21         $this->searchRunner = $searchRunner;
22         $this->entityContextManager = $entityContextManager;
23     }
24
25     /**
26      * Searches all entities.
27      */
28     public function search(Request $request)
29     {
30         $searchOpts = SearchOptions::fromRequest($request);
31         $fullSearchString = $searchOpts->toString();
32         $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
33
34         $page = intval($request->get('page', '0')) ?: 1;
35         $nextPageLink = url('/search?term=' . urlencode($fullSearchString) . '&page=' . ($page + 1));
36
37         $results = $this->searchRunner->searchEntities($searchOpts, 'all', $page, 20);
38
39         return view('search.all', [
40             'entities'     => $results['results'],
41             'totalResults' => $results['total'],
42             'searchTerm'   => $fullSearchString,
43             'hasNextPage'  => $results['has_more'],
44             'nextPageLink' => $nextPageLink,
45             'options'      => $searchOpts,
46         ]);
47     }
48
49     /**
50      * Searches all entities within a book.
51      */
52     public function searchBook(Request $request, int $bookId)
53     {
54         $term = $request->get('term', '');
55         $results = $this->searchRunner->searchBook($bookId, $term);
56
57         return view('partials.entity-list', ['entities' => $results]);
58     }
59
60     /**
61      * Searches all entities within a chapter.
62      */
63     public function searchChapter(Request $request, int $chapterId)
64     {
65         $term = $request->get('term', '');
66         $results = $this->searchRunner->searchChapter($chapterId, $term);
67
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
102         return view('partials.entity-list-basic', ['entities' => $entities, 'style' => 'compact']);
103     }
104 }