]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/Page.php
Apply fixes from StyleCI
[bookstack] / app / Entities / Models / Page.php
1 <?php
2
3 namespace BookStack\Entities\Models;
4
5 use BookStack\Entities\Tools\PageContent;
6 use BookStack\Uploads\Attachment;
7 use Illuminate\Database\Eloquent\Builder;
8 use Illuminate\Database\Eloquent\Collection;
9 use Illuminate\Database\Eloquent\Relations\BelongsTo;
10 use Illuminate\Database\Eloquent\Relations\HasMany;
11 use Permissions;
12
13 /**
14  * Class Page.
15  *
16  * @property int        $chapter_id
17  * @property string     $html
18  * @property string     $markdown
19  * @property string     $text
20  * @property bool       $template
21  * @property bool       $draft
22  * @property int        $revision_count
23  * @property Chapter    $chapter
24  * @property Collection $attachments
25  */
26 class Page extends BookChild
27 {
28     protected $fillable = ['name', 'priority', 'markdown'];
29
30     protected $simpleAttributes = ['name', 'id', 'slug'];
31
32     public $textField = 'text';
33
34     protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot', 'deleted_at'];
35
36     protected $casts = [
37         'draft'    => 'boolean',
38         'template' => 'boolean',
39     ];
40
41     /**
42      * Get the entities that are visible to the current user.
43      */
44     public function scopeVisible(Builder $query): Builder
45     {
46         $query = Permissions::enforceDraftVisibilityOnQuery($query);
47
48         return parent::scopeVisible($query);
49     }
50
51     /**
52      * Converts this page into a simplified array.
53      *
54      * @return mixed
55      */
56     public function toSimpleArray()
57     {
58         $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
59         $array['url'] = $this->getUrl();
60
61         return $array;
62     }
63
64     /**
65      * Get the chapter that this page is in, If applicable.
66      *
67      * @return BelongsTo
68      */
69     public function chapter()
70     {
71         return $this->belongsTo(Chapter::class);
72     }
73
74     /**
75      * Check if this page has a chapter.
76      *
77      * @return bool
78      */
79     public function hasChapter()
80     {
81         return $this->chapter()->count() > 0;
82     }
83
84     /**
85      * Get the associated page revisions, ordered by created date.
86      * Only provides actual saved page revision instances, Not drafts.
87      */
88     public function revisions(): HasMany
89     {
90         return $this->allRevisions()
91             ->where('type', '=', 'version')
92             ->orderBy('created_at', 'desc')
93             ->orderBy('id', 'desc');
94     }
95
96     /**
97      * Get all revision instances assigned to this page.
98      * Includes all types of revisions.
99      */
100     public function allRevisions(): HasMany
101     {
102         return $this->hasMany(PageRevision::class);
103     }
104
105     /**
106      * Get the attachments assigned to this page.
107      *
108      * @return HasMany
109      */
110     public function attachments()
111     {
112         return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
113     }
114
115     /**
116      * Get the url of this page.
117      */
118     public function getUrl($path = ''): string
119     {
120         $parts = [
121             'books',
122             urlencode($this->getAttribute('bookSlug') ?? $this->book->slug),
123             $this->draft ? 'draft' : 'page',
124             $this->draft ? $this->id : urlencode($this->slug),
125             trim($path, '/'),
126         ];
127
128         return url('/' . implode('/', $parts));
129     }
130
131     /**
132      * Get the current revision for the page if existing.
133      *
134      * @return PageRevision|null
135      */
136     public function getCurrentRevision()
137     {
138         return $this->revisions()->first();
139     }
140
141     /**
142      * Get this page for JSON display.
143      */
144     public function forJsonDisplay(): Page
145     {
146         $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
147         $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
148         $refreshed->html = (new PageContent($refreshed))->render();
149
150         return $refreshed;
151     }
152 }