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