3 namespace BookStack\Entities\Models;
5 use BookStack\Entities\Tools\EntityDefaultTemplate;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Relations\HasMany;
8 use Illuminate\Support\Collection;
11 * @property Collection<Page> $pages
12 * @property ?int $default_template_id
13 * @property string $description
14 * @property string $description_html
16 class Chapter extends BookChild implements HasDescriptionInterface, HasDefaultTemplateInterface
21 public float $searchFactor = 1.2;
22 protected $hidden = ['pivot', 'deleted_at', 'description_html', 'sort_rule_id', 'image_id', 'entity_id', 'entity_type', 'chapter_id'];
23 protected $fillable = ['name', 'priority'];
26 * Get the pages that this chapter contains.
28 * @return HasMany<Page, $this>
30 public function pages(string $dir = 'ASC'): HasMany
32 return $this->hasMany(Page::class)->orderBy('priority', $dir);
36 * Get the url of this chapter.
38 public function getUrl(string $path = ''): string
42 urlencode($this->book_slug ?? $this->book->slug),
44 urlencode($this->slug),
48 return url('/' . implode('/', $parts));
52 * Get the visible pages in this chapter.
53 * @return Collection<Page>
55 public function getVisiblePages(): Collection
59 ->orderBy('draft', 'desc')
60 ->orderBy('priority', 'asc')
64 public function defaultTemplate(): EntityDefaultTemplate
66 return new EntityDefaultTemplate($this);