3 namespace BookStack\Entities\Models;
5 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;
10 use Illuminate\Database\Eloquent\Relations\HasMany;
11 use Illuminate\Support\Collection;
16 * @property string $description
17 * @property int $image_id
18 * @property Image|null $cover
19 * @property \Illuminate\Database\Eloquent\Collection $chapters
20 * @property \Illuminate\Database\Eloquent\Collection $pages
21 * @property \Illuminate\Database\Eloquent\Collection $directPages
22 * @property \Illuminate\Database\Eloquent\Collection $shelves
24 class Book extends Entity implements HasCoverImage
28 public $searchFactor = 1.2;
30 protected $fillable = ['name', 'description', 'default_template'];
31 protected $hidden = ['pivot', 'image_id', 'deleted_at'];
34 * Get the url for this book.
36 public function getUrl(string $path = ''): string
38 return url('/books/' . implode('/', [urlencode($this->slug), trim($path, '/')]));
42 * Returns book cover image, if book cover not exists return default cover image.
44 public function getBookCover(int $width = 440, int $height = 250): string
46 $default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
47 if (!$this->image_id || !$this->cover) {
52 return $this->cover->getThumb($width, $height, false) ?? $default;
53 } catch (Exception $err) {
59 * Get the cover image of the book.
61 public function cover(): BelongsTo
63 return $this->belongsTo(Image::class, 'image_id');
67 * Get the type of the image model that is used when storing a cover image.
69 public function coverImageTypeKey(): string
75 * Get the Page that is used as default template for newly created pages within this Book.
77 public function defaultTemplate(): BelongsTo
79 return $this->belongsTo(Page::class, 'default_template');
83 * Get all pages within this book.
85 public function pages(): HasMany
87 return $this->hasMany(Page::class);
91 * Get the direct child pages of this book.
93 public function directPages(): HasMany
95 return $this->pages()->where('chapter_id', '=', '0');
99 * Get all chapters within this book.
101 public function chapters(): HasMany
103 return $this->hasMany(Chapter::class);
107 * Get the shelves this book is contained within.
109 public function shelves(): BelongsToMany
111 return $this->belongsToMany(Bookshelf::class, 'bookshelves_books', 'book_id', 'bookshelf_id');
115 * Get the direct child items within this book.
117 public function getDirectChildren(): Collection
119 $pages = $this->directPages()->scopes('visible')->get();
120 $chapters = $this->chapters()->scopes('visible')->get();
122 return $pages->concat($chapters)->sortBy('priority')->sortByDesc('draft');
126 * Get a visible book by its slug.
127 * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
129 public static function getBySlug(string $slug): self
131 return static::visible()->where('slug', '=', $slug)->firstOrFail();