]> BookStack Code Mirror - bookstack/blob - app/Entities/Chapter.php
Update passwords.php
[bookstack] / app / Entities / Chapter.php
1 <?php namespace BookStack\Entities;
2
3 class Chapter extends Entity
4 {
5     public $searchFactor = 1.3;
6
7     protected $fillable = ['name', 'description', 'priority', 'book_id'];
8
9     /**
10      * Get the morph class for this model.
11      * @return string
12      */
13     public function getMorphClass()
14     {
15         return 'BookStack\\Chapter';
16     }
17
18     /**
19      * Get the book this chapter is within.
20      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
21      */
22     public function book()
23     {
24         return $this->belongsTo(Book::class);
25     }
26
27     /**
28      * Get the pages that this chapter contains.
29      * @param string $dir
30      * @return mixed
31      */
32     public function pages($dir = 'ASC')
33     {
34         return $this->hasMany(Page::class)->orderBy('priority', $dir);
35     }
36
37     /**
38      * Get the url of this chapter.
39      * @param string|bool $path
40      * @return string
41      */
42     public function getUrl($path = false)
43     {
44         $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
45         $fullPath = '/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug);
46
47         if ($path !== false) {
48             $fullPath .= '/' . trim($path, '/');
49         }
50
51         return url($fullPath);
52     }
53
54     /**
55      * Get an excerpt of this chapter's description to the specified length or less.
56      * @param int $length
57      * @return string
58      */
59     public function getExcerpt(int $length = 100)
60     {
61         $description = $this->text ?? $this->description;
62         return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
63     }
64
65     /**
66      * Return a generalised, common raw query that can be 'unioned' across entities.
67      * @return string
68      */
69     public function entityRawQuery()
70     {
71         return "'BookStack\\\\Chapter' as entity_type, id, id as entity_id, slug, name, {$this->textField} as text, '' as html, book_id, priority, '0' as chapter_id, '0' as draft, created_by, updated_by, updated_at, created_at";
72     }
73
74     /**
75      * Check if this chapter has any child pages.
76      * @return bool
77      */
78     public function hasChildren()
79     {
80         return count($this->pages) > 0;
81     }
82 }