]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/Chapter.php
Added test for logical-theme-system command registration
[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\Database\Eloquent\Relations\HasMany;
7 use Illuminate\Support\Collection;
8
9 /**
10  * Class Chapter.
11  *
12  * @property Collection<Page> $pages
13  * @property string           $description
14  */
15 class Chapter extends BookChild
16 {
17     use HasFactory;
18
19     public $searchFactor = 1.2;
20
21     protected $fillable = ['name', 'description', 'priority', 'book_id'];
22     protected $hidden = ['restricted', 'pivot', 'deleted_at'];
23
24     /**
25      * Get the pages that this chapter contains.
26      */
27     public function pages(string $dir = 'ASC'): HasMany
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(string $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 }