1 <?php namespace BookStack\Entities;
3 use BookStack\Uploads\Attachment;
5 class Page extends Entity
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();
34 * Get the book this page sits in.
35 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
37 public function book()
39 return $this->belongsTo(Book::class);
44 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
46 public function parent()
48 return $this->chapter_id ? $this->chapter() : $this->book();
52 * Get the chapter that this page is in, If applicable.
53 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
55 public function chapter()
57 return $this->belongsTo(Chapter::class);
61 * Check if this page has a chapter.
64 public function hasChapter()
66 return $this->chapter()->count() > 0;
70 * Get the associated page revisions, ordered by created date.
73 public function revisions()
75 return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc');
79 * Get the attachments assigned to this page.
80 * @return \Illuminate\Database\Eloquent\Relations\HasMany
82 public function attachments()
84 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
88 * Get the url for this page.
89 * @param string|bool $path
92 public function getUrl($path = false)
94 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
95 $midText = $this->draft ? '/draft/' : '/page/';
96 $idComponent = $this->draft ? $this->id : urlencode($this->slug);
98 if ($path !== false) {
99 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
102 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent);
106 * Return a generalised, common raw query that can be 'unioned' across entities.
107 * @param bool $withContent
110 public function entityRawQuery($withContent = false)
112 $htmlQuery = $withContent ? 'html' : "'' as html";
113 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";
117 * Get the current revision for the page if existing
118 * @return \BookStack\Entities\PageRevision|null
120 public function getCurrentRevision()
122 return $this->revisions()->first();