]> BookStack Code Mirror - bookstack/blob - app/Entities/Book.php
Fix Book form (create) returning to the full books list on cancel
[bookstack] / app / Entities / Book.php
1 <?php namespace BookStack\Entities;
2
3 use BookStack\Uploads\Image;
4
5 /**
6  * Class Book
7  * @property string $description
8  * @property int $image_id
9  * @property Image|null $cover
10  * @package BookStack\Entities
11  */
12 class Book extends Entity
13 {
14     public $searchFactor = 2;
15
16     protected $fillable = ['name', 'description', 'image_id'];
17
18     /**
19      * Get the morph class for this model.
20      * @return string
21      */
22     public function getMorphClass()
23     {
24         return 'BookStack\\Book';
25     }
26
27     /**
28      * Get the url for this book.
29      * @param string|bool $path
30      * @return string
31      */
32     public function getUrl($path = false)
33     {
34         if ($path !== false) {
35             return url('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
36         }
37         return url('/books/' . urlencode($this->slug));
38     }
39
40     /**
41      * Returns book cover image, if book cover not exists return default cover image.
42      * @param int $width - Width of the image
43      * @param int $height - Height of the image
44      * @return string
45      */
46     public function getBookCover($width = 440, $height = 250)
47     {
48         $default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
49         if (!$this->image_id) {
50             return $default;
51         }
52
53         try {
54             $cover = $this->cover ? url($this->cover->getThumb($width, $height, false)) : $default;
55         } catch (\Exception $err) {
56             $cover = $default;
57         }
58         return $cover;
59     }
60
61     /**
62      * Get the cover image of the book
63      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
64      */
65     public function cover()
66     {
67         return $this->belongsTo(Image::class, 'image_id');
68     }
69
70     /**
71      * Get all pages within this book.
72      * @return \Illuminate\Database\Eloquent\Relations\HasMany
73      */
74     public function pages()
75     {
76         return $this->hasMany(Page::class);
77     }
78
79     /**
80      * Get the direct child pages of this book.
81      * @return \Illuminate\Database\Eloquent\Relations\HasMany
82      */
83     public function directPages()
84     {
85         return $this->pages()->where('chapter_id', '=', '0');
86     }
87
88     /**
89      * Get all chapters within this book.
90      * @return \Illuminate\Database\Eloquent\Relations\HasMany
91      */
92     public function chapters()
93     {
94         return $this->hasMany(Chapter::class);
95     }
96
97     /**
98      * Get the shelves this book is contained within.
99      * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
100      */
101     public function shelves()
102     {
103         return $this->belongsToMany(Bookshelf::class, 'bookshelves_books', 'book_id', 'bookshelf_id');
104     }
105
106     /**
107      * Get an excerpt of this book's description to the specified length or less.
108      * @param int $length
109      * @return string
110      */
111     public function getExcerpt(int $length = 100)
112     {
113         $description = $this->description;
114         return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
115     }
116
117     /**
118      * Return a generalised, common raw query that can be 'unioned' across entities.
119      * @return string
120      */
121     public function entityRawQuery()
122     {
123         return "'BookStack\\\\Book' as entity_type, id, id as entity_id, slug, name, {$this->textField} as text,'' as html, '0' as book_id, '0' as priority, '0' as chapter_id, '0' as draft, created_by, updated_by, updated_at, created_at";
124     }
125 }