]> BookStack Code Mirror - bookstack/blob - app/Bookshelf.php
Spanish translation
[bookstack] / app / Bookshelf.php
1 <?php namespace BookStack;
2
3 class Bookshelf extends Entity
4 {
5     protected $table = 'bookshelves';
6
7     public $searchFactor = 3;
8
9     protected $fillable = ['name', 'description', 'image_id'];
10
11     /**
12      * Get the books in this shelf.
13      * Should not be used directly since does not take into account permissions.
14      * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
15      */
16     public function books()
17     {
18         return $this->belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id')->orderBy('order', 'asc');
19     }
20
21     /**
22      * Get the url for this bookshelf.
23      * @param string|bool $path
24      * @return string
25      */
26     public function getUrl($path = false)
27     {
28         if ($path !== false) {
29             return baseUrl('/shelves/' . urlencode($this->slug) . '/' . trim($path, '/'));
30         }
31         return baseUrl('/shelves/' . urlencode($this->slug));
32     }
33
34     /**
35      * Returns BookShelf cover image, if cover does not exists return default cover image.
36      * @param int $width - Width of the image
37      * @param int $height - Height of the image
38      * @return string
39      */
40     public function getBookCover($width = 440, $height = 250)
41     {
42         $default = baseUrl('/book_default_cover.png');
43         if (!$this->image_id) {
44             return $default;
45         }
46
47         try {
48             $cover = $this->cover ? baseUrl($this->cover->getThumb($width, $height, false)) : $default;
49         } catch (\Exception $err) {
50             $cover = $default;
51         }
52         return $cover;
53     }
54
55     /**
56      * Get the cover image of the book
57      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
58      */
59     public function cover()
60     {
61         return $this->belongsTo(Image::class, 'image_id');
62     }
63
64     /**
65      * Get an excerpt of this book's description to the specified length or less.
66      * @param int $length
67      * @return string
68      */
69     public function getExcerpt($length = 100)
70     {
71         $description = $this->description;
72         return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
73     }
74
75     /**
76      * Return a generalised, common raw query that can be 'unioned' across entities.
77      * @return string
78      */
79     public function entityRawQuery()
80     {
81         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";
82     }
83 }