]> BookStack Code Mirror - bookstack/blobdiff - app/Services/SearchService.php
Updated 'Spanish Argentina' translation.
[bookstack] / app / Services / SearchService.php
index cb445eed1e5242fa3cc1d1ff92740e9efb13a21d..6390b8bc4582f3c348cf6a534544c25499ffb5d0 100644 (file)
@@ -64,10 +64,10 @@ class SearchService
      * @param string $searchString
      * @param string $entityType
      * @param int $page
-     * @param int $count
+     * @param int $count - Count of each entity to search, Total returned could can be larger and not guaranteed.
      * @return array[int, Collection];
      */
-    public function searchEntities($searchString, $entityType = 'all', $page = 1, $count = 20)
+    public function searchEntities($searchString, $entityType = 'all', $page = 1, $count = 20, $action = 'view')
     {
         $terms = $this->parseSearchString($searchString);
         $entityTypes = array_keys($this->entities);
@@ -81,21 +81,26 @@ class SearchService
 
         $results = collect();
         $total = 0;
+        $hasMore = false;
 
         foreach ($entityTypesToSearch as $entityType) {
             if (!in_array($entityType, $entityTypes)) {
                 continue;
             }
-            $search = $this->searchEntityTable($terms, $entityType, $page, $count + 1);
-            $total += $this->searchEntityTable($terms, $entityType, $page, $count + 1, true);
+            $search = $this->searchEntityTable($terms, $entityType, $page, $count, $action);
+            $entityTotal = $this->searchEntityTable($terms, $entityType, $page, $count, $action, true);
+            if ($entityTotal > $page * $count) {
+                $hasMore = true;
+            }
+            $total += $entityTotal;
             $results = $results->merge($search);
         }
 
         return [
             'total' => $total,
             'count' => count($results),
-            'has_more' => $results->count() > $count,
-            'results' => $results->sortByDesc('score')->slice(0, $count)->values()
+            'has_more' => $hasMore,
+            'results' => $results->sortByDesc('score')->values()
         ];
     }
 
@@ -142,12 +147,13 @@ class SearchService
      * @param string $entityType
      * @param int $page
      * @param int $count
+     * @param string $action
      * @param bool $getCount Return the total count of the search
      * @return \Illuminate\Database\Eloquent\Collection|int|static[]
      */
-    public function searchEntityTable($terms, $entityType = 'page', $page = 1, $count = 20, $getCount = false)
+    public function searchEntityTable($terms, $entityType = 'page', $page = 1, $count = 20, $action = 'view', $getCount = false)
     {
-        $query = $this->buildEntitySearchQuery($terms, $entityType);
+        $query = $this->buildEntitySearchQuery($terms, $entityType, $action);
         if ($getCount) {
             return $query->count();
         }
@@ -160,9 +166,10 @@ class SearchService
      * Create a search query for an entity
      * @param array $terms
      * @param string $entityType
+     * @param string $action
      * @return \Illuminate\Database\Eloquent\Builder
      */
-    protected function buildEntitySearchQuery($terms, $entityType = 'page')
+    protected function buildEntitySearchQuery($terms, $entityType = 'page', $action = 'view')
     {
         $entity = $this->getEntity($entityType);
         $entitySelect = $entity->newQuery();
@@ -207,7 +214,7 @@ class SearchService
             }
         }
 
-        return $this->permissionService->enforceEntityRestrictions($entityType, $entitySelect, 'view');
+        return $this->permissionService->enforceEntityRestrictions($entityType, $entitySelect, $action);
     }