]> BookStack Code Mirror - bookstack/blob - app/Entities/Chapter.php
added missing comma that caused the testprocess to fail.
[bookstack] / app / Entities / Chapter.php
1 <?php namespace BookStack\Entities;
2
3 use Illuminate\Support\Collection;
4
5 /**
6  * Class Chapter
7  * @property Collection<Page> $pages
8  * @package BookStack\Entities
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'];
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      * @param string|bool $path
30      * @return string
31      */
32     public function getUrl($path = false)
33     {
34         $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
35         $fullPath = '/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug);
36
37         if ($path !== false) {
38             $fullPath .= '/' . trim($path, '/');
39         }
40
41         return url($fullPath);
42     }
43
44     /**
45      * Get an excerpt of this chapter's description to the specified length or less.
46      * @param int $length
47      * @return string
48      */
49     public function getExcerpt(int $length = 100)
50     {
51         $description = $this->text ?? $this->description;
52         return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
53     }
54
55     /**
56      * Check if this chapter has any child pages.
57      * @return bool
58      */
59     public function hasChildren()
60     {
61         return count($this->pages) > 0;
62     }
63
64     /**
65      * Get the visible pages in this chapter.
66      */
67     public function getVisiblePages(): Collection
68     {
69         return $this->pages()->visible()
70         ->orderBy('draft', 'desc')
71         ->orderBy('priority', 'asc')
72         ->get();
73     }
74 }