]> BookStack Code Mirror - bookstack/blob - app/Entities/Bookshelf.php
Updated the Swedish language files
[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')
30             ->withPivot('order')
31             ->orderBy('order', 'asc');
32     }
33
34     /**
35      * Get the url for this bookshelf.
36      * @param string|bool $path
37      * @return string
38      */
39     public function getUrl($path = false)
40     {
41         if ($path !== false) {
42             return baseUrl('/shelves/' . urlencode($this->slug) . '/' . trim($path, '/'));
43         }
44         return baseUrl('/shelves/' . urlencode($this->slug));
45     }
46
47     /**
48      * Returns BookShelf cover image, if cover does not exists return default cover image.
49      * @param int $width - Width of the image
50      * @param int $height - Height of the image
51      * @return string
52      */
53     public function getBookCover($width = 440, $height = 250)
54     {
55         // TODO - Make generic, focused on books right now, Perhaps set-up a better image
56         $default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
57         if (!$this->image_id) {
58             return $default;
59         }
60
61         try {
62             $cover = $this->cover ? baseUrl($this->cover->getThumb($width, $height, false)) : $default;
63         } catch (\Exception $err) {
64             $cover = $default;
65         }
66         return $cover;
67     }
68
69     /**
70      * Get the cover image of the shelf
71      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
72      */
73     public function cover()
74     {
75         return $this->belongsTo(Image::class, 'image_id');
76     }
77
78     /**
79      * Get an excerpt of this book's description to the specified length or less.
80      * @param int $length
81      * @return string
82      */
83     public function getExcerpt(int $length = 100)
84     {
85         $description = $this->description;
86         return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
87     }
88
89     /**
90      * Return a generalised, common raw query that can be 'unioned' across entities.
91      * @return string
92      */
93     public function entityRawQuery()
94     {
95         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";
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)
104     {
105         return $this->books()->where('id', '=', $book->id)->count() > 0;
106     }
107 }