]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/Bookshelf.php
e4d9775b70b470813bd7562d49e04d87e2872934
[bookstack] / app / Entities / Models / Bookshelf.php
1 <?php
2
3 namespace BookStack\Entities\Models;
4
5 use BookStack\Uploads\Image;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Relations\BelongsTo;
8 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9
10 class Bookshelf extends Entity implements HasCoverImage
11 {
12     use HasFactory;
13
14     protected $table = 'bookshelves';
15
16     public $searchFactor = 1.2;
17
18     protected $fillable = ['name', 'description', 'image_id'];
19
20     protected $hidden = ['restricted', 'image_id', 'deleted_at'];
21
22     /**
23      * Get the books in this shelf.
24      * Should not be used directly since does not take into account permissions.
25      *
26      * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
27      */
28     public function books()
29     {
30         return $this->belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id')
31             ->withPivot('order')
32             ->orderBy('order', 'asc');
33     }
34
35     /**
36      * Related books that are visible to the current user.
37      */
38     public function visibleBooks(): BelongsToMany
39     {
40         return $this->books()->visible();
41     }
42
43     /**
44      * Get the url for this bookshelf.
45      */
46     public function getUrl(string $path = ''): string
47     {
48         return url('/shelves/' . implode('/', [urlencode($this->slug), trim($path, '/')]));
49     }
50
51     /**
52      * Returns BookShelf cover image, if cover does not exists return default cover image.
53      *
54      * @param int $width  - Width of the image
55      * @param int $height - Height of the image
56      *
57      * @return string
58      */
59     public function getBookCover($width = 440, $height = 250)
60     {
61         // TODO - Make generic, focused on books right now, Perhaps set-up a better image
62         $default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
63         if (!$this->image_id) {
64             return $default;
65         }
66
67         try {
68             $cover = $this->cover ? url($this->cover->getThumb($width, $height, false)) : $default;
69         } catch (\Exception $err) {
70             $cover = $default;
71         }
72
73         return $cover;
74     }
75
76     /**
77      * Get the cover image of the shelf.
78      */
79     public function cover(): BelongsTo
80     {
81         return $this->belongsTo(Image::class, 'image_id');
82     }
83
84     /**
85      * Get the type of the image model that is used when storing a cover image.
86      */
87     public function coverImageTypeKey(): string
88     {
89         return 'cover_shelf';
90     }
91
92     /**
93      * Check if this shelf contains the given book.
94      *
95      * @param Book $book
96      *
97      * @return bool
98      */
99     public function contains(Book $book): bool
100     {
101         return $this->books()->where('id', '=', $book->id)->count() > 0;
102     }
103
104     /**
105      * Add a book to the end of this shelf.
106      *
107      * @param Book $book
108      */
109     public function appendBook(Book $book)
110     {
111         if ($this->contains($book)) {
112             return;
113         }
114
115         $maxOrder = $this->books()->max('order');
116         $this->books()->attach($book->id, ['order' => $maxOrder + 1]);
117     }
118 }