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