1 <?php namespace BookStack;
3 class Page extends Entity
5 protected $fillable = ['name', 'html', 'priority', 'markdown'];
7 protected $simpleAttributes = ['name', 'id', 'slug'];
9 public $textField = 'text';
12 * Converts this page into a simplified array.
15 public function toSimpleArray()
17 $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
18 $array['url'] = $this->getUrl();
23 * Get the book this page sits in.
24 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
26 public function book()
28 return $this->belongsTo(Book::class);
33 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
35 public function parent()
37 return $this->chapter_id ? $this->chapter() : $this->book();
41 * Get the chapter that this page is in, If applicable.
42 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
44 public function chapter()
46 return $this->belongsTo(Chapter::class);
50 * Check if this page has a chapter.
53 public function hasChapter()
55 return $this->chapter()->count() > 0;
59 * Get the associated page revisions, ordered by created date.
62 public function revisions()
64 return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc');
68 * Get the attachments assigned to this page.
69 * @return \Illuminate\Database\Eloquent\Relations\HasMany
71 public function attachments()
73 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
77 * Get the url for this page.
78 * @param string|bool $path
81 public function getUrl($path = false)
83 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
84 $midText = $this->draft ? '/draft/' : '/page/';
85 $idComponent = $this->draft ? $this->id : urlencode($this->slug);
87 if ($path !== false) {
88 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
91 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent);
95 * Get an excerpt of this page's content to the specified length.
99 public function getExcerpt($length = 100)
101 $text = strlen($this->text) > $length ? substr($this->text, 0, $length-3) . '...' : $this->text;
102 return mb_convert_encoding($text, 'UTF-8');
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";