3 namespace BookStack\Entities\Tools;
5 use BookStack\Actions\Tag;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Entities\Repos\BookRepo;
11 use BookStack\Entities\Repos\ChapterRepo;
12 use BookStack\Entities\Repos\PageRepo;
13 use BookStack\Uploads\Image;
14 use BookStack\Uploads\ImageService;
15 use Illuminate\Http\UploadedFile;
19 protected PageRepo $pageRepo;
20 protected ChapterRepo $chapterRepo;
21 protected BookRepo $bookRepo;
22 protected ImageService $imageService;
24 public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo, BookRepo $bookRepo, ImageService $imageService)
26 $this->pageRepo = $pageRepo;
27 $this->chapterRepo = $chapterRepo;
28 $this->bookRepo = $bookRepo;
29 $this->imageService = $imageService;
33 * Clone the given page into the given parent using the provided name.
35 public function clonePage(Page $original, Entity $parent, string $newName): Page
37 $copyPage = $this->pageRepo->getNewDraftPage($parent);
38 $pageData = $this->entityToInputData($original);
39 $pageData['name'] = $newName;
41 return $this->pageRepo->publishDraft($copyPage, $pageData);
45 * Clone the given page into the given parent using the provided name.
46 * Clones all child pages.
48 public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
50 $chapterDetails = $this->entityToInputData($original);
51 $chapterDetails['name'] = $newName;
53 $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
55 if (userCan('page-create', $copyChapter)) {
56 /** @var Page $page */
57 foreach ($original->getVisiblePages() as $page) {
58 $this->clonePage($page, $copyChapter, $page->name);
66 * Clone the given book.
67 * Clones all child chapters & pages.
69 public function cloneBook(Book $original, string $newName): Book
71 $bookDetails = $this->entityToInputData($original);
72 $bookDetails['name'] = $newName;
74 $copyBook = $this->bookRepo->create($bookDetails);
76 $directChildren = $original->getDirectChildren();
77 foreach ($directChildren as $child) {
78 if ($child instanceof Chapter && userCan('chapter-create', $copyBook)) {
79 $this->cloneChapter($child, $copyBook, $child->name);
82 if ($child instanceof Page && !$child->draft && userCan('page-create', $copyBook)) {
83 $this->clonePage($child, $copyBook, $child->name);
91 * Convert an entity to a raw data array of input data.
93 * @return array<string, mixed>
95 public function entityToInputData(Entity $entity): array
97 $inputData = $entity->getAttributes();
98 $inputData['tags'] = $this->entityTagsToInputArray($entity);
100 // Add a cover to the data if existing on the original entity
101 if ($entity->cover instanceof Image) {
102 $uploadedFile = $this->imageToUploadedFile($entity->cover);
103 $inputData['image'] = $uploadedFile;
110 * Copy the permission settings from the source entity to the target entity.
112 public function copyEntityPermissions(Entity $sourceEntity, Entity $targetEntity): void
114 $targetEntity->restricted = $sourceEntity->restricted;
115 $permissions = $sourceEntity->permissions()->get(['role_id', 'action'])->toArray();
116 $targetEntity->permissions()->delete();
117 $targetEntity->permissions()->createMany($permissions);
118 $targetEntity->rebuildPermissions();
122 * Convert an image instance to an UploadedFile instance to mimic
123 * a file being uploaded.
125 protected function imageToUploadedFile(Image $image): ?UploadedFile
127 $imgData = $this->imageService->getImageData($image);
128 $tmpImgFilePath = tempnam(sys_get_temp_dir(), 'bs_cover_clone_');
129 file_put_contents($tmpImgFilePath, $imgData);
131 return new UploadedFile($tmpImgFilePath, basename($image->path));
135 * Convert the tags on the given entity to the raw format
136 * that's used for incoming request data.
138 protected function entityTagsToInputArray(Entity $entity): array
143 foreach ($entity->tags as $tag) {
144 $tags[] = ['name' => $tag->name, 'value' => $tag->value];