1 <?php namespace BookStack\Entities;
3 use BookStack\Uploads\Attachment;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Chapter;
6 use BookStack\Entities\Entity;
7 use BookStack\Entities\PageRevision;
9 class Page extends Entity
11 protected $fillable = ['name', 'html', 'priority', 'markdown'];
13 protected $simpleAttributes = ['name', 'id', 'slug'];
15 public $textField = 'text';
18 * Get the morph class for this model.
21 public function getMorphClass()
23 return 'BookStack\\Page';
27 * Converts this page into a simplified array.
30 public function toSimpleArray()
32 $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
33 $array['url'] = $this->getUrl();
38 * Get the book this page sits in.
39 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
41 public function book()
43 return $this->belongsTo(Book::class);
48 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
50 public function parent()
52 return $this->chapter_id ? $this->chapter() : $this->book();
56 * Get the chapter that this page is in, If applicable.
57 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
59 public function chapter()
61 return $this->belongsTo(Chapter::class);
65 * Check if this page has a chapter.
68 public function hasChapter()
70 return $this->chapter()->count() > 0;
74 * Get the associated page revisions, ordered by created date.
77 public function revisions()
79 return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc');
83 * Get the attachments assigned to this page.
84 * @return \Illuminate\Database\Eloquent\Relations\HasMany
86 public function attachments()
88 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
92 * Get the url for this page.
93 * @param string|bool $path
96 public function getUrl($path = false)
98 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
99 $midText = $this->draft ? '/draft/' : '/page/';
100 $idComponent = $this->draft ? $this->id : urlencode($this->slug);
102 if ($path !== false) {
103 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
106 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent);
110 * Get an excerpt of this page's content to the specified length.
114 public function getExcerpt($length = 100)
116 $text = strlen($this->text) > $length ? substr($this->text, 0, $length-3) . '...' : $this->text;
117 return mb_convert_encoding($text, 'UTF-8');
121 * Return a generalised, common raw query that can be 'unioned' across entities.
122 * @param bool $withContent
125 public function entityRawQuery($withContent = false)
127 $htmlQuery = $withContent ? 'html' : "'' as html";
128 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";
132 * Get the current revision for the page if existing
133 * @return \BookStack\Entities\PageRevision|null
135 public function getCurrentRevision()
137 return $this->revisions()->first();