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