public function copyContents($target, $source) { // check if the type is the same if (get_class($source) != get_class($target)) { throw new \Exception("Source and target have to be the same type"); } //load all in case of lazy loading fields self::loadAllObjectFields($source); $new = clone $source; $new->setChilds($target->getChilds()); $new->setId($target->getId()); $new->setPath($target->getRealPath()); $new->setKey($target->getKey()); $new->setParentId($target->getParentId()); $new->setScheduledTasks($source->getScheduledTasks()); $new->setProperties($source->getProperties()); $new->setUserModification($this->_user->getId()); $new->save(); $target = AbstractObject::getById($new->getId()); return $target; }
public function copyAction() { $success = false; $message = ""; $sourceId = intval($this->getParam("sourceId")); $source = Object::getById($sourceId); $session = Tool\Session::get("pimcore_copy"); $targetId = intval($this->getParam("targetId")); if ($this->getParam("targetParentId")) { $sourceParent = Object::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 = Object::getById($session->{$this->getParam("transactionId")}["parentId"]); } else { $targetParent = Object::getById($this->getParam("targetParentId")); } $targetPath = preg_replace("@^" . $sourceParent->getFullPath() . "@", $targetParent . "/", $source->getPath()); $target = Object::getByPath($targetPath); } else { $target = Object::getById($targetId); } if ($target->isAllowed("create")) { $source = Object::getById($sourceId); if ($source != null) { try { if ($this->getParam("type") == "child") { $newObject = $this->_objectService->copyAsChild($target, $source); $session->{$this->getParam("transactionId")}["idMapping"][(int) $source->getId()] = (int) $newObject->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"] = $newObject->getId(); Tool\Session::writeClose(); } } else { if ($this->getParam("type") == "replace") { $this->_objectService->copyContents($target, $source); } } $success = true; } catch (\Exception $e) { \Logger::err($e); $success = false; $message = $e->getMessage() . " in object " . $source->getFullPath() . " [id: " . $source->getId() . "]"; } } else { \Logger::error("could not execute copy/paste, source object with id [ {$sourceId} ] not found"); $this->_helper->json(array("success" => false, "message" => "source object not found")); } } else { \Logger::error("could not execute copy/paste because of missing permissions on target [ " . $targetId . " ]"); $this->_helper->json(array("error" => false, "message" => "missing_permission")); } $this->_helper->json(array("success" => $success, "message" => $message)); }