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 = $original->getAttributes();
56 $pageData['name'] = $newName;
57 $pageData['tags'] = $this->entityTagsToInputArray($original);
59 return $this->pageRepo->publishDraft($copyPage, $pageData);
63 * Clone the given page into the given parent using the provided name.
64 * Clones all child pages.
66 public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
68 $chapterDetails = $original->getAttributes();
69 $chapterDetails['name'] = $newName;
70 $chapterDetails['tags'] = $this->entityTagsToInputArray($original);
72 $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
74 if (userCan('page-create', $copyChapter)) {
75 /** @var Page $page */
76 foreach ($original->getVisiblePages() as $page) {
77 $this->clonePage($page, $copyChapter, $page->name);
85 * Clone the given book.
86 * Clones all child chapters & pages.
88 public function cloneBook(Book $original, string $newName): Book
90 $bookDetails = $original->getAttributes();
91 $bookDetails['name'] = $newName;
92 $bookDetails['tags'] = $this->entityTagsToInputArray($original);
94 $copyBook = $this->bookRepo->create($bookDetails);
96 $directChildren = $original->getDirectChildren();
97 foreach ($directChildren as $child) {
98 if ($child instanceof Chapter && userCan('chapter-create', $copyBook)) {
99 $this->cloneChapter($child, $copyBook, $child->name);
102 if ($child instanceof Page && !$child->draft && userCan('page-create', $copyBook)) {
103 $this->clonePage($child, $copyBook, $child->name);
107 if ($original->cover) {
109 $tmpImgFile = tmpfile();
110 $uploadedFile = $this->imageToUploadedFile($original->cover, $tmpImgFile);
111 $this->bookRepo->updateCoverImage($copyBook, $uploadedFile, false);
112 } catch (\Exception $exception) {
120 * Convert an image instance to an UploadedFile instance to mimic
121 * a file being uploaded.
123 protected function imageToUploadedFile(Image $image, &$tmpFile): ?UploadedFile
125 $imgData = $this->imageService->getImageData($image);
126 $tmpImgFilePath = stream_get_meta_data($tmpFile)['uri'];
127 file_put_contents($tmpImgFilePath, $imgData);
129 return new UploadedFile($tmpImgFilePath, basename($image->path));
133 * Convert the tags on the given entity to the raw format
134 * that's used for incoming request data.
136 protected function entityTagsToInputArray(Entity $entity): array
141 foreach ($entity->tags as $tag) {
142 $tags[] = ['name' => $tag->name, 'value' => $tag->value];