]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/Chapter.php
Merge pull request #5668 from bumperbox/patch-1
[bookstack] / app / Entities / Models / Chapter.php
1 <?php
2
3 namespace BookStack\Entities\Models;
4
5 use Illuminate\Database\Eloquent\Relations\BelongsTo;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Relations\HasMany;
8 use Illuminate\Support\Collection;
9
10 /**
11  * Class Chapter.
12  *
13  * @property Collection<Page> $pages
14  * @property ?int             $default_template_id
15  * @property ?Page            $defaultTemplate
16  */
17 class Chapter extends BookChild
18 {
19     use HasFactory;
20     use HasHtmlDescription;
21
22     public float $searchFactor = 1.2;
23
24     protected $fillable = ['name', 'description', 'priority'];
25     protected $hidden = ['pivot', 'deleted_at', 'description_html'];
26
27     /**
28      * Get the pages that this chapter contains.
29      *
30      * @return HasMany<Page>
31      */
32     public function pages(string $dir = 'ASC'): HasMany
33     {
34         return $this->hasMany(Page::class)->orderBy('priority', $dir);
35     }
36
37     /**
38      * Get the url of this chapter.
39      */
40     public function getUrl(string $path = ''): string
41     {
42         $parts = [
43             'books',
44             urlencode($this->book_slug ?? $this->book->slug),
45             'chapter',
46             urlencode($this->slug),
47             trim($path, '/'),
48         ];
49
50         return url('/' . implode('/', $parts));
51     }
52
53     /**
54      * Get the Page that is used as default template for newly created pages within this Chapter.
55      */
56     public function defaultTemplate(): BelongsTo
57     {
58         return $this->belongsTo(Page::class, 'default_template_id');
59     }
60
61     /**
62      * Get the visible pages in this chapter.
63      * @returns Collection<Page>
64      */
65     public function getVisiblePages(): Collection
66     {
67         return $this->pages()
68         ->scopes('visible')
69         ->orderBy('draft', 'desc')
70         ->orderBy('priority', 'asc')
71         ->get();
72     }
73 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.