]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/Chapter.php
Laravel 8 shift squash & merge (#3029)
[bookstack] / app / Entities / Models / Chapter.php
1 <?php
2
3 namespace BookStack\Entities\Models;
4
5 use Illuminate\Database\Eloquent\Factories\HasFactory;
6 use Illuminate\Support\Collection;
7
8 /**
9  * Class Chapter.
10  *
11  * @property Collection<Page> $pages
12  * @property mixed description
13  */
14 class Chapter extends BookChild
15 {
16     use HasFactory;
17
18     public $searchFactor = 1.3;
19
20     protected $fillable = ['name', 'description', 'priority', 'book_id'];
21     protected $hidden = ['restricted', 'pivot', 'deleted_at'];
22
23     /**
24      * Get the pages that this chapter contains.
25      *
26      * @param string $dir
27      *
28      * @return mixed
29      */
30     public function pages($dir = 'ASC')
31     {
32         return $this->hasMany(Page::class)->orderBy('priority', $dir);
33     }
34
35     /**
36      * Get the url of this chapter.
37      */
38     public function getUrl($path = ''): string
39     {
40         $parts = [
41             'books',
42             urlencode($this->book_slug ?? $this->book->slug),
43             'chapter',
44             urlencode($this->slug),
45             trim($path, '/'),
46         ];
47
48         return url('/' . implode('/', $parts));
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 }