1 <?php namespace BookStack\Entities;
3 use BookStack\Uploads\Image;
5 class Book extends Entity
7 public $searchFactor = 2;
9 protected $fillable = ['name', 'description', 'image_id'];
12 * Get the morph class for this model.
15 public function getMorphClass()
17 return 'BookStack\\Book';
21 * Get the url for this book.
22 * @param string|bool $path
25 public function getUrl($path = false)
27 if ($path !== false) {
28 return baseUrl('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
30 return baseUrl('/books/' . urlencode($this->slug));
34 * Returns book cover image, if book cover not exists return default cover image.
35 * @param int $width - Width of the image
36 * @param int $height - Height of the image
39 public function getBookCover($width = 440, $height = 250)
41 $default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
42 if (!$this->image_id) {
47 $cover = $this->cover ? baseUrl($this->cover->getThumb($width, $height, false)) : $default;
48 } catch (\Exception $err) {
55 * Get the cover image of the book
56 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
58 public function cover()
60 return $this->belongsTo(Image::class, 'image_id');
64 * Get all pages within this book.
65 * @return \Illuminate\Database\Eloquent\Relations\HasMany
67 public function pages()
69 return $this->hasMany(Page::class);
73 * Get the direct child pages of this book.
74 * @return \Illuminate\Database\Eloquent\Relations\HasMany
76 public function directPages()
78 return $this->pages()->where('chapter_id', '=', '0');
82 * Get all chapters within this book.
83 * @return \Illuminate\Database\Eloquent\Relations\HasMany
85 public function chapters()
87 return $this->hasMany(Chapter::class);
91 * Get the shelves this book is contained within.
92 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
94 public function shelves()
96 return $this->belongsToMany(Bookshelf::class, 'bookshelves_books', 'book_id', 'bookshelf_id');
100 * Get an excerpt of this book's description to the specified length or less.
104 public function getExcerpt(int $length = 100)
106 $description = $this->description;
107 return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
111 * Return a generalised, common raw query that can be 'unioned' across entities.
114 public function entityRawQuery()
116 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";