1 <?php namespace BookStack\Entities\Models;
3 use BookStack\Entities\Tools\PageContent;
4 use BookStack\Uploads\Attachment;
5 use Illuminate\Database\Eloquent\Builder;
6 use Illuminate\Database\Eloquent\Collection;
7 use Illuminate\Database\Eloquent\Relations\BelongsTo;
8 use Illuminate\Database\Eloquent\Relations\HasMany;
13 * @property int $chapter_id
14 * @property string $html
15 * @property string $markdown
16 * @property string $text
17 * @property bool $template
18 * @property bool $draft
19 * @property int $revision_count
20 * @property Chapter $chapter
21 * @property Collection $attachments
23 class Page extends BookChild
25 protected $fillable = ['name', 'priority', 'markdown'];
27 protected $simpleAttributes = ['name', 'id', 'slug'];
29 public $textField = 'text';
31 protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot', 'deleted_at'];
35 'template' => 'boolean',
39 * Get the entities that are visible to the current user.
41 public function scopeVisible(Builder $query): Builder
43 $query = Permissions::enforceDraftVisiblityOnQuery($query);
44 return parent::scopeVisible($query);
48 * Converts this page into a simplified array.
51 public function toSimpleArray()
53 $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
54 $array['url'] = $this->getUrl();
59 * 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.
71 public function hasChapter()
73 return $this->chapter()->count() > 0;
77 * Get the associated page revisions, ordered by created date.
80 public function revisions()
82 return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc')->orderBy('id', 'desc');
86 * Get the attachments assigned to this page.
89 public function attachments()
91 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
95 * Get the url of this page.
97 public function getUrl($path = ''): string
101 urlencode($this->getAttribute('bookSlug') ?? $this->book->slug),
102 $this->draft ? 'draft' : 'page',
103 $this->draft ? $this->id : urlencode($this->slug),
107 return url('/' . implode('/', $parts));
111 * Get the current revision for the page if existing
112 * @return PageRevision|null
114 public function getCurrentRevision()
116 return $this->revisions()->first();
120 * Get this page for JSON display.
122 public function forJsonDisplay(): Page
124 $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
125 $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
126 $refreshed->html = (new PageContent($refreshed))->render();
130 * Get the parent chapter ID.
132 public function getParentChapter()
134 $chapterId = $this->chapter()->visible()