]> BookStack Code Mirror - bookstack/blob - app/Chapter.php
replace GPL diff lib with MIT lib
[bookstack] / app / Chapter.php
1 <?php namespace BookStack;
2
3
4 class Chapter extends Entity
5 {
6     protected $fillable = ['name', 'description', 'priority', 'book_id'];
7
8     /**
9      * Get the book this chapter is within.
10      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
11      */
12     public function book()
13     {
14         return $this->belongsTo(Book::class);
15     }
16
17     /**
18      * Get the pages that this chapter contains.
19      * @return mixed
20      */
21     public function pages()
22     {
23         return $this->hasMany(Page::class)->orderBy('priority', 'ASC');
24     }
25
26     /**
27      * Get the url of this chapter.
28      * @return string
29      */
30     public function getUrl()
31     {
32         $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
33         return '/books/' . $bookSlug. '/chapter/' . $this->slug;
34     }
35
36     /**
37      * Get an excerpt of this chapter's description to the specified length or less.
38      * @param int $length
39      * @return string
40      */
41     public function getExcerpt($length = 100)
42     {
43         $description = $this->description;
44         return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
45     }
46
47 }