/**
* Creates a draft from a published or archived version.
*
* If no version is given, the current published version is used.
* 4.x: The draft is created with the initialLanguage code of the source version or if not present with the main language.
* It can be changed on updating the version.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to create the draft
*
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
* @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
* @param \eZ\Publish\API\Repository\Values\User\User $creator if set given user is used to create the draft - otherwise the current-user is used
*
* @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft
*/
public function createContentDraft(ContentInfo $contentInfo, APIVersionInfo $versionInfo = null, User $creator = null)
{
$contentInfo = $this->loadContentInfo($contentInfo->id);
if ($versionInfo !== null) {
// Check that given $contentInfo and $versionInfo belong to the same content
if ($versionInfo->getContentInfo()->id != $contentInfo->id) {
throw new InvalidArgumentException("\$versionInfo", "VersionInfo does not belong to the same content as given ContentInfo");
}
$versionInfo = $this->loadVersionInfoById($contentInfo->id, $versionInfo->versionNo);
switch ($versionInfo->status) {
case VersionInfo::STATUS_PUBLISHED:
case VersionInfo::STATUS_ARCHIVED:
break;
default:
// @todo: throw an exception here, to be defined
throw new BadStateException("\$versionInfo", "Draft can not be created from a draft version");
}
$versionNo = $versionInfo->versionNo;
} else {
if ($contentInfo->published) {
$versionNo = $contentInfo->currentVersionNo;
} else {
// @todo: throw an exception here, to be defined
throw new BadStateException("\$contentInfo", "Content is not published, draft can be created only from published or archived version");
}
}
if ($creator === null) {
$creator = $this->repository->getCurrentUser();
} else {
$creator = $this->repository->getUserService()->loadUser($creator->id);
}
if (!$this->repository->canUser('content', 'edit', $contentInfo)) {
throw new UnauthorizedException('content', 'edit', array('contentId' => $contentInfo->id));
}
$this->repository->beginTransaction();
try {
$spiContent = $this->persistenceHandler->contentHandler()->createDraftFromVersion($contentInfo->id, $versionNo, $creator->id);
$this->repository->commit();
} catch (Exception $e) {
$this->repository->rollback();
throw $e;
}
return $this->domainMapper->buildContentDomainObject($spiContent);
}