3 namespace BookStack\Entities\Models;
5 use BookStack\Entities\Tools\PageContent;
6 use BookStack\Entities\Tools\PageEditorType;
7 use BookStack\Permissions\PermissionApplicator;
8 use BookStack\Uploads\Attachment;
9 use Illuminate\Database\Eloquent\Builder;
10 use Illuminate\Database\Eloquent\Collection;
11 use Illuminate\Database\Eloquent\Factories\HasFactory;
12 use Illuminate\Database\Eloquent\Relations\BelongsTo;
13 use Illuminate\Database\Eloquent\Relations\HasMany;
14 use Illuminate\Database\Eloquent\Relations\HasOne;
19 * @property int $chapter_id
20 * @property string $html
21 * @property string $markdown
22 * @property string $text
23 * @property bool $template
24 * @property bool $draft
25 * @property int $revision_count
26 * @property string $editor
27 * @property Chapter $chapter
28 * @property Collection $attachments
29 * @property Collection $revisions
30 * @property PageRevision $currentRevision
32 class Page extends BookChild
36 protected $fillable = ['name', 'priority'];
38 public string $textField = 'text';
39 public string $htmlField = 'html';
41 protected $hidden = ['html', 'markdown', 'text', 'pivot', 'deleted_at'];
45 'template' => 'boolean',
49 * Get the entities that are visible to the current user.
51 public function scopeVisible(Builder $query): Builder
53 $query = app()->make(PermissionApplicator::class)->restrictDraftsOnPageQuery($query);
55 return parent::scopeVisible($query);
59 * Get the chapter that this page is in, If applicable.
63 public function chapter()
65 return $this->belongsTo(Chapter::class);
69 * Check if this page has a chapter.
71 public function hasChapter(): bool
73 return $this->chapter()->count() > 0;
77 * Get the associated page revisions, ordered by created date.
78 * Only provides actual saved page revision instances, Not drafts.
80 public function revisions(): HasMany
82 return $this->allRevisions()
83 ->where('type', '=', 'version')
84 ->orderBy('created_at', 'desc')
85 ->orderBy('id', 'desc');
89 * Get the current revision for the page if existing.
91 public function currentRevision(): HasOne
93 return $this->hasOne(PageRevision::class)
94 ->where('type', '=', 'version')
95 ->orderBy('created_at', 'desc')
96 ->orderBy('id', 'desc');
100 * Get all revision instances assigned to this page.
101 * Includes all types of revisions.
103 public function allRevisions(): HasMany
105 return $this->hasMany(PageRevision::class);
109 * Get the attachments assigned to this page.
113 public function attachments()
115 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
119 * Get the url of this page.
121 public function getUrl(string $path = ''): string
125 urlencode($this->book_slug ?? $this->book->slug),
126 $this->draft ? 'draft' : 'page',
127 $this->draft ? $this->id : urlencode($this->slug),
131 return url('/' . implode('/', $parts));
135 * Get this page for JSON display.
137 public function forJsonDisplay(): self
139 $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
140 $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
141 $refreshed->setAttribute('raw_html', $refreshed->html);
142 $refreshed->html = (new PageContent($refreshed))->render();