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