]> BookStack Code Mirror - bookstack/blob - app/Page.php
Merge branch 'diff' of git://github.com/younes0/BookStack into younes0-diff
[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      * @param string|bool $path
60      * @return string
61      */
62     public function getUrl($path = false)
63     {
64         $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
65         $midText = $this->draft ? '/draft/' : '/page/';
66         $idComponent = $this->draft ? $this->id : $this->slug;
67
68         if ($path !== false) {
69             return baseUrl('/books/' . $bookSlug . $midText . $idComponent . '/' . trim($path, '/'));
70         }
71
72         return baseUrl('/books/' . $bookSlug . $midText . $idComponent);
73     }
74
75     /**
76      * Get an excerpt of this page's content to the specified length.
77      * @param int $length
78      * @return mixed
79      */
80     public function getExcerpt($length = 100)
81     {
82         $text = strlen($this->text) > $length ? substr($this->text, 0, $length-3) . '...' : $this->text;
83         return mb_convert_encoding($text, 'UTF-8');
84     }
85
86 }