public function copyAction() { $success = false; $message = ""; $sourceId = intval($this->_getParam("sourceId")); $source = Object_Abstract::getById($sourceId); $session = new Zend_Session_Namespace("pimcore_copy"); $targetId = intval($this->_getParam("targetId")); if ($this->_getParam("targetParentId")) { $sourceParent = Object_Abstract::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_Abstract::getById($session->{$this->_getParam("transactionId")}["parentId"]); } else { $targetParent = Object_Abstract::getById($this->_getParam("targetParentId")); } $targetPath = preg_replace("@^" . $sourceParent->getFullPath() . "@", $targetParent . "/", $source->getPath()); $target = Object_Abstract::getByPath($targetPath); } else { $target = Object_Abstract::getById($targetId); } $target->getPermissionsForUser($this->getUser()); if ($target->isAllowed("create")) { $source = Object_Abstract::getById($sourceId); if ($source != null) { try { if ($this->_getParam("type") == "child") { $newObject = $this->_objectService->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"] = $newObject->getId(); } } 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)); }
public function testCopyAndDeleteObject() { $objectList = new Object_List(); $objectList->setCondition("o_key like '%_data%' and o_type = 'object'"); $objects = $objectList->load(); $parent = $objects[0]; $this->assertTrue($parent instanceof Object_Unittest); //remove childs if there are some if ($parent->hasChilds()) { foreach ($parent->getChilds() as $child) { $child->delete(); } } $this->assertFalse($parent->hasChilds()); $service = new Object_Service(User::getById(1)); //copy as child $service->copyAsChild($parent, $parent); $this->assertTrue($parent->hasChilds()); $this->assertTrue(count($parent->getChilds()) == 1); //copy as child no. 2 $service->copyAsChild($parent, $parent); $this->assertTrue($parent->hasChilds()); $this->assertTrue(count($parent->getChilds()) == 2); $childs = $parent->getChilds(); //load all in case of lazy loading fields Object_Service::loadAllObjectFields($parent); Object_Service::loadAllObjectFields($childs[0]); Object_Service::loadAllObjectFields($childs[1]); $this->assertTrue(Test_Tool::objectsAreEqual($parent, $childs[0], true)); $this->assertTrue(Test_Tool::objectsAreEqual($parent, $childs[1], true)); //copy recursivley $rootNode = Object_Abstract::getById(1); $copy = $service->copyRecursive($rootNode, $parent); $this->assertTrue($copy->hasChilds()); Object_Service::loadAllObjectFields($copy); $this->assertTrue(count($copy->getChilds()) == 2); $this->assertTrue(Test_Tool::objectsAreEqual($parent, $copy, true)); //create empty object $emptyObject = new Object_Unittest(); $emptyObject->setOmitMandatoryCheck(true); $emptyObject->setParentId(1); $emptyObject->setUserOwner(1); $emptyObject->setUserModification(1); $emptyObject->setCreationDate(time()); $emptyObject->setKey(uniqid() . rand(10, 99)); $emptyObject->save(); $emptyObject->setOmitMandatoryCheck(false); $this->assertFalse(Test_Tool::objectsAreEqual($emptyObject, $copy, true)); //copy contents $emptyObject = $service->copyContents($emptyObject, $copy); $this->assertTrue(Test_Tool::objectsAreEqual($emptyObject, $copy, true)); //todo copy contents must fail if types differ //delete recusively $shouldBeDeleted[] = $copy->getId(); $childs = $copy->getChilds(); foreach ($childs as $child) { $shouldBeDeleted[] = $child->getId(); } $copy->delete(); foreach ($shouldBeDeleted as $id) { $o = Object_Abstract::getById($id); $this->assertFalse($o instanceof Object_Abstract); } }