]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/Chapter.php
Applied another round of static analysis updates
[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      * @return HasMany<Page>
27      */
28     public function pages(string $dir = 'ASC'): HasMany
29     {
30         return $this->hasMany(Page::class)->orderBy('priority', $dir);
31     }
32
33     /**
34      * Get the url of this chapter.
35      */
36     public function getUrl(string $path = ''): string
37     {
38         $parts = [
39             'books',
40             urlencode($this->book_slug ?? $this->book->slug),
41             'chapter',
42             urlencode($this->slug),
43             trim($path, '/'),
44         ];
45
46         return url('/' . implode('/', $parts));
47     }
48
49     /**
50      * Get the visible pages in this chapter.
51      */
52     public function getVisiblePages(): Collection
53     {
54         return $this->pages()
55         ->scopes('visible')
56         ->orderBy('draft', 'desc')
57         ->orderBy('priority', 'asc')
58         ->get();
59     }
60 }