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