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'];
15 protected $hidden = ['restricted', 'pivot'];
18 * Get the pages that this chapter contains.
22 public function pages($dir = 'ASC')
24 return $this->hasMany(Page::class)->orderBy('priority', $dir);
28 * Get the url of this chapter.
29 * @param string|bool $path
32 public function getUrl($path = false)
34 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
35 $fullPath = '/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug);
37 if ($path !== false) {
38 $fullPath .= '/' . trim($path, '/');
41 return url($fullPath);
45 * Get an excerpt of this chapter's description to the specified length or less.
49 public function getExcerpt(int $length = 100)
51 $description = $this->text ?? $this->description;
52 return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
56 * Check if this chapter has any child pages.
59 public function hasChildren()
61 return count($this->pages) > 0;
65 * Get the visible pages in this chapter.
67 public function getVisiblePages(): Collection
69 return $this->pages()->visible()
70 ->orderBy('draft', 'desc')
71 ->orderBy('priority', 'asc')