1 <?php namespace BookStack;
3 class Book extends Entity
6 protected $fillable = ['name', 'description', 'image'];
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));
21 public function getBookCover()
23 $default = baseUrl('/default.png');
24 $image = $this->image;
25 if ($image === 0 || $image === '0' || $image === null)
28 $cover = $this->cover ? baseUrl($this->cover->getThumb(120, 192, false)) : $default;
29 } catch (\Exception $err) {
35 public function cover()
37 return $this->belongsTo(Image::class, 'image');
40 * Get the edit url for this book.
43 public function getEditUrl()
45 return $this->getUrl() . '/edit';
49 * Get all pages within this book.
50 * @return \Illuminate\Database\Eloquent\Relations\HasMany
52 public function pages()
54 return $this->hasMany(Page::class);
58 * Get all chapters within this book.
59 * @return \Illuminate\Database\Eloquent\Relations\HasMany
61 public function chapters()
63 return $this->hasMany(Chapter::class);
67 * Get an excerpt of this book's description to the specified length or less.
71 public function getExcerpt($length = 100)
73 $description = $this->description;
74 return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
78 * Return a generalised, common raw query that can be 'unioned' across entities.
81 public function entityRawQuery()
83 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";