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