]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Models/Page.php
Merge branch 'create-content-meta-tags' of https://github.com/james-geiger/BookStack...
[bookstack] / app / Entities / Models / Page.php
index b3eb213212fc89f4eee5258de10dda62b08d5245..6e521b2b83460928a7a343cd2a8f2cc3c3abb349 100644 (file)
@@ -1,5 +1,6 @@
 <?php namespace BookStack\Entities\Models;
 
+use BookStack\Entities\Tools\PageContent;
 use BookStack\Uploads\Attachment;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Collection;
@@ -27,14 +28,19 @@ class Page extends BookChild
 
     public $textField = 'text';
 
-    protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot'];
+    protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot', 'deleted_at'];
+
+    protected $casts = [
+        'draft' => 'boolean',
+        'template' => 'boolean',
+    ];
 
     /**
      * Get the entities that are visible to the current user.
      */
     public function scopeVisible(Builder $query): Builder
     {
-        $query = Permissions::enforceDraftVisiblityOnQuery($query);
+        $query = Permissions::enforceDraftVisibilityOnQuery($query);
         return parent::scopeVisible($query);
     }
 
@@ -69,11 +75,23 @@ class Page extends BookChild
 
     /**
      * Get the associated page revisions, ordered by created date.
-     * @return mixed
+     * Only provides actual saved page revision instances, Not drafts.
+     */
+    public function revisions(): HasMany
+    {
+        return $this->allRevisions()
+            ->where('type', '=', 'version')
+            ->orderBy('created_at', 'desc')
+            ->orderBy('id', 'desc');
+    }
+
+    /**
+     * Get all revision instances assigned to this page.
+     * Includes all types of revisions.
      */
-    public function revisions()
+    public function allRevisions(): HasMany
     {
-        return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc')->orderBy('id', 'desc');
+        return $this->hasMany(PageRevision::class);
     }
 
     /**
@@ -109,4 +127,34 @@ class Page extends BookChild
     {
         return $this->revisions()->first();
     }
+
+    /**
+     * Get this page for JSON display.
+     */
+    public function forJsonDisplay(): Page
+    {
+        $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
+        $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
+        $refreshed->html = (new PageContent($refreshed))->render();
+        return $refreshed;
+    }
+
+    /**
+     * Returns URL to a cover image for the page.
+     */
+    public function getCoverImage()
+    {
+        //$default = $this->book->getBookCover();
+        $default = url('/logo.png');
+
+        $firstImage = (new PageContent($this))->fetchFirstImage();
+
+        try {
+            $cover = $firstImage ? $firstImage : $default;
+        } catch (\Exception $err) {
+            $cover = $default;
+        }
+        return $cover;
+    }
+    
 }