]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/Chapter.php
Code cleanup, bug squashing
[bookstack] / app / Entities / Models / Chapter.php
1 <?php namespace BookStack\Entities\Models;
2
3 use Illuminate\Support\Collection;
4
5 /**
6  * Class Chapter
7  * @property Collection<Page> $pages
8  */
9 class Chapter extends BookChild
10 {
11     public $searchFactor = 1.3;
12
13     protected $fillable = ['name', 'description', 'priority', 'book_id'];
14     protected $hidden = ['restricted', 'pivot', 'deleted_at'];
15
16     /**
17      * Get the pages that this chapter contains.
18      * @param string $dir
19      * @return mixed
20      */
21     public function pages($dir = 'ASC')
22     {
23         return $this->hasMany(Page::class)->orderBy('priority', $dir);
24     }
25
26     /**
27      * Get the url of this chapter.
28      */
29     public function getUrl($path = ''): string
30     {
31         $parts = [
32             'books',
33             urlencode($this->getAttribute('bookSlug') ?? $this->book->slug),
34             'chapter',
35             urlencode($this->slug),
36             trim($path, '/'),
37         ];
38
39         return url('/' . implode('/', $parts));
40     }
41
42     /**
43      * Check if this chapter has any child pages.
44      * @return bool
45      */
46     public function hasChildren()
47     {
48         return count($this->pages) > 0;
49     }
50
51     /**
52      * Get the visible pages in this chapter.
53      */
54     public function getVisiblePages(): Collection
55     {
56         return $this->pages()->visible()
57         ->orderBy('draft', 'desc')
58         ->orderBy('priority', 'asc')
59         ->get();
60     }
61 }