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