1 <?php namespace BookStack;
3 class Page extends Entity
5 protected $fillable = ['name', 'html', 'priority', 'markdown'];
7 protected $simpleAttributes = ['name', 'id', 'slug'];
9 protected $with = ['book'];
10 public $textField = 'text';
13 * Converts this page into a simplified array.
16 public function toSimpleArray()
18 $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
19 $array['url'] = $this->getUrl();
24 * Get the book this page sits in.
25 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
27 public function book()
29 return $this->belongsTo(Book::class);
33 * Get the chapter that this page is in, If applicable.
34 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
36 public function chapter()
38 return $this->belongsTo(Chapter::class);
42 * Check if this page has a chapter.
45 public function hasChapter()
47 return $this->chapter()->count() > 0;
51 * Get the associated page revisions, ordered by created date.
54 public function revisions()
56 return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc');
60 * Get the attachments assigned to this page.
61 * @return \Illuminate\Database\Eloquent\Relations\HasMany
63 public function attachments()
65 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
69 * Get the url for this page.
70 * @param string|bool $path
73 public function getUrl($path = false)
75 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
76 $midText = $this->draft ? '/draft/' : '/page/';
77 $idComponent = $this->draft ? $this->id : urlencode($this->slug);
79 if ($path !== false) {
80 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
83 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent);
87 * Get an excerpt of this page's content to the specified length.
91 public function getExcerpt($length = 100)
93 $text = strlen($this->text) > $length ? substr($this->text, 0, $length-3) . '...' : $this->text;
94 return mb_convert_encoding($text, 'UTF-8');
98 * Return a generalised, common raw query that can be 'unioned' across entities.
99 * @param bool $withContent
102 public function entityRawQuery($withContent = false)
104 $htmlQuery = $withContent ? 'html' : "'' as html";
105 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";