public function xliffExportAction()
{
$id = $this->getParam("id");
$data = \Zend_Json::decode($this->getParam("data"));
$source = $this->getParam("source");
$target = $this->getParam("target");
$exportFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/" . $id . ".xliff";
if (!is_file($exportFile)) {
// create initial xml file structure
File::put($exportFile, '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<xliff version="1.2"></xliff>');
}
$xliff = simplexml_load_file($exportFile, null, LIBXML_NOCDATA);
foreach ($data as $el) {
$element = Element\Service::getElementById($el["type"], $el["id"]);
$file = $xliff->addChild('file');
$file->addAttribute('original', Element\Service::getElementType($element) . '-' . $element->getId());
$file->addAttribute('source-language', $source);
$file->addAttribute('target-language', $target);
$file->addAttribute('datatype', "html");
$file->addAttribute('tool', "pimcore");
$file->addAttribute('category', Element\Service::getElementType($element));
$header = $file->addChild('header');
$body = $file->addChild('body');
$addedElements = false;
// elements
if ($element instanceof Document) {
$elements = [];
$doc = $element;
// get also content of inherited document elements
while ($doc) {
if (method_exists($doc, "getElements")) {
$elements = array_merge($elements, $doc->getElements());
}
if (method_exists($doc, "getContentMasterDocument")) {
$doc = $doc->getContentMasterDocument();
} else {
$doc = null;
}
}
foreach ($elements as $tag) {
if (in_array($tag->getType(), ["wysiwyg", "input", "textarea", "image"])) {
if ($tag->getType() == "image") {
$content = $tag->getText();
} else {
$content = $tag->getData();
}
if (is_string($content)) {
$contentCheck = trim(strip_tags($content));
if (!empty($contentCheck)) {
$this->addTransUnitNode($body, "tag~-~" . $tag->getName(), $content, $source);
$addedElements = true;
}
}
}
}
if ($element instanceof Document\Page) {
$data = ["title" => $element->getTitle(), "description" => $element->getDescription()];
foreach ($data as $key => $content) {
if (!empty($content)) {
$this->addTransUnitNode($body, "settings~-~" . $key, $content, $source);
$addedElements = true;
}
}
}
} elseif ($element instanceof Object\Concrete) {
if ($fd = $element->getClass()->getFieldDefinition("localizedfields")) {
$definitions = $fd->getFielddefinitions();
$locale = new \Zend_Locale(str_replace("-", "_", $source));
if (Tool::isValidLanguage((string) $locale)) {
$locale = (string) $locale;
} else {
$locale = $locale->getLanguage();
}
foreach ($definitions as $definition) {
// check allowed datatypes
if (!in_array($definition->getFieldtype(), ["input", "textarea", "wysiwyg"])) {
continue;
}
$content = $element->{"get" . ucfirst($definition->getName())}($locale);
if (!empty($content)) {
$this->addTransUnitNode($body, "localizedfield~-~" . $definition->getName(), $content, $source);
$addedElements = true;
}
}
}
}
// properties
$properties = $element->getProperties();
if (is_array($properties)) {
foreach ($properties as $property) {
if ($property->getType() == "text" && !$property->isInherited()) {
// exclude text properties
if ($element instanceof Document) {
if (in_array($property->getName(), ["language", "navigation_target", "navigation_exclude", "navigation_class", "navigation_anchor", "navigation_parameters", "navigation_relation", "navigation_accesskey", "navigation_tabindex"])) {
continue;
}
}
$content = $property->getData();
if (!empty($content)) {
$this->addTransUnitNode($body, "property~-~" . $property->getName(), $content, $source);
$addedElements = true;
}
}
}
}
// remove file if it is empty
if (!$addedElements) {
$file = dom_import_simplexml($file);
$file->parentNode->removeChild($file);
}
}
$xliff->asXML($exportFile);
$this->_helper->json(["success" => true]);
}