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