3 namespace BookStack\Entities\Models;
5 use BookStack\Entities\Tools\EntityCover;
6 use BookStack\Uploads\Image;
7 use Illuminate\Database\Eloquent\Factories\HasFactory;
8 use Illuminate\Database\Eloquent\Relations\BelongsTo;
9 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
12 * @property string $description
13 * @property string $description_html
15 class Bookshelf extends Entity implements HasDescriptionInterface, HasCoverInterface
20 public float $searchFactor = 1.2;
22 protected $hidden = ['image_id', 'deleted_at', 'description_html', 'priority', 'default_template_id', 'sort_rule_id', 'entity_id', 'entity_type', 'chapter_id', 'book_id'];
23 protected $fillable = ['name'];
26 * Get the books in this shelf.
27 * Should not be used directly since it does not take into account permissions.
29 public function books(): BelongsToMany
31 return $this->belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id')
32 ->select(['entities.*', 'entity_container_data.*'])
34 ->orderBy('order', 'asc');
38 * Related books that are visible to the current user.
40 public function visibleBooks(): BelongsToMany
42 return $this->books()->scopes('visible');
46 * Get the url for this bookshelf.
48 public function getUrl(string $path = ''): string
50 return url('/shelves/' . implode('/', [urlencode($this->slug), trim($path, '/')]));
54 * Check if this shelf contains the given book.
56 public function contains(Book $book): bool
58 return $this->books()->where('id', '=', $book->id)->count() > 0;
62 * Add a book to the end of this shelf.
64 public function appendBook(Book $book): void
66 if ($this->contains($book)) {
70 $maxOrder = $this->books()->max('order');
71 $this->books()->attach($book->id, ['order' => $maxOrder + 1]);
74 public function coverInfo(): EntityCover
76 return new EntityCover($this);
79 public function cover(): BelongsTo
81 return $this->belongsTo(Image::class, 'image_id');