]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/Cloner.php
Removed bad trailing comma in method
[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     protected PageRepo $pageRepo;
20     protected ChapterRepo $chapterRepo;
21     protected BookRepo $bookRepo;
22     protected ImageService $imageService;
23
24     public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo, BookRepo $bookRepo, ImageService $imageService)
25     {
26         $this->pageRepo = $pageRepo;
27         $this->chapterRepo = $chapterRepo;
28         $this->bookRepo = $bookRepo;
29         $this->imageService = $imageService;
30     }
31
32     /**
33      * Clone the given page into the given parent using the provided name.
34      */
35     public function clonePage(Page $original, Entity $parent, string $newName): Page
36     {
37         $copyPage = $this->pageRepo->getNewDraftPage($parent);
38         $pageData = $this->entityToInputData($original);
39         $pageData['name'] = $newName;
40
41         return $this->pageRepo->publishDraft($copyPage, $pageData);
42     }
43
44     /**
45      * Clone the given page into the given parent using the provided name.
46      * Clones all child pages.
47      */
48     public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
49     {
50         $chapterDetails = $this->entityToInputData($original);
51         $chapterDetails['name'] = $newName;
52
53         $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
54
55         if (userCan('page-create', $copyChapter)) {
56             /** @var Page $page */
57             foreach ($original->getVisiblePages() as $page) {
58                 $this->clonePage($page, $copyChapter, $page->name);
59             }
60         }
61
62         return $copyChapter;
63     }
64
65     /**
66      * Clone the given book.
67      * Clones all child chapters & pages.
68      */
69     public function cloneBook(Book $original, string $newName): Book
70     {
71         $bookDetails = $this->entityToInputData($original);
72         $bookDetails['name'] = $newName;
73
74         $copyBook = $this->bookRepo->create($bookDetails);
75
76         $directChildren = $original->getDirectChildren();
77         foreach ($directChildren as $child) {
78             if ($child instanceof Chapter && userCan('chapter-create', $copyBook)) {
79                 $this->cloneChapter($child, $copyBook, $child->name);
80             }
81
82             if ($child instanceof Page && !$child->draft && userCan('page-create', $copyBook)) {
83                 $this->clonePage($child, $copyBook, $child->name);
84             }
85         }
86
87         return $copyBook;
88     }
89
90     /**
91      * Convert an entity to a raw data array of input data.
92      *
93      * @return array<string, mixed>
94      */
95     public function entityToInputData(Entity $entity): array
96     {
97         $inputData = $entity->getAttributes();
98         $inputData['tags'] = $this->entityTagsToInputArray($entity);
99
100         // Add a cover to the data if existing on the original entity
101         if ($entity->cover instanceof Image) {
102             $uploadedFile = $this->imageToUploadedFile($entity->cover);
103             $inputData['image'] = $uploadedFile;
104         }
105
106         return $inputData;
107     }
108
109     /**
110      * Copy the permission settings from the source entity to the target entity.
111      */
112     public function copyEntityPermissions(Entity $sourceEntity, Entity $targetEntity): void
113     {
114         $targetEntity->restricted = $sourceEntity->restricted;
115         $permissions = $sourceEntity->permissions()->get(['role_id', 'action'])->toArray();
116         $targetEntity->permissions()->delete();
117         $targetEntity->permissions()->createMany($permissions);
118         $targetEntity->rebuildPermissions();
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): ?UploadedFile
126     {
127         $imgData = $this->imageService->getImageData($image);
128         $tmpImgFilePath = tempnam(sys_get_temp_dir(), 'bs_cover_clone_');
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 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.