1 <?php namespace BookStack\Entities;
3 use BookStack\Uploads\Attachment;
4 use Illuminate\Database\Eloquent\Builder;
5 use Illuminate\Database\Eloquent\Collection;
6 use Illuminate\Database\Eloquent\Relations\BelongsTo;
7 use Illuminate\Database\Eloquent\Relations\HasMany;
12 * @property int $chapter_id
13 * @property string $html
14 * @property string $markdown
15 * @property string $text
16 * @property bool $template
17 * @property bool $draft
18 * @property int $revision_count
19 * @property Chapter $chapter
20 * @property Collection $attachments
22 class Page extends BookChild
24 protected $fillable = ['name', 'priority', 'markdown'];
26 protected $simpleAttributes = ['name', 'id', 'slug'];
28 public $textField = 'text';
30 protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot'];
33 * Get the entities that are visible to the current user.
35 public function scopeVisible(Builder $query)
37 $query = Permissions::enforceDraftVisiblityOnQuery($query);
38 return parent::scopeVisible($query);
42 * Converts this page into a simplified array.
45 public function toSimpleArray()
47 $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
48 $array['url'] = $this->getUrl();
53 * Get the chapter that this page is in, If applicable.
56 public function chapter()
58 return $this->belongsTo(Chapter::class);
62 * Check if this page has a chapter.
65 public function hasChapter()
67 return $this->chapter()->count() > 0;
71 * Get the associated page revisions, ordered by created date.
74 public function revisions()
76 return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc')->orderBy('id', 'desc');
80 * Get the attachments assigned to this page.
83 public function attachments()
85 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
89 * Get the url for this page.
90 * @param string|bool $path
93 public function getUrl($path = false)
95 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
96 $midText = $this->draft ? '/draft/' : '/page/';
97 $idComponent = $this->draft ? $this->id : urlencode($this->slug);
99 $url = '/books/' . urlencode($bookSlug) . $midText . $idComponent;
100 if ($path !== false) {
101 $url .= '/' . trim($path, '/');
108 * Get the current revision for the page if existing
109 * @return PageRevision|null
111 public function getCurrentRevision()
113 return $this->revisions()->first();