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