]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/Cloner.php
do some cleanup and add doc
[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 = $original->getAttributes();
54
55         // Update name & tags
56         $pageData['name'] = $newName;
57         $pageData['tags'] = $this->entityTagsToInputArray($original);
58
59         return $this->pageRepo->publishDraft($copyPage, $pageData);
60     }
61
62     /**
63      * Clone the given page into the given parent using the provided name.
64      * Clones all child pages.
65      */
66     public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
67     {
68         $chapterDetails = $original->getAttributes();
69         $chapterDetails['name'] = $newName;
70         $chapterDetails['tags'] = $this->entityTagsToInputArray($original);
71
72         $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
73
74         if (userCan('page-create', $copyChapter)) {
75             /** @var Page $page */
76             foreach ($original->getVisiblePages() as $page) {
77                 $this->clonePage($page, $copyChapter, $page->name);
78             }
79         }
80
81         return $copyChapter;
82     }
83
84     /**
85      * Clone the given book.
86      * Clones all child chapters & pages.
87      */
88     public function cloneBook(Book $original, string $newName): Book
89     {
90         $bookDetails = $original->getAttributes();
91         $bookDetails['name'] = $newName;
92         $bookDetails['tags'] = $this->entityTagsToInputArray($original);
93
94         $copyBook = $this->bookRepo->create($bookDetails);
95
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);
100             }
101
102             if ($child instanceof Page && !$child->draft && userCan('page-create', $copyBook)) {
103                 $this->clonePage($child, $copyBook, $child->name);
104             }
105         }
106
107         if ($original->cover) {
108             try {
109                 $tmpImgFile = tmpfile();
110                 $uploadedFile = $this->imageToUploadedFile($original->cover, $tmpImgFile);
111                 $this->bookRepo->updateCoverImage($copyBook, $uploadedFile, false);
112             } catch (\Exception $exception) {
113             }
114         }
115
116         return $copyBook;
117     }
118
119     /**
120      * Convert an image instance to an UploadedFile instance to mimic
121      * a file being uploaded.
122      */
123     protected function imageToUploadedFile(Image $image, &$tmpFile): ?UploadedFile
124     {
125         $imgData = $this->imageService->getImageData($image);
126         $tmpImgFilePath = stream_get_meta_data($tmpFile)['uri'];
127         file_put_contents($tmpImgFilePath, $imgData);
128
129         return new UploadedFile($tmpImgFilePath, basename($image->path));
130     }
131
132     /**
133      * Convert the tags on the given entity to the raw format
134      * that's used for incoming request data.
135      */
136     protected function entityTagsToInputArray(Entity $entity): array
137     {
138         $tags = [];
139
140         /** @var Tag $tag */
141         foreach ($entity->tags as $tag) {
142             $tags[] = ['name' => $tag->name, 'value' => $tag->value];
143         }
144
145         return $tags;
146     }
147 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.