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;
28 protected $chapterRepo;
38 protected $imageService;
40 public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo, BookRepo $bookRepo, ImageService $imageService)
42 $this->pageRepo = $pageRepo;
43 $this->chapterRepo = $chapterRepo;
44 $this->bookRepo = $bookRepo;
45 $this->imageService = $imageService;
49 * Clone the given page into the given parent using the provided name.
51 public function clonePage(Page $original, Entity $parent, string $newName): Page
53 $copyPage = $this->pageRepo->getNewDraftPage($parent);
54 $pageData = $original->getAttributes();
57 $pageData['name'] = $newName;
58 $pageData['tags'] = $this->entityTagsToInputArray($original);
60 return $this->pageRepo->publishDraft($copyPage, $pageData);
64 * Clone the given page into the given parent using the provided name.
65 * Clones all child pages.
67 public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
69 $chapterDetails = $original->getAttributes();
70 $chapterDetails['name'] = $newName;
71 $chapterDetails['tags'] = $this->entityTagsToInputArray($original);
73 $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
75 if (userCan('page-create', $copyChapter)) {
76 /** @var Page $page */
77 foreach ($original->getVisiblePages() as $page) {
78 $this->clonePage($page, $copyChapter, $page->name);
86 * Clone the given book.
87 * Clones all child chapters & pages.
89 public function cloneBook(Book $original, string $newName): Book
91 $bookDetails = $original->getAttributes();
92 $bookDetails['name'] = $newName;
93 $bookDetails['tags'] = $this->entityTagsToInputArray($original);
95 $copyBook = $this->bookRepo->create($bookDetails);
97 $directChildren = $original->getDirectChildren();
98 foreach ($directChildren as $child) {
100 if ($child instanceof Chapter && userCan('chapter-create', $copyBook)) {
101 $this->cloneChapter($child, $copyBook, $child->name);
104 if ($child instanceof Page && !$child->draft && userCan('page-create', $copyBook)) {
105 $this->clonePage($child, $copyBook, $child->name);
109 if ($original->cover) {
111 $tmpImgFile = tmpfile();
112 $uploadedFile = $this->imageToUploadedFile($original->cover, $tmpImgFile);
113 $this->bookRepo->updateCoverImage($copyBook, $uploadedFile, false);
114 } catch (\Exception $exception) {
122 * Convert an image instance to an UploadedFile instance to mimic
123 * a file being uploaded.
125 protected function imageToUploadedFile(Image $image, &$tmpFile): ?UploadedFile
127 $imgData = $this->imageService->getImageData($image);
128 $tmpImgFilePath = stream_get_meta_data($tmpFile)['uri'];
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];