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\HasCoverImage;
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\Uploads\Image;
16 use BookStack\Uploads\ImageService;
17 use Illuminate\Http\UploadedFile;
21 public function __construct(
22 protected PageRepo $pageRepo,
23 protected ChapterRepo $chapterRepo,
24 protected BookRepo $bookRepo,
25 protected ImageService $imageService,
30 * Clone the given page into the given parent using the provided name.
32 public function clonePage(Page $original, Entity $parent, string $newName): Page
34 $copyPage = $this->pageRepo->getNewDraftPage($parent);
35 $pageData = $this->entityToInputData($original);
36 $pageData['name'] = $newName;
38 return $this->pageRepo->publishDraft($copyPage, $pageData);
42 * Clone the given page into the given parent using the provided name.
43 * Clones all child pages.
45 public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
47 $chapterDetails = $this->entityToInputData($original);
48 $chapterDetails['name'] = $newName;
50 $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
52 if (userCan('page-create', $copyChapter)) {
53 /** @var Page $page */
54 foreach ($original->getVisiblePages() as $page) {
55 $this->clonePage($page, $copyChapter, $page->name);
63 * Clone the given book.
64 * Clones all child chapters & pages.
66 public function cloneBook(Book $original, string $newName): Book
68 $bookDetails = $this->entityToInputData($original);
69 $bookDetails['name'] = $newName;
72 $copyBook = $this->bookRepo->create($bookDetails);
75 $directChildren = $original->getDirectVisibleChildren();
76 foreach ($directChildren as $child) {
77 if ($child instanceof Chapter && userCan('chapter-create', $copyBook)) {
78 $this->cloneChapter($child, $copyBook, $child->name);
81 if ($child instanceof Page && !$child->draft && userCan('page-create', $copyBook)) {
82 $this->clonePage($child, $copyBook, $child->name);
86 // Clone bookshelf relationships
87 /** @var Bookshelf $shelf */
88 foreach ($original->shelves as $shelf) {
89 if (userCan('bookshelf-update', $shelf)) {
90 $shelf->appendBook($copyBook);
98 * Convert an entity to a raw data array of input data.
100 * @return array<string, mixed>
102 public function entityToInputData(Entity $entity): array
104 $inputData = $entity->getAttributes();
105 $inputData['tags'] = $this->entityTagsToInputArray($entity);
107 // Add a cover to the data if existing on the original entity
108 if ($entity instanceof HasCoverImage) {
109 $cover = $entity->cover()->first();
111 $inputData['image'] = $this->imageToUploadedFile($cover);
119 * Copy the permission settings from the source entity to the target entity.
121 public function copyEntityPermissions(Entity $sourceEntity, Entity $targetEntity): void
123 $permissions = $sourceEntity->permissions()->get(['role_id', 'view', 'create', 'update', 'delete'])->toArray();
124 $targetEntity->permissions()->delete();
125 $targetEntity->permissions()->createMany($permissions);
126 $targetEntity->rebuildPermissions();
130 * Convert an image instance to an UploadedFile instance to mimic
131 * a file being uploaded.
133 protected function imageToUploadedFile(Image $image): ?UploadedFile
135 $imgData = $this->imageService->getImageData($image);
136 $tmpImgFilePath = tempnam(sys_get_temp_dir(), 'bs_cover_clone_');
137 file_put_contents($tmpImgFilePath, $imgData);
139 return new UploadedFile($tmpImgFilePath, basename($image->path));
143 * Convert the tags on the given entity to the raw format
144 * that's used for incoming request data.
146 protected function entityTagsToInputArray(Entity $entity): array
151 foreach ($entity->tags as $tag) {
152 $tags[] = ['name' => $tag->name, 'value' => $tag->value];