3 namespace BookStack\Entities\Models;
5 use BookStack\Entities\Tools\PageContent;
6 use BookStack\Permissions\PermissionApplicator;
7 use BookStack\Uploads\Attachment;
8 use Illuminate\Database\Eloquent\Builder;
9 use Illuminate\Database\Eloquent\Collection;
10 use Illuminate\Database\Eloquent\Factories\HasFactory;
11 use Illuminate\Database\Eloquent\Relations\BelongsTo;
12 use Illuminate\Database\Eloquent\Relations\HasMany;
13 use Illuminate\Database\Eloquent\Relations\HasOne;
18 * @property int $chapter_id
19 * @property string $html
20 * @property string $markdown
21 * @property string $text
22 * @property bool $template
23 * @property bool $draft
24 * @property int $revision_count
25 * @property string $editor
26 * @property Chapter $chapter
27 * @property Collection $attachments
28 * @property Collection $revisions
29 * @property PageRevision $currentRevision
31 class Page extends BookChild
35 public static $listAttributes = ['name', 'id', 'slug', 'book_id', 'chapter_id', 'draft', 'template', 'text', 'created_at', 'updated_at', 'priority'];
36 public static $contentAttributes = ['name', 'id', 'slug', 'book_id', 'chapter_id', 'draft', 'template', 'html', 'text', 'created_at', 'updated_at', 'priority'];
38 protected $fillable = ['name', 'priority'];
40 public $textField = 'text';
42 protected $hidden = ['html', 'markdown', 'text', 'pivot', 'deleted_at'];
46 'template' => 'boolean',
50 * Get the entities that are visible to the current user.
52 public function scopeVisible(Builder $query): Builder
54 $query = app()->make(PermissionApplicator::class)->restrictDraftsOnPageQuery($query);
56 return parent::scopeVisible($query);
60 * Get the chapter that this page is in, If applicable.
64 public function chapter()
66 return $this->belongsTo(Chapter::class);
70 * Check if this page has a chapter.
72 public function hasChapter(): bool
74 return $this->chapter()->count() > 0;
78 * Get the associated page revisions, ordered by created date.
79 * Only provides actual saved page revision instances, Not drafts.
81 public function revisions(): HasMany
83 return $this->allRevisions()
84 ->where('type', '=', 'version')
85 ->orderBy('created_at', 'desc')
86 ->orderBy('id', 'desc');
90 * Get the current revision for the page if existing.
92 public function currentRevision(): HasOne
94 return $this->hasOne(PageRevision::class)
95 ->where('type', '=', 'version')
96 ->orderBy('created_at', 'desc')
97 ->orderBy('id', 'desc');
101 * Get all revision instances assigned to this page.
102 * Includes all types of revisions.
104 public function allRevisions(): HasMany
106 return $this->hasMany(PageRevision::class);
110 * Get the attachments assigned to this page.
114 public function attachments()
116 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
120 * Get the url of this page.
122 public function getUrl(string $path = ''): string
126 urlencode($this->book_slug ?? $this->book->slug),
127 $this->draft ? 'draft' : 'page',
128 $this->draft ? $this->id : urlencode($this->slug),
132 return url('/' . implode('/', $parts));
136 * Get this page for JSON display.
138 public function forJsonDisplay(): self
140 $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
141 $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
142 $refreshed->setAttribute('raw_html', $refreshed->html);
143 $refreshed->html = (new PageContent($refreshed))->render();
149 * Get a visible page by its book and page slugs.
150 * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
152 public static function getBySlugs(string $bookSlug, string $pageSlug): self
154 return static::visible()->whereSlugs($bookSlug, $pageSlug)->firstOrFail();