]> BookStack Code Mirror - bookstack/blob - app/Entities/Chapter.php
Update entities.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         if ($path !== false) {
46             return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug) . '/' . trim($path, '/'));
47         }
48         return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug));
49     }
50
51     /**
52      * Get an excerpt of this chapter's description to the specified length or less.
53      * @param int $length
54      * @return string
55      */
56     public function getExcerpt(int $length = 100)
57     {
58         $description = $this->text ?? $this->description;
59         return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
60     }
61
62     /**
63      * Return a generalised, common raw query that can be 'unioned' across entities.
64      * @return string
65      */
66     public function entityRawQuery()
67     {
68         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";
69     }
70
71     /**
72      * Check if this chapter has any child pages.
73      * @return bool
74      */
75     public function hasChildren()
76     {
77         return count($this->pages) > 0;
78     }
79 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.