]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/Book.php
Apply fixes from StyleCI
[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  */
19 class Book extends Entity implements HasCoverImage
20 {
21     public $searchFactor = 2;
22
23     protected $fillable = ['name', 'description'];
24     protected $hidden = ['restricted', 'pivot', 'image_id', 'deleted_at'];
25
26     /**
27      * Get the url for this book.
28      */
29     public function getUrl(string $path = ''): string
30     {
31         return url('/books/' . implode('/', [urlencode($this->slug), trim($path, '/')]));
32     }
33
34     /**
35      * Returns book cover image, if book cover not exists return default cover image.
36      *
37      * @param int $width  - Width of the image
38      * @param int $height - Height of the image
39      *
40      * @return string
41      */
42     public function getBookCover($width = 440, $height = 250)
43     {
44         $default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
45         if (!$this->image_id) {
46             return $default;
47         }
48
49         try {
50             $cover = $this->cover ? url($this->cover->getThumb($width, $height, false)) : $default;
51         } catch (Exception $err) {
52             $cover = $default;
53         }
54
55         return $cover;
56     }
57
58     /**
59      * Get the cover image of the book.
60      */
61     public function cover(): BelongsTo
62     {
63         return $this->belongsTo(Image::class, 'image_id');
64     }
65
66     /**
67      * Get the type of the image model that is used when storing a cover image.
68      */
69     public function coverImageTypeKey(): string
70     {
71         return 'cover_book';
72     }
73
74     /**
75      * Get all pages within this book.
76      *
77      * @return HasMany
78      */
79     public function pages()
80     {
81         return $this->hasMany(Page::class);
82     }
83
84     /**
85      * Get the direct child pages of this book.
86      *
87      * @return HasMany
88      */
89     public function directPages()
90     {
91         return $this->pages()->where('chapter_id', '=', '0');
92     }
93
94     /**
95      * Get all chapters within this book.
96      *
97      * @return HasMany
98      */
99     public function chapters()
100     {
101         return $this->hasMany(Chapter::class);
102     }
103
104     /**
105      * Get the shelves this book is contained within.
106      *
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      *
117      * @return Collection
118      */
119     public function getDirectChildren(): Collection
120     {
121         $pages = $this->directPages()->visible()->get();
122         $chapters = $this->chapters()->visible()->get();
123
124         return $pages->concat($chapters)->sortBy('priority')->sortByDesc('draft');
125     }
126 }