1 <?php namespace BookStack\Entities;
3 use Illuminate\Support\Collection;
7 * @property Collection<Page> $pages
8 * @package BookStack\Entities
10 class Chapter extends BookChild
12 public $searchFactor = 1.3;
14 protected $fillable = ['name', 'description', 'priority', 'book_id'];
17 * Get the pages that this chapter contains.
21 public function pages($dir = 'ASC')
23 return $this->hasMany(Page::class)->orderBy('priority', $dir);
27 * Get the url of this chapter.
28 * @param string|bool $path
31 public function getUrl($path = false)
33 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
34 $fullPath = '/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug);
36 if ($path !== false) {
37 $fullPath .= '/' . trim($path, '/');
40 return url($fullPath);
44 * Get an excerpt of this chapter's description to the specified length or less.
48 public function getExcerpt(int $length = 100)
50 $description = $this->text ?? $this->description;
51 return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
55 * Check if this chapter has any child pages.
58 public function hasChildren()
60 return count($this->pages) > 0;
64 * Get the visible pages in this chapter.
66 public function getVisiblePages(): Collection
68 return $this->pages()->visible()
69 ->orderBy('draft', 'desc')
70 ->orderBy('priority', 'asc')