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