/** * @param $sourceDocument * @param $intendedLanguage * @return bool|\Pimcore\Model\Document */ public static function getDocumentInOtherLanguage($sourceDocument, $intendedLanguage) { if ($sourceDocument instanceof \Pimcore\Model\Document) { $documentId = $sourceDocument->getId(); } else { $documentId = $sourceDocument; } $tSource = new Keys(); $tTarget = new Keys(); $select = $tSource->select()->from(array("s" => $tSource->info("name")), array())->from(array("t" => $tTarget->info("name")), array("document_id"))->where("s.document_id = ?", $documentId)->where("s.sourcePath = t.sourcePath")->where("t.language = ?", $intendedLanguage); $row = $tSource->fetchRow($select); if (!empty($row)) { return \Pimcore\Model\Document::getById($row->document_id); } else { return false; } }
/** * @param $parentId * @param $languageToAdd */ protected function addDocuments($parentId, $languageToAdd) { $list = new Listing(); $list->setCondition('parentId = ?', $parentId); $list = $list->load(); foreach ($list as $document) { // Create new document $targetParent = \Pimcore\Model\Document::getById(\Multilingual\Document::getDocumentIdInOtherLanguage($document->getParentId(), $languageToAdd)); /** @var Document_Page $target */ $target = clone $document; $target->id = null; $target->setParent($targetParent); // Only sync properties when it is allowed if (!$target->hasProperty('doNotSyncProperties') && !$document->hasProperty('doNotSyncProperties')) { $editableDocumentTypes = array('page', 'email', 'snippet'); if (in_array($document->getType(), $editableDocumentTypes)) { $target->setContentMasterDocument($document); } // Set the properties the same $sourceProperties = $document->getProperties(); /** @var string $key * @var Property $value */ foreach ($sourceProperties as $key => $value) { if (!$target->hasProperty($key)) { $propertyValue = $value->getData(); if ($value->getType() == 'document') { $propertyValue = \Multilingual\Document::getDocumentIdInOtherLanguage($value->getData()->getId(), $languageToAdd); } $target->setProperty($key, $value->getType(), $propertyValue, false, $value->getInheritable()); } } } $target->save(); // Add Link to other languages $t = new Keys(); $t->insert(array("document_id" => $target->getId(), "language" => $languageToAdd, "sourcePath" => $document->getFullPath())); // Check for children if (count($document->getChilds()) >= 1) { // Add the kids $this->addDocuments($document->getId(), $languageToAdd); } } }
/** * Event that handles the update of a Document * * @param $e \Zend_EventManager_Event * @throws Exception * @throws \Zend_Exception */ public function updateDocument($e) { // Check if this function is not in progress if (\Zend_Registry::isRegistered('Multilingual_' . __FUNCTION__) && \Zend_Registry::get('Multilingual_' . __FUNCTION__) == 1) { return; } // Lock this event-trigger \Zend_Registry::set('Multilingual_' . __FUNCTION__, 1); /**@var Document\Page $sourceDocument */ $sourceDocument = $e->getTarget(); // Get current language $sourceLanguage = $sourceDocument->getProperty('language'); // Get the Source Parent (we have to do it this way, due to a bug in Pimcore) $sourceParent = Document::getById($sourceDocument->getParentId()); // Update SourcePath in Multilingual table $t = new Keys(); $row = $t->fetchRow('document_id = ' . $sourceDocument->getId()); $t->update(array('sourcePath' => $sourceDocument->getFullPath()), 'sourcePath = "' . $row->sourcePath . '"'); // Update each language $languages = Tool::getValidLanguages(); foreach ($languages as $language) { if ($language != $sourceLanguage) { // Find the target document /** @var Document $targetDocument */ $targetDocument = \Multilingual\Document::getDocumentInOtherLanguage($sourceDocument, $language); if ($targetDocument) { // Find the parent // If the parent is the root, document, no need to do a lookup if ($sourceParent->getId() == 1) { $targetParent = $sourceParent; } else { $targetParent = \Multilingual\Document::getDocumentInOtherLanguage($sourceParent, $language); } // Only sync properties when it is allowed if (!$targetDocument->hasProperty('doNotSyncProperties') && !$sourceDocument->hasProperty('doNotSyncProperties')) { $typeHasChanged = false; // Set document type (for conversion) if ($targetDocument->getType() != $sourceDocument->getType()) { $typeHasChanged = true; $targetDocument->setType($sourceDocument->getType()); if ($targetDocument->getType() == "hardlink" || $targetDocument->getType() == "folder") { // remove navigation settings foreach (["name", "title", "target", "exclude", "class", "anchor", "parameters", "relation", "accesskey", "tabindex"] as $propertyName) { $targetDocument->removeProperty("navigation_" . $propertyName); } } // overwrite internal store to avoid "duplicate full path" error \Zend_Registry::set("document_" . $targetDocument->getId(), $targetDocument); } // Set the controller the same $editableDocumentTypes = array('page', 'email', 'snippet'); if (!$typeHasChanged && in_array($sourceDocument->getType(), $editableDocumentTypes)) { /** @var Document\Page $targetDocument */ $targetDocument->setController($sourceDocument->getController()); $targetDocument->setAction($sourceDocument->getAction()); } // Set the properties the same // But only if they have not been added already $sourceProperties = $sourceDocument->getProperties(); /** @var string $key * @var Property $value */ foreach ($sourceProperties as $key => $value) { if (strpos($key, 'navigation_') === false) { if (!$targetDocument->hasProperty($key)) { $propertyValue = $value->getData(); if ($value->getType() == 'document') { $propertyValue = \Multilingual\Document::getDocumentIdInOtherLanguage($value->getData()->getId(), $language); } $targetDocument->setProperty($key, $value->getType(), $propertyValue, false, $value->getInheritable()); } } } } // Make sure the parent stays the same $targetDocument->setParent($targetParent); $targetDocument->setParentId($targetParent->getId()); $targetDocument->setPath($targetParent->getFullPath() . '/'); // Make sure the index stays the same $targetDocument->setIndex($sourceDocument->getIndex()); // Make sure the index follows in all the pages at current level $list = new Document\Listing(); $list->setCondition("parentId = ? AND id != ?", array($targetParent->getId(), $sourceDocument->getId())); $list->setOrderKey("index"); $list->setOrder("asc"); $childsList = $list->load(); $count = 0; /** @var Document $child */ foreach ($childsList as $child) { if ($count == intval($targetDocument->getIndex())) { $count++; } $child->saveIndex($count); $count++; } $targetDocument->save(); } } } // Unlock this event-trigger \Zend_Registry::set('Multilingual_' . __FUNCTION__, 0); }