Pimcore\Model\Asset\Service::copyAsChild PHP Метод

copyAsChild() публичный Метод

public copyAsChild ( Asset $target, Asset $source ) : Asset
$target Pimcore\Model\Asset
$source Pimcore\Model\Asset
Результат Pimcore\Model\Asset copied asset
    public function copyAsChild($target, $source)
    {
        $source->getProperties();
        $new = clone $source;
        $new->id = null;
        if ($new instanceof Asset\Folder) {
            $new->setChilds(null);
        }
        $new->setFilename(Element\Service::getSaveCopyName("asset", $new->getFilename(), $target));
        $new->setParentId($target->getId());
        $new->setUserOwner($this->_user->getId());
        $new->setUserModification($this->_user->getId());
        $new->setDao(null);
        $new->setLocked(false);
        $new->setCreationDate(time());
        $new->setStream($source->getStream());
        $new->save();
        if ($target instanceof Asset\Folder) {
            $this->updateChilds($target, $new);
        }
        // triggers actions after the complete asset cloning
        \Pimcore::getEventManager()->trigger('asset.postCopy', $new, ['base_element' => $source]);
        return $new;
    }

Usage Example

 public function copyAction()
 {
     $success = false;
     $sourceId = intval($this->getParam("sourceId"));
     $source = Asset::getById($sourceId);
     $session = Tool\Session::get("pimcore_copy");
     $targetId = intval($this->getParam("targetId"));
     if ($this->getParam("targetParentId")) {
         $sourceParent = Asset::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 = Asset::getById($session->{$this->getParam("transactionId")}["parentId"]);
         } else {
             $targetParent = Asset::getById($this->getParam("targetParentId"));
         }
         $targetPath = preg_replace("@^" . $sourceParent->getFullPath() . "@", $targetParent . "/", $source->getPath());
         $target = Asset::getByPath($targetPath);
     } else {
         $target = Asset::getById($targetId);
     }
     if ($target->isAllowed("create")) {
         $source = Asset::getById($sourceId);
         if ($source != null) {
             if ($this->getParam("type") == "child") {
                 $newAsset = $this->_assetService->copyAsChild($target, $source);
                 // 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"] = $newAsset->getId();
                 }
             } else {
                 if ($this->getParam("type") == "replace") {
                     $this->_assetService->copyContents($target, $source);
                 }
             }
             $success = true;
         } else {
             \Logger::debug("prevended copy/paste because asset 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("error" => false, "message" => "missing_permission"));
     }
     Tool\Session::writeClose();
     $this->_helper->json(array("success" => $success));
 }