1 <?php namespace BookStack;
4 class Page extends Entity
6 protected $fillable = ['name', 'html', 'priority', 'markdown'];
8 protected $simpleAttributes = ['name', 'id', 'slug'];
11 * Converts this page into a simplified array.
14 public function toSimpleArray()
16 $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
17 $array['url'] = $this->getUrl();
22 * Get the book this page sits in.
23 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
25 public function book()
27 return $this->belongsTo(Book::class);
31 * Get the chapter that this page is in, If applicable.
32 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
34 public function chapter()
36 return $this->belongsTo(Chapter::class);
40 * Check if this page has a chapter.
43 public function hasChapter()
45 return $this->chapter()->count() > 0;
49 * Get the associated page revisions, ordered by created date.
52 public function revisions()
54 return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc');
58 * Get the url for this page.
59 * @param string|bool $path
62 public function getUrl($path = false)
64 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
65 $midText = $this->draft ? '/draft/' : '/page/';
66 $idComponent = $this->draft ? $this->id : $this->slug;
68 if ($path !== false) {
69 return baseUrl('/books/' . $bookSlug . $midText . $idComponent . '/' . trim($path, '/'));
72 return baseUrl('/books/' . $bookSlug . $midText . $idComponent);
76 * Get an excerpt of this page's content to the specified length.
80 public function getExcerpt($length = 100)
82 $text = strlen($this->text) > $length ? substr($this->text, 0, $length-3) . '...' : $this->text;
83 return mb_convert_encoding($text, 'UTF-8');