2021-12-19 13:56:27 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Tools;
|
|
|
|
|
2021-12-19 16:40:52 +01:00
|
|
|
use BookStack\Actions\Tag;
|
|
|
|
use BookStack\Entities\Models\Book;
|
|
|
|
use BookStack\Entities\Models\Chapter;
|
2021-12-19 13:56:27 +01:00
|
|
|
use BookStack\Entities\Models\Entity;
|
|
|
|
use BookStack\Entities\Models\Page;
|
2021-12-19 16:40:52 +01:00
|
|
|
use BookStack\Entities\Repos\ChapterRepo;
|
2021-12-19 13:56:27 +01:00
|
|
|
use BookStack\Entities\Repos\PageRepo;
|
|
|
|
|
|
|
|
class Cloner
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PageRepo
|
|
|
|
*/
|
|
|
|
protected $pageRepo;
|
|
|
|
|
2021-12-19 16:40:52 +01:00
|
|
|
/**
|
|
|
|
* @var ChapterRepo
|
|
|
|
*/
|
|
|
|
protected $chapterRepo;
|
|
|
|
|
|
|
|
public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo)
|
2021-12-19 13:56:27 +01:00
|
|
|
{
|
|
|
|
$this->pageRepo = $pageRepo;
|
2021-12-19 16:40:52 +01:00
|
|
|
$this->chapterRepo = $chapterRepo;
|
2021-12-19 13:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clone the given page into the given parent using the provided name.
|
|
|
|
*/
|
|
|
|
public function clonePage(Page $original, Entity $parent, string $newName): Page
|
|
|
|
{
|
|
|
|
$copyPage = $this->pageRepo->getNewDraftPage($parent);
|
|
|
|
$pageData = $original->getAttributes();
|
|
|
|
|
2021-12-19 16:40:52 +01:00
|
|
|
// Update name & tags
|
2021-12-19 13:56:27 +01:00
|
|
|
$pageData['name'] = $newName;
|
2021-12-19 16:40:52 +01:00
|
|
|
$pageData['tags'] = $this->entityTagsToInputArray($original);
|
|
|
|
|
|
|
|
return $this->pageRepo->publishDraft($copyPage, $pageData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clone the given page into the given parent using the provided name.
|
|
|
|
* Clones all child pages.
|
|
|
|
*/
|
|
|
|
public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
|
|
|
|
{
|
|
|
|
$chapterDetails = $original->getAttributes();
|
|
|
|
$chapterDetails['name'] = $newName;
|
|
|
|
$chapterDetails['tags'] = $this->entityTagsToInputArray($original);
|
2021-12-19 13:56:27 +01:00
|
|
|
|
2021-12-19 16:40:52 +01:00
|
|
|
$copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
|
|
|
|
|
|
|
|
if (userCan('page-create', $copyChapter)) {
|
|
|
|
/** @var Page $page */
|
|
|
|
foreach ($original->getVisiblePages() as $page) {
|
|
|
|
$this->clonePage($page, $copyChapter, $page->name);
|
2021-12-19 13:56:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-19 16:40:52 +01:00
|
|
|
return $copyChapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the tags on the given entity to the raw format
|
|
|
|
* that's used for incoming request data.
|
|
|
|
*/
|
|
|
|
protected function entityTagsToInputArray(Entity $entity): array
|
|
|
|
{
|
|
|
|
$tags = [];
|
|
|
|
|
|
|
|
/** @var Tag $tag */
|
|
|
|
foreach ($entity->tags as $tag) {
|
|
|
|
$tags[] = ['name' => $tag->name, 'value' => $tag->value];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tags;
|
2021-12-19 13:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|