public function extractRelations($element, $apiElementKeys, $recursive, $includeRelations) { $foundRelations = array(); if ($includeRelations) { $dependency = $element->getDependencies(); if ($dependency) { foreach ($dependency->getRequires() as $r) { if ($e = Element_Service::getDependedElement($r)) { if ($element->getId() != $e->getId() and !in_array(Element_Service::getElementType($e) . "_" . $e->getId(), $apiElementKeys)) { $foundRelations[Element_Service::getElementType($e) . "_" . $e->getId()] = array("elementType" => Element_Service::getType($e), "element" => $e->getId(), "recursive" => false); } } } } } $childs = $element->getChilds(); if ($recursive and $childs) { foreach ($childs as $child) { if (!in_array(Element_Service::getType($child) . "_" . $child->getId(), $apiElementKeys)) { $foundRelations[Element_Service::getType($child) . "_" . $child->getId()] = array("elementType" => Element_Service::getType($child), "element" => $child->getId(), "recursive" => $recursive); } } } return $foundRelations; }
/** * @param Element_Interface $webResource * @return void */ public function __construct($webResource) { $this->id = $webResource->getId(); if ($webResource instanceof Element_Interface) { $this->type = Element_Service::getType($webResource); } else { $this->type = "unknown"; } }
/** * converts data to be exposed via webservices * @param string $object * @return mixed */ public function getForWebserviceExport($object) { $key = $this->getName(); $getter = "get" . ucfirst($key); $data = $object->{$getter}(); if ($data instanceof Element_Interface) { return array("type" => Element_Service::getType($data), "subtype" => $data->getType(), "id" => $data->getId()); } else { return null; } }
/** * @param Element_interface $element * @return array */ public static function gridElementData(Element_Interface $element) { $data = array("id" => $element->getId(), "fullpath" => $element->getFullPath(), "type" => Element_Service::getType($element), "subtype" => $element->getType(), "filename" => self::getFilename($element), "creationDate" => $element->getCreationDate(), "modificationDate" => $element->getModificationDate()); if (method_exists($element, "isPublished")) { $data["published"] = $element->isPublished(); } else { $data["published"] = true; } return $data; }
/** * @throws Exception * @param $rootElement * @param $apiKey * @param $path * @param $apiElement * @param bool $overwrite * @param $elementCounter * @return Element_Interface */ public function create($rootElement, $apiKey, $path, $apiElement, $overwrite, $elementCounter) { //correct relative path if (strpos($path, "/") !== 0) { $path = $rootElement->getFullPath() . "/" . $path; } $type = $apiElement->type; if ($apiElement instanceof Webservice_Data_Asset) { $className = "Asset_" . ucfirst($type); $parentClassName = "Asset"; $maintype = "asset"; $fullPath = $path . $apiElement->filename; } else { if ($apiElement instanceof Webservice_Data_Object) { $maintype = "object"; if ($type == "object") { $className = "Object_" . ucfirst($apiElement->className); if (!class_exists($className)) { throw new Exception("Unknown class [ " . $className . " ]"); } } else { $className = "Object_" . ucfirst($type); } $parentClassName = "Object_Abstract"; $fullPath = $path . $apiElement->key; } else { if ($apiElement instanceof Webservice_Data_Document) { $maintype = "document"; $className = "Document_" . ucfirst($type); $parentClassName = "Document"; $fullPath = $path . $apiElement->key; } else { throw new Exception("Unknown import element"); } } } $existingElement = $className::getByPath($fullPath); if ($overwrite && $existingElement) { $apiElement->parentId = $existingElement->getParentId(); return $existingElement; } $element = new $className(); $element->setId(null); $element->setCreationDate(time()); if ($element instanceof Asset) { $element->setFilename($apiElement->filename); $element->setData(base64_decode($apiElement->data)); } else { if ($element instanceof Object_Concrete) { $element->setKey($apiElement->key); $element->setO_className($apiElement->className); $class = Object_Class::getByName($apiElement->className); if (!$class instanceof Object_Class) { throw new Exception("Unknown object class [ " . $apiElement->className . " ] "); } $element->setO_classId($class->getId()); } else { $element->setKey($apiElement->key); } } $this->setModificationParams($element, true); $key = $element->getKey(); if (empty($key) and $apiElement->id == 1) { if ($element instanceof Asset) { $element->setFilename("home_" . uniqid()); } else { $element->setKey("home_" . uniqid()); } } else { if (empty($key)) { throw new Exception("Cannot create element without key "); } } $parent = $parentClassName::getByPath($path); if (Element_Service::getType($rootElement) == $maintype and $parent) { $element->setParentId($parent->getId()); $apiElement->parentId = $parent->getId(); $existingElement = $parentClassName::getByPath($parent->getFullPath() . "/" . $element->getKey()); if ($existingElement) { //set dummy key to avoid duplicate paths if ($element instanceof Asset) { $element->setFilename(str_replace("/", "_", $apiElement->path) . uniqid() . "_" . $elementCounter . "_" . $element->getFilename()); } else { $element->setKey(str_replace("/", "_", $apiElement->path) . uniqid() . "_" . $elementCounter . "_" . $element->getKey()); } } } else { if (Element_Service::getType($rootElement) != $maintype) { //this is a related element - try to import it to it's original path or set the parent to home folder $potentialParent = $parentClassName::getByPath($path); //set dummy key to avoid duplicate paths if ($element instanceof Asset) { $element->setFilename(str_replace("/", "_", $apiElement->path) . uniqid() . "_" . $elementCounter . "_" . $element->getFilename()); } else { $element->setKey(str_replace("/", "_", $apiElement->path) . uniqid() . "_" . $elementCounter . "_" . $element->getKey()); } if ($potentialParent) { $element->setParentId($potentialParent->getId()); //set actual id and path for second run $apiElements[$apiKey]["path"] = $potentialParent->getFullPath(); $apiElement->parentId = $potentialParent->getId(); } else { $element->setParentId(1); //set actual id and path for second run $apiElements[$apiKey]["path"] = "/"; $apiElement->parentId = 1; } } else { $element->setParentId($rootElement->getId()); //set actual id and path for second run $apiElements[$apiKey]["path"] = $rootElement->getFullPath(); $apiElement->parentId = $rootElement->getId(); //set dummy key to avoid duplicate paths if ($element instanceof Asset) { $element->setFilename(str_replace("/", "_", $apiElement->path) . uniqid() . "_" . $elementCounter . "_" . $element->getFilename()); } else { $element->setKey(str_replace("/", "_", $apiElement->path) . uniqid() . "_" . $elementCounter . "_" . $element->getKey()); } } } //if element exists, make temp key permanent by setting it in apiElement if ($parentClassName::getByPath($fullPath)) { if ($element instanceof Asset) { $apiElement->filename = $element->getFilename(); } else { $apiElement->key = $element->getKey(); } } $element->save(); //todo save type and id for later rollback $this->importInfo[Element_Service::getType($element) . "_" . $element->getId()] = array("id" => $element->getId(), "type" => Element_Service::getType($element), "fullpath" => $element->getFullPath()); return $element; }
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))); } }
/** * converts data to be exposed via webservices * @param string $object * @return mixed */ public function getForWebserviceExport($object) { $key = $this->getName(); $getter = "get" . ucfirst($key); $data = $object->{$getter}(); if (is_array($data)) { $items = array(); foreach ($data as $eo) { if ($eo instanceof Element_Interface) { $items[] = array("type" => Element_Service::getType($eo), "subtype" => $eo->getType(), "id" => $eo->getId()); } } return $items; } else { return null; } }
/** * get correct type of object as string * @param mixed $data * @return void */ public function getObjectType($object = null) { if (!$object) { $object = $this->o; } if ($object instanceof Element_Interface) { return Element_Service::getType($object); } else { return false; } }
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()); }
/** * @param Element_Interface $element */ public function setElement(Element_Interface $element) { $this->setCid($element->getId()); $this->setCtype(Element_Service::getType($element)); }