]> BookStack Code Mirror - bookstack/blob - app/Chapter.php
Applied baseUrl to login redirect
[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      * @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         if ($path !== false) {
35             return baseUrl('/books/' . $bookSlug. '/chapter/' . $this->slug . '/' . trim($path, '/'));
36         }
37         return baseUrl('/books/' . $bookSlug. '/chapter/' . $this->slug);
38     }
39
40     /**
41      * Get an excerpt of this chapter's description to the specified length or less.
42      * @param int $length
43      * @return string
44      */
45     public function getExcerpt($length = 100)
46     {
47         $description = $this->description;
48         return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
49     }
50
51 }