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\Factories\HasFactory;
10 use Illuminate\Database\Eloquent\Relations\BelongsTo;
11 use Illuminate\Database\Eloquent\Relations\HasMany;
17 * @property int $chapter_id
18 * @property string $html
19 * @property string $markdown
20 * @property string $text
21 * @property bool $template
22 * @property bool $draft
23 * @property int $revision_count
24 * @property Chapter $chapter
25 * @property Collection $attachments
27 class Page extends BookChild
31 public static $listAttributes = ['name', 'id', 'slug', 'book_id', 'chapter_id', 'draft', 'template', 'text', 'created_at', 'updated_at', 'priority'];
32 public static $contentAttributes = ['name', 'id', 'slug', 'book_id', 'chapter_id', 'draft', 'template', 'html', 'text', 'created_at', 'updated_at', 'priority'];
34 protected $fillable = ['name', 'priority'];
36 public $textField = 'text';
38 protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot', 'deleted_at'];
42 'template' => 'boolean',
46 * Get the entities that are visible to the current user.
48 public function scopeVisible(Builder $query): Builder
50 $query = Permissions::enforceDraftVisibilityOnQuery($query);
52 return parent::scopeVisible($query);
56 * Get the chapter that this page is in, If applicable.
60 public function chapter()
62 return $this->belongsTo(Chapter::class);
66 * Check if this page has a chapter.
70 public function hasChapter()
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 all revision instances assigned to this page.
89 * Includes all types of revisions.
91 public function allRevisions(): HasMany
93 return $this->hasMany(PageRevision::class);
97 * Get the attachments assigned to this page.
101 public function attachments()
103 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
107 * Get the url of this page.
109 public function getUrl(string $path = ''): string
113 urlencode($this->book_slug ?? $this->book->slug),
114 $this->draft ? 'draft' : 'page',
115 $this->draft ? $this->id : urlencode($this->slug),
119 return url('/' . implode('/', $parts));
123 * Get the current revision for the page if existing.
125 * @return PageRevision|null
127 public function getCurrentRevision()
129 return $this->revisions()->first();
133 * Get this page for JSON display.
135 public function forJsonDisplay(): self
137 $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
138 $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
139 $refreshed->html = (new PageContent($refreshed))->render();