]> BookStack Code Mirror - bookstack/blob - app/Entities/Bookshelf.php
Update settings.php
[bookstack] / app / Entities / Bookshelf.php
1 <?php namespace BookStack\Entities;
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'];
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      * @param string|bool $path
40      * @return string
41      */
42     public function getUrl($path = false)
43     {
44         if ($path !== false) {
45             return url('/shelves/' . urlencode($this->slug) . '/' . trim($path, '/'));
46         }
47         return url('/shelves/' . urlencode($this->slug));
48     }
49
50     /**
51      * Returns BookShelf cover image, if cover does not exists return default cover image.
52      * @param int $width - Width of the image
53      * @param int $height - Height of the image
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         return $cover;
70     }
71
72     /**
73      * Get the cover image of the shelf
74      */
75     public function cover(): BelongsTo
76     {
77         return $this->belongsTo(Image::class, 'image_id');
78     }
79
80     /**
81      * Get the type of the image model that is used when storing a cover image.
82      */
83     public function coverImageTypeKey(): string
84     {
85         return 'cover_shelf';
86     }
87
88     /**
89      * Get an excerpt of this book's description to the specified length or less.
90      * @param int $length
91      * @return string
92      */
93     public function getExcerpt(int $length = 100)
94     {
95         $description = $this->description;
96         return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
97     }
98
99     /**
100      * Check if this shelf contains the given book.
101      * @param Book $book
102      * @return bool
103      */
104     public function contains(Book $book): bool
105     {
106         return $this->books()->where('id', '=', $book->id)->count() > 0;
107     }
108
109     /**
110      * Add a book to the end of this shelf.
111      * @param Book $book
112      */
113     public function appendBook(Book $book)
114     {
115         if ($this->contains($book)) {
116             return;
117         }
118
119         $maxOrder = $this->books()->max('order');
120         $this->books()->attach($book->id, ['order' => $maxOrder + 1]);
121     }
122 }