namespace BookStack\Entities\Models;
use BookStack\Uploads\Image;
+use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Bookshelf extends Entity implements HasCoverImage
{
use HasFactory;
+ use HasHtmlDescription;
protected $table = 'bookshelves';
- public $searchFactor = 1.2;
+ public float $searchFactor = 1.2;
protected $fillable = ['name', 'description', 'image_id'];
- protected $hidden = ['restricted', 'image_id', 'deleted_at'];
+ protected $hidden = ['image_id', 'deleted_at', 'description_html'];
/**
* Get the books in this shelf.
*/
public function visibleBooks(): BelongsToMany
{
- return $this->books()->visible();
+ return $this->books()->scopes('visible');
}
/**
}
/**
- * Returns BookShelf cover image, if cover does not exists return default cover image.
- *
- * @param int $width - Width of the image
- * @param int $height - Height of the image
- *
- * @return string
+ * Returns shelf cover image, if cover not exists return default cover image.
*/
- public function getBookCover($width = 440, $height = 250)
+ public function getBookCover(int $width = 440, int $height = 250): string
{
// TODO - Make generic, focused on books right now, Perhaps set-up a better image
$default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
- if (!$this->image_id) {
+ if (!$this->image_id || !$this->cover) {
return $default;
}
try {
- $cover = $this->cover ? url($this->cover->getThumb($width, $height, false)) : $default;
- } catch (\Exception $err) {
- $cover = $default;
+ return $this->cover->getThumb($width, $height, false) ?? $default;
+ } catch (Exception $err) {
+ return $default;
}
-
- return $cover;
}
/**
*/
public function coverImageTypeKey(): string
{
- return 'cover_shelf';
+ return 'cover_bookshelf';
}
/**
* Check if this shelf contains the given book.
- *
- * @param Book $book
- *
- * @return bool
*/
public function contains(Book $book): bool
{
/**
* Add a book to the end of this shelf.
- *
- * @param Book $book
*/
public function appendBook(Book $book)
{
$maxOrder = $this->books()->max('order');
$this->books()->attach($book->id, ['order' => $maxOrder + 1]);
}
+
+ /**
+ * Get a visible shelf by its slug.
+ * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
+ */
+ public static function getBySlug(string $slug): self
+ {
+ return static::visible()->where('slug', '=', $slug)->firstOrFail();
+ }
}