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 getAction() { $user = User::getById(intval($this->_getParam("id"))); $userObjects = Object_Service::getObjectsReferencingUser($user->getId()); $userObjectData = array(); $currentUser = Zend_Registry::get("pimcore_user"); foreach ($userObjects as $o) { $hasHidden = false; $o->getUserPermissions($currentUser); if ($o->isAllowed("list")) { $userObjectData[] = array("path" => $o->getFullPath(), "id" => $o->getId(), "subtype" => $o->getClass()->getName()); } else { $hasHidden = true; } } $user->setPassword(null); $conf = Pimcore_Config::getSystemConfig(); $this->_helper->json(array("wsenabled" => $conf->webservice->enabled, "user" => $user->getIterator(), "objectDependencies" => array("hasHidden" => $hasHidden, "dependencies" => $userObjectData))); }
/** * @return mixed */ public function getValueFromParent($key) { $parent = Object_Service::hasInheritableParentObject($this->getObject()); if (!empty($parent)) { $containerGetter = "get" . ucfirst($this->fieldname); $brickGetter = "get" . ucfirst($this->getType()); $getter = "get" . ucfirst($key); if ($parent->{$containerGetter}()->{$brickGetter}()) { return $parent->{$containerGetter}()->{$brickGetter}()->{$getter}(); } } return null; }
/** * @param int $id * @return Webservice_Data_Object_Concrete_Out */ public function getObjectConcreteById($id) { try { $object = Object_Concrete::getById($id); if ($object instanceof Object_Concrete) { // load all data (eg. lazy loaded fields like multihref, object, ...) Object_Service::loadAllObjectFields($object); $apiObject = Webservice_Data_Mapper::map($object, "Webservice_Data_Object_Concrete_Out", "out"); return $apiObject; } throw new Exception("Object with given ID (" . $id . ") does not exist."); } catch (Exception $e) { Logger::error($e); throw $e; } }
public function correctPath() { // set path if ($this->getId() != 1) { // not for the root node $parent = Object_Abstract::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->setO_parentId(1); $this->setO_path("/"); } } if (Object_Service::pathExists($this->getFullPath())) { $duplicate = Object_Abstract::getByPath($this->getFullPath()); if ($duplicate instanceof Object_Abstract and $duplicate->getId() != $this->getId()) { throw new Exception("Duplicate full path [ " . $this->getFullPath() . " ] - cannot create object"); } } }
/** * gets recursively attribute data from parent and fills objectData and metaData * * @param $object * @param $key * @param $fielddefinition * @return void */ private function getDataForField($item, $key, $fielddefinition, $level, $baseObject, $getter, $objectFromVersion) { $result = new stdClass(); $parent = Object_Service::hasInheritableParentObject($baseObject); $valueGetter = "get" . ucfirst($key); // relations but not for objectsMetadata, because they have additional data which cannot be loaded directly from the DB if (!$objectFromVersion && method_exists($fielddefinition, "getLazyLoading") and $fielddefinition->getLazyLoading() and !$fielddefinition instanceof Object_Class_Data_ObjectsMetadata) { //lazy loading data is fetched from DB differently, so that not every relation object is instantiated if ($fielddefinition->isRemoteOwner()) { $refKey = $fielddefinition->getOwnerFieldName(); $refId = $fielddefinition->getOwnerClassId(); } else { $refKey = $key; } $relations = $item->getRelationData($refKey, !$fielddefinition->isRemoteOwner(), $refId); if (empty($relations) && !empty($parent)) { $parentItem = $parent->{"get" . ucfirst($this->getName())}(); if (!empty($parentItem)) { $parentItem = $parentItem->{$getter}(); if ($parentItem) { return $this->getDataForField($parentItem, $key, $fielddefinition, $level + 1, $parent, $getter, $objectFromVersion); } } } $data = array(); if ($fielddefinition instanceof Object_Class_Data_Href) { $data = $relations[0]; } else { foreach ($relations as $rel) { if ($fielddefinition instanceof Object_Class_Data_Objects) { $data[] = array($rel["id"], $rel["path"], $rel["subtype"]); } else { $data[] = array($rel["id"], $rel["path"], $rel["type"], $rel["subtype"]); } } } $result->objectData = $data; $result->metaData['objectid'] = $baseObject->getId(); $result->metaData['inherited'] = $level != 0; } else { $value = null; if (!empty($item)) { $value = $fielddefinition->getDataForEditmode($item->{$valueGetter}(), $baseObject); } if (empty($value) && !empty($parent)) { $parentItem = $parent->{"get" . ucfirst($this->getName())}()->{$getter}(); if (!empty($parentItem)) { return $this->getDataForField($parentItem, $key, $fielddefinition, $level + 1, $parent, $getter, $objectFromVersion); } } $result->objectData = $value; $result->metaData['objectid'] = $baseObject->getId(); $result->metaData['inherited'] = $level != 0; } return $result; }
public function getVariantsAction() { // get list of variants if ($this->_getParam("xaction") == "update") { $data = Zend_Json::decode($this->_getParam("data")); // save $object = Object_Abstract::getById($data["id"]); $objectData = array(); foreach ($data as $key => $value) { $parts = explode("~", $key); if (count($parts) > 1) { $brickType = $parts[0]; $brickKey = $parts[1]; $brickField = Object_Service::getFieldForBrickType($object->getClass(), $brickType); $fieldGetter = "get" . ucfirst($brickField); $brickGetter = "get" . ucfirst($brickType); $valueSetter = "set" . ucfirst($brickKey); $brick = $object->{$fieldGetter}()->{$brickGetter}(); if (empty($brick)) { $classname = "Object_Objectbrick_Data_" . ucfirst($brickType); $brickSetter = "set" . ucfirst($brickType); $brick = new $classname($object); $object->{$fieldGetter}()->{$brickSetter}($brick); } $brick->{$valueSetter}($value); } else { $objectData[$key] = $value; } } $object->setValues($objectData); try { $object->save(); $this->_helper->json(array("data" => Object_Service::gridObjectData($object, $this->_getParam("fields")), "success" => true)); } catch (Exception $e) { $this->_helper->json(array("success" => false, "message" => $e->getMessage())); } } else { $parentObject = Object_Concrete::getById($this->_getParam("objectId")); if (empty($parentObject)) { throw new Exception("No Object found with id " . $this->_getParam("objectId")); } $class = $parentObject->getO_class(); $className = $parentObject->getO_class()->getName(); $start = 0; $limit = 15; $orderKey = "o_id"; $order = "ASC"; $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]; } } } if ($this->_getParam("limit")) { $limit = $this->_getParam("limit"); } if ($this->_getParam("start")) { $start = $this->_getParam("start"); } if ($this->_getParam("sort")) { if ($this->_getParam("sort") == "fullpath") { $orderKey = array("o_path", "o_key"); } else { if ($this->_getParam("sort") == "id") { $orderKey = "o_id"; } else { if ($this->_getParam("sort") == "published") { $orderKey = "o_published"; } else { if ($this->_getParam("sort") == "modificationDate") { $orderKey = "o_modificationDate"; } else { if ($this->_getParam("sort") == "creationDate") { $orderKey = "o_creationDate"; } else { $orderKey = $this->_getParam("sort"); } } } } } } if ($this->_getParam("dir")) { $order = $this->_getParam("dir"); } $listClass = "Object_" . ucfirst($className) . "_List"; $conditionFilters = "o_parentId = " . $parentObject->getId(); // create filter condition if ($this->_getParam("filter")) { $conditionFilters .= Object_Service::getFilterCondition($this->_getParam("filter"), $class); } if ($this->_getParam("condition")) { $conditionFilters .= " AND (" . $this->_getParam("condition") . ")"; } $list = new $listClass(); if (!empty($bricks)) { foreach ($bricks as $b) { $list->addObjectbrick($b); } } $list->setCondition($conditionFilters); $list->setLimit($limit); $list->setOffset($start); $list->setOrder($order); $list->setOrderKey($orderKey); $list->setIgnoreLocale(true); $list->setObjectTypes(array(Object_Abstract::OBJECT_TYPE_VARIANT)); $list->load(); $objects = array(); foreach ($list->getObjects() as $object) { $o = Object_Service::gridObjectData($object, $fields); $objects[] = $o; } $this->_helper->json(array("data" => $objects, "success" => true, "total" => $list->getTotalCount())); } }
/** * @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); } } } }
public function batchAction() { $success = true; try { $object = Object_Abstract::getById($this->_getParam("job")); if ($object) { $className = $object->getO_className(); $class = Object_Class::getByName($className); $value = $this->_getParam("value"); if ($this->_getParam("valueType") == "object") { $value = Zend_Json::decode($value); } $name = $this->_getParam("name"); $parts = explode("~", $name); // check for bricks if (count($parts) > 1) { $brickType = $parts[0]; $brickKey = $parts[1]; $brickField = Object_Service::getFieldForBrickType($object->getClass(), $brickType); $fieldGetter = "get" . ucfirst($brickField); $brickGetter = "get" . ucfirst($brickType); $valueSetter = "set" . ucfirst($brickKey); $brick = $object->{$fieldGetter}()->{$brickGetter}(); if (empty($brick)) { $classname = "Object_Objectbrick_Data_" . ucfirst($brickType); $brickSetter = "set" . ucfirst($brickType); $brick = new $classname($object); $object->{$fieldGetter}()->{$brickSetter}($brick); } $brickClass = Object_Objectbrick_Definition::getByKey($brickType); $field = $brickClass->getFieldDefinition($brickKey); $brick->{$valueSetter}($field->getDataFromEditmode($value, $object)); } else { // everything else $field = $class->getFieldDefinition($name); if ($field) { $object->setValue($name, $field->getDataFromEditmode($value, $object)); } else { // seems to be a system field, this is actually only possible for the "published" field yet if ($name == "published") { if ($value == "false" || empty($value)) { $object->setPublished(false); } else { $object->setPublished(true); } } } } try { // don't check for mandatory fields here $object->setOmitMandatoryCheck(true); $object->save(); $success = true; } catch (Exception $e) { $this->_helper->json(array("success" => false, "message" => $e->getMessage())); } } else { Logger::debug("ObjectController::batchAction => There is no object left to update."); $this->_helper->json(array("success" => false, "message" => "ObjectController::batchAction => There is no object left to update.")); } } catch (Exception $e) { Logger::err($e); $this->_helper->json(array("success" => false, "message" => $e->getMessage())); } $this->_helper->json(array("success" => $success)); }
/** * @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(); }
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); } }
/** * @static * @param Element_Interface $element * @return Element_Interface */ public static function loadAllFields(Element_Interface $element) { if ($element instanceof Document) { Document_Service::loadAllDocumentFields($element); } else { if ($element instanceof Object_Concrete) { Object_Service::loadAllObjectFields($element); } } return $element; }
public function getAction() { if (intval($this->_getParam("id")) < 1) { $this->_helper->json(array("success" => false)); } $user = User::getById(intval($this->_getParam("id"))); // workspaces $types = array("asset", "document", "object"); foreach ($types as $type) { $workspaces = $user->{"getWorkspaces" . ucfirst($type)}(); foreach ($workspaces as $workspace) { $el = Element_Service::getElementById($type, $workspace->getCid()); if ($el) { // direct injection => not nice but in this case ok ;-) $workspace->path = $el->getFullPath(); } } } // object <=> user dependencies $userObjects = Object_Service::getObjectsReferencingUser($user->getId()); $userObjectData = array(); $currentUser = Zend_Registry::get("pimcore_user"); foreach ($userObjects as $o) { $hasHidden = false; if ($o->isAllowed("list")) { $userObjectData[] = array("path" => $o->getFullPath(), "id" => $o->getId(), "subtype" => $o->getClass()->getName()); } else { $hasHidden = true; } } // get available permissions $availableUserPermissionsList = new User_Permission_Definition_List(); $availableUserPermissions = $availableUserPermissionsList->load(); // get available roles $roles = array(); $list = new User_Role_List(); $list->setCondition("`type` = ?", array("role")); $list->load(); $roles = array(); if (is_array($list->getItems())) { foreach ($list->getItems() as $role) { $roles[] = array($role->getId(), $role->getName()); } } $user->setPassword(null); $conf = Pimcore_Config::getSystemConfig(); $this->_helper->json(array("success" => true, "wsenabled" => $conf->webservice->enabled, "user" => $user, "roles" => $roles, "permissions" => $user->generatePermissionList(), "availablePermissions" => $availableUserPermissions, "objectDependencies" => array("hasHidden" => $hasHidden, "dependencies" => $userObjectData))); }