Example #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));
 }
Example #2
0
 public function correctPath()
 {
     // set path
     if ($this->getId() != 1) {
         // not for the root node
         $parent = Asset::getById($this->getParentId());
         if ($parent) {
             $this->setPath(str_replace("//", "/", $parent->getFullPath() . "/"));
         } else {
             // parent document doesn't exist anymore, so delete this document
             //$this->delete();
             // parent document doesn't exist anymore, set the parent to to root
             $this->setParentId(1);
             $this->setPath("/");
         }
     }
     if (Asset_Service::pathExists($this->getFullPath())) {
         $duplicate = Asset::getByPath($this->getFullPath());
         if ($duplicate instanceof Asset and $duplicate->getId() != $this->getId()) {
             throw new Exception("Duplicate full path [ " . $this->getFullPath() . " ] - cannot create asset");
         }
     }
 }
Example #3
0
 /**
  * @static
  * @param $type
  * @param $path
  * @return bool
  */
 public static function pathExists($type, $path)
 {
     if ($type == "asset") {
         return Asset_Service::pathExists($path);
     } else {
         if ($type == "document") {
             return Document_Service::pathExists($path);
         } else {
             if ($type == "object") {
                 return Object_Service::pathExists($path);
             }
         }
     }
 }
 /**
  * @return void
  */
 public function findAction()
 {
     $user = $this->getUser();
     $query = $this->_getParam("query");
     if ($query == "*") {
         $query = "";
     }
     $query = str_replace("%", "*", $query);
     $types = explode(",", $this->_getParam("type"));
     $subtypes = explode(",", $this->_getParam("subtype"));
     $classnames = explode(",", $this->_getParam("class"));
     $offset = intval($this->_getParam("start"));
     $limit = intval($this->_getParam("limit"));
     $offset = $offset ? $offset : 0;
     $limit = $limit ? $limit : 50;
     $searcherList = new Search_Backend_Data_List();
     $conditionParts = array();
     $db = Pimcore_Resource::get();
     //exclude forbidden assets
     if (in_array("asset", $types)) {
         if (!$user->isAllowed("assets")) {
             $forbiddenConditions[] = " `type` != 'asset' ";
         } else {
             $forbiddenAssetPaths = Element_Service::findForbiddenPaths("asset", $user);
             if (count($forbiddenAssetPaths) > 0) {
                 for ($i = 0; $i < count($forbiddenAssetPaths); $i++) {
                     $forbiddenAssetPaths[$i] = " (maintype = 'asset' AND fullpath not like " . $db->quote($forbiddenAssetPaths[$i] . "%") . ")";
                 }
                 $forbiddenConditions[] = implode(" AND ", $forbiddenAssetPaths);
             }
         }
     }
     //exclude forbidden documents
     if (in_array("document", $types)) {
         if (!$user->isAllowed("documents")) {
             $forbiddenConditions[] = " `type` != 'document' ";
         } else {
             $forbiddenDocumentPaths = Element_Service::findForbiddenPaths("document", $user);
             if (count($forbiddenDocumentPaths) > 0) {
                 for ($i = 0; $i < count($forbiddenDocumentPaths); $i++) {
                     $forbiddenDocumentPaths[$i] = " (maintype = 'document' AND fullpath not like " . $db->quote($forbiddenDocumentPaths[$i] . "%") . ")";
                 }
                 $forbiddenConditions[] = implode(" AND ", $forbiddenDocumentPaths);
             }
         }
     }
     //exclude forbidden objects
     if (in_array("object", $types)) {
         if (!$user->isAllowed("objects")) {
             $forbiddenConditions[] = " `type` != 'object' ";
         } else {
             $forbiddenObjectPaths = Element_Service::findForbiddenPaths("object", $user);
             if (count($forbiddenObjectPaths) > 0) {
                 for ($i = 0; $i < count($forbiddenObjectPaths); $i++) {
                     $forbiddenObjectPaths[$i] = " (maintype = 'object' AND fullpath not like " . $db->quote($forbiddenObjectPaths[$i] . "%") . ")";
                 }
                 $forbiddenConditions[] = implode(" AND ", $forbiddenObjectPaths);
             }
         }
     }
     if ($forbiddenConditions) {
         $conditionParts[] = "(" . implode(" AND ", $forbiddenConditions) . ")";
     }
     if (!empty($query)) {
         $queryCondition = "( MATCH (`data`,`properties`) AGAINST (" . $db->quote($query) . " IN BOOLEAN MODE) )";
         // the following should be done with an exact-search now "ID", because the Element-ID is now in the fulltext index
         // if the query is numeric the user might want to search by id
         //if(is_numeric($query)) {
         //$queryCondition = "(" . $queryCondition . " OR id = " . $db->quote($query) ." )";
         //}
         $conditionParts[] = $queryCondition;
     }
     //For objects - handling of bricks
     $fields = array();
     $bricks = array();
     if ($this->_getParam("fields")) {
         $fields = $this->_getParam("fields");
         foreach ($fields as $f) {
             $parts = explode("~", $f);
             if (count($parts) > 1) {
                 $bricks[$parts[0]] = $parts[0];
             }
         }
     }
     // filtering for objects
     if ($this->_getParam("filter")) {
         $class = Object_Class::getByName($this->_getParam("class"));
         $conditionFilters = Object_Service::getFilterCondition($this->_getParam("filter"), $class);
         $join = "";
         foreach ($bricks as $ob) {
             $join .= " LEFT JOIN object_brick_query_" . $ob . "_" . $class->getId();
             $join .= " `" . $ob . "`";
             $join .= " ON `" . $ob . "`.o_id = `object_" . $class->getId() . "`.o_id";
         }
         $conditionParts[] = "( id IN (SELECT `object_" . $class->getId() . "`.o_id FROM object_" . $class->getId() . $join . " WHERE 1=1 " . $conditionFilters . ") )";
     }
     if (is_array($types) and !empty($types[0])) {
         foreach ($types as $type) {
             $conditionTypeParts[] = $db->quote($type);
         }
         $conditionParts[] = "( maintype IN (" . implode(",", $conditionTypeParts) . ") )";
     }
     if (is_array($subtypes) and !empty($subtypes[0])) {
         foreach ($subtypes as $subtype) {
             $conditionSubtypeParts[] = $db->quote($subtype);
         }
         $conditionParts[] = "( type IN (" . implode(",", $conditionSubtypeParts) . ") )";
     }
     if (is_array($classnames) and !empty($classnames[0])) {
         if (in_array("folder", $subtypes)) {
             $classnames[] = "folder";
         }
         foreach ($classnames as $classname) {
             $conditionClassnameParts[] = $db->quote($classname);
         }
         $conditionParts[] = "( subtype IN (" . implode(",", $conditionClassnameParts) . ") )";
     }
     if (count($conditionParts) > 0) {
         $condition = implode(" AND ", $conditionParts);
         //echo $condition; die();
         $searcherList->setCondition($condition);
     }
     $searcherList->setOffset($offset);
     $searcherList->setLimit($limit);
     // do not sort per default, it is VERY SLOW
     //$searcherList->setOrder("desc");
     //$searcherList->setOrderKey("modificationdate");
     if ($this->_getParam("sort")) {
         $searcherList->setOrderKey($this->_getParam("sort"));
     }
     if ($this->_getParam("dir")) {
         $searcherList->setOrder($this->_getParam("dir"));
     }
     $hits = $searcherList->load();
     $elements = array();
     foreach ($hits as $hit) {
         $element = Element_Service::getElementById($hit->getId()->getType(), $hit->getId()->getId());
         if ($element->isAllowed("list")) {
             if ($element instanceof Object_Abstract) {
                 $data = Object_Service::gridObjectData($element, $fields);
             } else {
                 if ($element instanceof Document) {
                     $data = Document_Service::gridDocumentData($element);
                 } else {
                     if ($element instanceof Asset) {
                         $data = Asset_Service::gridAssetData($element);
                     }
                 }
             }
             $elements[] = $data;
         } else {
             //TODO: any message that view is blocked?
             //$data = Element_Service::gridElementData($element);
         }
     }
     // only get the real total-count when the limit parameter is given otherwise use the default limit
     if ($this->_getParam("limit")) {
         $totalMatches = $searcherList->getTotalCount();
     } else {
         $totalMatches = count($elements);
     }
     $this->_helper->json(array("data" => $elements, "success" => true, "total" => $totalMatches));
     $this->removeViewRenderer();
 }
Example #5
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);
     }
 }