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 protected PageRepo $pageRepo;
22 protected ChapterRepo $chapterRepo;
23 protected BookRepo $bookRepo;
24 protected ImageService $imageService;
26 public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo, BookRepo $bookRepo, ImageService $imageService)
28 $this->pageRepo = $pageRepo;
29 $this->chapterRepo = $chapterRepo;
30 $this->bookRepo = $bookRepo;
31 $this->imageService = $imageService;
35 * Clone the given page into the given parent using the provided name.
37 public function clonePage(Page $original, Entity $parent, string $newName): Page
39 $copyPage = $this->pageRepo->getNewDraftPage($parent);
40 $pageData = $this->entityToInputData($original);
41 $pageData['name'] = $newName;
43 return $this->pageRepo->publishDraft($copyPage, $pageData);
47 * Clone the given page into the given parent using the provided name.
48 * Clones all child pages.
50 public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
52 $chapterDetails = $this->entityToInputData($original);
53 $chapterDetails['name'] = $newName;
55 $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
57 if (userCan('page-create', $copyChapter)) {
58 /** @var Page $page */
59 foreach ($original->getVisiblePages() as $page) {
60 $this->clonePage($page, $copyChapter, $page->name);
68 * Clone the given book.
69 * Clones all child chapters & pages.
71 public function cloneBook(Book $original, string $newName): Book
73 $bookDetails = $this->entityToInputData($original);
74 $bookDetails['name'] = $newName;
77 $copyBook = $this->bookRepo->create($bookDetails);
80 $directChildren = $original->getDirectVisibleChildren();
81 foreach ($directChildren as $child) {
82 if ($child instanceof Chapter && userCan('chapter-create', $copyBook)) {
83 $this->cloneChapter($child, $copyBook, $child->name);
86 if ($child instanceof Page && !$child->draft && userCan('page-create', $copyBook)) {
87 $this->clonePage($child, $copyBook, $child->name);
91 // Clone bookshelf relationships
92 /** @var Bookshelf $shelf */
93 foreach ($original->shelves as $shelf) {
94 if (userCan('bookshelf-update', $shelf)) {
95 $shelf->appendBook($copyBook);
103 * Convert an entity to a raw data array of input data.
105 * @return array<string, mixed>
107 public function entityToInputData(Entity $entity): array
109 $inputData = $entity->getAttributes();
110 $inputData['tags'] = $this->entityTagsToInputArray($entity);
112 // Add a cover to the data if existing on the original entity
113 if ($entity instanceof HasCoverImage) {
114 $cover = $entity->cover()->first();
116 $inputData['image'] = $this->imageToUploadedFile($cover);
124 * Copy the permission settings from the source entity to the target entity.
126 public function copyEntityPermissions(Entity $sourceEntity, Entity $targetEntity): void
128 $permissions = $sourceEntity->permissions()->get(['role_id', 'view', 'create', 'update', 'delete'])->toArray();
129 $targetEntity->permissions()->delete();
130 $targetEntity->permissions()->createMany($permissions);
131 $targetEntity->rebuildPermissions();
135 * Convert an image instance to an UploadedFile instance to mimic
136 * a file being uploaded.
138 protected function imageToUploadedFile(Image $image): ?UploadedFile
140 $imgData = $this->imageService->getImageData($image);
141 $tmpImgFilePath = tempnam(sys_get_temp_dir(), 'bs_cover_clone_');
142 file_put_contents($tmpImgFilePath, $imgData);
144 return new UploadedFile($tmpImgFilePath, basename($image->path));
148 * Convert the tags on the given entity to the raw format
149 * that's used for incoming request data.
151 protected function entityTagsToInputArray(Entity $entity): array
156 foreach ($entity->tags as $tag) {
157 $tags[] = ['name' => $tag->name, 'value' => $tag->value];