]> 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 076850e6304f093309bbd80d3e7d117ce2933958..6e521b2b83460928a7a343cd2a8f2cc3c3abb349 100644 (file)
@@ -40,7 +40,7 @@ class Page extends BookChild
      */
     public function scopeVisible(Builder $query): Builder
     {
-        $query = Permissions::enforceDraftVisiblityOnQuery($query);
+        $query = Permissions::enforceDraftVisibilityOnQuery($query);
         return parent::scopeVisible($query);
     }
 
@@ -75,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()
+    public function revisions(): HasMany
     {
-        return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc')->orderBy('id', 'desc');
+        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 allRevisions(): HasMany
+    {
+        return $this->hasMany(PageRevision::class);
     }
 
     /**
@@ -121,23 +133,28 @@ class Page extends BookChild
      */
     public function forJsonDisplay(): Page
     {
-        $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy']);
+        $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;
     }
 
-    public function getCoverImage(): string
+    /**
+     * Returns URL to a cover image for the page.
+     */
+    public function getCoverImage()
     {
-        $dom = new \DomDocument();
-        $dom->loadHTML($this->html);
-        $images = $dom->getElementsByTagName('img');
+        //$default = $this->book->getBookCover();
+        $default = url('/logo.png');
+
+        $firstImage = (new PageContent($this))->fetchFirstImage();
 
         try {
-            $cover = $images->length > 0 ? $images[0]->getAttribute('src') : $this->book->getBookCover();
-        } catch (Exception $err) {
-            $cover = $this->book->getBookCover();
+            $cover = $firstImage ? $firstImage : $default;
+        } catch (\Exception $err) {
+            $cover = $default;
         }
         return $cover;
     }
+    
 }