3 namespace BookStack\Entities\Tools;
5 use BookStack\Actions\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\Page;
11 use BookStack\Entities\Repos\BookRepo;
12 use BookStack\Entities\Repos\ChapterRepo;
13 use BookStack\Entities\Repos\PageRepo;
14 use BookStack\Uploads\Image;
15 use BookStack\Uploads\ImageService;
16 use Illuminate\Http\UploadedFile;
20 protected PageRepo $pageRepo;
21 protected ChapterRepo $chapterRepo;
22 protected BookRepo $bookRepo;
23 protected ImageService $imageService;
25 public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo, BookRepo $bookRepo, ImageService $imageService)
27 $this->pageRepo = $pageRepo;
28 $this->chapterRepo = $chapterRepo;
29 $this->bookRepo = $bookRepo;
30 $this->imageService = $imageService;
34 * Clone the given page into the given parent using the provided name.
36 public function clonePage(Page $original, Entity $parent, string $newName): Page
38 $copyPage = $this->pageRepo->getNewDraftPage($parent);
39 $pageData = $this->entityToInputData($original);
40 $pageData['name'] = $newName;
42 return $this->pageRepo->publishDraft($copyPage, $pageData);
46 * Clone the given page into the given parent using the provided name.
47 * Clones all child pages.
49 public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
51 $chapterDetails = $this->entityToInputData($original);
52 $chapterDetails['name'] = $newName;
54 $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
56 if (userCan('page-create', $copyChapter)) {
57 /** @var Page $page */
58 foreach ($original->getVisiblePages() as $page) {
59 $this->clonePage($page, $copyChapter, $page->name);
67 * Clone the given book.
68 * Clones all child chapters & pages.
70 public function cloneBook(Book $original, string $newName): Book
72 $bookDetails = $this->entityToInputData($original);
73 $bookDetails['name'] = $newName;
76 $copyBook = $this->bookRepo->create($bookDetails);
79 $directChildren = $original->getDirectChildren();
80 foreach ($directChildren as $child) {
81 if ($child instanceof Chapter && userCan('chapter-create', $copyBook)) {
82 $this->cloneChapter($child, $copyBook, $child->name);
85 if ($child instanceof Page && !$child->draft && userCan('page-create', $copyBook)) {
86 $this->clonePage($child, $copyBook, $child->name);
90 // Clone bookshelf relationships
91 /** @var Bookshelf $shelf */
92 foreach ($original->shelves as $shelf) {
93 if (userCan('bookshelf-update', $shelf)) {
94 $shelf->appendBook($copyBook);
102 * Convert an entity to a raw data array of input data.
104 * @return array<string, mixed>
106 public function entityToInputData(Entity $entity): array
108 $inputData = $entity->getAttributes();
109 $inputData['tags'] = $this->entityTagsToInputArray($entity);
111 // Add a cover to the data if existing on the original entity
112 if ($entity->cover instanceof Image) {
113 $uploadedFile = $this->imageToUploadedFile($entity->cover);
114 $inputData['image'] = $uploadedFile;
121 * Copy the permission settings from the source entity to the target entity.
123 public function copyEntityPermissions(Entity $sourceEntity, Entity $targetEntity): void
125 $permissions = $sourceEntity->permissions()->get(['role_id', 'view', 'create', 'update', 'delete'])->toArray();
126 $targetEntity->permissions()->delete();
127 $targetEntity->permissions()->createMany($permissions);
128 $targetEntity->rebuildPermissions();
132 * Convert an image instance to an UploadedFile instance to mimic
133 * a file being uploaded.
135 protected function imageToUploadedFile(Image $image): ?UploadedFile
137 $imgData = $this->imageService->getImageData($image);
138 $tmpImgFilePath = tempnam(sys_get_temp_dir(), 'bs_cover_clone_');
139 file_put_contents($tmpImgFilePath, $imgData);
141 return new UploadedFile($tmpImgFilePath, basename($image->path));
145 * Convert the tags on the given entity to the raw format
146 * that's used for incoming request data.
148 protected function entityTagsToInputArray(Entity $entity): array
153 foreach ($entity->tags as $tag) {
154 $tags[] = ['name' => $tag->name, 'value' => $tag->value];