]> BookStack Code Mirror - bookstack/blob - app/Page.php
Fixes a corner case with exclamation in the ID.
[bookstack] / app / Page.php
1 <?php namespace BookStack;
2
3 class Page extends Entity
4 {
5     protected $fillable = ['name', 'html', 'priority', 'markdown'];
6
7     protected $simpleAttributes = ['name', 'id', 'slug'];
8
9     public $textField = 'text';
10
11     /**
12      * Converts this page into a simplified array.
13      * @return mixed
14      */
15     public function toSimpleArray()
16     {
17         $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
18         $array['url'] = $this->getUrl();
19         return $array;
20     }
21
22     /**
23      * Get the book this page sits in.
24      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
25      */
26     public function book()
27     {
28         return $this->belongsTo(Book::class);
29     }
30
31     /**
32      * Get the chapter that this page is in, If applicable.
33      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
34      */
35     public function chapter()
36     {
37         return $this->belongsTo(Chapter::class);
38     }
39
40     /**
41      * Check if this page has a chapter.
42      * @return bool
43      */
44     public function hasChapter()
45     {
46         return $this->chapter()->count() > 0;
47     }
48
49     /**
50      * Get the associated page revisions, ordered by created date.
51      * @return mixed
52      */
53     public function revisions()
54     {
55         return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc');
56     }
57
58     /**
59      * Get the attachments assigned to this page.
60      * @return \Illuminate\Database\Eloquent\Relations\HasMany
61      */
62     public function attachments()
63     {
64         return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
65     }
66
67     /**
68      * Get the url for this page.
69      * @param string|bool $path
70      * @return string
71      */
72     public function getUrl($path = false)
73     {
74         $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
75         $midText = $this->draft ? '/draft/' : '/page/';
76         $idComponent = $this->draft ? $this->id : urlencode($this->slug);
77
78         if ($path !== false) {
79             return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
80         }
81
82         return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent);
83     }
84
85     /**
86      * Get an excerpt of this page's content to the specified length.
87      * @param int $length
88      * @return mixed
89      */
90     public function getExcerpt($length = 100)
91     {
92         $text = strlen($this->text) > $length ? substr($this->text, 0, $length-3) . '...' : $this->text;
93         return mb_convert_encoding($text, 'UTF-8');
94     }
95
96     /**
97      * Return a generalised, common raw query that can be 'unioned' across entities.
98      * @param bool $withContent
99      * @return string
100      */
101     public function entityRawQuery($withContent = false)
102     {
103         $htmlQuery = $withContent ? 'html' : "'' as html";
104         return "'BookStack\\\\Page' as entity_type, id, id as entity_id, slug, name, {$this->textField} as text, {$htmlQuery}, book_id, priority, chapter_id, draft, created_by, updated_by, updated_at, created_at";
105     }
106 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.