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
23 * @property ?Page $defaultTemplate
25 class Book extends Entity implements HasCoverImage
29 public $searchFactor = 1.2;
31 protected $fillable = ['name', 'description'];
32 protected $hidden = ['pivot', 'image_id', 'deleted_at'];
35 * Get the url for this book.
37 public function getUrl(string $path = ''): string
39 return url('/books/' . implode('/', [urlencode($this->slug), trim($path, '/')]));
43 * Returns book cover image, if book cover not exists return default cover image.
45 public function getBookCover(int $width = 440, int $height = 250): string
47 $default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
48 if (!$this->image_id || !$this->cover) {
53 return $this->cover->getThumb($width, $height, false) ?? $default;
54 } catch (Exception $err) {
60 * Get the cover image of the book.
62 public function cover(): BelongsTo
64 return $this->belongsTo(Image::class, 'image_id');
68 * Get the type of the image model that is used when storing a cover image.
70 public function coverImageTypeKey(): string
76 * Get the Page that is used as default template for newly created pages within this Book.
78 public function defaultTemplate(): BelongsTo
80 return $this->belongsTo(Page::class, 'default_template');
84 * Get all pages within this book.
86 public function pages(): HasMany
88 return $this->hasMany(Page::class);
92 * Get the direct child pages of this book.
94 public function directPages(): HasMany
96 return $this->pages()->where('chapter_id', '=', '0');
100 * Get all chapters within this book.
102 public function chapters(): HasMany
104 return $this->hasMany(Chapter::class);
108 * Get the shelves this book is contained within.
110 public function shelves(): BelongsToMany
112 return $this->belongsToMany(Bookshelf::class, 'bookshelves_books', 'book_id', 'bookshelf_id');
116 * Get the direct child items within this book.
118 public function getDirectChildren(): Collection
120 $pages = $this->directPages()->scopes('visible')->get();
121 $chapters = $this->chapters()->scopes('visible')->get();
123 return $pages->concat($chapters)->sortBy('priority')->sortByDesc('draft');
127 * Get a visible book by its slug.
128 * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
130 public static function getBySlug(string $slug): self
132 return static::visible()->where('slug', '=', $slug)->firstOrFail();