]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/Cloner.php
Merge pull request #5668 from bumperbox/patch-1
[bookstack] / app / Entities / Tools / Cloner.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
5 use BookStack\Activity\Models\Tag;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\Entity;
10 use BookStack\Entities\Models\HasCoverImage;
11 use BookStack\Entities\Models\Page;
12 use BookStack\Entities\Repos\BookRepo;
13 use BookStack\Entities\Repos\ChapterRepo;
14 use BookStack\Entities\Repos\PageRepo;
15 use BookStack\Uploads\Image;
16 use BookStack\Uploads\ImageService;
17 use Illuminate\Http\UploadedFile;
18
19 class Cloner
20 {
21     public function __construct(
22         protected PageRepo $pageRepo,
23         protected ChapterRepo $chapterRepo,
24         protected BookRepo $bookRepo,
25         protected ImageService $imageService,
26     ) {
27     }
28
29     /**
30      * Clone the given page into the given parent using the provided name.
31      */
32     public function clonePage(Page $original, Entity $parent, string $newName): Page
33     {
34         $copyPage = $this->pageRepo->getNewDraftPage($parent);
35         $pageData = $this->entityToInputData($original);
36         $pageData['name'] = $newName;
37
38         return $this->pageRepo->publishDraft($copyPage, $pageData);
39     }
40
41     /**
42      * Clone the given page into the given parent using the provided name.
43      * Clones all child pages.
44      */
45     public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
46     {
47         $chapterDetails = $this->entityToInputData($original);
48         $chapterDetails['name'] = $newName;
49
50         $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
51
52         if (userCan('page-create', $copyChapter)) {
53             /** @var Page $page */
54             foreach ($original->getVisiblePages() as $page) {
55                 $this->clonePage($page, $copyChapter, $page->name);
56             }
57         }
58
59         return $copyChapter;
60     }
61
62     /**
63      * Clone the given book.
64      * Clones all child chapters & pages.
65      */
66     public function cloneBook(Book $original, string $newName): Book
67     {
68         $bookDetails = $this->entityToInputData($original);
69         $bookDetails['name'] = $newName;
70
71         // Clone book
72         $copyBook = $this->bookRepo->create($bookDetails);
73
74         // Clone contents
75         $directChildren = $original->getDirectVisibleChildren();
76         foreach ($directChildren as $child) {
77             if ($child instanceof Chapter && userCan('chapter-create', $copyBook)) {
78                 $this->cloneChapter($child, $copyBook, $child->name);
79             }
80
81             if ($child instanceof Page && !$child->draft && userCan('page-create', $copyBook)) {
82                 $this->clonePage($child, $copyBook, $child->name);
83             }
84         }
85
86         // Clone bookshelf relationships
87         /** @var Bookshelf $shelf */
88         foreach ($original->shelves as $shelf) {
89             if (userCan('bookshelf-update', $shelf)) {
90                 $shelf->appendBook($copyBook);
91             }
92         }
93
94         return $copyBook;
95     }
96
97     /**
98      * Convert an entity to a raw data array of input data.
99      *
100      * @return array<string, mixed>
101      */
102     public function entityToInputData(Entity $entity): array
103     {
104         $inputData = $entity->getAttributes();
105         $inputData['tags'] = $this->entityTagsToInputArray($entity);
106
107         // Add a cover to the data if existing on the original entity
108         if ($entity instanceof HasCoverImage) {
109             $cover = $entity->cover()->first();
110             if ($cover) {
111                 $inputData['image'] = $this->imageToUploadedFile($cover);
112             }
113         }
114
115         return $inputData;
116     }
117
118     /**
119      * Copy the permission settings from the source entity to the target entity.
120      */
121     public function copyEntityPermissions(Entity $sourceEntity, Entity $targetEntity): void
122     {
123         $permissions = $sourceEntity->permissions()->get(['role_id', 'view', 'create', 'update', 'delete'])->toArray();
124         $targetEntity->permissions()->delete();
125         $targetEntity->permissions()->createMany($permissions);
126         $targetEntity->rebuildPermissions();
127     }
128
129     /**
130      * Convert an image instance to an UploadedFile instance to mimic
131      * a file being uploaded.
132      */
133     protected function imageToUploadedFile(Image $image): ?UploadedFile
134     {
135         $imgData = $this->imageService->getImageData($image);
136         $tmpImgFilePath = tempnam(sys_get_temp_dir(), 'bs_cover_clone_');
137         file_put_contents($tmpImgFilePath, $imgData);
138
139         return new UploadedFile($tmpImgFilePath, basename($image->path));
140     }
141
142     /**
143      * Convert the tags on the given entity to the raw format
144      * that's used for incoming request data.
145      */
146     protected function entityTagsToInputArray(Entity $entity): array
147     {
148         $tags = [];
149
150         /** @var Tag $tag */
151         foreach ($entity->tags as $tag) {
152             $tags[] = ['name' => $tag->name, 'value' => $tag->value];
153         }
154
155         return $tags;
156     }
157 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.