1 <?php namespace BookStack;
4 class Bookshelf extends Entity
6 protected $table = 'bookshelves';
8 public $searchFactor = 3;
10 protected $fillable = ['name', 'description', 'image_id'];
13 * Get the books in this shelf.
14 * Should not be used directly since does not take into account permissions.
15 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
17 public function books()
19 return $this->belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id')->orderBy('order', 'asc');
23 * Get the url for this bookshelf.
24 * @param string|bool $path
27 public function getUrl($path = false)
29 if ($path !== false) {
30 return baseUrl('/shelves/' . urlencode($this->slug) . '/' . trim($path, '/'));
32 return baseUrl('/shelves/' . urlencode($this->slug));
36 * Returns BookShelf cover image, if cover does not exists return default cover image.
37 * @param int $width - Width of the image
38 * @param int $height - Height of the image
41 public function getBookCover($width = 440, $height = 250)
43 $default = baseUrl('/book_default_cover.png');
44 if (!$this->image_id) {
49 $cover = $this->cover ? baseUrl($this->cover->getThumb($width, $height, false)) : $default;
50 } catch (\Exception $err) {
57 * Get the cover image of the book
58 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
60 public function cover()
62 return $this->belongsTo(Image::class, 'image_id');
66 * Get an excerpt of this book's description to the specified length or less.
70 public function getExcerpt($length = 100)
72 $description = $this->description;
73 return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
77 * Return a generalised, common raw query that can be 'unioned' across entities.
80 public function entityRawQuery()
82 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";