src/AppBundle/EventListener/DataObjectEventsListener.php line 30

Open in your IDE?
  1. <?php 
  2. namespace AppBundle\EventListener;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use \Pimcore\Model\DataObject;
  5. use \Pimcore\Model\Asset;
  6. class DataObjectEventsListener implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @inheritDoc
  10.      */
  11.     const LANGUAGES = array(
  12.         "it_IT",
  13.         "de",
  14.         "en",
  15.         "fr_FR",
  16.         "es_ES"
  17.     );
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return [
  21.             \Pimcore\Event\DataObjectEvents::PRE_UPDATE => 'onPreUpdate'
  22.         ];
  23.     }
  24.     // Working only backend
  25.     public function onPreUpdate(\Pimcore\Event\Model\DataObjectEvent $event) {
  26.         // https://github.com/pimcore/pimcore/blob/9271c03f39c085c9bea3e297e4b6013b71f4bc32/lib/Event/DataObjectEvents.php#L51
  27.         $element $event->getElement();
  28.         $id $element->getId();
  29.         // \Pimcore\Log\Simple::log("miolog", get_class($element));
  30.         if ($element instanceof DataObject\Variante)
  31.             $this->parseVarianteApprofondimenti($element);
  32.         if ($element instanceof DataObject\Design)
  33.             $this->parseDesignApprofondimenti($element);
  34.         if ($element instanceof DataObject\Prodotto)
  35.             $this->parseProdottoLaboratorioCheckList($element);
  36.     }
  37.     private function parseVarianteApprofondimenti($variante) {
  38.         $languages self::LANGUAGES;
  39.         // Disable Fallback languages
  40.         \Pimcore\Model\DataObject\Localizedfield::setGetFallbackValues(false);
  41.         // Retrieve parent design
  42.         $_varianteParent $variante->getParent();
  43.         while ( !($_varianteParent instanceof DataObject\Design)) {
  44.             if (is_null($_varianteParent))
  45.                 return;
  46.             $_varianteParent $_varianteParent->getParent();
  47.         }
  48.         if (is_null($_varianteParent))
  49.             return;
  50.         // Retrieve approfondimenti design
  51.         $_approfondimentiDesign $_varianteParent->getApprofondimento_new();
  52.         // Retrieve approfondimenti variante
  53.         $_approfondimentiVariante $variante->getApprofondimento_new();
  54.         // Merge approfondimenti
  55.         $_approfondimenti array_merge($_approfondimentiDesign$_approfondimentiVariante);
  56.         $_approfondimenti array_filter($_approfondimenti, function ($_v) {
  57.             if (strtoupper($_v->getTipo()) == "NOTA" && strtoupper($_v->getArgomento()) == 5000)
  58.                 return false;
  59.             return true;
  60.         });
  61.         // Unique approfondimenti
  62.         $_approfondimenti array_unique($_approfondimentiSORT_REGULAR);
  63.         // Sort by ordinamento
  64.         usort($_approfondimenti, function ($a$b) {
  65.             $ordinamento_a $a->getOrdinamento();
  66.             $ordinamento_b $b->getOrdinamento();
  67.             if ($ordinamento_a == $ordinamento_b)
  68.                 return 0;
  69.             return ($ordinamento_a $ordinamento_b) ? -1;
  70.         });
  71.         // Map value
  72.         foreach ($languages as $language) {
  73.             $_approfondimentiMap array_map(function ($v) use ($language) {
  74.                 return $v->getRiga_approfondimento($language);
  75.             }, $_approfondimenti);
  76.             $_approfondimentiMap array_filter($_approfondimentiMap, function ($v) {
  77.                 $v trim($v);
  78.                 if (empty($v))
  79.                     return false;
  80.                 if ($v == "")
  81.                     return false;
  82.                 return true;
  83.             });
  84.             // Implode rows
  85.             $_approfondimentiImplode "• " implode(PHP_EOL "• "$_approfondimentiMap);
  86.             // Set value
  87.             $variante->setApprofondimento_preview($_approfondimentiImplode$language);
  88.         }
  89.     }
  90.     private function parseDesignApprofondimenti($design) {
  91.         $languages self::LANGUAGES;
  92.         // Disable Fallback languages
  93.         \Pimcore\Model\DataObject\Localizedfield::setGetFallbackValues(false);
  94.         // Merge approfondimenti
  95.         $_approfondimenti $design->getApprofondimento_new();
  96.         // Unique approfondimenti
  97.         $_approfondimenti array_unique($_approfondimentiSORT_REGULAR);
  98.         // Sort by ordinamento
  99.         usort($_approfondimenti, function ($a$b) {
  100.             $ordinamento_a $a->getOrdinamento();
  101.             $ordinamento_b $b->getOrdinamento();
  102.             if ($ordinamento_a == $ordinamento_b)
  103.                 return 0;
  104.             return ($ordinamento_a $ordinamento_b) ? -1;
  105.         });
  106.         // Map value
  107.         foreach ($languages as $language) {
  108.             $_approfondimentiMap array_map(function ($v) use ($language) {
  109.                 return $v->getRiga_approfondimento($language);
  110.             }, $_approfondimenti);
  111.             $_approfondimentiMap array_filter($_approfondimentiMap, function ($v) {
  112.                 $v trim($v);
  113.                 if (empty($v))
  114.                     return false;
  115.                 if ($v == "")
  116.                     return false;
  117.                 return true;
  118.             });
  119.             // Implode rows
  120.             $_approfondimentiImplode "• " implode(PHP_EOL "• "$_approfondimentiMap);
  121.             // Set value
  122.             $design->setApprofondimento_preview($_approfondimentiImplode$language);
  123.         }
  124.     }
  125.     private function parseProdottoLaboratorioCheckList($prodottoLaboratorio) {
  126.         $_documentoChecklist $prodottoLaboratorio->getDocumento_checklist_key();
  127.         // Get document 
  128.         $_documentoLab DataObject\Doclab::getByPath("/Lab-reference/Documenti/DocProdotti/CheckList/{$_documentoChecklist}");
  129.         if (is_null($_documentoLab)) {
  130.             $prodottoLaboratorio->setDocumento_checklist(null);
  131.             $prodottoLaboratorio->setDocumento_checklist_preview(null);
  132.             return;
  133.         }
  134.         $prodottoLaboratorio->setDocumento_checklist($_documentoLab);
  135.         // Get asset
  136.         $_assetLab $_documentoLab->getDocumento_asset();
  137.         if (is_null($_assetLab))
  138.             $_assetLab Asset::getById(1258);
  139.         $prodottoLaboratorio->setDocumento_checklist_preview($_assetLab);
  140.     }
  141. }