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