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