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 string $textField = 'text';
41 public string $htmlField = 'html';
43 protected $hidden = ['html', 'markdown', 'text', 'pivot', 'deleted_at'];
47 'template' => 'boolean',
51 * Get the entities that are visible to the current user.
53 public function scopeVisible(Builder $query): Builder
55 $query = app()->make(PermissionApplicator::class)->restrictDraftsOnPageQuery($query);
57 return parent::scopeVisible($query);
61 * Get the chapter that this page is in, If applicable.
65 public function chapter()
67 return $this->belongsTo(Chapter::class);
71 * Check if this page has a chapter.
73 public function hasChapter(): bool
75 return $this->chapter()->count() > 0;
79 * Get the associated page revisions, ordered by created date.
80 * Only provides actual saved page revision instances, Not drafts.
82 public function revisions(): HasMany
84 return $this->allRevisions()
85 ->where('type', '=', 'version')
86 ->orderBy('created_at', 'desc')
87 ->orderBy('id', 'desc');
91 * Get the current revision for the page if existing.
93 public function currentRevision(): HasOne
95 return $this->hasOne(PageRevision::class)
96 ->where('type', '=', 'version')
97 ->orderBy('created_at', 'desc')
98 ->orderBy('id', 'desc');
102 * Get all revision instances assigned to this page.
103 * Includes all types of revisions.
105 public function allRevisions(): HasMany
107 return $this->hasMany(PageRevision::class);
111 * Get the attachments assigned to this page.
115 public function attachments()
117 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
121 * Get the url of this page.
123 public function getUrl(string $path = ''): string
127 urlencode($this->book_slug ?? $this->book->slug),
128 $this->draft ? 'draft' : 'page',
129 $this->draft ? $this->id : urlencode($this->slug),
133 return url('/' . implode('/', $parts));
137 * Get this page for JSON display.
139 public function forJsonDisplay(): self
141 $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
142 $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
143 $refreshed->setAttribute('raw_html', $refreshed->html);
144 $refreshed->html = (new PageContent($refreshed))->render();
150 * Get a visible page by its book and page slugs.
151 * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
153 public static function getBySlugs(string $bookSlug, string $pageSlug): self
155 return static::visible()->whereSlugs($bookSlug, $pageSlug)->firstOrFail();