1313use BookStack \Entities \Repos \ChapterRepo ;
1414use BookStack \Entities \Repos \PageRepo ;
1515use BookStack \Permissions \Permission ;
16+ use BookStack \References \ReferenceChangeContext ;
17+ use BookStack \References \ReferenceUpdater ;
1618use BookStack \Uploads \Image ;
1719use BookStack \Uploads \ImageService ;
1820use Illuminate \Http \UploadedFile ;
1921
2022class Cloner
2123{
24+ protected ReferenceChangeContext $ referenceChangeContext ;
25+
2226 public function __construct (
2327 protected PageRepo $ pageRepo ,
2428 protected ChapterRepo $ chapterRepo ,
2529 protected BookRepo $ bookRepo ,
2630 protected ImageService $ imageService ,
31+ protected ReferenceUpdater $ referenceUpdater ,
2732 ) {
33+ $ this ->referenceChangeContext = new ReferenceChangeContext ();
2834 }
2935
3036 /**
3137 * Clone the given page into the given parent using the provided name.
3238 */
3339 public function clonePage (Page $ original , Entity $ parent , string $ newName ): Page
40+ {
41+ $ context = $ this ->newReferenceChangeContext ();
42+ $ page = $ this ->createPageClone ($ original , $ parent , $ newName );
43+ $ this ->referenceUpdater ->changeReferencesUsingContext ($ context );
44+ return $ page ;
45+ }
46+
47+ protected function createPageClone (Page $ original , Entity $ parent , string $ newName ): Page
3448 {
3549 $ copyPage = $ this ->pageRepo ->getNewDraftPage ($ parent );
3650 $ pageData = $ this ->entityToInputData ($ original );
3751 $ pageData ['name ' ] = $ newName ;
3852
39- return $ this ->pageRepo ->publishDraft ($ copyPage , $ pageData );
53+ $ newPage = $ this ->pageRepo ->publishDraft ($ copyPage , $ pageData );
54+ $ this ->referenceChangeContext ->add ($ original , $ newPage );
55+
56+ return $ newPage ;
4057 }
4158
4259 /**
4360 * Clone the given page into the given parent using the provided name.
4461 * Clones all child pages.
4562 */
4663 public function cloneChapter (Chapter $ original , Book $ parent , string $ newName ): Chapter
64+ {
65+ $ context = $ this ->newReferenceChangeContext ();
66+ $ chapter = $ this ->createChapterClone ($ original , $ parent , $ newName );
67+ $ this ->referenceUpdater ->changeReferencesUsingContext ($ context );
68+ return $ chapter ;
69+ }
70+
71+ protected function createChapterClone (Chapter $ original , Book $ parent , string $ newName ): Chapter
4772 {
4873 $ chapterDetails = $ this ->entityToInputData ($ original );
4974 $ chapterDetails ['name ' ] = $ newName ;
@@ -65,6 +90,14 @@ public function cloneChapter(Chapter $original, Book $parent, string $newName):
6590 * Clones all child chapters and pages.
6691 */
6792 public function cloneBook (Book $ original , string $ newName ): Book
93+ {
94+ $ context = $ this ->newReferenceChangeContext ();
95+ $ book = $ this ->createBookClone ($ original , $ newName );
96+ $ this ->referenceUpdater ->changeReferencesUsingContext ($ context );
97+ return $ book ;
98+ }
99+
100+ protected function createBookClone (Book $ original , string $ newName ): Book
68101 {
69102 $ bookDetails = $ this ->entityToInputData ($ original );
70103 $ bookDetails ['name ' ] = $ newName ;
@@ -155,4 +188,10 @@ protected function entityTagsToInputArray(Entity $entity): array
155188
156189 return $ tags ;
157190 }
191+
192+ protected function newReferenceChangeContext (): ReferenceChangeContext
193+ {
194+ $ this ->referenceChangeContext = new ReferenceChangeContext ();
195+ return $ this ->referenceChangeContext ;
196+ }
158197}
0 commit comments