]> BookStack Code Mirror - bookstack/blob - app/Book.php
Merge branch 'master' of https://github.com/OsmosysSoftware/BookStack
[bookstack] / app / Book.php
1 <?php namespace BookStack;
2
3 class Book extends Entity
4 {
5
6     protected $fillable = ['name', 'description', 'image'];
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     public function getBookCover()
22     {
23         $default = baseUrl('/default.png');
24         $image = $this->image;
25         if ($image === 0 || $image === '0' || $image === null) 
26             return $default;
27         try {
28             $cover = $this->cover ? baseUrl($this->cover->getThumb(120, 192, false)) : $default;
29         } catch (\Exception $err) {
30             $cover = $default;
31         }
32         return $cover;
33     }
34
35     public function getHeadingExcerpt($length = 35)
36     {
37         $heading = $this->name;
38         return strlen($heading) > $length ? substr($heading, 0, $length-3) . '...' : $heading;
39     }
40     
41     public function cover()
42     {
43         return $this->belongsTo(Image::class, 'image');
44     }
45     /*
46      * Get the edit url for this book.
47      * @return string
48      */
49     public function getEditUrl()
50     {
51         return $this->getUrl() . '/edit';
52     }
53
54     /**
55      * Get all pages within this book.
56      * @return \Illuminate\Database\Eloquent\Relations\HasMany
57      */
58     public function pages()
59     {
60         return $this->hasMany(Page::class);
61     }
62
63     /**
64      * Get all chapters within this book.
65      * @return \Illuminate\Database\Eloquent\Relations\HasMany
66      */
67     public function chapters()
68     {
69         return $this->hasMany(Chapter::class);
70     }
71
72     /**
73      * Get an excerpt of this book's description to the specified length or less.
74      * @param int $length
75      * @return string
76      */
77     public function getExcerpt($length = 100)
78     {
79         $description = $this->description;
80         return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;
81     }
82
83     /**
84      * Return a generalised, common raw query that can be 'unioned' across entities.
85      * @return string
86      */
87     public function entityRawQuery()
88     {
89         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";
90     }
91
92 }