]> BookStack Code Mirror - bookstack/blob - app/Page.php
Improved permission regen performance by factor of 4
[bookstack] / app / Page.php
1 <?php namespace BookStack;
2
3
4 class Page extends Entity
5 {
6     protected $fillable = ['name', 'html', 'priority', 'markdown'];
7
8     protected $simpleAttributes = ['name', 'id', 'slug'];
9
10     public function toSimpleArray()
11     {
12         $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
13         $array['url'] = $this->getUrl();
14         return $array;
15     }
16
17     public function book()
18     {
19         return $this->belongsTo('BookStack\Book');
20     }
21
22     public function chapter()
23     {
24         return $this->belongsTo('BookStack\Chapter');
25     }
26
27     public function hasChapter()
28     {
29         return $this->chapter()->count() > 0;
30     }
31
32     public function revisions()
33     {
34         return $this->hasMany('BookStack\PageRevision')->where('type', '=', 'version')->orderBy('created_at', 'desc');
35     }
36
37     public function getUrl()
38     {
39         $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
40         $midText = $this->draft ? '/draft/' : '/page/';
41         $idComponent = $this->draft ? $this->id : $this->slug;
42         return '/books/' . $bookSlug . $midText . $idComponent;
43     }
44
45     public function getExcerpt($length = 100)
46     {
47         $text = strlen($this->text) > $length ? substr($this->text, 0, $length-3) . '...' : $this->text;
48         return mb_convert_encoding($text, 'UTF-8');
49     }
50
51 }