]> BookStack Code Mirror - bookstack/blob - app/Entities/Chapter.php
Fixed 'interaction_required' response for azure
[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
16     /**
17      * Get the pages that this chapter contains.
18      * @param string $dir
19      * @return mixed
20      */
21     public function pages($dir = 'ASC')
22     {
23         return $this->hasMany(Page::class)->orderBy('priority', $dir);
24     }
25
26     /**
27      * Get the url of this chapter.
28      * @param string|bool $path
29      * @return string
30      */
31     public function getUrl($path = false)
32     {
33         $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
34         $fullPath = '/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug);
35
36         if ($path !== false) {
37             $fullPath .= '/' . trim($path, '/');
38         }
39
40         return url($fullPath);
41     }
42
43     /**
44      * Get an excerpt of this chapter's description to the specified length or less.
45      * @param int $length
46      * @return string
47      */
48     public function getExcerpt(int $length = 100)
49     {
50         $description = $this->text ?? $this->description;
51         return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
52     }
53
54     /**
55      * Check if this chapter has any child pages.
56      * @return bool
57      */
58     public function hasChildren()
59     {
60         return count($this->pages) > 0;
61     }
62
63     /**
64      * Get the visible pages in this chapter.
65      */
66     public function getVisiblePages(): Collection
67     {
68         return $this->pages()->visible()
69         ->orderBy('draft', 'desc')
70         ->orderBy('priority', 'asc')
71         ->get();
72     }
73 }