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