1 <?php namespace BookStack\Entities;
3 class Chapter extends Entity
5 public $searchFactor = 1.3;
7 protected $fillable = ['name', 'description', 'priority', 'book_id'];
10 * Get the morph class for this model.
13 public function getMorphClass()
15 return 'BookStack\\Chapter';
19 * Get the book this chapter is within.
20 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
22 public function book()
24 return $this->belongsTo(Book::class);
28 * Get the pages that this chapter contains.
32 public function pages($dir = 'ASC')
34 return $this->hasMany(Page::class)->orderBy('priority', $dir);
38 * Get the url of this chapter.
39 * @param string|bool $path
42 public function getUrl($path = false)
44 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
45 if ($path !== false) {
46 return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug) . '/' . trim($path, '/'));
48 return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug));
52 * Get an excerpt of this chapter's description to the specified length or less.
56 public function getExcerpt(int $length = 100)
58 $description = $this->text ?? $this->description;
59 return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
63 * Return a generalised, common raw query that can be 'unioned' across entities.
66 public function entityRawQuery()
68 return "'BookStack\\\\Chapter' as entity_type, id, id as entity_id, slug, name, {$this->textField} as text, '' as html, book_id, priority, '0' as chapter_id, '0' as draft, created_by, updated_by, updated_at, created_at";
72 * Check if this chapter has any child pages.
75 public function hasChildren()
77 return count($this->pages) > 0;