]> BookStack Code Mirror - bookstack/blob - app/Page.php
replace GPL diff lib with MIT lib
[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     /**
11      * Converts this page into a simplified array.
12      * @return mixed
13      */
14     public function toSimpleArray()
15     {
16         $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
17         $array['url'] = $this->getUrl();
18         return $array;
19     }
20
21     /**
22      * Get the book this page sits in.
23      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
24      */
25     public function book()
26     {
27         return $this->belongsTo(Book::class);
28     }
29
30     /**
31      * Get the chapter that this page is in, If applicable.
32      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
33      */
34     public function chapter()
35     {
36         return $this->belongsTo(Chapter::class);
37     }
38
39     /**
40      * Check if this page has a chapter.
41      * @return bool
42      */
43     public function hasChapter()
44     {
45         return $this->chapter()->count() > 0;
46     }
47
48     /**
49      * Get the associated page revisions, ordered by created date.
50      * @return mixed
51      */
52     public function revisions()
53     {
54         return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc');
55     }
56
57     /**
58      * Get the url for this page.
59      * @return string
60      */
61     public function getUrl()
62     {
63         $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
64         $midText = $this->draft ? '/draft/' : '/page/';
65         $idComponent = $this->draft ? $this->id : $this->slug;
66         return '/books/' . $bookSlug . $midText . $idComponent;
67     }
68
69     /**
70      * Get an excerpt of this page's content to the specified length.
71      * @param int $length
72      * @return mixed
73      */
74     public function getExcerpt($length = 100)
75     {
76         $text = strlen($this->text) > $length ? substr($this->text, 0, $length-3) . '...' : $this->text;
77         return mb_convert_encoding($text, 'UTF-8');
78     }
79
80 }