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 protected $fillable = ['name', 'priority'];
37 public string $textField = 'text';
38 public string $htmlField = 'html';
40 protected $hidden = ['html', 'markdown', 'text', 'pivot', 'deleted_at'];
44 'template' => 'boolean',
48 * Get the entities that are visible to the current user.
50 public function scopeVisible(Builder $query): Builder
52 $query = app()->make(PermissionApplicator::class)->restrictDraftsOnPageQuery($query);
54 return parent::scopeVisible($query);
58 * Get the chapter that this page is in, If applicable.
62 public function chapter()
64 return $this->belongsTo(Chapter::class);
68 * Check if this page has a chapter.
70 public function hasChapter(): bool
72 return $this->chapter()->count() > 0;
76 * Get the associated page revisions, ordered by created date.
77 * Only provides actual saved page revision instances, Not drafts.
79 public function revisions(): HasMany
81 return $this->allRevisions()
82 ->where('type', '=', 'version')
83 ->orderBy('created_at', 'desc')
84 ->orderBy('id', 'desc');
88 * Get the current revision for the page if existing.
90 public function currentRevision(): HasOne
92 return $this->hasOne(PageRevision::class)
93 ->where('type', '=', 'version')
94 ->orderBy('created_at', 'desc')
95 ->orderBy('id', 'desc');
99 * Get all revision instances assigned to this page.
100 * Includes all types of revisions.
102 public function allRevisions(): HasMany
104 return $this->hasMany(PageRevision::class);
108 * Get the attachments assigned to this page.
112 public function attachments()
114 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
118 * Get the url of this page.
120 public function getUrl(string $path = ''): string
124 urlencode($this->book_slug ?? $this->book->slug),
125 $this->draft ? 'draft' : 'page',
126 $this->draft ? $this->id : urlencode($this->slug),
130 return url('/' . implode('/', $parts));
134 * Get this page for JSON display.
136 public function forJsonDisplay(): self
138 $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
139 $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
140 $refreshed->setAttribute('raw_html', $refreshed->html);
141 $refreshed->html = (new PageContent($refreshed))->render();