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\Entity;
10 use BookStack\Entities\Models\CoverImageInterface;
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\Uploads\Image;
17 use BookStack\Uploads\ImageService;
18 use Illuminate\Http\UploadedFile;
22 public function __construct(
23 protected PageRepo $pageRepo,
24 protected ChapterRepo $chapterRepo,
25 protected BookRepo $bookRepo,
26 protected ImageService $imageService,
31 * Clone the given page into the given parent using the provided name.
33 public function clonePage(Page $original, Entity $parent, string $newName): Page
35 $copyPage = $this->pageRepo->getNewDraftPage($parent);
36 $pageData = $this->entityToInputData($original);
37 $pageData['name'] = $newName;
39 return $this->pageRepo->publishDraft($copyPage, $pageData);
43 * Clone the given page into the given parent using the provided name.
44 * Clones all child pages.
46 public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
48 $chapterDetails = $this->entityToInputData($original);
49 $chapterDetails['name'] = $newName;
51 $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
53 if (userCan(Permission::PageCreate, $copyChapter)) {
54 /** @var Page $page */
55 foreach ($original->getVisiblePages() as $page) {
56 $this->clonePage($page, $copyChapter, $page->name);
64 * Clone the given book.
65 * Clones all child chapters and pages.
67 public function cloneBook(Book $original, string $newName): Book
69 $bookDetails = $this->entityToInputData($original);
70 $bookDetails['name'] = $newName;
73 $copyBook = $this->bookRepo->create($bookDetails);
76 $directChildren = $original->getDirectVisibleChildren();
77 foreach ($directChildren as $child) {
78 if ($child instanceof Chapter && userCan(Permission::ChapterCreate, $copyBook)) {
79 $this->cloneChapter($child, $copyBook, $child->name);
82 if ($child instanceof Page && !$child->draft && userCan(Permission::PageCreate, $copyBook)) {
83 $this->clonePage($child, $copyBook, $child->name);
87 // Clone bookshelf relationships
88 /** @var Bookshelf $shelf */
89 foreach ($original->shelves as $shelf) {
90 if (userCan(Permission::BookshelfUpdate, $shelf)) {
91 $shelf->appendBook($copyBook);
99 * Convert an entity to a raw data array of input data.
101 * @return array<string, mixed>
103 public function entityToInputData(Entity $entity): array
105 $inputData = $entity->getAttributes();
106 $inputData['tags'] = $this->entityTagsToInputArray($entity);
108 // Add a cover to the data if existing on the original entity
109 if ($entity instanceof CoverImageInterface) {
110 $cover = $entity->cover()->first();
112 $inputData['image'] = $this->imageToUploadedFile($cover);
120 * Copy the permission settings from the source entity to the target entity.
122 public function copyEntityPermissions(Entity $sourceEntity, Entity $targetEntity): void
124 $permissions = $sourceEntity->permissions()->get(['role_id', 'view', 'create', 'update', 'delete'])->toArray();
125 $targetEntity->permissions()->delete();
126 $targetEntity->permissions()->createMany($permissions);
127 $targetEntity->rebuildPermissions();
131 * Convert an image instance to an UploadedFile instance to mimic
132 * a file being uploaded.
134 protected function imageToUploadedFile(Image $image): ?UploadedFile
136 $imgData = $this->imageService->getImageData($image);
137 $tmpImgFilePath = tempnam(sys_get_temp_dir(), 'bs_cover_clone_');
138 file_put_contents($tmpImgFilePath, $imgData);
140 return new UploadedFile($tmpImgFilePath, basename($image->path));
144 * Convert the tags on the given entity to the raw format
145 * that's used for incoming request data.
147 protected function entityTagsToInputArray(Entity $entity): array
152 foreach ($entity->tags as $tag) {
153 $tags[] = ['name' => $tag->name, 'value' => $tag->value];