]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/Page.php
Minor capitalisation fix for Estonian
[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     public static $listAttributes = ['name', 'id', 'slug', 'book_id', 'chapter_id', 'draft', 'template', 'text', 'created_at', 'updated_at', 'priority'];
29     public static $contentAttributes = ['name', 'id', 'slug', 'book_id', 'chapter_id', 'draft', 'template', 'html', 'text', 'created_at', 'updated_at', 'priority'];
30
31     protected $fillable = ['name', 'priority'];
32
33     public $textField = 'text';
34
35     protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot', 'deleted_at'];
36
37     protected $casts = [
38         'draft'    => 'boolean',
39         'template' => 'boolean',
40     ];
41
42     /**
43      * Get the entities that are visible to the current user.
44      */
45     public function scopeVisible(Builder $query): Builder
46     {
47         $query = Permissions::enforceDraftVisibilityOnQuery($query);
48
49         return parent::scopeVisible($query);
50     }
51
52     /**
53      * Get the chapter that this page is in, If applicable.
54      *
55      * @return BelongsTo
56      */
57     public function chapter()
58     {
59         return $this->belongsTo(Chapter::class);
60     }
61
62     /**
63      * Check if this page has a chapter.
64      *
65      * @return bool
66      */
67     public function hasChapter()
68     {
69         return $this->chapter()->count() > 0;
70     }
71
72     /**
73      * Get the associated page revisions, ordered by created date.
74      * Only provides actual saved page revision instances, Not drafts.
75      */
76     public function revisions(): HasMany
77     {
78         return $this->allRevisions()
79             ->where('type', '=', 'version')
80             ->orderBy('created_at', 'desc')
81             ->orderBy('id', 'desc');
82     }
83
84     /**
85      * Get all revision instances assigned to this page.
86      * Includes all types of revisions.
87      */
88     public function allRevisions(): HasMany
89     {
90         return $this->hasMany(PageRevision::class);
91     }
92
93     /**
94      * Get the attachments assigned to this page.
95      *
96      * @return HasMany
97      */
98     public function attachments()
99     {
100         return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
101     }
102
103     /**
104      * Get the url of this page.
105      */
106     public function getUrl($path = ''): string
107     {
108         $parts = [
109             'books',
110             urlencode($this->book_slug ?? $this->book->slug),
111             $this->draft ? 'draft' : 'page',
112             $this->draft ? $this->id : urlencode($this->slug),
113             trim($path, '/'),
114         ];
115
116         return url('/' . implode('/', $parts));
117     }
118
119     /**
120      * Get the current revision for the page if existing.
121      *
122      * @return PageRevision|null
123      */
124     public function getCurrentRevision()
125     {
126         return $this->revisions()->first();
127     }
128
129     /**
130      * Get this page for JSON display.
131      */
132     public function forJsonDisplay(): Page
133     {
134         $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
135         $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
136         $refreshed->html = (new PageContent($refreshed))->render();
137
138         return $refreshed;
139     }
140 }