]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SearchController.php
Fixed some mis-refactoring and split search service
[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 Illuminate\Http\Request;
11
12 class SearchController extends Controller
13 {
14     protected $viewService;
15     protected $searchRunner;
16     protected $entityContextManager;
17
18     public function __construct(
19         ViewService $viewService,
20         SearchRunner $searchRunner,
21         ShelfContext $entityContextManager
22     ) {
23         $this->viewService = $viewService;
24         $this->searchRunner = $searchRunner;
25         $this->entityContextManager = $entityContextManager;
26     }
27
28     /**
29      * Searches all entities.
30      */
31     public function search(Request $request)
32     {
33         $searchOpts = SearchOptions::fromRequest($request);
34         $fullSearchString = $searchOpts->toString();
35         $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
36
37         $page = intval($request->get('page', '0')) ?: 1;
38         $nextPageLink = url('/search?term=' . urlencode($fullSearchString) . '&page=' . ($page+1));
39
40         $results = $this->searchRunner->searchEntities($searchOpts, 'all', $page, 20);
41
42         return view('search.all', [
43             'entities'   => $results['results'],
44             'totalResults' => $results['total'],
45             'searchTerm' => $fullSearchString,
46             'hasNextPage' => $results['has_more'],
47             'nextPageLink' => $nextPageLink,
48             'options' => $searchOpts,
49         ]);
50     }
51
52     /**
53      * Searches all entities within a book.
54      */
55     public function searchBook(Request $request, int $bookId)
56     {
57         $term = $request->get('term', '');
58         $results = $this->searchRunner->searchBook($bookId, $term);
59         return view('partials.entity-list', ['entities' => $results]);
60     }
61
62     /**
63      * Searches all entities within a chapter.
64      */
65     public function searchChapter(Request $request, int $chapterId)
66     {
67         $term = $request->get('term', '');
68         $results = $this->searchRunner->searchChapter($chapterId, $term);
69         return view('partials.entity-list', ['entities' => $results]);
70     }
71
72     /**
73      * Search for a list of entities and return a partial HTML response of matching entities.
74      * Returns the most popular entities if no search is provided.
75      */
76     public function searchEntitiesAjax(Request $request)
77     {
78         $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
79         $searchTerm =  $request->get('term', false);
80         $permission = $request->get('permission', 'view');
81
82         // Search for entities otherwise show most popular
83         if ($searchTerm !== false) {
84             $searchTerm .= ' {type:'. implode('|', $entityTypes) .'}';
85             $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20, $permission)['results'];
86         } else {
87             $entities = $this->viewService->getPopular(20, 0, $entityTypes, $permission);
88         }
89
90         return view('search.entity-ajax-list', ['entities' => $entities]);
91     }
92
93     /**
94      * Search siblings items in the system.
95      */
96     public function searchSiblings(Request $request)
97     {
98         $type = $request->get('entity_type', null);
99         $id = $request->get('entity_id', null);
100
101         $entity = Entity::getEntityInstance($type)->newQuery()->visible()->find($id);
102         if (!$entity) {
103             return $this->jsonError(trans('errors.entity_not_found'), 404);
104         }
105
106         $entities = [];
107
108         // Page in chapter
109         if ($entity->isA('page') && $entity->chapter) {
110             $entities = $entity->chapter->getVisiblePages();
111         }
112
113         // Page in book or chapter
114         if (($entity->isA('page') && !$entity->chapter) || $entity->isA('chapter')) {
115             $entities = $entity->book->getDirectChildren();
116         }
117
118         // Book
119         // Gets just the books in a shelf if shelf is in context
120         if ($entity->isA('book')) {
121             $contextShelf = $this->entityContextManager->getContextualShelfForBook($entity);
122             if ($contextShelf) {
123                 $entities = $contextShelf->visibleBooks()->get();
124             } else {
125                 $entities = Book::visible()->get();
126             }
127         }
128
129         // Shelve
130         if ($entity->isA('bookshelf')) {
131             $entities = Bookshelf::visible()->get();
132         }
133
134         return view('partials.entity-list-basic', ['entities' => $entities, 'style' => 'compact']);
135     }
136 }