use BookStack\Entities\Repos\ChapterRepo;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Permissions\Permission;
+use BookStack\References\ReferenceChangeContext;
+use BookStack\References\ReferenceUpdater;
use BookStack\Uploads\Image;
use BookStack\Uploads\ImageService;
use Illuminate\Http\UploadedFile;
class Cloner
{
+ protected ReferenceChangeContext $referenceChangeContext;
+
public function __construct(
protected PageRepo $pageRepo,
protected ChapterRepo $chapterRepo,
protected BookRepo $bookRepo,
protected ImageService $imageService,
+ protected ReferenceUpdater $referenceUpdater,
) {
+ $this->referenceChangeContext = new ReferenceChangeContext();
}
/**
* Clone the given page into the given parent using the provided name.
*/
public function clonePage(Page $original, Entity $parent, string $newName): Page
+ {
+ $context = $this->newReferenceChangeContext();
+ $page = $this->createPageClone($original, $parent, $newName);
+ $this->referenceUpdater->changeReferencesUsingContext($context);
+ return $page;
+ }
+
+ protected function createPageClone(Page $original, Entity $parent, string $newName): Page
{
$copyPage = $this->pageRepo->getNewDraftPage($parent);
$pageData = $this->entityToInputData($original);
$pageData['name'] = $newName;
- return $this->pageRepo->publishDraft($copyPage, $pageData);
+ $newPage = $this->pageRepo->publishDraft($copyPage, $pageData);
+ $this->referenceChangeContext->add($original, $newPage);
+
+ return $newPage;
}
/**
* Clones all child pages.
*/
public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
+ {
+ $context = $this->newReferenceChangeContext();
+ $chapter = $this->createChapterClone($original, $parent, $newName);
+ $this->referenceUpdater->changeReferencesUsingContext($context);
+ return $chapter;
+ }
+
+ protected function createChapterClone(Chapter $original, Book $parent, string $newName): Chapter
{
$chapterDetails = $this->entityToInputData($original);
$chapterDetails['name'] = $newName;
* Clones all child chapters and pages.
*/
public function cloneBook(Book $original, string $newName): Book
+ {
+ $context = $this->newReferenceChangeContext();
+ $book = $this->createBookClone($original, $newName);
+ $this->referenceUpdater->changeReferencesUsingContext($context);
+ return $book;
+ }
+
+ protected function createBookClone(Book $original, string $newName): Book
{
$bookDetails = $this->entityToInputData($original);
$bookDetails['name'] = $newName;
return $tags;
}
+
+ protected function newReferenceChangeContext(): ReferenceChangeContext
+ {
+ $this->referenceChangeContext = new ReferenceChangeContext();
+ return $this->referenceChangeContext;
+ }
}