bundles/MultilingualBundle/EventListener/DocumentListener.php line 130

Open in your IDE?
  1. <?php
  2. namespace MultilingualBundle\EventListener;
  3. use Pimcore\Event\Model\ElementEventInterface;
  4. use Pimcore\Event\Model\DataObjectEvent;
  5. use Pimcore\Event\Model\AssetEvent;
  6. use Pimcore\Event\Model\DocumentEvent;
  7. use Pimcore\Model\Asset;
  8. use Pimcore\Model\Asset\Document;
  9. use Pimcore\Tool;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. class DocumentListener {
  12.     protected static $_tableName 'plugin_multilingual_keys';
  13.     protected static $_tableDocumentTrans 'documents_translations';
  14.     protected static $_tableDocumentElements 'documents_editables';
  15.     private $dbMultiLang;
  16.     public function __construct(){
  17.         $container \Pimcore::getContainer();
  18.         $this->dbMultiLang $container->get('doctrine.dbal.default_connection');
  19.     }
  20.     public function createDocument (ElementEventInterface $e)
  21.     {
  22.         if($e instanceof DocumentEvent) {
  23.             // Check if this function is not in progress
  24.             if (\Pimcore\Cache\Runtime::isRegistered('Multilingual_add') && \Pimcore\Cache\Runtime::get('Multilingual_add') == 1) {
  25.                 return;
  26.             }
  27.             // Lock this event-trigger
  28.             \Pimcore\Cache\Runtime::set('Multilingual_add'1);
  29.             $doc $e->getDocument();
  30.             if($doc->getParentId() != 1){ // TODO: check if this is site or subsite
  31.                 // Get current language
  32. //                $sourceLanguage = $doc->getProperty('language');
  33.                 $sourceParent $doc->getParent();
  34.                 $sourceLanguage $sourceParent->getProperty('language');
  35.                 $doc->setProperty("language",'text'$sourceLanguagetruetrue);
  36.                 $doc->save();
  37.                 $sourceHash $doc->getFullPath();
  38.                 $sourceDocId $doc->getId();
  39.                 // Add Link to other languages
  40.                 $this->dbMultiLang->createQueryBuilder()
  41.                     ->insert(self::$_tableName)
  42.                     ->setValue('document_id''?')
  43.                     ->setValue('language''?')
  44.                     ->setValue('sourcePath''?')
  45.                     ->setParameter(0$doc->getId())
  46.                     ->setParameter(1$sourceLanguage)
  47.                     ->setParameter(2$sourceHash)
  48.                     ->execute();
  49.                 // Create folders for each Language
  50.                 $languages Tool::getValidLanguages();
  51.                 foreach ($languages as $language) {
  52.                     if ($language != $sourceLanguage) {
  53. //                        throw new NotFoundHttpException("Page found ". $sourceParent->getId(). ' & '.$language);
  54.                         $targetParent \MultilingualBundle\Document\Document::getDocumentInOtherLanguage($sourceParent$language);
  55.                         if ($targetParent) {
  56.                             /** @var \Pimcore\Model\Document\Page $target */
  57.                             $target = clone $doc;
  58.                             // Reset ID (so it will create a new document)
  59.                             $target->setId(null);
  60.                             // Set new parent
  61.                             $target->setParent($targetParent);
  62.                             $languageTarget $targetParent->getProperty("language");
  63.                             $target->setProperty("language",'text'$languageTargetfalsetrue);
  64.                             // Check if we can link a master document
  65.                             $editableDocumentTypes = array('page''email''snippet');
  66.                             if (in_array($doc->getType(), $editableDocumentTypes)) {
  67.                                 $target->setContentMasterDocument($doc);
  68.                             }
  69.                             // Save the new document
  70.                             $target->save();
  71.                             // Add Link to other languages
  72.                             $this->dbMultiLang->createQueryBuilder()
  73.                                 ->insert(self::$_tableName)
  74.                                 ->setValue('document_id''?')
  75.                                 ->setValue('language''?')
  76.                                 ->setValue('sourcePath''?')
  77.                                 ->setParameter(0$target->getId())
  78.                                 ->setParameter(1$language)
  79.                                 ->setParameter(2$sourceHash)
  80.                                 ->execute();
  81.                             // Connect to other lang
  82.                             $this->dbMultiLang->createQueryBuilder()
  83.                                 ->insert(self::$_tableDocumentTrans)
  84.                                 ->setValue('id''?')
  85.                                 ->setValue('sourceId''?')
  86.                                 ->setValue('language''?')
  87.                                 ->setParameter(0$target->getId())
  88.                                 ->setParameter(1$sourceDocId)
  89.                                 ->setParameter(2$language)
  90.                                 ->execute();
  91.                             // Remove elementlinks
  92.                             $this->dbMultiLang->createQueryBuilder()
  93.                                 ->delete(self::$_tableDocumentElements)
  94.                                 ->where('documentId = '.$target->getId())
  95.                                 ->execute();
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.             // Unlock this event-trigger
  101.             \Pimcore\Cache\Runtime::set('Multilingual_add'0);
  102.         }
  103.     }
  104.     public function deleteDocument (ElementEventInterface $e)
  105.     {
  106.         if($e instanceof DocumentEvent) {
  107.             // Check if this function is not in progress
  108.             if (\Pimcore\Cache\Runtime::isRegistered('Multilingual_delete') && \Pimcore\Cache\Runtime::get('Multilingual_delete') == 1) {
  109.                 return;
  110.             }
  111.             // Lock this event-trigger
  112.             \Pimcore\Cache\Runtime::set('Multilingual_delete'1);
  113.             $sourceDocument $e->getDocument();
  114.             // Get current language
  115.             $sourceLanguage $sourceDocument->getProperty('language');
  116.             // Remove document in each language
  117.             $languages Tool::getValidLanguages();
  118.             foreach ($languages as $language) {
  119.                 if ($language != $sourceLanguage) {
  120.                     $target \MultilingualBundle\Document\Document::getDocumentInOtherLanguage($sourceDocument$language);
  121.                     if ($target) {
  122.                         // Remove link to other documents
  123.                         $this->dbMultiLang->createQueryBuilder()
  124.                             ->delete(self::$_tableName)
  125.                             ->where('document_id = :docid')
  126.                             ->setParameter('docid'$target->getId())
  127.                             ->execute();
  128.                         $target->delete();
  129.                     }
  130.                 }
  131.             }
  132.             // Remove link to other documents
  133.             $this->dbMultiLang->createQueryBuilder()
  134.                 ->delete(self::$_tableName)
  135.                 ->where('document_id = :docid')
  136.                 ->setParameter('docid'$sourceDocument->getId())
  137.                 ->execute();
  138.             // Remove connection to other lang
  139.             $this->dbMultiLang->createQueryBuilder()
  140.                 ->delete(self::$_tableDocumentTrans)
  141.                 ->where('sourceId = :docid')
  142.                 ->setParameter('docid'$sourceDocument->getId())
  143.                 ->execute();
  144.             // Unlock this event-trigger
  145.             \Pimcore\Cache\Runtime::set('Multilingual_delete'0);
  146.         }
  147.     }
  148.     public function updateDocument (ElementEventInterface $e)
  149.     {
  150.         if ($e instanceof DocumentEvent) {
  151.             if ($e->hasArgument('saveVersionOnly') && $e->getArgument('saveVersionOnly')) {
  152.                 return;
  153.             }
  154.             $sourceDocument $e->getDocument();
  155.             if ($sourceDocument->getId() == 1) {
  156.                 return;
  157.             }
  158.             // Check if this function is not in progress
  159.             if (\Pimcore\Cache\Runtime::isRegistered('Multilingual_' __FUNCTION__) && \Pimcore\Cache\Runtime::get('Multilingual_' __FUNCTION__) == 1) {
  160.                 return;
  161.             }
  162.             // Lock this event-trigger
  163.             \Pimcore\Cache\Runtime::set('Multilingual_' __FUNCTION__1);
  164.             // Get current language
  165. //            $sourceLanguage = $sourceDocument->getProperty('language');
  166.             $sourceParent $sourceDocument->getParent();
  167.             if($sourceParent->getId() != 1){
  168.                 // Fetch FullDocumentUrl Primary
  169.                 $languages Tool::getValidLanguages();
  170.                 $primaryLanguage $languages[0];
  171.                 $sourceDocumentFullPathDoc \MultilingualBundle\Document\Document::getDocumentInOtherLanguage($sourceDocument,$primaryLanguage);
  172.                 if($sourceDocumentFullPathDoc){
  173.                     $sourceDocumentFullPath $sourceDocumentFullPathDoc->getFullPath();
  174.                 }else{
  175.                     $sourceDocumentFullPath $sourceDocument->getFullPath();
  176.                 }
  177. //            throw new NotFoundHttpException("Page found ". $sourceDocumentFullPath. ' & '.$primaryLanguage);
  178.                 $sourceLanguage $sourceParent->getProperty('language');
  179.                 if($sourceLanguage != ''){
  180.                     $sourceDocument->setProperty("language",'text'$sourceLanguagefalsetrue);
  181.                     $sourceDocument->save();
  182.                     // Get the Source Parent (we have to do it this way, due to a bug in Pimcore)
  183.                     $sourceParent \Pimcore\Model\Document::getById($sourceDocument->getParentId());
  184.                     // Update each language
  185.                     $languages Tool::getValidLanguages();
  186.                     foreach ($languages as $language) {
  187.                         if ($language != $sourceLanguage) {
  188.                             // Find the target document
  189.                             /** @var Document $targetDocument */
  190.                             $targetDocument \MultilingualBundle\Document\Document::getDocumentInOtherLanguage($sourceDocument,$language);
  191.                             if ($targetDocument) {
  192.                                 // Find the parent
  193.                                 // If the parent is the root, document, no need to do a lookup
  194.                                 if ($sourceParent->getId() == 1) {
  195.                                     $targetParent $sourceParent;
  196.                                 } else {
  197.                                     $targetParent \MultilingualBundle\Document\Document::getDocumentInOtherLanguage($sourceParent,$language);
  198.                                 }
  199.                                 // Only sync properties when it is allowed
  200.                                 if (!$targetDocument->hasProperty('doNotSyncProperties') && !$sourceDocument->hasProperty('doNotSyncProperties')) {
  201.                                     $typeHasChanged false;
  202.                                     // Set document type (for conversion)
  203.                                     if ($targetDocument->getType() != $sourceDocument->getType()) {
  204.                                         $typeHasChanged true;
  205.                                         $targetDocument->setType($sourceDocument->getType());
  206.                                         if ($targetDocument->getType() == "hardlink" || $targetDocument->getType() == "folder") {
  207.                                             // remove navigation settings
  208.                                             foreach ([
  209.                                                          "name",
  210.                                                          "title",
  211.                                                          "target",
  212.                                                          "exclude",
  213.                                                          "class",
  214.                                                          "anchor",
  215.                                                          "parameters",
  216.                                                          "relation",
  217.                                                          "accesskey",
  218.                                                          "tabindex"
  219.                                                      ] as $propertyName) {
  220.                                                 $targetDocument->removeProperty("navigation_" $propertyName);
  221.                                             }
  222.                                         }
  223.                                         \Pimcore\Cache\Runtime::set("document_" $targetDocument->getId(), $targetDocument);
  224.                                     }
  225.                                     // Set the controller the same
  226.                                     $editableDocumentTypes = array('page''email''snippet');
  227.                                     if (!$typeHasChanged && in_array($sourceDocument->getType(), $editableDocumentTypes)) {
  228.                                         /** @var \Pimcore\Model\Document\Page $targetDocument */
  229.                                         $targetDocument->setController($sourceDocument->getController());
  230.                                     }
  231.                                     // Set the properties the same
  232.                                     // But only if they have not been added already
  233.                                     $sourceProperties $sourceDocument->getProperties();
  234.                                     /** @var string $key
  235.                                      * @var Property $value
  236.                                      */
  237.                                     foreach ($sourceProperties as $key => $value) {
  238.                                         if (strpos($key'navigation_') === false) {
  239.                                             if (!$targetDocument->hasProperty($key)) {
  240.                                                 $propertyValue $value->getData();
  241.                                                 if ($value->getType() == 'document') {
  242.                                                     $propertyValue \MultilingualBundle\Document\Document::getDocumentIdInOtherLanguage(
  243.                                                         $value->getData()->getId(),
  244.                                                         $language
  245.                                                     );
  246.                                                 }
  247.                                                 $targetDocument->setProperty(
  248.                                                     $key,
  249.                                                     $value->getType(),
  250.                                                     $propertyValue,
  251.                                                     false,
  252.                                                     $value->getInheritable()
  253.                                                 );
  254.                                             }
  255.                                         }
  256.                                     }
  257.                                 }
  258.                                 // Make sure the parent stays the same
  259.                                 $targetDocument->setParent($targetParent);
  260.                                 $targetDocument->setParentId($targetParent->getId());
  261.                                 $targetDocument->setPath($targetParent->getFullPath() . '/');
  262.                                 // Make sure the index stays the same
  263.                                 $targetDocument->setIndex($sourceDocument->getIndex());
  264.                                 // Make sure the index follows in all the pages at current level
  265.                                 $list = new \Pimcore\Model\Document\Listing();
  266.                                 $list->setCondition(
  267.                                     "parentId = ? AND id != ?",
  268.                                     array($targetParent->getId(), $sourceDocument->getId())
  269.                                 );
  270.                                 $list->setOrderKey("index");
  271.                                 $list->setOrder("asc");
  272.                                 $childsList $list->load();
  273.                                 $count 0;
  274.                                 /** @var Document $child */
  275.                                 foreach ($childsList as $child) {
  276.                                     if ($count == intval($targetDocument->getIndex())) {
  277.                                         $count++;
  278.                                     }
  279.                                     $child->saveIndex($count);
  280.                                     $count++;
  281.                                 }
  282.                                 $targetDocument->save();
  283.                                 // change url
  284.                                 $this->dbMultiLang->createQueryBuilder()
  285.                                     ->update(self::$_tableName)
  286.                                     ->set('sourcePath'':docFullPath')
  287.                                     ->where('document_id = :docId')
  288.                                     ->setParameter('docFullPath'$sourceDocumentFullPath)
  289.                                     ->setParameter('docId'$targetDocument->getId())
  290.                                     ->execute();
  291.                             }
  292.                         }
  293.                     }
  294.                 }
  295.                 // change url
  296.                 $this->dbMultiLang->createQueryBuilder()
  297.                     ->update(self::$_tableName)
  298.                     ->set('sourcePath'':docFullPath')
  299.                     ->where('document_id = :docId')
  300.                     ->setParameter('docFullPath'$sourceDocumentFullPath)
  301.                     ->setParameter('docId'$sourceDocument->getId())
  302.                     ->execute();
  303.             }
  304.         }
  305.         // Lock this event-trigger
  306.         \Pimcore\Cache\Runtime::set('Multilingual_' __FUNCTION__0);
  307.     }
  308. }