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