6
6
use BookStack \Entities \Models \Book ;
7
7
use BookStack \Entities \Models \Chapter ;
8
8
use BookStack \Entities \Models \Entity ;
9
+ use BookStack \Entities \Models \EntityPageData ;
9
10
use BookStack \Entities \Models \Page ;
10
11
use BookStack \Entities \Models \PageRevision ;
11
12
use BookStack \Entities \Queries \EntityQueries ;
@@ -37,34 +38,37 @@ public function __construct(
37
38
/**
38
39
* Get a new draft page belonging to the given parent entity.
39
40
*/
40
- public function getNewDraftPage (Entity $ parent )
41
+ public function getNewDraftPage (Entity $ parent ): Page
41
42
{
42
43
$ page = (new Page ())->forceFill ([
43
44
'name ' => trans ('entities.pages_initial_name ' ),
44
45
'created_by ' => user ()->id ,
45
46
'owned_by ' => user ()->id ,
46
47
'updated_by ' => user ()->id ,
48
+ ]);
49
+ $ pageData = (new EntityPageData ())->forceFill ([
47
50
'draft ' => true ,
48
51
'editor ' => PageEditorType::getSystemDefault ()->value ,
49
52
]);
50
53
51
54
if ($ parent instanceof Chapter) {
52
- $ page ->chapter_id = $ parent ->id ;
55
+ $ pageData ->chapter_id = $ parent ->id ;
53
56
$ page ->book_id = $ parent ->book_id ;
54
57
} else {
55
58
$ page ->book_id = $ parent ->id ;
56
59
}
57
60
58
- $ defaultTemplate = $ page ->chapter ->defaultTemplate ?? $ page ->book ->defaultTemplate ;
61
+ $ defaultTemplate = $ page ->chapter ->containerData -> defaultTemplate ?? $ page ->book -> containerData ->defaultTemplate ;
59
62
if ($ defaultTemplate && userCan (Permission::PageView, $ defaultTemplate )) {
60
- $ page ->forceFill ([
63
+ $ pageData ->forceFill ([
61
64
'html ' => $ defaultTemplate ->html ,
62
65
'markdown ' => $ defaultTemplate ->markdown ,
63
66
]);
64
67
}
65
68
66
- (new DatabaseTransaction (function () use ($ page ) {
69
+ (new DatabaseTransaction (function () use ($ page, $ pageData ) {
67
70
$ page ->save ();
71
+ $ page ->pageData ()->save ($ pageData );
68
72
$ page ->refresh ()->rebuildPermissions ();
69
73
}))->run ();
70
74
@@ -77,9 +81,9 @@ public function getNewDraftPage(Entity $parent)
77
81
public function publishDraft (Page $ draft , array $ input ): Page
78
82
{
79
83
return (new DatabaseTransaction (function () use ($ draft , $ input ) {
80
- $ draft ->draft = false ;
81
- $ draft ->revision_count = 1 ;
82
- $ draft ->priority = $ this ->getNewPriority ($ draft );
84
+ $ draft ->pageData -> draft = false ;
85
+ $ draft ->pageData -> revision_count = 1 ;
86
+ $ draft ->pageData -> priority = $ this ->getNewPriority ($ draft );
83
87
$ this ->updateTemplateStatusAndContentFromInput ($ draft , $ input );
84
88
$ this ->baseRepo ->update ($ draft , $ input );
85
89
$ draft ->rebuildPermissions ();
@@ -112,15 +116,16 @@ public function setContentFromInput(Page $page, array $input): void
112
116
public function update (Page $ page , array $ input ): Page
113
117
{
114
118
// Hold the old details to compare later
115
- $ oldHtml = $ page ->html ;
116
119
$ oldName = $ page ->name ;
117
- $ oldMarkdown = $ page ->markdown ;
120
+ $ oldHtml = $ page ->pageData ->html ;
121
+ $ oldMarkdown = $ page ->pageData ->markdown ;
118
122
119
123
$ this ->updateTemplateStatusAndContentFromInput ($ page , $ input );
120
124
$ this ->baseRepo ->update ($ page , $ input );
121
125
122
126
// Update with new details
123
- $ page ->revision_count ++;
127
+ $ page ->pageData ->revision_count ++;
128
+ $ page ->pageData ->save ();
124
129
$ page ->save ();
125
130
126
131
// Remove all update drafts for this user and page.
@@ -144,7 +149,7 @@ public function update(Page $page, array $input): Page
144
149
protected function updateTemplateStatusAndContentFromInput (Page $ page , array $ input ): void
145
150
{
146
151
if (isset ($ input ['template ' ]) && userCan (Permission::TemplatesManage)) {
147
- $ page ->template = ($ input ['template ' ] === 'true ' );
152
+ $ page ->pageData -> template = ($ input ['template ' ] === 'true ' );
148
153
}
149
154
150
155
$ pageContent = new PageContent ($ page );
@@ -166,22 +171,22 @@ protected function updateTemplateStatusAndContentFromInput(Page $page, array $in
166
171
$ pageContent ->setNewHTML ($ input ['html ' ], user ());
167
172
}
168
173
169
- if (($ newEditor !== $ currentEditor || empty ($ page ->editor )) && userCan (Permission::EditorChange)) {
170
- $ page ->editor = $ newEditor ->value ;
171
- } elseif (empty ($ page ->editor )) {
172
- $ page ->editor = $ defaultEditor ->value ;
174
+ if (($ newEditor !== $ currentEditor || empty ($ page ->pageData -> editor )) && userCan (Permission::EditorChange)) {
175
+ $ page ->pageData -> editor = $ newEditor ->value ;
176
+ } elseif (empty ($ page ->pageData -> editor )) {
177
+ $ page ->pageData -> editor = $ defaultEditor ->value ;
173
178
}
174
179
}
175
180
176
181
/**
177
182
* Save a page update draft.
178
183
*/
179
- public function updatePageDraft (Page $ page , array $ input )
184
+ public function updatePageDraft (Page $ page , array $ input ): Page | PageRevision
180
185
{
181
- // If the page itself is a draft simply update that
186
+ // If the page itself is a draft, simply update that
182
187
if ($ page ->draft ) {
183
188
$ this ->updateTemplateStatusAndContentFromInput ($ page , $ input );
184
- $ page ->fill ( $ input );
189
+ $ page ->forceFill ( array_intersect_key ( $ input, array_flip ([ ' name ' ])))-> save ( );
185
190
$ page ->save ();
186
191
187
192
return $ page ;
@@ -209,7 +214,7 @@ public function updatePageDraft(Page $page, array $input)
209
214
*
210
215
* @throws Exception
211
216
*/
212
- public function destroy (Page $ page )
217
+ public function destroy (Page $ page ): void
213
218
{
214
219
$ this ->trashCan ->softDestroyPage ($ page );
215
220
Activity::add (ActivityType::PAGE_DELETE , $ page );
@@ -222,7 +227,7 @@ public function destroy(Page $page)
222
227
public function restoreRevision (Page $ page , int $ revisionId ): Page
223
228
{
224
229
$ oldUrl = $ page ->getUrl ();
225
- $ page ->revision_count ++;
230
+ $ page ->pageData -> revision_count ++;
226
231
227
232
/** @var PageRevision $revision */
228
233
$ revision = $ page ->revisions ()->where ('id ' , '= ' , $ revisionId )->first ();
@@ -238,6 +243,7 @@ public function restoreRevision(Page $page, int $revisionId): Page
238
243
239
244
$ page ->updated_by = user ()->id ;
240
245
$ page ->refreshSlug ();
246
+ $ page ->pageData ->save ();
241
247
$ page ->save ();
242
248
$ page ->indexForSearch ();
243
249
$ this ->referenceStore ->updateForEntity ($ page );
@@ -277,7 +283,7 @@ public function move(Page $page, string $parentIdentifier): Entity
277
283
}
278
284
279
285
return (new DatabaseTransaction (function () use ($ page , $ parent ) {
280
- $ page ->chapter_id = ($ parent instanceof Chapter) ? $ parent ->id : null ;
286
+ $ page ->pageData -> chapter_id = ($ parent instanceof Chapter) ? $ parent ->id : null ;
281
287
$ newBookId = ($ parent instanceof Chapter) ? $ parent ->book ->id : $ parent ->id ;
282
288
$ page ->changeBook ($ newBookId );
283
289
$ page ->rebuildPermissions ();
0 commit comments