1 <?php namespace BookStack\Entities;
3 use BookStack\Uploads\Image;
7 * @property string $description
8 * @property int $image_id
9 * @property Image|null $cover
10 * @package BookStack\Entities
12 class Book extends Entity
14 public $searchFactor = 2;
16 protected $fillable = ['name', 'description', 'image_id'];
19 * Get the morph class for this model.
22 public function getMorphClass()
24 return 'BookStack\\Book';
28 * Get the url for this book.
29 * @param string|bool $path
32 public function getUrl($path = false)
34 if ($path !== false) {
35 return url('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
37 return url('/books/' . urlencode($this->slug));
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
46 public function getBookCover($width = 440, $height = 250)
48 $default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
49 if (!$this->image_id) {
54 $cover = $this->cover ? url($this->cover->getThumb($width, $height, false)) : $default;
55 } catch (\Exception $err) {
62 * Get the cover image of the book
63 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
65 public function cover()
67 return $this->belongsTo(Image::class, 'image_id');
71 * Get all pages within this book.
72 * @return \Illuminate\Database\Eloquent\Relations\HasMany
74 public function pages()
76 return $this->hasMany(Page::class);
80 * Get the direct child pages of this book.
81 * @return \Illuminate\Database\Eloquent\Relations\HasMany
83 public function directPages()
85 return $this->pages()->where('chapter_id', '=', '0');
89 * Get all chapters within this book.
90 * @return \Illuminate\Database\Eloquent\Relations\HasMany
92 public function chapters()
94 return $this->hasMany(Chapter::class);
98 * Get the shelves this book is contained within.
99 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
101 public function shelves()
103 return $this->belongsToMany(Bookshelf::class, 'bookshelves_books', 'book_id', 'bookshelf_id');
107 * Get an excerpt of this book's description to the specified length or less.
111 public function getExcerpt(int $length = 100)
113 $description = $this->description;
114 return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
118 * Return a generalised, common raw query that can be 'unioned' across entities.
121 public function entityRawQuery()
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";