3 namespace BookStack\Entities\Models;
5 use BookStack\Sorting\SortRule;
6 use BookStack\Uploads\Image;
8 use Illuminate\Database\Eloquent\Factories\HasFactory;
9 use Illuminate\Database\Eloquent\Relations\BelongsTo;
10 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
11 use Illuminate\Database\Eloquent\Relations\HasMany;
12 use Illuminate\Support\Collection;
17 * @property string $description
18 * @property int $image_id
19 * @property ?int $default_template_id
20 * @property ?int $sort_rule_id
21 * @property Image|null $cover
22 * @property \Illuminate\Database\Eloquent\Collection $chapters
23 * @property \Illuminate\Database\Eloquent\Collection $pages
24 * @property \Illuminate\Database\Eloquent\Collection $directPages
25 * @property \Illuminate\Database\Eloquent\Collection $shelves
26 * @property ?Page $defaultTemplate
27 * @property ?SortRule $sortRule
29 class Book extends Entity implements HasCoverImage
32 use HasHtmlDescription;
34 public float $searchFactor = 1.2;
36 protected $fillable = ['name'];
37 protected $hidden = ['pivot', 'image_id', 'deleted_at', 'description_html'];
40 * Get the url for this book.
42 public function getUrl(string $path = ''): string
44 return url('/books/' . implode('/', [urlencode($this->slug), trim($path, '/')]));
48 * Returns book cover image, if book cover not exists return default cover image.
50 public function getBookCover(int $width = 440, int $height = 250): string
52 $default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
53 if (!$this->image_id || !$this->cover) {
58 return $this->cover->getThumb($width, $height, false) ?? $default;
59 } catch (Exception $err) {
65 * Get the cover image of the book.
67 public function cover(): BelongsTo
69 return $this->belongsTo(Image::class, 'image_id');
73 * Get the type of the image model that is used when storing a cover image.
75 public function coverImageTypeKey(): string
81 * Get the Page that is used as default template for newly created pages within this Book.
83 public function defaultTemplate(): BelongsTo
85 return $this->belongsTo(Page::class, 'default_template_id');
89 * Get the sort set assigned to this book, if existing.
91 public function sortRule(): BelongsTo
93 return $this->belongsTo(SortRule::class);
97 * Get all pages within this book.
99 public function pages(): HasMany
101 return $this->hasMany(Page::class);
105 * Get the direct child pages of this book.
107 public function directPages(): HasMany
109 return $this->pages()->where('chapter_id', '=', '0');
113 * Get all chapters within this book.
115 public function chapters(): HasMany
117 return $this->hasMany(Chapter::class);
121 * Get the shelves this book is contained within.
123 public function shelves(): BelongsToMany
125 return $this->belongsToMany(Bookshelf::class, 'bookshelves_books', 'book_id', 'bookshelf_id');
129 * Get the direct child items within this book.
131 public function getDirectVisibleChildren(): Collection
133 $pages = $this->directPages()->scopes('visible')->get();
134 $chapters = $this->chapters()->scopes('visible')->get();
136 return $pages->concat($chapters)->sortBy('priority')->sortByDesc('draft');