public function reverseMap($object) { $keys = get_object_vars($this); foreach ($keys as $key => $value) { $method = "set" . $key; if (method_exists($object, $method)) { $object->{$method}($value); } } $object->setProperties(null); if (is_array($this->properties)) { foreach ($this->properties as $propertyWs) { $dat = $propertyWs->data; $type = $propertyWs->type; if (in_array($type, array("object", "document", "asset"))) { $dat = Element_Service::getElementById($propertyWs->type, $propertyWs->data); if (is_numeric($propertyWs->data) and !$dat) { throw new Exception("cannot import property [ " . $propertyWs->name . " ] because it references unknown " . $propertyWs->type); } } else { if ($type == "date") { $dat = new Pimcore_Date(strtotime($propertyWs->data)); } else { $dat = $propertyWs->data; } } $object->setProperty($propertyWs->name, $propertyWs->type, $dat, $propertyWs->inherited); } } }
public function addAction() { $element = Element_Service::getElementById($this->_getParam("type"), $this->_getParam("id")); if ($element) { Element_Recyclebin_Item::create($element, $this->getUser()); $this->_helper->json(array("success" => true)); } else { $this->_helper->json(array("success" => false)); } }
public function setElements() { if (empty($this->elements)) { $this->elements = array(); foreach ($this->elementIds as $elementId) { $el = Element_Service::getElementById($elementId["type"], $elementId["id"]); if ($el instanceof Element_Interface) { $this->elements[] = $el; } } } }
/** * Get the data for the object from database for the given id * * @param integer $cid * @param string $ctype * @return void */ public function getByElement($cid, $ctype) { $data = $this->db->fetchRow("SELECT * FROM edit_lock WHERE cid = ? AND ctype = ?", array($cid, $ctype)); if (!$data["id"]) { throw new Exception("Lock with cid " . $cid . " and ctype " . $ctype . " not found"); } $this->assignVariablesToModel($data); // add elements path $element = Element_Service::getElementById($ctype, $cid); if ($element) { $this->model->setCpath($element->getFullpath()); } }
/** * renews all references, for example after unserializing an Element_Interface * @param Document|Asset|Object_Abstract $data * @return mixed */ public static function renewReferences($data, $initial = true) { if (is_array($data)) { foreach ($data as &$value) { $value = self::renewReferences($value, false); } return $data; } else { if (is_object($data)) { if ($data instanceof Element_Interface && !$initial) { return Element_Service::getElementById(Element_Service::getElementType($data), $data->getId()); } else { // if this is the initial element set the correct path and key if ($data instanceof Element_Interface && $initial) { $originalElement = Element_Service::getElementById(Element_Service::getElementType($data), $data->getId()); if ($originalElement) { if ($data instanceof Asset) { $data->setFilename($originalElement->getFilename()); } else { if ($data instanceof Document) { $data->setKey($originalElement->getKey()); } else { if ($data instanceof Object_Abstract) { $data->setKey($originalElement->getKey()); } } } $data->setPath($originalElement->getPath()); } } $properties = get_object_vars($data); foreach ($properties as $name => $value) { $data->{$name} = self::renewReferences($value, false); } return $data; } } } return $data; }
/** * @static * @param $text * @return array */ public static function getElementsInWysiwyg($text) { $hash = "elements_wysiwyg_text_" . md5($text); if (Zend_Registry::isRegistered($hash)) { return Zend_Registry::get($hash); } $elements = array(); $matches = self::getElementsTagsInWysiwyg($text); if (count($matches[2]) > 0) { for ($i = 0; $i < count($matches[2]); $i++) { preg_match("/[0-9]+/", $matches[2][$i], $idMatches); preg_match("/asset|object|document/", $matches[3][$i], $typeMatches); $id = $idMatches[0]; $type = $typeMatches[0]; $element = Element_Service::getElementById($type, $id); if ($id && $type && $element instanceof Element_Interface) { $elements[] = array("id" => $id, "type" => $type, "element" => $element); } } } Zend_Registry::set($hash, $elements); return $elements; }
/** * @return bool */ public function sanityCheck() { $sane = true; if ($this->id) { $el = Element_Service::getElementById($this->type, $this->id); if (!$el instanceof Element_Interface) { $sane = false; Logger::notice("Detected insane relation, removing reference to non existent " . $this->type . " with id [" . $this->id . "]"); $this->id = null; $this->type = null; $this->subtype = null; $this->element = null; } } return $sane; }
public function doExportJobsAction() { $exportSession = new Zend_Session_Namespace("element_export"); $exportName = "export_" . Zend_Session::getId(); $exportDir = PIMCORE_WEBSITE_PATH . "/var/tmp/" . $exportName; if (!$exportSession->elements) { $exportSession->type = $this->_getParam("type"); $exportSession->id = $this->_getParam("id"); $exportSession->includeRelations = (bool) $this->_getParam("includeRelations"); $exportSession->recursive = (bool) $this->_getParam("recursive"); $exportSession->counter = 0; $element = Element_Service::getElementById($exportSession->type, $exportSession->id); $exportSession->rootPath = $element->getPath(); $exportSession->rootType = Element_Service::getType($element); $exportSession->elements = array(Element_Service::getType($element) . "_" . $element->getId() => array("elementType" => Element_Service::getType($element), "element" => $element->getId(), "recursive" => $exportSession->recursive)); $exportSession->apiElements = array(); if (is_dir($exportDir)) { recursiveDelete($exportDir); } mkdir($exportDir, 0755, true); $this->_helper->json(array("more" => true, "totalElementsDone" => 0, "totalElementsFound" => 0)); } else { $data = array_pop($exportSession->elements); $element = Element_Service::getElementById($data["elementType"], $data["element"]); $recursive = $data["recursive"]; $exportService = new Element_Export_Service(); $apiElement = $exportService->getApiElement($element); $exportSession->foundRelations = $exportService->extractRelations($element, array_keys($exportSession->apiElements), $recursive, $exportSession->includeRelations); //make path relative to root if (Element_Service::getType($element) == $exportSession->rootType and $exportSession->rootPath == $element->getPath()) { $apiElement->path = ""; } else { if (Element_Service::getType($element) == $exportSession->rootType and strpos($element->getPath(), $exportSession->rootPath) === 0) { if ($exportSession->rootPath === "/") { $len = 1; } else { $len = strlen($exportSession->rootPath) - 1; } $apiElement->path = substr($element->getPath(), $len); } else { $apiElement->path = $element->getPath(); } } $path = $apiElement->path; //convert the Webservice _Out element to _In elements $outClass = get_class($apiElement); $inClass = str_replace("_Out", "_In", $outClass); $apiElementIn = new $inClass(); $objectVars = get_object_vars($apiElementIn); foreach ($objectVars as $var => $value) { if (property_exists(get_class($apiElement), $var)) { $apiElementIn->{$var} = $apiElement->{$var}; } } //remove parentId, add path $apiElementIn->parentId = null; $apiElement = $apiElementIn; $key = Element_Service::getType($element) . "_" . $element->getId(); $exportSession->apiElements[$key] = array("element" => $apiElement, "path" => $path); $exportFile = $exportDir . "/" . $exportSession->counter . "_" . $key; file_put_contents($exportFile, serialize(array("element" => $apiElement, "path" => $path))); chmod($exportFile, 0766); $exportSession->elements = array_merge($exportSession->elements, $exportSession->foundRelations); if (count($exportSession->elements) == 0) { $exportArchive = $exportDir . ".zip"; if (is_file($exportArchive)) { unlink($exportArchive); } $zip = new ZipArchive(); $created = $zip->open($exportArchive, ZipArchive::CREATE); if ($created === TRUE) { $dh = opendir($exportDir); while ($file = readdir($dh)) { if ($file != '.' && $file != '..') { $fullFilePath = $exportDir . "/" . $file; if (is_file($fullFilePath)) { $zip->addFile($fullFilePath, str_replace($exportDir . "/", "", $fullFilePath)); } } } closedir($dh); $zip->close(); } } $exportSession->counter++; $this->_helper->json(array("more" => count($exportSession->elements) != 0, "totalElementsDone" => count($exportSession->apiElements), "totalElementsFound" => count($exportSession->foundRelations))); } }
/** * @see Object_Class_Data::getDataFromEditmode * @param array $data * @param null|Object_Abstract $object * @return array */ public function getDataFromEditmode($data, $object = null) { //if not set, return null if ($data === null or $data === FALSE) { return null; } $elements = array(); $data = explode(",", $data); if (is_array($data) && count($data) > 0) { foreach ($data as $id) { $elements[] = Element_Service::getElementById("object", $id); } } //must return array if data shall be set return $elements; }
/** * converts data to be imported via webservices * @param mixed $value * @return mixed */ public function getFromWebserviceImport($value) { if (empty($value)) { return null; } else { if (is_array($value) and !empty($value['text']) and !empty($value['direct'])) { $link = new Object_Data_Link(); foreach ($value as $key => $value) { $method = "set" . ucfirst($key); if (method_exists($link, $method)) { $link->{$method}($value); } else { throw new Exception("cannot get values from web service import - invalid data. Unknown Object_Data_Link setter [ " . $method . " ]"); } } return $link; } else { if (is_array($value) and !empty($value['text']) and !empty($value['internalType']) and !empty($value['internal'])) { $element = Element_Service::getElementById($value['internalType'], $value['internal']); if (!$element) { throw new Exception("cannot get values from web service import - referencing unknown internal element with type [ " . $value['internalType'] . " ] and id [ " . $value['internal'] . " ]"); } $link = new Object_Data_Link(); foreach ($value as $key => $value) { $method = "set" . ucfirst($key); if (method_exists($link, $method)) { $link->{$method}($value); } else { throw new Exception("cannot get values from web service import - invalid data. Unknown Object_Data_Link setter [ " . $method . " ]"); } } return $link; } else { throw new Exception("cannot get values from web service import - invalid data"); } } } }
public function noteListAction() { $list = new Element_Note_List(); $list->setLimit($this->_getParam("limit")); $list->setOffset($this->_getParam("start")); if ($this->_getParam("sort")) { $list->setOrderKey($this->_getParam("sort")); $list->setOrder($this->_getParam("dir")); } else { $list->setOrderKey("date"); $list->setOrder("DESC"); } $conditions = array(); if ($this->_getParam("filter")) { $conditions[] = "(`title` LIKE " . $list->quote("%" . $this->_getParam("filter") . "%") . " OR `description` LIKE " . $list->quote("%" . $this->_getParam("filter") . "%") . " OR `type` LIKE " . $list->quote("%" . $this->_getParam("filter") . "%") . ")"; } if ($this->_getParam("cid") && $this->_getParam("ctype")) { $conditions[] = "(cid = " . $list->quote($this->_getParam("cid")) . " AND ctype = " . $list->quote($this->_getParam("ctype")) . ")"; } if (!empty($conditions)) { $list->setCondition(implode(" AND ", $conditions)); } $list->load(); $notes = array(); foreach ($list->getNotes() as $note) { $cpath = ""; if ($note->getCid() && $note->getCtype()) { if ($element = Element_Service::getElementById($note->getCtype(), $note->getCid())) { $cpath = $element->getFullpath(); } } $e = array("id" => $note->getId(), "type" => $note->getType(), "cid" => $note->getCid(), "ctype" => $note->getCtype(), "cpath" => $cpath, "date" => $note->getDate(), "title" => $note->getTitle(), "description" => $note->getDescription()); // prepare key-values $keyValues = array(); if (is_array($note->getData())) { foreach ($note->getData() as $name => $d) { $type = $d["type"]; $data = $d["data"]; if ($type == "document" || $type == "object" || $type == "asset") { if ($d["data"] instanceof Element_Interface) { $data = array("id" => $d["data"]->getId(), "path" => $d["data"]->getFullpath(), "type" => $d["data"]->getType()); } } else { if ($type == "date") { if ($d["data"] instanceof Zend_Date) { $data = $d["data"]->getTimestamp(); } } } $keyValue = array("type" => $type, "name" => $name, "data" => $data); $keyValues[] = $keyValue; } } $e["data"] = $keyValues; // prepare user data if ($note->getUser()) { $user = User::getById($note->getUser()); if ($user) { $e["user"] = array("id" => $user->getId(), "name" => $user->getName()); } else { $e["user"] = ""; } } $notes[] = $e; } $this->_helper->json(array("data" => $notes, "success" => true, "total" => $list->getTotalCount())); }
public function doImportJobsAction() { $importSession = new Zend_Session_Namespace("element_import"); if (!$importSession->elementCounter) { $importSession->elementCounter = 0; } if (!$importSession->idMapping) { $importSession->idMapping = array(); } $this->removeViewRenderer(); $importDir = PIMCORE_TEMPORARY_DIRECTORY . "/import_" . Zend_Session::getId(); $file = $this->_getParam("file"); $task = $this->_getParam("task"); $parentId = $this->_getParam("parentId"); $type = $this->_getParam("type"); $overwrite = $this->_getParam("overwrite"); if ($overwrite == 1) { $overwrite = true; } else { $overwrite = false; } $importService = new Element_Import_Service($this->getUser()); if ($type == "document") { $rootElement = Document::getById($parentId); } else { if ($type == "object") { $rootElement = Object_Abstract::getById($parentId); } else { if ($type == "asset") { $rootElement = Asset::getById($parentId); } } } if (!$rootElement) { throw new Exception("Invalid root element for import"); } $importData = file_get_contents($importDir . "/" . $file); $apiData = Pimcore_Tool_Serialize::unserialize($importData); //first run - just save elements so that they are there if ($task == "create") { $apiElement = $apiData["element"]; $path = $apiData["path"]; $element = $importService->create($rootElement, $file, $path, $apiElement, $overwrite, $importSession->elementCounter); //set actual ID //store id mapping $importSession->idMapping[Element_Service::getType($element)][$apiElement->id] = $element->getId(); $importSession->elementCounter++; $importFile = $importDir . "/" . $file; file_put_contents($importFile, Pimcore_Tool_Serialize::serialize($apiData)); chmod($importFile, 0766); } else { if ($task == "resolveRelations") { $apiElement = $apiData["element"]; $type = $this->findElementType($apiElement); $importService->correctElementIdRelations($apiElement, $type, $importSession->idMapping); //correct relations if ($apiElement instanceof Webservice_Data_Object_Concrete) { $importService->correctObjectRelations($apiElement, $importSession->idMapping); } else { if ($apiElement instanceof Webservice_Data_Document_PageSnippet) { $importService->correctDocumentRelations($apiElement, $importSession->idMapping); } else { if ($apiElement instanceof Webservice_Data_Document_Link and $apiElement->internal) { $apiElement->target = $importSession->idMapping[$apiElement->internalType][$apiElement->target]; } } } $importFile = $importDir . "/" . $file; file_put_contents($importFile, Pimcore_Tool_Serialize::serialize($apiData)); chmod($importFile, 0766); } else { if ($task == "update") { $apiElement = $apiData["element"]; try { $this->updateImportElement($apiElement, $importService); } catch (Exception $e) { $type = $this->findElementType($apiElement); $parent = Element_Service::getElementById($type, $apiElement->parentId); $apiElement->key = $this->getImportCopyName($parent->getFullPath(), $apiElement->key, $apiElement->id, $type); //try again with different key $this->updateImportElement($apiElement, $importService); } } else { if ($task == "cleanup") { recursiveDelete($importDir); } } } } $this->_helper->json(array("success" => true)); //p_r($importService->getImportInfo()); }
/** * @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(); }
/** * Checks if data is valid for current data field * * @param mixed $data * @param boolean $omitMandatoryCheck * @throws Exception */ public function checkValidity($data, $omitMandatoryCheck = false) { if (!$omitMandatoryCheck and $this->getMandatory() and empty($data)) { throw new Exception("Empty mandatory field [ " . $this->getName() . " ]"); } $dependencies = Pimcore_Tool_Text::getDependenciesOfWysiwygText($data); if (is_array($dependencies)) { foreach ($dependencies as $key => $value) { $el = Element_Service::getElementById($value['type'], $value['id']); if (!$el) { throw new Exception("invalid dependency in wysiwyg text"); } } } }
/** * @see Object_Class_Data::getDataFromEditmode * @param array $data * @param null|Object_Abstract $object * @return Asset|Document|Object_Abstract */ public function getDataFromEditmode($data, $object = null) { return Element_Service::getElementById("object", $data); }
/** * @see Object_Class_Data::getDataFromEditmode * @param array $data * @param null|Object_Abstract $object * @return Asset|Document|Object_Abstract */ public function getDataFromEditmode($data, $object = null) { if ($data["id"] && $data["type"]) { return Element_Service::getElementById($data["type"], $data["id"]); } return null; }
/** * converts data to be imported via webservices * @param mixed $value * @return mixed */ public function getFromWebserviceImport($value) { if (empty($value)) { return null; } else { if (is_array($value)) { $hrefs = array(); foreach ($value as $href) { if (is_array($href) and key_exists("id", $href) and key_exists("type", $href)) { $e = Element_Service::getElementById($href["type"], $href["id"]); if ($e instanceof Element_Interface) { $hrefs[] = $e; } else { throw new Exception("cannot get values from web service import - unknown element of type [ " . $href["type"] . " ] with id [" . $href["id"] . "] is referenced"); } } } return $hrefs; } else { throw new Exception("cannot get values from web service import - invalid data"); } } }
public function roleGetAction() { $role = User_Role::getById(intval($this->_getParam("id"))); // workspaces $types = array("asset", "document", "object"); foreach ($types as $type) { $workspaces = $role->{"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(); } } } // get available permissions $availableUserPermissionsList = new User_Permission_Definition_List(); $availableUserPermissions = $availableUserPermissionsList->load(); $this->_helper->json(array("success" => true, "role" => $role, "permissions" => $role->generatePermissionList(), "availablePermissions" => $availableUserPermissions)); }