]> BookStack Code Mirror - bookstack/blob - app/Entities/Book.php
Update entities.php
[bookstack] / app / Entities / Book.php
1 <?php namespace BookStack\Entities;
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  * @package BookStack\Entities
16  */
17 class Book extends Entity implements HasCoverImage
18 {
19     public $searchFactor = 2;
20
21     protected $fillable = ['name', 'description', 'image_id'];
22
23     /**
24      * Get the url for this book.
25      * @param string|bool $path
26      * @return string
27      */
28     public function getUrl($path = false)
29     {
30         if ($path !== false) {
31             return url('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
32         }
33         return url('/books/' . urlencode($this->slug));
34     }
35
36     /**
37      * Returns book cover image, if book cover not exists return default cover image.
38      * @param int $width - Width of the image
39      * @param int $height - Height of the image
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         return $cover;
55     }
56
57     /**
58      * Get the cover image of the book
59      */
60     public function cover(): BelongsTo
61     {
62         return $this->belongsTo(Image::class, 'image_id');
63     }
64
65     /**
66      * Get the type of the image model that is used when storing a cover image.
67      */
68     public function coverImageTypeKey(): string
69     {
70         return 'cover_book';
71     }
72
73     /**
74      * Get all pages within this book.
75      * @return HasMany
76      */
77     public function pages()
78     {
79         return $this->hasMany(Page::class);
80     }
81
82     /**
83      * Get the direct child pages of this book.
84      * @return HasMany
85      */
86     public function directPages()
87     {
88         return $this->pages()->where('chapter_id', '=', '0');
89     }
90
91     /**
92      * Get all chapters within this book.
93      * @return HasMany
94      */
95     public function chapters()
96     {
97         return $this->hasMany(Chapter::class);
98     }
99
100     /**
101      * Get the shelves this book is contained within.
102      * @return BelongsToMany
103      */
104     public function shelves()
105     {
106         return $this->belongsToMany(Bookshelf::class, 'bookshelves_books', 'book_id', 'bookshelf_id');
107     }
108
109     /**
110      * Get the direct child items within this book.
111      * @return Collection
112      */
113     public function getDirectChildren(): Collection
114     {
115         $pages = $this->directPages()->visible()->get();
116         $chapters = $this->chapters()->visible()->get();
117         return $pages->contact($chapters)->sortBy('priority')->sortByDesc('draft');
118     }
119
120     /**
121      * Get an excerpt of this book's description to the specified length or less.
122      * @param int $length
123      * @return string
124      */
125     public function getExcerpt(int $length = 100)
126     {
127         $description = $this->description;
128         return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
129     }
130 }