66use BookStack \Entities \Models \Book ;
77use BookStack \Entities \Models \Chapter ;
88use BookStack \Entities \Models \Entity ;
9+ use BookStack \Entities \Models \EntityPageData ;
910use BookStack \Entities \Models \Page ;
1011use BookStack \Entities \Models \PageRevision ;
1112use BookStack \Entities \Queries \EntityQueries ;
@@ -37,34 +38,37 @@ public function __construct(
3738 /**
3839 * Get a new draft page belonging to the given parent entity.
3940 */
40- public function getNewDraftPage (Entity $ parent )
41+ public function getNewDraftPage (Entity $ parent ): Page
4142 {
4243 $ page = (new Page ())->forceFill ([
4344 'name ' => trans ('entities.pages_initial_name ' ),
4445 'created_by ' => user ()->id ,
4546 'owned_by ' => user ()->id ,
4647 'updated_by ' => user ()->id ,
48+ ]);
49+ $ pageData = (new EntityPageData ())->forceFill ([
4750 'draft ' => true ,
4851 'editor ' => PageEditorType::getSystemDefault ()->value ,
4952 ]);
5053
5154 if ($ parent instanceof Chapter) {
52- $ page ->chapter_id = $ parent ->id ;
55+ $ pageData ->chapter_id = $ parent ->id ;
5356 $ page ->book_id = $ parent ->book_id ;
5457 } else {
5558 $ page ->book_id = $ parent ->id ;
5659 }
5760
58- $ defaultTemplate = $ page ->chapter ->defaultTemplate ?? $ page ->book ->defaultTemplate ;
61+ $ defaultTemplate = $ page ->chapter ->containerData -> defaultTemplate ?? $ page ->book -> containerData ->defaultTemplate ;
5962 if ($ defaultTemplate && userCan (Permission::PageView, $ defaultTemplate )) {
60- $ page ->forceFill ([
63+ $ pageData ->forceFill ([
6164 'html ' => $ defaultTemplate ->html ,
6265 'markdown ' => $ defaultTemplate ->markdown ,
6366 ]);
6467 }
6568
66- (new DatabaseTransaction (function () use ($ page ) {
69+ (new DatabaseTransaction (function () use ($ page, $ pageData ) {
6770 $ page ->save ();
71+ $ page ->pageData ()->save ($ pageData );
6872 $ page ->refresh ()->rebuildPermissions ();
6973 }))->run ();
7074
@@ -77,9 +81,9 @@ public function getNewDraftPage(Entity $parent)
7781 public function publishDraft (Page $ draft , array $ input ): Page
7882 {
7983 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 );
8387 $ this ->updateTemplateStatusAndContentFromInput ($ draft , $ input );
8488 $ this ->baseRepo ->update ($ draft , $ input );
8589 $ draft ->rebuildPermissions ();
@@ -112,15 +116,16 @@ public function setContentFromInput(Page $page, array $input): void
112116 public function update (Page $ page , array $ input ): Page
113117 {
114118 // Hold the old details to compare later
115- $ oldHtml = $ page ->html ;
116119 $ oldName = $ page ->name ;
117- $ oldMarkdown = $ page ->markdown ;
120+ $ oldHtml = $ page ->pageData ->html ;
121+ $ oldMarkdown = $ page ->pageData ->markdown ;
118122
119123 $ this ->updateTemplateStatusAndContentFromInput ($ page , $ input );
120124 $ this ->baseRepo ->update ($ page , $ input );
121125
122126 // Update with new details
123- $ page ->revision_count ++;
127+ $ page ->pageData ->revision_count ++;
128+ $ page ->pageData ->save ();
124129 $ page ->save ();
125130
126131 // Remove all update drafts for this user and page.
@@ -144,7 +149,7 @@ public function update(Page $page, array $input): Page
144149 protected function updateTemplateStatusAndContentFromInput (Page $ page , array $ input ): void
145150 {
146151 if (isset ($ input ['template ' ]) && userCan (Permission::TemplatesManage)) {
147- $ page ->template = ($ input ['template ' ] === 'true ' );
152+ $ page ->pageData -> template = ($ input ['template ' ] === 'true ' );
148153 }
149154
150155 $ pageContent = new PageContent ($ page );
@@ -166,22 +171,22 @@ protected function updateTemplateStatusAndContentFromInput(Page $page, array $in
166171 $ pageContent ->setNewHTML ($ input ['html ' ], user ());
167172 }
168173
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 ;
173178 }
174179 }
175180
176181 /**
177182 * Save a page update draft.
178183 */
179- public function updatePageDraft (Page $ page , array $ input )
184+ public function updatePageDraft (Page $ page , array $ input ): Page | PageRevision
180185 {
181- // If the page itself is a draft simply update that
186+ // If the page itself is a draft, simply update that
182187 if ($ page ->draft ) {
183188 $ this ->updateTemplateStatusAndContentFromInput ($ page , $ input );
184- $ page ->fill ( $ input );
189+ $ page ->forceFill ( array_intersect_key ( $ input, array_flip ([ ' name ' ])))-> save ( );
185190 $ page ->save ();
186191
187192 return $ page ;
@@ -209,7 +214,7 @@ public function updatePageDraft(Page $page, array $input)
209214 *
210215 * @throws Exception
211216 */
212- public function destroy (Page $ page )
217+ public function destroy (Page $ page ): void
213218 {
214219 $ this ->trashCan ->softDestroyPage ($ page );
215220 Activity::add (ActivityType::PAGE_DELETE , $ page );
@@ -222,7 +227,7 @@ public function destroy(Page $page)
222227 public function restoreRevision (Page $ page , int $ revisionId ): Page
223228 {
224229 $ oldUrl = $ page ->getUrl ();
225- $ page ->revision_count ++;
230+ $ page ->pageData -> revision_count ++;
226231
227232 /** @var PageRevision $revision */
228233 $ revision = $ page ->revisions ()->where ('id ' , '= ' , $ revisionId )->first ();
@@ -238,6 +243,7 @@ public function restoreRevision(Page $page, int $revisionId): Page
238243
239244 $ page ->updated_by = user ()->id ;
240245 $ page ->refreshSlug ();
246+ $ page ->pageData ->save ();
241247 $ page ->save ();
242248 $ page ->indexForSearch ();
243249 $ this ->referenceStore ->updateForEntity ($ page );
@@ -277,7 +283,7 @@ public function move(Page $page, string $parentIdentifier): Entity
277283 }
278284
279285 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 ;
281287 $ newBookId = ($ parent instanceof Chapter) ? $ parent ->book ->id : $ parent ->id ;
282288 $ page ->changeBook ($ newBookId );
283289 $ page ->rebuildPermissions ();
0 commit comments