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