]> BookStack Code Mirror - bookstack/blobdiff - app/Search/SearchController.php
respective book and chapter structure added.
[bookstack] / app / Search / SearchController.php
index 09a67f2b5cae6b351cb70c6ec1a29de39d4207a0..2fce6a3d53fb86e14e1b773f4126f0b1ba456fb2 100644 (file)
@@ -2,7 +2,8 @@
 
 namespace BookStack\Search;
 
-use BookStack\Entities\Queries\Popular;
+use BookStack\Entities\Queries\PageQueries;
+use BookStack\Entities\Queries\QueryPopular;
 use BookStack\Entities\Tools\SiblingFetcher;
 use BookStack\Http\Controller;
 use Illuminate\Http\Request;
@@ -10,7 +11,8 @@ use Illuminate\Http\Request;
 class SearchController extends Controller
 {
     public function __construct(
-        protected SearchRunner $searchRunner
+        protected SearchRunner $searchRunner,
+        protected PageQueries $pageQueries,
     ) {
     }
 
@@ -65,7 +67,7 @@ class SearchController extends Controller
      * Search for a list of entities and return a partial HTML response of matching entities.
      * Returns the most popular entities if no search is provided.
      */
-    public function searchForSelector(Request $request)
+    public function searchForSelector(Request $request, QueryPopular $queryPopular)
     {
         $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
         $searchTerm = $request->get('term', false);
@@ -76,12 +78,37 @@ class SearchController extends Controller
             $searchTerm .= ' {type:' . implode('|', $entityTypes) . '}';
             $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20)['results'];
         } else {
-            $entities = (new Popular())->run(20, 0, $entityTypes);
+            $entities = $queryPopular->run(20, 0, $entityTypes);
         }
 
         return view('search.parts.entity-selector-list', ['entities' => $entities, 'permission' => $permission]);
     }
 
+    /**
+     * Search for a list of templates to choose from.
+     */
+    public function templatesForSelector(Request $request)
+    {
+        $searchTerm = $request->get('term', false);
+
+        if ($searchTerm !== false) {
+            $searchOptions = SearchOptions::fromString($searchTerm);
+            $searchOptions->setFilter('is_template');
+            $entities = $this->searchRunner->searchEntities($searchOptions, 'page', 1, 20)['results'];
+        } else {
+            $entities = $this->pageQueries->visibleTemplates()
+                ->where('draft', '=', false)
+                ->orderBy('updated_at', 'desc')
+                ->take(20)
+                ->get();
+        }
+
+        return view('search.parts.entity-selector-list', [
+            'entities' => $entities,
+            'permission' => 'view'
+        ]);
+    }
+
     /**
      * Search for a list of entities and return a partial HTML response of matching entities
      * to be used as a result preview suggestion list for global system searches.
@@ -103,12 +130,12 @@ class SearchController extends Controller
     /**
      * Search siblings items in the system.
      */
-    public function searchSiblings(Request $request)
+    public function searchSiblings(Request $request, SiblingFetcher $siblingFetcher)
     {
         $type = $request->get('entity_type', null);
         $id = $request->get('entity_id', null);
 
-        $entities = (new SiblingFetcher())->fetch($type, $id);
+        $entities = $siblingFetcher->fetch($type, $id);
 
         return view('entities.list-basic', ['entities' => $entities, 'style' => 'compact']);
     }