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'];
11 public $textField = 'text';
14 * Converts this page into a simplified array.
17 public function toSimpleArray()
19 $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
20 $array['url'] = $this->getUrl();
25 * Get the book this page sits in.
26 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
28 public function book()
30 return $this->belongsTo(Book::class);
34 * Get the chapter that this page is in, If applicable.
35 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
37 public function chapter()
39 return $this->belongsTo(Chapter::class);
43 * Check if this page has a chapter.
46 public function hasChapter()
48 return $this->chapter()->count() > 0;
52 * Get the associated page revisions, ordered by created date.
55 public function revisions()
57 return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc');
61 * Get the attachments assigned to this page.
62 * @return \Illuminate\Database\Eloquent\Relations\HasMany
64 public function attachments()
66 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
69 public function comments() {
70 return $this->hasMany(Comment::class, 'page_id')->orderBy('created_on', 'asc');
74 * Get the url for this page.
75 * @param string|bool $path
78 public function getUrl($path = false)
80 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
81 $midText = $this->draft ? '/draft/' : '/page/';
82 $idComponent = $this->draft ? $this->id : urlencode($this->slug);
84 if ($path !== false) {
85 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
88 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent);
92 * Get an excerpt of this page's content to the specified length.
96 public function getExcerpt($length = 100)
98 $text = strlen($this->text) > $length ? substr($this->text, 0, $length-3) . '...' : $this->text;
99 return mb_convert_encoding($text, 'UTF-8');
103 * Return a generalised, common raw query that can be 'unioned' across entities.
104 * @param bool $withContent
107 public function entityRawQuery($withContent = false)
108 { $htmlQuery = $withContent ? 'html' : "'' as html";
109 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";