3 namespace BookStack\Entities\Models;
5 use BookStack\Entities\Tools\PageContent;
6 use BookStack\Uploads\Attachment;
7 use Illuminate\Database\Eloquent\Builder;
8 use Illuminate\Database\Eloquent\Collection;
9 use Illuminate\Database\Eloquent\Relations\BelongsTo;
10 use Illuminate\Database\Eloquent\Relations\HasMany;
16 * @property int $chapter_id
17 * @property string $html
18 * @property string $markdown
19 * @property string $text
20 * @property bool $template
21 * @property bool $draft
22 * @property int $revision_count
23 * @property Chapter $chapter
24 * @property Collection $attachments
26 class Page extends BookChild
28 protected $fillable = ['name', 'priority', 'markdown'];
30 protected $simpleAttributes = ['name', 'id', 'slug'];
32 public $textField = 'text';
34 protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot', 'deleted_at'];
38 'template' => 'boolean',
42 * Get the entities that are visible to the current user.
44 public function scopeVisible(Builder $query): Builder
46 $query = Permissions::enforceDraftVisibilityOnQuery($query);
48 return parent::scopeVisible($query);
52 * Converts this page into a simplified array.
56 public function toSimpleArray()
58 $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
59 $array['url'] = $this->getUrl();
65 * Get the chapter that this page is in, If applicable.
69 public function chapter()
71 return $this->belongsTo(Chapter::class);
75 * Check if this page has a chapter.
79 public function hasChapter()
81 return $this->chapter()->count() > 0;
85 * Get the associated page revisions, ordered by created date.
86 * Only provides actual saved page revision instances, Not drafts.
88 public function revisions(): HasMany
90 return $this->allRevisions()
91 ->where('type', '=', 'version')
92 ->orderBy('created_at', 'desc')
93 ->orderBy('id', 'desc');
97 * Get all revision instances assigned to this page.
98 * Includes all types of revisions.
100 public function allRevisions(): HasMany
102 return $this->hasMany(PageRevision::class);
106 * Get the attachments assigned to this page.
110 public function attachments()
112 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
116 * Get the url of this page.
118 public function getUrl($path = ''): string
122 urlencode($this->getAttribute('bookSlug') ?? $this->book->slug),
123 $this->draft ? 'draft' : 'page',
124 $this->draft ? $this->id : urlencode($this->slug),
128 return url('/' . implode('/', $parts));
132 * Get the current revision for the page if existing.
134 * @return PageRevision|null
136 public function getCurrentRevision()
138 return $this->revisions()->first();
142 * Get this page for JSON display.
144 public function forJsonDisplay(): Page
146 $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
147 $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
148 $refreshed->html = (new PageContent($refreshed))->render();