3 namespace BookStack\Entities\Tools;
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\ChapterRepo;
11 use BookStack\Entities\Repos\PageRepo;
24 protected $chapterRepo;
26 public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo)
28 $this->pageRepo = $pageRepo;
29 $this->chapterRepo = $chapterRepo;
33 * Clone the given page into the given parent using the provided name.
35 public function clonePage(Page $original, Entity $parent, string $newName): Page
37 $copyPage = $this->pageRepo->getNewDraftPage($parent);
38 $pageData = $original->getAttributes();
41 $pageData['name'] = $newName;
42 $pageData['tags'] = $this->entityTagsToInputArray($original);
44 return $this->pageRepo->publishDraft($copyPage, $pageData);
48 * Clone the given page into the given parent using the provided name.
49 * Clones all child pages.
51 public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
53 $chapterDetails = $original->getAttributes();
54 $chapterDetails['name'] = $newName;
55 $chapterDetails['tags'] = $this->entityTagsToInputArray($original);
57 $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
59 if (userCan('page-create', $copyChapter)) {
60 /** @var Page $page */
61 foreach ($original->getVisiblePages() as $page) {
62 $this->clonePage($page, $copyChapter, $page->name);
70 * Convert the tags on the given entity to the raw format
71 * that's used for incoming request data.
73 protected function entityTagsToInputArray(Entity $entity): array
78 foreach ($entity->tags as $tag) {
79 $tags[] = ['name' => $tag->name, 'value' => $tag->value];