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