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