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 $fullPath = '/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug);
47 if ($path !== false) {
48 $fullPath .= '/' . trim($path, '/');
51 return url($fullPath);
55 * Get an excerpt of this chapter's description to the specified length or less.
59 public function getExcerpt(int $length = 100)
61 $description = $this->text ?? $this->description;
62 return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
66 * Return a generalised, common raw query that can be 'unioned' across entities.
69 public function entityRawQuery()
71 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";
75 * Check if this chapter has any child pages.
78 public function hasChildren()
80 return count($this->pages) > 0;