]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/Book.php
Fixed some mis-refactoring and split search service
[bookstack] / app / Entities / Models / Book.php
1 <?php namespace BookStack\Entities\Models;
2
3 use BookStack\Entities\Models\Bookshelf;
4 use BookStack\Entities\Models\Chapter;
5 use BookStack\Entities\Models\Entity;
6 use BookStack\Entities\Models\HasCoverImage;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Uploads\Image;
9 use Exception;
10 use Illuminate\Database\Eloquent\Relations\BelongsTo;
11 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
12 use Illuminate\Database\Eloquent\Relations\HasMany;
13 use Illuminate\Support\Collection;
14
15 /**
16  * Class Book
17  * @property string $description
18  * @property int $image_id
19  * @property Image|null $cover
20  */
21 class Book extends Entity implements HasCoverImage
22 {
23     public $searchFactor = 2;
24
25     protected $fillable = ['name', 'description'];
26     protected $hidden = ['restricted', 'pivot', 'image_id'];
27
28     /**
29      * Get the url for this book.
30      * @param string|bool $path
31      * @return string
32      */
33     public function getUrl($path = false)
34     {
35         if ($path !== false) {
36             return url('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
37         }
38         return url('/books/' . urlencode($this->slug));
39     }
40
41     /**
42      * Returns book cover image, if book cover not exists return default cover image.
43      * @param int $width - Width of the image
44      * @param int $height - Height of the image
45      * @return string
46      */
47     public function getBookCover($width = 440, $height = 250)
48     {
49         $default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
50         if (!$this->image_id) {
51             return $default;
52         }
53
54         try {
55             $cover = $this->cover ? url($this->cover->getThumb($width, $height, false)) : $default;
56         } catch (Exception $err) {
57             $cover = $default;
58         }
59         return $cover;
60     }
61
62     /**
63      * Get the cover image of the book
64      */
65     public function cover(): BelongsTo
66     {
67         return $this->belongsTo(Image::class, 'image_id');
68     }
69
70     /**
71      * Get the type of the image model that is used when storing a cover image.
72      */
73     public function coverImageTypeKey(): string
74     {
75         return 'cover_book';
76     }
77
78     /**
79      * Get all pages within this book.
80      * @return HasMany
81      */
82     public function pages()
83     {
84         return $this->hasMany(Page::class);
85     }
86
87     /**
88      * Get the direct child pages of this book.
89      * @return HasMany
90      */
91     public function directPages()
92     {
93         return $this->pages()->where('chapter_id', '=', '0');
94     }
95
96     /**
97      * Get all chapters within this book.
98      * @return HasMany
99      */
100     public function chapters()
101     {
102         return $this->hasMany(Chapter::class);
103     }
104
105     /**
106      * Get the shelves this book is contained within.
107      * @return BelongsToMany
108      */
109     public function shelves()
110     {
111         return $this->belongsToMany(Bookshelf::class, 'bookshelves_books', 'book_id', 'bookshelf_id');
112     }
113
114     /**
115      * Get the direct child items within this book.
116      * @return Collection
117      */
118     public function getDirectChildren(): Collection
119     {
120         $pages = $this->directPages()->visible()->get();
121         $chapters = $this->chapters()->visible()->get();
122         return $pages->concat($chapters)->sortBy('priority')->sortByDesc('draft');
123     }
124
125     /**
126      * Get an excerpt of this book's description to the specified length or less.
127      * @param int $length
128      * @return string
129      */
130     public function getExcerpt(int $length = 100)
131     {
132         $description = $this->description;
133         return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
134     }
135 }