]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/Book.php
Added translation string for tasklist WYSIWYG action
[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     public function pages(): HasMany
84     {
85         return $this->hasMany(Page::class);
86     }
87
88     /**
89      * Get the direct child pages of this book.
90      */
91     public function directPages(): HasMany
92     {
93         return $this->pages()->where('chapter_id', '=', '0');
94     }
95
96     /**
97      * Get all chapters within this book.
98      */
99     public function chapters(): HasMany
100     {
101         return $this->hasMany(Chapter::class);
102     }
103
104     /**
105      * Get the shelves this book is contained within.
106      */
107     public function shelves(): BelongsToMany
108     {
109         return $this->belongsToMany(Bookshelf::class, 'bookshelves_books', 'book_id', 'bookshelf_id');
110     }
111
112     /**
113      * Get the direct child items within this book.
114      */
115     public function getDirectChildren(): Collection
116     {
117         $pages = $this->directPages()->scopes('visible')->get();
118         $chapters = $this->chapters()->scopes('visible')->get();
119
120         return $pages->concat($chapters)->sortBy('priority')->sortByDesc('draft');
121     }
122 }