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