Exemplo n.º 1
0
 public function copyAction()
 {
     $success = false;
     $sourceId = intval($this->_getParam("sourceId"));
     $source = Asset::getById($sourceId);
     $session = new Zend_Session_Namespace("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);
     }
     $target->getPermissionsForUser($this->getUser());
     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"));
     }
     $this->_helper->json(array("success" => $success));
 }
Exemplo n.º 2
0
 public function testCopyAndDeleteAsset()
 {
     $assetList = new Asset_List();
     $assetList->setCondition("`filename` like '%_data%' and `type` = 'folder'");
     $assets = $assetList->load();
     $parent = $assets[0];
     $this->assertTrue($parent instanceof Asset_Folder);
     //remove childs if there are some
     if ($parent->hasChilds()) {
         foreach ($parent->getChilds() as $child) {
             $child->delete();
         }
     }
     $assetList = new Asset_List();
     $assetList->setCondition("`filename` like '%_data%' and `type` != 'folder'");
     $assets = $assetList->load();
     $image = $assets[0];
     $this->assertTrue($image instanceof Asset_Image);
     $this->assertFalse($parent->hasChilds());
     $service = new Asset_Service(User::getById(1));
     //copy as child
     $service->copyAsChild($parent, $parent);
     $this->assertTrue($parent->hasChilds());
     $this->assertTrue(count($parent->getChilds()) == 1);
     $childs = $parent->getChilds();
     $this->assertTrue(Test_Tool::assetsAreEqual($parent, $childs[0], true));
     //copy as child no. 2
     $service->copyAsChild($parent, $image);
     $this->assertTrue($parent->hasChilds());
     $this->assertTrue(count($parent->getChilds()) == 2);
     //copy recursivley
     $rootNode = Asset::getById(1);
     $copy = $service->copyRecursive($rootNode, $parent);
     $this->assertTrue($copy->hasChilds());
     $this->assertTrue(count($copy->getChilds()) == 2);
     $this->assertTrue(Test_Tool::assetsAreEqual($parent, $copy, true));
     //create unequal assets
     $asset1 = Asset_Image::create(1, array("filename" => uniqid() . rand(10, 99) . ".jpg", "data" => file_get_contents(TESTS_PATH . "/resources/assets/images/image1" . ".jpg"), "userOwner" => 1));
     $asset2 = Asset_Image::create(1, array("filename" => uniqid() . rand(10, 99) . ".jpg", "data" => file_get_contents(TESTS_PATH . "/resources/assets/images/image2" . ".jpg"), "userOwner" => 1));
     $this->assertFalse(Test_Tool::assetsAreEqual($asset1, $asset2, true));
     //copy contents
     $asset1 = $service->copyContents($asset1, $asset2);
     $this->assertTrue(Test_Tool::assetsAreEqual($asset1, $asset2, 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 = Asset::getById($id);
         $this->assertFalse($o instanceof Asset);
     }
 }