]> BookStack Code Mirror - bookstack/blob - app/Entities/Page.php
59de338cb79494e3de328d8cb7fdbe680ed07a0d
[bookstack] / app / Entities / Page.php
1 <?php namespace BookStack\Entities;
2
3 use BookStack\Uploads\Attachment;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Chapter;
6 use BookStack\Entities\Entity;
7 use BookStack\Entities\PageRevision;
8
9 class Page extends Entity
10 {
11     protected $fillable = ['name', 'html', 'priority', 'markdown'];
12
13     protected $simpleAttributes = ['name', 'id', 'slug'];
14
15     public $textField = 'text';
16
17     /**
18      * Get the morph class for this model.
19      * @return string
20      */
21     public function getMorphClass()
22     {
23         return 'BookStack\\Page';
24     }
25
26     /**
27      * Converts this page into a simplified array.
28      * @return mixed
29      */
30     public function toSimpleArray()
31     {
32         $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
33         $array['url'] = $this->getUrl();
34         return $array;
35     }
36
37     /**
38      * Get the book this page sits in.
39      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
40      */
41     public function book()
42     {
43         return $this->belongsTo(Book::class);
44     }
45
46     /**
47      * Get the parent item
48      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
49      */
50     public function parent()
51     {
52         return $this->chapter_id ? $this->chapter() : $this->book();
53     }
54
55     /**
56      * Get the chapter that this page is in, If applicable.
57      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
58      */
59     public function chapter()
60     {
61         return $this->belongsTo(Chapter::class);
62     }
63
64     /**
65      * Check if this page has a chapter.
66      * @return bool
67      */
68     public function hasChapter()
69     {
70         return $this->chapter()->count() > 0;
71     }
72
73     /**
74      * Get the associated page revisions, ordered by created date.
75      * @return mixed
76      */
77     public function revisions()
78     {
79         return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc');
80     }
81
82     /**
83      * Get the attachments assigned to this page.
84      * @return \Illuminate\Database\Eloquent\Relations\HasMany
85      */
86     public function attachments()
87     {
88         return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
89     }
90
91     /**
92      * Get the url for this page.
93      * @param string|bool $path
94      * @return string
95      */
96     public function getUrl($path = false)
97     {
98         $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
99         $midText = $this->draft ? '/draft/' : '/page/';
100         $idComponent = $this->draft ? $this->id : urlencode($this->slug);
101
102         if ($path !== false) {
103             return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
104         }
105
106         return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent);
107     }
108
109     /**
110      * Get an excerpt of this page's content to the specified length.
111      * @param int $length
112      * @return mixed
113      */
114     public function getExcerpt($length = 100)
115     {
116         $text = strlen($this->text) > $length ? substr($this->text, 0, $length-3) . '...' : $this->text;
117         return mb_convert_encoding($text, 'UTF-8');
118     }
119
120     /**
121      * Return a generalised, common raw query that can be 'unioned' across entities.
122      * @param bool $withContent
123      * @return string
124      */
125     public function entityRawQuery($withContent = false)
126     {
127         $htmlQuery = $withContent ? 'html' : "'' as html";
128         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";
129     }
130
131     /**
132      * Get the current revision for the page if existing
133      * @return \BookStack\Entities\PageRevision|null
134      */
135     public function getCurrentRevision()
136     {
137         return $this->revisions()->first();
138     }
139 }