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