mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-10-01 01:36:00 -04:00
3f9527f166
Fundemental refactor for planned additional clone operations. No behaviour change intended in this commit.
44 lines
1.0 KiB
PHP
44 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace BookStack\Entities\Tools;
|
|
|
|
use BookStack\Entities\Models\Entity;
|
|
use BookStack\Entities\Models\Page;
|
|
use BookStack\Entities\Repos\PageRepo;
|
|
|
|
class Cloner
|
|
{
|
|
|
|
/**
|
|
* @var PageRepo
|
|
*/
|
|
protected $pageRepo;
|
|
|
|
public function __construct(PageRepo $pageRepo)
|
|
{
|
|
$this->pageRepo = $pageRepo;
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
|
|
// Update name
|
|
$pageData['name'] = $newName;
|
|
|
|
// Copy tags from previous page if set
|
|
if ($original->tags) {
|
|
$pageData['tags'] = [];
|
|
foreach ($original->tags as $tag) {
|
|
$pageData['tags'][] = ['name' => $tag->name, 'value' => $tag->value];
|
|
}
|
|
}
|
|
|
|
return $this->pageRepo->publishDraft($copyPage, $pageData);
|
|
}
|
|
|
|
} |