Pimcore\Model\Document\Service::copyAsChild PHP Method

copyAsChild() public method

public copyAsChild ( $target, $source, boolean $enableInheritance = false, boolean $resetIndex = false ) : Document
$target
$source
$enableInheritance boolean
$resetIndex boolean
return Pimcore\Model\Document
    public function copyAsChild($target, $source, $enableInheritance = false, $resetIndex = false)
    {
        if (method_exists($source, "getElements")) {
            $source->getElements();
        }
        $source->getProperties();
        $new = clone $source;
        $new->id = null;
        $new->setChilds(null);
        $new->setKey(Element\Service::getSaveCopyName("document", $new->getKey(), $target));
        $new->setParentId($target->getId());
        $new->setUserOwner($this->_user->getId());
        $new->setUserModification($this->_user->getId());
        $new->setDao(null);
        $new->setLocked(false);
        $new->setCreationDate(time());
        if ($resetIndex) {
            // this needs to be after $new->setParentId($target->getId()); -> dependency!
            $new->setIndex($new->getDao()->getNextIndex());
        }
        if (method_exists($new, "setPrettyUrl")) {
            $new->setPrettyUrl(null);
        }
        if ($enableInheritance && $new instanceof Document\PageSnippet) {
            $new->setElements([]);
            $new->setContentMasterDocumentId($source->getId());
        }
        $new->save();
        $this->updateChilds($target, $new);
        // triggers actions after the complete document cloning
        \Pimcore::getEventManager()->trigger('document.postCopy', $new, ['base_element' => $source]);
        return $new;
    }

Usage Example

Exemplo n.º 1
0
 public function copyAction()
 {
     $success = false;
     $sourceId = intval($this->getParam("sourceId"));
     $source = Document::getById($sourceId);
     $session = Session::get("pimcore_copy");
     $targetId = intval($this->getParam("targetId"));
     if ($this->getParam("targetParentId")) {
         $sourceParent = Document::getById($this->getParam("sourceParentId"));
         // this is because the key can get the prefix "_copy" if the target does already exists
         if ($session->{$this->getParam("transactionId")}["parentId"]) {
             $targetParent = Document::getById($session->{$this->getParam("transactionId")}["parentId"]);
         } else {
             $targetParent = Document::getById($this->getParam("targetParentId"));
         }
         $targetPath = preg_replace("@^" . $sourceParent->getFullPath() . "@", $targetParent . "/", $source->getPath());
         $target = Document::getByPath($targetPath);
     } else {
         $target = Document::getById($targetId);
     }
     if ($target instanceof Document) {
         if ($target->isAllowed("create")) {
             if ($source != null) {
                 if ($this->getParam("type") == "child") {
                     $enableInheritance = $this->getParam("enableInheritance") == "true" ? true : false;
                     $resetIndex = $this->getParam("resetIndex") == "true" ? true : false;
                     $newDocument = $this->_documentService->copyAsChild($target, $source, $enableInheritance, $resetIndex);
                     $session->{$this->getParam("transactionId")}["idMapping"][(int) $source->getId()] = (int) $newDocument->getId();
                     // this is because the key can get the prefix "_copy" if the target does already exists
                     if ($this->getParam("saveParentId")) {
                         $session->{$this->getParam("transactionId")}["parentId"] = $newDocument->getId();
                     }
                     Session::writeClose();
                 } else {
                     if ($this->getParam("type") == "replace") {
                         $this->_documentService->copyContents($target, $source);
                     }
                 }
                 $success = true;
             } else {
                 \Logger::error("prevended copy/paste because document with same path+key already exists in this location");
             }
         } else {
             \Logger::error("could not execute copy/paste because of missing permissions on target [ " . $targetId . " ]");
             $this->_helper->json(array("success" => false, "message" => "missing_permission"));
         }
     }
     $this->_helper->json(array("success" => $success));
 }