]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Models/Page.php
Merge pull request #5917 from BookStackApp/copy_references
[bookstack] / app / Entities / Models / Page.php
index b8467c38cf58ce6cc8334b287959824bf8f5fd16..a1d3fc7b40d53338f269d05ede4e4b50f98bdd1a 100644 (file)
@@ -3,36 +3,39 @@
 namespace BookStack\Entities\Models;
 
 use BookStack\Entities\Tools\PageContent;
+use BookStack\Permissions\PermissionApplicator;
 use BookStack\Uploads\Attachment;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Collection;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
 use Illuminate\Database\Eloquent\Relations\HasMany;
-use Permissions;
+use Illuminate\Database\Eloquent\Relations\HasOne;
 
 /**
  * Class Page.
- *
- * @property int        $chapter_id
- * @property string     $html
- * @property string     $markdown
- * @property string     $text
- * @property bool       $template
- * @property bool       $draft
- * @property int        $revision_count
- * @property Chapter    $chapter
- * @property Collection $attachments
+ * @property EntityPageData $pageData
+ * @property int          $chapter_id
+ * @property string       $html
+ * @property string       $markdown
+ * @property string       $text
+ * @property bool         $template
+ * @property bool         $draft
+ * @property int          $revision_count
+ * @property string       $editor
+ * @property Chapter      $chapter
+ * @property Collection   $attachments
+ * @property Collection   $revisions
+ * @property PageRevision $currentRevision
  */
 class Page extends BookChild
 {
-    public static $listAttributes = ['name', 'id', 'slug', 'book_id', 'chapter_id', 'draft', 'template', 'text', 'created_at', 'updated_at', 'priority'];
-    public static $contentAttributes = ['name', 'id', 'slug', 'book_id', 'chapter_id', 'draft', 'template', 'html', 'text', 'created_at', 'updated_at', 'priority'];
+    use HasFactory;
 
-    protected $fillable = ['name', 'priority', 'markdown'];
-
-    public $textField = 'text';
-
-    protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot', 'deleted_at'];
+    public string $textField = 'text';
+    public string $htmlField = 'html';
+    protected $hidden = ['html', 'markdown', 'text', 'pivot', 'deleted_at',  'entity_id', 'entity_type'];
+    protected $fillable = ['name', 'priority'];
 
     protected $casts = [
         'draft'    => 'boolean',
@@ -44,27 +47,23 @@ class Page extends BookChild
      */
     public function scopeVisible(Builder $query): Builder
     {
-        $query = Permissions::enforceDraftVisibilityOnQuery($query);
+        $query = app()->make(PermissionApplicator::class)->restrictDraftsOnPageQuery($query);
 
         return parent::scopeVisible($query);
     }
 
     /**
      * Get the chapter that this page is in, If applicable.
-     *
-     * @return BelongsTo
      */
-    public function chapter()
+    public function chapter(): BelongsTo
     {
         return $this->belongsTo(Chapter::class);
     }
 
     /**
      * Check if this page has a chapter.
-     *
-     * @return bool
      */
-    public function hasChapter()
+    public function hasChapter(): bool
     {
         return $this->chapter()->count() > 0;
     }
@@ -81,6 +80,17 @@ class Page extends BookChild
             ->orderBy('id', 'desc');
     }
 
+    /**
+     * Get the current revision for the page if existing.
+     */
+    public function currentRevision(): HasOne
+    {
+        return $this->hasOne(PageRevision::class)
+            ->where('type', '=', 'version')
+            ->orderBy('created_at', 'desc')
+            ->orderBy('id', 'desc');
+    }
+
     /**
      * Get all revision instances assigned to this page.
      * Includes all types of revisions.
@@ -92,10 +102,8 @@ class Page extends BookChild
 
     /**
      * Get the attachments assigned to this page.
-     *
-     * @return HasMany
      */
-    public function attachments()
+    public function attachments(): HasMany
     {
         return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
     }
@@ -103,7 +111,7 @@ class Page extends BookChild
     /**
      * Get the url of this page.
      */
-    public function getUrl($path = ''): string
+    public function getUrl(string $path = ''): string
     {
         $parts = [
             'books',
@@ -117,24 +125,31 @@ class Page extends BookChild
     }
 
     /**
-     * Get the current revision for the page if existing.
-     *
-     * @return PageRevision|null
+     * Get the ID-based permalink for this page.
      */
-    public function getCurrentRevision()
+    public function getPermalink(): string
     {
-        return $this->revisions()->first();
+        return url("/link/{$this->id}");
     }
 
     /**
      * Get this page for JSON display.
      */
-    public function forJsonDisplay(): Page
+    public function forJsonDisplay(): self
     {
         $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
         $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
-        $refreshed->html = (new PageContent($refreshed))->render();
+        $refreshed->setAttribute('raw_html', $refreshed->html);
+        $refreshed->setAttribute('html', (new PageContent($refreshed))->render());
 
         return $refreshed;
     }
+
+    /**
+     * @return HasOne<EntityPageData, $this>
+     */
+    public function relatedData(): HasOne
+    {
+        return $this->hasOne(EntityPageData::class, 'page_id', 'id');
+    }
 }