1 <?php namespace BookStack;
3 class Book extends Entity
6 protected $fillable = ['name', 'description', 'image_id'];
9 * Get the url for this book.
10 * @param string|bool $path
13 public function getUrl($path = false)
15 if ($path !== false) {
16 return baseUrl('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
18 return baseUrl('/books/' . urlencode($this->slug));
22 * Returns book cover image, if book cover not exists return default cover image.
23 * @param int $width - Width of the image
24 * @param int $height - Height of the image
27 public function getBookCover($width = 440, $height = 250)
29 $default = baseUrl('/book_default_cover.png');
30 if (!$this->image_id) {
35 $cover = $this->cover ? baseUrl($this->cover->getThumb($width, $height, false)) : $default;
36 } catch (\Exception $err) {
43 * Get the cover image of the book
44 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
46 public function cover()
48 return $this->belongsTo(Image::class, 'image_id');
51 * Get the edit url for this book.
54 public function getEditUrl()
56 return $this->getUrl() . '/edit';
60 * Get all pages within this book.
61 * @return \Illuminate\Database\Eloquent\Relations\HasMany
63 public function pages()
65 return $this->hasMany(Page::class);
69 * Get all chapters within this book.
70 * @return \Illuminate\Database\Eloquent\Relations\HasMany
72 public function chapters()
74 return $this->hasMany(Chapter::class);
78 * Get an excerpt of this book's description to the specified length or less.
82 public function getExcerpt($length = 100)
84 $description = $this->description;
85 return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
89 * Return a generalised, common raw query that can be 'unioned' across entities.
92 public function entityRawQuery()
94 return "'BookStack\\\\Book' as entity_type, id, id as entity_id, slug, name, {$this->textField} as text,'' as html, '0' as book_id, '0' as priority, '0' as chapter_id, '0' as draft, created_by, updated_by, updated_at, created_at";