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;
27 protected $chapterRepo;
37 protected $imageService;
39 public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo, BookRepo $bookRepo, ImageService $imageService)
41 $this->pageRepo = $pageRepo;
42 $this->chapterRepo = $chapterRepo;
43 $this->bookRepo = $bookRepo;
44 $this->imageService = $imageService;
48 * Clone the given page into the given parent using the provided name.
50 public function clonePage(Page $original, Entity $parent, string $newName): Page
52 $copyPage = $this->pageRepo->getNewDraftPage($parent);
53 $pageData = $this->entityToInputData($original);
54 $pageData['name'] = $newName;
56 return $this->pageRepo->publishDraft($copyPage, $pageData);
60 * Clone the given page into the given parent using the provided name.
61 * Clones all child pages.
63 public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
65 $chapterDetails = $this->entityToInputData($original);
66 $chapterDetails['name'] = $newName;
68 $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
70 if (userCan('page-create', $copyChapter)) {
71 /** @var Page $page */
72 foreach ($original->getVisiblePages() as $page) {
73 $this->clonePage($page, $copyChapter, $page->name);
81 * Clone the given book.
82 * Clones all child chapters & pages.
84 public function cloneBook(Book $original, string $newName): Book
86 $bookDetails = $this->entityToInputData($original);
87 $bookDetails['name'] = $newName;
89 $copyBook = $this->bookRepo->create($bookDetails);
91 $directChildren = $original->getDirectChildren();
92 foreach ($directChildren as $child) {
93 if ($child instanceof Chapter && userCan('chapter-create', $copyBook)) {
94 $this->cloneChapter($child, $copyBook, $child->name);
97 if ($child instanceof Page && !$child->draft && userCan('page-create', $copyBook)) {
98 $this->clonePage($child, $copyBook, $child->name);
106 * Convert an entity to a raw data array of input data.
107 * @return array<string, mixed>
109 public function entityToInputData(Entity $entity): array
111 $inputData = $entity->getAttributes();
112 $inputData['tags'] = $this->entityTagsToInputArray($entity);
114 // Add a cover to the data if existing on the original entity
115 if ($entity->cover instanceof Image) {
116 $tmpImgFile = tmpfile();
117 $uploadedFile = $this->imageToUploadedFile($entity->cover, $tmpImgFile);
118 $inputData['image'] = $uploadedFile;
125 * Convert an image instance to an UploadedFile instance to mimic
126 * a file being uploaded.
128 protected function imageToUploadedFile(Image $image, &$tmpFile): ?UploadedFile
130 $imgData = $this->imageService->getImageData($image);
131 $tmpImgFilePath = stream_get_meta_data($tmpFile)['uri'];
132 file_put_contents($tmpImgFilePath, $imgData);
134 return new UploadedFile($tmpImgFilePath, basename($image->path));
138 * Convert the tags on the given entity to the raw format
139 * that's used for incoming request data.
141 protected function entityTagsToInputArray(Entity $entity): array
146 foreach ($entity->tags as $tag) {
147 $tags[] = ['name' => $tag->name, 'value' => $tag->value];