]> BookStack Code Mirror - bookstack/blob - app/Entities/Book.php
b8a29322d6b58f52bc142104312e79209a935f0b
[bookstack] / app / Entities / Book.php
1 <?php namespace BookStack\Entities;
2
3 use BookStack\Uploads\Image;
4 use BookStack\Entities\Page;
5
6 class Book extends Entity
7 {
8     public $searchFactor = 2;
9
10     protected $fillable = ['name', 'description', 'image_id'];
11
12     /**
13      * Get the morph class for this model.
14      * @return string
15      */
16     public function getMorphClass()
17     {
18         return 'BookStack\\Book';
19     }
20
21     /**
22      * Get the url for this book.
23      * @param string|bool $path
24      * @return string
25      */
26     public function getUrl($path = false)
27     {
28         if ($path !== false) {
29             return baseUrl('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
30         }
31         return baseUrl('/books/' . urlencode($this->slug));
32     }
33
34     /**
35      * Returns book cover image, if book cover not exists return default cover image.
36      * @param int $width - Width of the image
37      * @param int $height - Height of the image
38      * @return string
39      */
40     public function getBookCover($width = 440, $height = 250)
41     {
42         $default = baseUrl('/book_default_cover.png');
43         if (!$this->image_id) {
44             return $default;
45         }
46
47         try {
48             $cover = $this->cover ? baseUrl($this->cover->getThumb($width, $height, false)) : $default;
49         } catch (\Exception $err) {
50             $cover = $default;
51         }
52         return $cover;
53     }
54
55     /**
56      * Get the cover image of the book
57      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
58      */
59     public function cover()
60     {
61         return $this->belongsTo(Image::class, 'image_id');
62     }
63
64     /**
65      * Get all pages within this book.
66      * @return \Illuminate\Database\Eloquent\Relations\HasMany
67      */
68     public function pages()
69     {
70         return $this->hasMany(Page::class);
71     }
72
73     /**
74      * Get all chapters within this book.
75      * @return \Illuminate\Database\Eloquent\Relations\HasMany
76      */
77     public function chapters()
78     {
79         return $this->hasMany(Chapter::class);
80     }
81
82     /**
83      * Get the shelves this book is contained within.
84      * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
85      */
86     public function shelves()
87     {
88         return $this->belongsToMany(Bookshelf::class, 'bookshelves_books', 'book_id', 'bookshelf_id');
89     }
90
91     /**
92      * Get an excerpt of this book's description to the specified length or less.
93      * @param int $length
94      * @return string
95      */
96     public function getExcerpt($length = 100)
97     {
98         $description = $this->description;
99         return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
100     }
101
102     /**
103      * Return a generalised, common raw query that can be 'unioned' across entities.
104      * @return string
105      */
106     public function entityRawQuery()
107     {
108         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";
109     }
110 }