public function copyAction() { $success = false; $sourceId = intval($this->_getParam("sourceId")); $source = Document::getById($sourceId); $session = new Zend_Session_Namespace("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) { $target->getPermissionsForUser($this->getUser()); if ($target->isAllowed("create")) { if ($source != null) { if ($this->_getParam("type") == "child") { $newDocument = $this->_documentService->copyAsChild($target, $source); $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(); } } 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)); }
public function testCopyAndDeleteDocument() { $documentList = new Document_List(); $documentList->setCondition("`key` like '%_data%' and `type` = 'page'"); $documents = $documentList->load(); $parent = $documents[0]; $this->assertTrue($parent instanceof Document_Page); //remove childs if there are some if ($parent->hasChilds()) { foreach ($parent->getChilds() as $child) { $child->delete(); } } $this->assertFalse($parent->hasChilds()); $service = new Document_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(); $this->assertTrue(Test_Tool::documentsAreEqual($parent, $childs[0], true)); $this->assertTrue(Test_Tool::documentsAreEqual($parent, $childs[1], true)); //copy recursivley $rootNode = Document::getById(1); $copy = $service->copyRecursive($rootNode, $parent); $this->assertTrue($copy->hasChilds()); $this->assertTrue(count($copy->getChilds()) == 2); $this->assertTrue(Test_Tool::documentsAreEqual($parent, $copy, true)); //create empty document $emptyDoc = Document_Page::create(1, array("userOwner" => 1, "key" => uniqid() . rand(10, 99))); $this->assertFalse(Test_Tool::documentsAreEqual($emptyDoc, $copy, true)); //copy contents $emptyDoc = $service->copyContents($emptyDoc, $copy); $this->assertTrue(Test_Tool::documentsAreEqual($emptyDoc, $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 = Document::getById($id); $this->assertFalse($o instanceof Document); } }