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 * Get the comments in the page.
45 * @return \Illuminate\Database\Eloquent\Relations\HasMany
47 public function comment()
49 return $this->hasMany(Comment::class);
53 * Check if this page has a chapter.
56 public function hasChapter()
58 return $this->chapter()->count() > 0;
62 * Get the associated page revisions, ordered by created date.
65 public function revisions()
67 return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc');
71 * Get the attachments assigned to this page.
72 * @return \Illuminate\Database\Eloquent\Relations\HasMany
74 public function attachments()
76 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
80 * Get the url for this page.
81 * @param string|bool $path
84 public function getUrl($path = false)
86 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
87 $midText = $this->draft ? '/draft/' : '/page/';
88 $idComponent = $this->draft ? $this->id : urlencode($this->slug);
90 if ($path !== false) {
91 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
94 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent);
98 * Get an excerpt of this page's content to the specified length.
102 public function getExcerpt($length = 100)
104 $text = strlen($this->text) > $length ? substr($this->text, 0, $length-3) . '...' : $this->text;
105 return mb_convert_encoding($text, 'UTF-8');