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 ?int $default_template
19 * @property Image|null $cover
20 * @property \Illuminate\Database\Eloquent\Collection $chapters
21 * @property \Illuminate\Database\Eloquent\Collection $pages
22 * @property \Illuminate\Database\Eloquent\Collection $directPages
23 * @property \Illuminate\Database\Eloquent\Collection $shelves
24 * @property ?Page $defaultTemplate
26 class Book extends Entity implements HasCoverImage
30 public $searchFactor = 1.2;
32 protected $fillable = ['name', 'description'];
33 protected $hidden = ['pivot', 'image_id', 'deleted_at'];
36 * Get the url for this book.
38 public function getUrl(string $path = ''): string
40 return url('/books/' . implode('/', [urlencode($this->slug), trim($path, '/')]));
44 * Returns book cover image, if book cover not exists return default cover image.
46 public function getBookCover(int $width = 440, int $height = 250): string
48 $default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
49 if (!$this->image_id || !$this->cover) {
54 return $this->cover->getThumb($width, $height, false) ?? $default;
55 } catch (Exception $err) {
61 * Get the cover image of the book.
63 public function cover(): BelongsTo
65 return $this->belongsTo(Image::class, 'image_id');
69 * Get the type of the image model that is used when storing a cover image.
71 public function coverImageTypeKey(): string
77 * Get the Page that is used as default template for newly created pages within this Book.
79 public function defaultTemplate(): BelongsTo
81 return $this->belongsTo(Page::class, 'default_template');
85 * Get all pages within this book.
87 public function pages(): HasMany
89 return $this->hasMany(Page::class);
93 * Get the direct child pages of this book.
95 public function directPages(): HasMany
97 return $this->pages()->where('chapter_id', '=', '0');
101 * Get all chapters within this book.
103 public function chapters(): HasMany
105 return $this->hasMany(Chapter::class);
109 * Get the shelves this book is contained within.
111 public function shelves(): BelongsToMany
113 return $this->belongsToMany(Bookshelf::class, 'bookshelves_books', 'book_id', 'bookshelf_id');
117 * Get the direct child items within this book.
119 public function getDirectChildren(): Collection
121 $pages = $this->directPages()->scopes('visible')->get();
122 $chapters = $this->chapters()->scopes('visible')->get();
124 return $pages->concat($chapters)->sortBy('priority')->sortByDesc('draft');
128 * Get a visible book by its slug.
129 * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
131 public static function getBySlug(string $slug): self
133 return static::visible()->where('slug', '=', $slug)->firstOrFail();