3 namespace BookStack\Entities\Tools;
5 use BookStack\Activity\Models\Tag;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\HasCoverInterface;
10 use BookStack\Entities\Models\Entity;
11 use BookStack\Entities\Models\Page;
12 use BookStack\Entities\Repos\BookRepo;
13 use BookStack\Entities\Repos\ChapterRepo;
14 use BookStack\Entities\Repos\PageRepo;
15 use BookStack\Permissions\Permission;
16 use BookStack\References\ReferenceChangeContext;
17 use BookStack\References\ReferenceUpdater;
18 use BookStack\Uploads\Image;
19 use BookStack\Uploads\ImageService;
20 use Illuminate\Http\UploadedFile;
24 protected ReferenceChangeContext $referenceChangeContext;
26 public function __construct(
27 protected PageRepo $pageRepo,
28 protected ChapterRepo $chapterRepo,
29 protected BookRepo $bookRepo,
30 protected ImageService $imageService,
31 protected ReferenceUpdater $referenceUpdater,
33 $this->referenceChangeContext = new ReferenceChangeContext();
37 * Clone the given page into the given parent using the provided name.
39 public function clonePage(Page $original, Entity $parent, string $newName): Page
41 $context = $this->newReferenceChangeContext();
42 $page = $this->createPageClone($original, $parent, $newName);
43 $this->referenceUpdater->changeReferencesUsingContext($context);
47 protected function createPageClone(Page $original, Entity $parent, string $newName): Page
49 $copyPage = $this->pageRepo->getNewDraftPage($parent);
50 $pageData = $this->entityToInputData($original);
51 $pageData['name'] = $newName;
53 $newPage = $this->pageRepo->publishDraft($copyPage, $pageData);
54 $this->referenceChangeContext->add($original, $newPage);
60 * Clone the given page into the given parent using the provided name.
61 * Clones all child pages.
63 public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
65 $context = $this->newReferenceChangeContext();
66 $chapter = $this->createChapterClone($original, $parent, $newName);
67 $this->referenceUpdater->changeReferencesUsingContext($context);
71 protected function createChapterClone(Chapter $original, Book $parent, string $newName): Chapter
73 $chapterDetails = $this->entityToInputData($original);
74 $chapterDetails['name'] = $newName;
76 $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
78 if (userCan(Permission::PageCreate, $copyChapter)) {
79 /** @var Page $page */
80 foreach ($original->getVisiblePages() as $page) {
81 $this->createPageClone($page, $copyChapter, $page->name);
85 $this->referenceChangeContext->add($original, $copyChapter);
91 * Clone the given book.
92 * Clones all child chapters and pages.
94 public function cloneBook(Book $original, string $newName): Book
96 $context = $this->newReferenceChangeContext();
97 $book = $this->createBookClone($original, $newName);
98 $this->referenceUpdater->changeReferencesUsingContext($context);
102 protected function createBookClone(Book $original, string $newName): Book
104 $bookDetails = $this->entityToInputData($original);
105 $bookDetails['name'] = $newName;
108 $copyBook = $this->bookRepo->create($bookDetails);
111 $directChildren = $original->getDirectVisibleChildren();
112 foreach ($directChildren as $child) {
113 if ($child instanceof Chapter && userCan(Permission::ChapterCreate, $copyBook)) {
114 $this->createChapterClone($child, $copyBook, $child->name);
117 if ($child instanceof Page && !$child->draft && userCan(Permission::PageCreate, $copyBook)) {
118 $this->createPageClone($child, $copyBook, $child->name);
122 // Clone bookshelf relationships
123 /** @var Bookshelf $shelf */
124 foreach ($original->shelves as $shelf) {
125 if (userCan(Permission::BookshelfUpdate, $shelf)) {
126 $shelf->appendBook($copyBook);
130 $this->referenceChangeContext->add($original, $copyBook);
136 * Convert an entity to a raw data array of input data.
138 * @return array<string, mixed>
140 public function entityToInputData(Entity $entity): array
142 $inputData = $entity->getAttributes();
143 $inputData['tags'] = $this->entityTagsToInputArray($entity);
145 // Add a cover to the data if existing on the original entity
146 if ($entity instanceof HasCoverInterface) {
147 $cover = $entity->coverInfo()->getImage();
149 $inputData['image'] = $this->imageToUploadedFile($cover);
157 * Copy the permission settings from the source entity to the target entity.
159 public function copyEntityPermissions(Entity $sourceEntity, Entity $targetEntity): void
161 $permissions = $sourceEntity->permissions()->get(['role_id', 'view', 'create', 'update', 'delete'])->toArray();
162 $targetEntity->permissions()->delete();
163 $targetEntity->permissions()->createMany($permissions);
164 $targetEntity->rebuildPermissions();
168 * Convert an image instance to an UploadedFile instance to mimic
169 * a file being uploaded.
171 protected function imageToUploadedFile(Image $image): ?UploadedFile
173 $imgData = $this->imageService->getImageData($image);
174 $tmpImgFilePath = tempnam(sys_get_temp_dir(), 'bs_cover_clone_');
175 file_put_contents($tmpImgFilePath, $imgData);
177 return new UploadedFile($tmpImgFilePath, basename($image->path));
181 * Convert the tags on the given entity to the raw format
182 * that's used for incoming request data.
184 protected function entityTagsToInputArray(Entity $entity): array
189 foreach ($entity->tags as $tag) {
190 $tags[] = ['name' => $tag->name, 'value' => $tag->value];
196 protected function newReferenceChangeContext(): ReferenceChangeContext
198 $this->referenceChangeContext = new ReferenceChangeContext();
199 return $this->referenceChangeContext;