1 <?php namespace BookStack\Entities;
3 use BookStack\Uploads\Image;
5 class Bookshelf extends Entity
7 protected $table = 'bookshelves';
9 public $searchFactor = 3;
11 protected $fillable = ['name', 'description', 'image_id'];
14 * Get the morph class for this model.
17 public function getMorphClass()
19 return 'BookStack\\Bookshelf';
23 * Get the books in this shelf.
24 * Should not be used directly since does not take into account permissions.
25 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
27 public function books()
29 return $this->belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id')
31 ->orderBy('order', 'asc');
35 * Get the url for this bookshelf.
36 * @param string|bool $path
39 public function getUrl($path = false)
41 if ($path !== false) {
42 return baseUrl('/shelves/' . urlencode($this->slug) . '/' . trim($path, '/'));
44 return baseUrl('/shelves/' . urlencode($this->slug));
48 * Returns BookShelf cover image, if cover does not exists return default cover image.
49 * @param int $width - Width of the image
50 * @param int $height - Height of the image
53 public function getBookCover($width = 440, $height = 250)
55 // TODO - Make generic, focused on books right now, Perhaps set-up a better image
56 $default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
57 if (!$this->image_id) {
62 $cover = $this->cover ? baseUrl($this->cover->getThumb($width, $height, false)) : $default;
63 } catch (\Exception $err) {
70 * Get the cover image of the shelf
71 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
73 public function cover()
75 return $this->belongsTo(Image::class, 'image_id');
79 * Get an excerpt of this book's description to the specified length or less.
83 public function getExcerpt(int $length = 100)
85 $description = $this->description;
86 return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
90 * Return a generalised, common raw query that can be 'unioned' across entities.
93 public function entityRawQuery()
95 return "'BookStack\\\\BookShelf' 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";
99 * Check if this shelf contains the given book.
103 public function contains(Book $book)
105 return $this->books()->where('id', '=', $book->id)->count() > 0;