]> BookStack Code Mirror - bookstack/blob - app/Entities/Bookshelf.php
Update create new book button on shelves to 2019 design
[bookstack] / app / Entities / Bookshelf.php
1 <?php namespace BookStack\Entities;
2
3 use BookStack\Uploads\Image;
4
5 class Bookshelf extends Entity
6 {
7     protected $table = 'bookshelves';
8
9     public $searchFactor = 3;
10
11     protected $fillable = ['name', 'description', 'image_id'];
12
13     /**
14      * Get the morph class for this model.
15      * @return string
16      */
17     public function getMorphClass()
18     {
19         return 'BookStack\\Bookshelf';
20     }
21
22     /**
23      * Get the books in this shelf.
24      * Should not be used directly since does not take into account permissions.
25      * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
26      */
27     public function books()
28     {
29         return $this->belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id')->orderBy('order', 'asc');
30     }
31
32     /**
33      * Get the url for this bookshelf.
34      * @param string|bool $path
35      * @return string
36      */
37     public function getUrl($path = false)
38     {
39         if ($path !== false) {
40             return baseUrl('/shelves/' . urlencode($this->slug) . '/' . trim($path, '/'));
41         }
42         return baseUrl('/shelves/' . urlencode($this->slug));
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 ? baseUrl($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      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
70      */
71     public function cover()
72     {
73         return $this->belongsTo(Image::class, 'image_id');
74     }
75
76     /**
77      * Get an excerpt of this book's description to the specified length or less.
78      * @param int $length
79      * @return string
80      */
81     public function getExcerpt(int $length = 100)
82     {
83         $description = $this->description;
84         return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
85     }
86
87     /**
88      * Return a generalised, common raw query that can be 'unioned' across entities.
89      * @return string
90      */
91     public function entityRawQuery()
92     {
93         return "'BookStack\\\\BookShelf' as entity_type, id, id as entity_id, slug, name, {$this->textField} as text,'' as html, '0' as book_id, '0' as priority, '0' as chapter_id, '0' as draft, created_by, updated_by, updated_at, created_at";
94     }
95
96     /**
97      * Check if this shelf contains the given book.
98      * @param Book $book
99      * @return bool
100      */
101     public function contains(Book $book)
102     {
103         return $this->books()->where('id', '=', $book->id)->count() > 0;
104     }
105 }