]> BookStack Code Mirror - bookstack/blob - app/Chapter.php
Fixes a corner case with exclamation in the ID.
[bookstack] / app / Chapter.php
1 <?php namespace BookStack;
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 book this chapter is within.
11      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
12      */
13     public function book()
14     {
15         return $this->belongsTo(Book::class);
16     }
17
18     /**
19      * Get the pages that this chapter contains.
20      * @param string $dir
21      * @return mixed
22      */
23     public function pages($dir = 'ASC')
24     {
25         return $this->hasMany(Page::class)->orderBy('priority', $dir);
26     }
27
28     /**
29      * Get the url of this chapter.
30      * @param string|bool $path
31      * @return string
32      */
33     public function getUrl($path = false)
34     {
35         $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
36         if ($path !== false) {
37             return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug) . '/' . trim($path, '/'));
38         }
39         return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug));
40     }
41
42     /**
43      * Get an excerpt of this chapter's description to the specified length or less.
44      * @param int $length
45      * @return string
46      */
47     public function getExcerpt($length = 100)
48     {
49         $description = $this->description;
50         return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
51     }
52
53     /**
54      * Return a generalised, common raw query that can be 'unioned' across entities.
55      * @return string
56      */
57     public function entityRawQuery()
58     {
59         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";
60     }
61 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.