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