1 <?php namespace BookStack;
4 class Page extends Entity
6 protected $fillable = ['name', 'html', 'priority', 'markdown'];
8 protected $simpleAttributes = ['name', 'id', 'slug'];
10 protected $with = ['book'];
12 protected $fieldsToSearch = ['name', 'text'];
15 * Converts this page into a simplified array.
18 public function toSimpleArray()
20 $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
21 $array['url'] = $this->getUrl();
26 * Get the book this page sits in.
27 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
29 public function book()
31 return $this->belongsTo(Book::class);
35 * Get the chapter that this page is in, If applicable.
36 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
38 public function chapter()
40 return $this->belongsTo(Chapter::class);
44 * Check if this page has a chapter.
47 public function hasChapter()
49 return $this->chapter()->count() > 0;
53 * Get the associated page revisions, ordered by created date.
56 public function revisions()
58 return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc');
62 * Get the attachments assigned to this page.
63 * @return \Illuminate\Database\Eloquent\Relations\HasMany
65 public function attachments()
67 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
71 * Get the url for this page.
72 * @param string|bool $path
75 public function getUrl($path = false)
77 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
78 $midText = $this->draft ? '/draft/' : '/page/';
79 $idComponent = $this->draft ? $this->id : urlencode($this->slug);
81 if ($path !== false) {
82 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
85 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent);
89 * Get an excerpt of this page's content to the specified length.
93 public function getExcerpt($length = 100)
95 $text = strlen($this->text) > $length ? substr($this->text, 0, $length-3) . '...' : $this->text;
96 return mb_convert_encoding($text, 'UTF-8');