]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Queries/PageQueries.php
added routes for zip export
[bookstack] / app / Entities / Queries / PageQueries.php
index 1640dc2db54b32d0cd6f0b70c9461f867c729fc4..06298f470b902f8abfef4257c108a0fa42f1ecb5 100644 (file)
@@ -8,6 +8,16 @@ use Illuminate\Database\Eloquent\Builder;
 
 class PageQueries implements ProvidesEntityQueries
 {
+    protected static array $contentAttributes = [
+        'name', 'id', 'slug', 'book_id', 'chapter_id', 'draft',
+        'template', 'html', 'text', 'created_at', 'updated_at', 'priority',
+        'created_by', 'updated_by', 'owned_by',
+    ];
+    protected static array $listAttributes = [
+        'name', 'id', 'slug', 'book_id', 'chapter_id', 'draft',
+        'template', 'text', 'created_at', 'updated_at', 'priority', 'owned_by',
+    ];
+
     public function start(): Builder
     {
         return Page::query();
@@ -33,6 +43,7 @@ class PageQueries implements ProvidesEntityQueries
     {
         /** @var ?Page $page */
         $page = $this->start()->with('book')
+            ->scopes('visible')
             ->whereHas('book', function (Builder $query) use ($bookSlug) {
                 $query->where('slug', '=', $bookSlug);
             })
@@ -40,20 +51,41 @@ class PageQueries implements ProvidesEntityQueries
             ->first();
 
         if (is_null($page)) {
-            throw new NotFoundException(trans('errors.chapter_not_found'));
+            throw new NotFoundException(trans('errors.page_not_found'));
         }
 
         return $page;
     }
 
+    public function usingSlugs(string $bookSlug, string $pageSlug): Builder
+    {
+        return $this->start()
+            ->where('slug', '=', $pageSlug)
+            ->whereHas('book', function (Builder $query) use ($bookSlug) {
+                $query->where('slug', '=', $bookSlug);
+            });
+    }
+
     public function visibleForList(): Builder
     {
         return $this->start()
-            ->select(array_merge(Page::$listAttributes, ['book_slug' => function ($builder) {
-                $builder->select('slug')
-                    ->from('books')
-                    ->whereColumn('books.id', '=', 'pages.book_id');
-            }]));
+            ->scopes('visible')
+            ->select($this->mergeBookSlugForSelect(static::$listAttributes));
+    }
+
+    public function visibleForChapterList(int $chapterId): Builder
+    {
+        return $this->visibleForList()
+            ->where('chapter_id', '=', $chapterId)
+            ->orderBy('draft', 'desc')
+            ->orderBy('priority', 'asc');
+    }
+
+    public function visibleWithContents(): Builder
+    {
+        return $this->start()
+            ->scopes('visible')
+            ->select($this->mergeBookSlugForSelect(static::$contentAttributes));
     }
 
     public function currentUserDraftsForList(): Builder
@@ -68,4 +100,13 @@ class PageQueries implements ProvidesEntityQueries
         return $this->visibleForList()
             ->where('template', '=', true);
     }
+
+    protected function mergeBookSlugForSelect(array $columns): array
+    {
+        return array_merge($columns, ['book_slug' => function ($builder) {
+            $builder->select('slug')
+                ->from('books')
+                ->whereColumn('books.id', '=', 'pages.book_id');
+        }]);
+    }
 }