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