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 public static $listAttributes = ['name', 'id', 'slug', 'book_id', 'chapter_id', 'draft', 'template', 'text', 'created_at', 'updated_at', 'priority'];
29 public static $contentAttributes = ['name', 'id', 'slug', 'book_id', 'chapter_id', 'draft', 'template', 'html', 'text', 'created_at', 'updated_at', 'priority'];
31 protected $fillable = ['name', 'priority'];
33 public $textField = 'text';
35 protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot', 'deleted_at'];
39 'template' => 'boolean',
43 * Get the entities that are visible to the current user.
45 public function scopeVisible(Builder $query): Builder
47 $query = Permissions::enforceDraftVisibilityOnQuery($query);
49 return parent::scopeVisible($query);
53 * Get the chapter that this page is in, If applicable.
57 public function chapter()
59 return $this->belongsTo(Chapter::class);
63 * Check if this page has a chapter.
67 public function hasChapter()
69 return $this->chapter()->count() > 0;
73 * Get the associated page revisions, ordered by created date.
74 * Only provides actual saved page revision instances, Not drafts.
76 public function revisions(): HasMany
78 return $this->allRevisions()
79 ->where('type', '=', 'version')
80 ->orderBy('created_at', 'desc')
81 ->orderBy('id', 'desc');
85 * Get all revision instances assigned to this page.
86 * Includes all types of revisions.
88 public function allRevisions(): HasMany
90 return $this->hasMany(PageRevision::class);
94 * Get the attachments assigned to this page.
98 public function attachments()
100 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
104 * Get the url of this page.
106 public function getUrl($path = ''): string
110 urlencode($this->book_slug ?? $this->book->slug),
111 $this->draft ? 'draft' : 'page',
112 $this->draft ? $this->id : urlencode($this->slug),
116 return url('/' . implode('/', $parts));
120 * Get the current revision for the page if existing.
122 * @return PageRevision|null
124 public function getCurrentRevision()
126 return $this->revisions()->first();
130 * Get this page for JSON display.
132 public function forJsonDisplay(): Page
134 $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
135 $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
136 $refreshed->html = (new PageContent($refreshed))->render();