]> BookStack Code Mirror - bookstack/blob - app/Entities/Book.php
Update entities.php
[bookstack] / app / Entities / Book.php
1 <?php namespace BookStack\Entities;
2
3 use BookStack\Uploads\Image;
4
5 class Book extends Entity
6 {
7     public $searchFactor = 2;
8
9     protected $fillable = ['name', 'description', 'image_id'];
10
11     /**
12      * Get the morph class for this model.
13      * @return string
14      */
15     public function getMorphClass()
16     {
17         return 'BookStack\\Book';
18     }
19
20     /**
21      * Get the url for this book.
22      * @param string|bool $path
23      * @return string
24      */
25     public function getUrl($path = false)
26     {
27         if ($path !== false) {
28             return baseUrl('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
29         }
30         return baseUrl('/books/' . urlencode($this->slug));
31     }
32
33     /**
34      * Returns book cover image, if book cover not exists return default cover image.
35      * @param int $width - Width of the image
36      * @param int $height - Height of the image
37      * @return string
38      */
39     public function getBookCover($width = 440, $height = 250)
40     {
41         $default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
42         if (!$this->image_id) {
43             return $default;
44         }
45
46         try {
47             $cover = $this->cover ? baseUrl($this->cover->getThumb($width, $height, false)) : $default;
48         } catch (\Exception $err) {
49             $cover = $default;
50         }
51         return $cover;
52     }
53
54     /**
55      * Get the cover image of the book
56      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
57      */
58     public function cover()
59     {
60         return $this->belongsTo(Image::class, 'image_id');
61     }
62
63     /**
64      * Get all pages within this book.
65      * @return \Illuminate\Database\Eloquent\Relations\HasMany
66      */
67     public function pages()
68     {
69         return $this->hasMany(Page::class);
70     }
71
72     /**
73      * Get the direct child pages of this book.
74      * @return \Illuminate\Database\Eloquent\Relations\HasMany
75      */
76     public function directPages()
77     {
78         return $this->pages()->where('chapter_id', '=', '0');
79     }
80
81     /**
82      * Get all chapters within this book.
83      * @return \Illuminate\Database\Eloquent\Relations\HasMany
84      */
85     public function chapters()
86     {
87         return $this->hasMany(Chapter::class);
88     }
89
90     /**
91      * Get the shelves this book is contained within.
92      * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
93      */
94     public function shelves()
95     {
96         return $this->belongsToMany(Bookshelf::class, 'bookshelves_books', 'book_id', 'bookshelf_id');
97     }
98
99     /**
100      * Get an excerpt of this book's description to the specified length or less.
101      * @param int $length
102      * @return string
103      */
104     public function getExcerpt(int $length = 100)
105     {
106         $description = $this->description;
107         return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
108     }
109
110     /**
111      * Return a generalised, common raw query that can be 'unioned' across entities.
112      * @return string
113      */
114     public function entityRawQuery()
115     {
116         return "'BookStack\\\\Book' 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";
117     }
118 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.