1 <?php namespace BookStack;
3 class Page extends Entity
5 protected $fillable = ['name', 'html', 'priority', 'markdown'];
7 protected $simpleAttributes = ['name', 'id', 'slug'];
9 public $textField = 'text';
12 * Converts this page into a simplified array.
15 public function toSimpleArray()
17 $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
18 $array['url'] = $this->getUrl();
23 * Get the book this page sits in.
24 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
26 public function book()
28 return $this->belongsTo(Book::class);
32 * Get the chapter that this page is in, If applicable.
33 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
35 public function chapter()
37 return $this->belongsTo(Chapter::class);
41 * Check if this page has a chapter.
44 public function hasChapter()
46 return $this->chapter()->count() > 0;
50 * Get the associated page revisions, ordered by created date.
53 public function revisions()
55 return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc');
59 * Get the attachments assigned to this page.
60 * @return \Illuminate\Database\Eloquent\Relations\HasMany
62 public function attachments()
64 return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
68 * Get the url for this page.
69 * @param string|bool $path
72 public function getUrl($path = false)
74 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
75 $midText = $this->draft ? '/draft/' : '/page/';
76 $idComponent = $this->draft ? $this->id : urlencode($this->slug);
78 if ($path !== false) {
79 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
82 return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent);
86 * Get an excerpt of this page's content to the specified length.
90 public function getExcerpt($length = 100)
92 $text = strlen($this->text) > $length ? substr($this->text, 0, $length-3) . '...' : $this->text;
93 return mb_convert_encoding($text, 'UTF-8');
97 * Return a generalised, common raw query that can be 'unioned' across entities.
98 * @param bool $withContent
101 public function entityRawQuery($withContent = false)
103 $htmlQuery = $withContent ? 'html' : "'' as html";
104 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";