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