]> BookStack Code Mirror - bookstack/blob - app/Search/SearchController.php
Copying: Fixed issue with non-page links to page permalinks
[bookstack] / app / Search / SearchController.php
1 <?php
2
3 namespace BookStack\Search;
4
5 use BookStack\Entities\Queries\PageQueries;
6 use BookStack\Entities\Queries\QueryPopular;
7 use BookStack\Entities\Tools\SiblingFetcher;
8 use BookStack\Http\Controller;
9 use Illuminate\Http\Request;
10 use Illuminate\Pagination\LengthAwarePaginator;
11
12 class SearchController extends Controller
13 {
14     public function __construct(
15         protected SearchRunner $searchRunner,
16         protected PageQueries $pageQueries,
17     ) {
18     }
19
20     /**
21      * Searches all entities.
22      */
23     public function search(Request $request, SearchResultsFormatter $formatter)
24     {
25         $searchOpts = SearchOptions::fromRequest($request);
26         $fullSearchString = $searchOpts->toString();
27         $page = intval($request->get('page', '0')) ?: 1;
28
29         $results = $this->searchRunner->searchEntities($searchOpts, 'all', $page, 20);
30         $formatter->format($results['results']->all(), $searchOpts);
31         $paginator = new LengthAwarePaginator($results['results'], $results['total'], 20, $page);
32         $paginator->setPath('/search');
33         $paginator->appends($request->except('page'));
34
35         $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
36
37         return view('search.all', [
38             'entities'     => $results['results'],
39             'totalResults' => $results['total'],
40             'paginator'    => $paginator,
41             'searchTerm'   => $fullSearchString,
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 searchForSelector(Request $request, QueryPopular $queryPopular)
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 = $queryPopular->run(20, 0, $entityTypes);
84         }
85
86         return view('search.parts.entity-selector-list', ['entities' => $entities, 'permission' => $permission]);
87     }
88
89     /**
90      * Search for a list of templates to choose from.
91      */
92     public function templatesForSelector(Request $request)
93     {
94         $searchTerm = $request->get('term', false);
95
96         if ($searchTerm !== false) {
97             $searchOptions = SearchOptions::fromString($searchTerm);
98             $searchOptions->setFilter('is_template');
99             $entities = $this->searchRunner->searchEntities($searchOptions, 'page', 1, 20)['results'];
100         } else {
101             $entities = $this->pageQueries->visibleTemplates()
102                 ->where('draft', '=', false)
103                 ->orderBy('updated_at', 'desc')
104                 ->take(20)
105                 ->get();
106         }
107
108         return view('search.parts.entity-selector-list', [
109             'entities' => $entities,
110             'permission' => 'view'
111         ]);
112     }
113
114     /**
115      * Search for a list of entities and return a partial HTML response of matching entities
116      * to be used as a result preview suggestion list for global system searches.
117      */
118     public function searchSuggestions(Request $request)
119     {
120         $searchTerm = $request->get('term', '');
121         $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 5)['results'];
122
123         foreach ($entities as $entity) {
124             $entity->setAttribute('preview_content', '');
125         }
126
127         return view('search.parts.entity-suggestion-list', [
128             'entities' => $entities->slice(0, 5)
129         ]);
130     }
131
132     /**
133      * Search sibling items in the system.
134      */
135     public function searchSiblings(Request $request, SiblingFetcher $siblingFetcher)
136     {
137         $type = $request->get('entity_type', null);
138         $id = $request->get('entity_id', null);
139
140         $entities = $siblingFetcher->fetch($type, $id);
141
142         return view('entities.list-basic', ['entities' => $entities, 'style' => 'compact']);
143     }
144 }