1 <?php namespace BookStack\Entities;
3 use BookStack\Uploads\Attachment;
5 class Page extends BookChild
7 protected $fillable = ['name', 'html', 'priority', 'markdown'];
9 protected $simpleAttributes = ['name', 'id', 'slug'];
11 public $textField = 'text';
14 * Get the morph class for this model.
17 public function getMorphClass()
19 return 'BookStack\\Page';
23 * Converts this page into a simplified array.
26 public function toSimpleArray()
28 $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
29 $array['url'] = $this->getUrl();
35 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
37 public function parent()
39 return $this->chapter_id ? $this->chapter() : $this->book();
43 * Get the chapter that this page is in, If applicable.
44 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
46 public function chapter()
48 return $this->belongsTo(Chapter::class);
52 * Check if this page has a chapter.
55 public function hasChapter()
57 return $this->chapter()->count() > 0;
61 * Get the associated page revisions, ordered by created date.
64 public function revisions()
66 return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc');
70 * Get the attachments assigned to this page.
71 * @return \Illuminate\Database\Eloquent\Relations\HasMany
73 public function attachments()
75 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
79 * Get the url for this page.
80 * @param string|bool $path
83 public function getUrl($path = false)
85 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
86 $midText = $this->draft ? '/draft/' : '/page/';
87 $idComponent = $this->draft ? $this->id : urlencode($this->slug);
89 if ($path !== false) {
90 return url('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
93 return url('/books/' . urlencode($bookSlug) . $midText . $idComponent);
97 * Return a generalised, common raw query that can be 'unioned' across entities.
98 * @param bool $withContent
101 public function entityRawQuery($withContent = false)
103 $htmlQuery = $withContent ? 'html' : "'' as html";
104 return "'BookStack\\\\Page' as entity_type, id, id as entity_id, slug, name, {$this->textField} as text, {$htmlQuery}, book_id, priority, chapter_id, draft, created_by, updated_by, updated_at, created_at";
108 * Get the current revision for the page if existing
109 * @return \BookStack\Entities\PageRevision|null
111 public function getCurrentRevision()
113 return $this->revisions()->first();