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::enforceDraftVisibilityOnQuery($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.
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 all revision instances assigned to this page.
90 * Includes all types of revisions.
92 public function allRevisions(): HasMany
94 return $this->hasMany(PageRevision::class);
98 * 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($path = ''): string
113 urlencode($this->getAttribute('bookSlug') ?? $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
124 * @return PageRevision|null
126 public function getCurrentRevision()
128 return $this->revisions()->first();
132 * Get this page for JSON display.
134 public function forJsonDisplay(): Page
136 $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
137 $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
138 $refreshed->html = (new PageContent($refreshed))->render();
143 * Returns URL to a cover image for the page.
145 public function getCoverImage()
147 //$default = $this->book->getBookCover();
148 $default = url('/logo.png');
150 $firstImage = (new PageContent($this))->fetchFirstImage();
153 $cover = $firstImage ? $firstImage : $default;
154 } catch (\Exception $err) {