1 <?php namespace BookStack\Entities;
3 use Illuminate\Database\Eloquent\Relations\BelongsTo;
5 class Chapter extends BookChild
7 public $searchFactor = 1.3;
9 protected $fillable = ['name', 'description', 'priority', 'book_id'];
12 * Get the morph class for this model.
15 public function getMorphClass()
17 return 'BookStack\\Chapter';
21 * Get the pages that this chapter contains.
25 public function pages($dir = 'ASC')
27 return $this->hasMany(Page::class)->orderBy('priority', $dir);
31 * Get the url of this chapter.
32 * @param string|bool $path
35 public function getUrl($path = false)
37 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
38 $fullPath = '/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug);
40 if ($path !== false) {
41 $fullPath .= '/' . trim($path, '/');
44 return url($fullPath);
48 * Get an excerpt of this chapter's description to the specified length or less.
52 public function getExcerpt(int $length = 100)
54 $description = $this->text ?? $this->description;
55 return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
59 * Return a generalised, common raw query that can be 'unioned' across entities.
62 public function entityRawQuery()
64 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";
68 * Check if this chapter has any child pages.
71 public function hasChildren()
73 return count($this->pages) > 0;