]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/Cloner.php
3553a9db3cd569dbd9030439512ba4beb6737fb6
[bookstack] / app / Entities / Tools / Cloner.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
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;
16
17 class Cloner
18 {
19     /**
20      * @var PageRepo
21      */
22     protected $pageRepo;
23
24     /**
25      * @var ChapterRepo
26      */
27     protected $chapterRepo;
28
29     /**
30      * @var BookRepo
31      */
32     protected $bookRepo;
33
34     /**
35      * @var ImageService
36      */
37     protected $imageService;
38
39     public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo, BookRepo $bookRepo, ImageService $imageService)
40     {
41         $this->pageRepo = $pageRepo;
42         $this->chapterRepo = $chapterRepo;
43         $this->bookRepo = $bookRepo;
44         $this->imageService = $imageService;
45     }
46
47     /**
48      * Clone the given page into the given parent using the provided name.
49      */
50     public function clonePage(Page $original, Entity $parent, string $newName): Page
51     {
52         $copyPage = $this->pageRepo->getNewDraftPage($parent);
53         $pageData = $this->entityToInputData($original);
54         $pageData['name'] = $newName;
55
56         return $this->pageRepo->publishDraft($copyPage, $pageData);
57     }
58
59     /**
60      * Clone the given page into the given parent using the provided name.
61      * Clones all child pages.
62      */
63     public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
64     {
65         $chapterDetails = $this->entityToInputData($original);
66         $chapterDetails['name'] = $newName;
67
68         $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
69
70         if (userCan('page-create', $copyChapter)) {
71             /** @var Page $page */
72             foreach ($original->getVisiblePages() as $page) {
73                 $this->clonePage($page, $copyChapter, $page->name);
74             }
75         }
76
77         return $copyChapter;
78     }
79
80     /**
81      * Clone the given book.
82      * Clones all child chapters & pages.
83      */
84     public function cloneBook(Book $original, string $newName): Book
85     {
86         $bookDetails = $this->entityToInputData($original);
87         $bookDetails['name'] = $newName;
88
89         $copyBook = $this->bookRepo->create($bookDetails);
90
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);
95             }
96
97             if ($child instanceof Page && !$child->draft && userCan('page-create', $copyBook)) {
98                 $this->clonePage($child, $copyBook, $child->name);
99             }
100         }
101
102         return $copyBook;
103     }
104
105     /**
106      * Convert an entity to a raw data array of input data.
107      * @return array<string, mixed>
108      */
109     public function entityToInputData(Entity $entity): array
110     {
111         $inputData = $entity->getAttributes();
112         $inputData['tags'] = $this->entityTagsToInputArray($entity);
113
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;
119         }
120
121         return $inputData;
122     }
123
124     /**
125      * Convert an image instance to an UploadedFile instance to mimic
126      * a file being uploaded.
127      */
128     protected function imageToUploadedFile(Image $image, &$tmpFile): ?UploadedFile
129     {
130         $imgData = $this->imageService->getImageData($image);
131         $tmpImgFilePath = stream_get_meta_data($tmpFile)['uri'];
132         file_put_contents($tmpImgFilePath, $imgData);
133
134         return new UploadedFile($tmpImgFilePath, basename($image->path));
135     }
136
137     /**
138      * Convert the tags on the given entity to the raw format
139      * that's used for incoming request data.
140      */
141     protected function entityTagsToInputArray(Entity $entity): array
142     {
143         $tags = [];
144
145         /** @var Tag $tag */
146         foreach ($entity->tags as $tag) {
147             $tags[] = ['name' => $tag->name, 'value' => $tag->value];
148         }
149
150         return $tags;
151     }
152 }