]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SearchController.php
8598575004f04a5e1c679288e6f5d7060b57a54a
[bookstack] / app / Http / Controllers / SearchController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Entities\Queries\Popular;
4 use BookStack\Entities\Tools\SearchRunner;
5 use BookStack\Entities\Tools\ShelfContext;
6 use BookStack\Entities\Tools\SearchOptions;
7 use BookStack\Entities\Tools\SiblingFetcher;
8 use Illuminate\Http\Request;
9
10 class SearchController extends Controller
11 {
12     protected $searchRunner;
13     protected $entityContextManager;
14
15     public function __construct(
16         SearchRunner $searchRunner,
17         ShelfContext $entityContextManager
18     ) {
19         $this->searchRunner = $searchRunner;
20         $this->entityContextManager = $entityContextManager;
21     }
22
23     /**
24      * Searches all entities.
25      */
26     public function search(Request $request)
27     {
28         $searchOpts = SearchOptions::fromRequest($request);
29         $fullSearchString = $searchOpts->toString();
30         $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
31
32         $page = intval($request->get('page', '0')) ?: 1;
33         $nextPageLink = url('/search?term=' . urlencode($fullSearchString) . '&page=' . ($page+1));
34
35         $results = $this->searchRunner->searchEntities($searchOpts, 'all', $page, 20);
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         return view('partials.entity-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         return view('partials.entity-list', ['entities' => $results]);
65     }
66
67     /**
68      * Search for a list of entities and return a partial HTML response of matching entities.
69      * Returns the most popular entities if no search is provided.
70      */
71     public function searchEntitiesAjax(Request $request)
72     {
73         $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
74         $searchTerm =  $request->get('term', false);
75         $permission = $request->get('permission', 'view');
76
77         // Search for entities otherwise show most popular
78         if ($searchTerm !== false) {
79             $searchTerm .= ' {type:'. implode('|', $entityTypes) .'}';
80             $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20, $permission)['results'];
81         } else {
82             $entities = (new Popular)->run(20, 0, $entityTypes, $permission);
83         }
84
85         return view('search.entity-ajax-list', ['entities' => $entities]);
86     }
87
88     /**
89      * Search siblings items in the system.
90      */
91     public function searchSiblings(Request $request)
92     {
93         $type = $request->get('entity_type', null);
94         $id = $request->get('entity_id', null);
95
96         $entities = (new SiblingFetcher)->fetch($type, $id);
97         return view('partials.entity-list-basic', ['entities' => $entities, 'style' => 'compact']);
98     }
99 }