X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/a6633642232efd164d4708967ab59e498fbff896..refs/pull/5280/head:/app/Entities/Models/Bookshelf.php diff --git a/app/Entities/Models/Bookshelf.php b/app/Entities/Models/Bookshelf.php index 8ffd06d2e..9ffa0ea9c 100644 --- a/app/Entities/Models/Bookshelf.php +++ b/app/Entities/Models/Bookshelf.php @@ -1,22 +1,30 @@ -books()->visible(); + return $this->books()->scopes('visible'); } /** @@ -43,29 +51,25 @@ class Bookshelf extends Entity implements HasCoverImage } /** - * 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; } /** - * Get the cover image of the shelf + * Get the cover image of the shelf. */ public function cover(): BelongsTo { @@ -77,13 +81,11 @@ class Bookshelf extends Entity implements HasCoverImage */ 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 { @@ -92,7 +94,6 @@ class Bookshelf extends Entity implements HasCoverImage /** * Add a book to the end of this shelf. - * @param Book $book */ public function appendBook(Book $book) { @@ -103,4 +104,13 @@ class Bookshelf extends Entity implements HasCoverImage $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(); + } }