]> BookStack Code Mirror - bookstack/blob - app/Book.php
Updated Spanish translation
[bookstack] / app / Book.php
1 <?php namespace BookStack;
2
3 class Book extends Entity
4 {
5     public $searchFactor = 2;
6
7     protected $fillable = ['name', 'description', 'image_id'];
8
9     /**
10      * Get the url for this book.
11      * @param string|bool $path
12      * @return string
13      */
14     public function getUrl($path = false)
15     {
16         if ($path !== false) {
17             return baseUrl('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
18         }
19         return baseUrl('/books/' . urlencode($this->slug));
20     }
21
22     /**
23      * Returns book cover image, if book cover not exists return default cover image.
24      * @param int $width - Width of the image
25      * @param int $height - Height of the image
26      * @return string
27      */
28     public function getBookCover($width = 440, $height = 250)
29     {
30         $default = baseUrl('/book_default_cover.png');
31         if (!$this->image_id) {
32             return $default;
33         }
34
35         try {
36             $cover = $this->cover ? baseUrl($this->cover->getThumb($width, $height, false)) : $default;
37         } catch (\Exception $err) {
38             $cover = $default;
39         }
40         return $cover;
41     }
42
43     /**
44      * Get the cover image of the book
45      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
46      */
47     public function cover()
48     {
49         return $this->belongsTo(Image::class, 'image_id');
50     }
51     /*
52      * Get the edit url for this book.
53      * @return string
54      */
55     public function getEditUrl()
56     {
57         return $this->getUrl() . '/edit';
58     }
59
60     /**
61      * Get all pages within this book.
62      * @return \Illuminate\Database\Eloquent\Relations\HasMany
63      */
64     public function pages()
65     {
66         return $this->hasMany(Page::class);
67     }
68
69     /**
70      * Get all chapters within this book.
71      * @return \Illuminate\Database\Eloquent\Relations\HasMany
72      */
73     public function chapters()
74     {
75         return $this->hasMany(Chapter::class);
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($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\\\\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";
96     }
97 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.