src/EventListener/MyEventListener.php line 99

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. //use Pimcore\Cache;
  4. //use Pimcore\Db;
  5. use Pimcore\Logger;
  6. use Pimcore\Model\DataObject\ClassDefinition\CustomLayout;
  7. use Pimcore\Model\DataObject\Prodotti;
  8. use Pimcore\Model\DataObject\Service;
  9. use Symfony\Component\EventDispatcher\GenericEvent;
  10. use \Pimcore\Model\DataObject;
  11. use Pimcore\Event\Model\DataObjectEvent;
  12. use Pimcore\Event\Model\ElementEventInterface;
  13. // use Pimcore\Event\Model\ElementEventInterface;
  14. // use Pimcore\Event\Model\DataObjectEvent;
  15. class MyEventListener
  16. {
  17.     
  18.         public function updateProdottiGiacenze(ElementEventInterface $e)
  19.     {
  20.         
  21.         $object $e->getObject();
  22.         if ($e instanceof DataObjectEvent) {
  23.              
  24.             
  25.            // Logger::debug("DataObjectEvent................." . print_r($e->getElement(),true));
  26.             //Logger::debug("DataObjectEvent................." . print_r($e->getArguments(),true));
  27.             // do something with the object
  28.             if ($object instanceof Prodotti) {
  29.                 //\Pimcore\Log\Simple::log('stefanos', "update prodotti giacenze");
  30.                 //Logger::debug("prodottoooo onupdate" . $object->getCOD());
  31.                 $COD $object->getCOD();
  32.                 //\Pimcore\Log\Simple::log('stefanos', "COD ".$object->getCOD());
  33.                 // \Pimcore\Log\Simple::log('stefanos', "COD ".$object->getId());
  34.                 
  35.                 // $entries = new DataObject\Prodotti\Listing();
  36.                 // //$entries->setCondition("name LIKE :name AND date > :date", ["name" => "%bernie%", "date" => time()]);
  37.                 // $entries->filterByCOD($COD);
  38.                 // $entries->load();
  39.                 $results DataObject\Prodotti::getByCOD($COD, ['limit' => 1000'offset' => 0,'unpublished' => true]);
  40.                 foreach ($results as $entry) {
  41.                     \Pimcore\Log\Simple::log('stefanos'"mag ciclo ".$entry->getMagazzino()." != mag obj= ".$object->getMagazzino(). " stock ciclo ".$entry->getIn_stock()." != object stock"$object->getIn_stock());
  42.                     
  43.                     if(($entry->getMagazzino() != $object->getMagazzino()) || ($entry->getIn_stock() != $object->getIn_stock())){
  44.                         
  45.                          //Logger::debug("prodottoooo onupdate ".$entry->getCOD()." ". $entry->getNome());
  46.                          //\Pimcore\Log\Simple::log('stefanos', "prodottoooo onupdate ".$entry->getCOD()." ". $entry->getNome() );
  47.                          \Pimcore\Log\Simple::log('stefanos'"mag ciclo ".$entry->getMagazzino()." != mag obj= ".$object->getMagazzino(). " stock ciclo ".$entry->getIn_stock()." != object stock"$object->getIn_stock());
  48.                          $entry->setMagazzino($object->getMagazzino());
  49.                          $entry->setIn_stock($object->getIn_stock());
  50.                          $entry->save();
  51.                     }
  52.                     
  53.                 }
  54.                 // $object->setMagazzino(15);
  55.                 // $object->setIn_stock(false);
  56.                 //$object->save();
  57.                 
  58.                 
  59.             }
  60.             //Logger::debug("fired onupdate");
  61.             // we don't have to call save here as we are in the pre-update event anyway ;-)
  62.         }
  63.     }
  64.     
  65.     public function test(ElementEventInterface $e) {
  66.     \Pimcore\Log\Simple::log('stefanos'"ElementEventInterface event fired ");
  67.     $object $e->getObject();
  68.         if ($e instanceof DataObjectEvent) {
  69.             if ($object instanceof Prodotti) {
  70.                //\Pimcore\Log\Simple::log('stefanos', "COD ".print_r($object,true));
  71.                \Pimcore\Log\Simple::log('stefanos'"COD ".$object->getCOD());
  72.                 //$lastModifiedUser = $object->getModificationUser();
  73.                 //$lastModifiedUser = User::getById($object->getModificationUser());
  74.                 \Pimcore\Log\Simple::log('stefanos'"COD ".date('H:i'));
  75.                 //$object->setIn_stock(1);
  76.                 //$object->save();
  77.             }
  78.         }
  79.     }
  80.     
  81.     public function OnPreUpdateSetCODchiaveForManualInput(DataObjectEvent $e) {
  82.     $object $e->getObject();
  83.         if ($e instanceof DataObjectEvent) {
  84.             if ($object instanceof Prodotti) {
  85.                 
  86.                 $cod_chiave=$object->getCODchiave();
  87.                 $org=$object->getOrganizzazione();
  88.                 $cod=$object->getCOD();
  89.                 
  90.                 if($cod && $org){
  91.                     if($cod_chiave==""){
  92.                         \Pimcore\Log\Simple::log('stefanos'"cod chiave null");
  93.                         //\Pimcore\Log\Simple::log('stefanos', $cod." ".$cod_chiave." ".$org);
  94.                         if($org=="/Organizzazioni/BeCulture"){
  95.                             $orgPrefix="beculture";
  96.                         }else if($org=="/Organizzazioni/Biennale di Venezia"){
  97.                             $orgPrefix="biennaledivenezia";
  98.                         }else if($org=="/Organizzazioni/Galleria degli Uffizi"){
  99.                             $orgPrefix="uffizi";
  100.                         }else if($org=="/Organizzazioni/Firenze Musei Store"){
  101.                             $orgPrefix="firenzemusei";
  102.                         }
  103.                         
  104.                         $object->setCODchiave($orgPrefix.$cod);
  105.                         
  106.                         
  107.                     }else{
  108.                         \Pimcore\Log\Simple::log('stefanos'"cod chiave presente");
  109.                         \Pimcore\Log\Simple::log('stefanos'$cod." ".$cod_chiave." ".$org);
  110.                     }
  111.                 }
  112.             }
  113.         }
  114.     }
  115.     
  116.                      //\Pimcore\Log\Simple::log('stefanos', "on pre add");
  117.                 //\Pimcore\Log\Simple::log('stefanos', $object->getCODchiave);
  118.                //\Pimcore\Log\Simple::log('stefanos', "COD ".print_r($object,true));
  119.                //\Pimcore\Log\Simple::log('stefanos', "COD ".$object->getCOD());
  120.                 //$lastModifiedUser = $object->getModificationUser();
  121.                 //$lastModifiedUser = User::getById($object->getModificationUser());
  122.                 //\Pimcore\Log\Simple::log('stefanos', "COD ".date('H:i'));
  123.                 //$object->setIn_stock(1);
  124.                 //$object->save();
  125.     // public function onUpdate (ElementEventInterface $e) {
  126.     //     if ($e instanceof DataObjectEvent) {
  127.     //         // do something with the object
  128.     //         $foo = $e->getObject();
  129.     //         Logger::debug("onupdate");
  130.     //         // we don't have to call save here as we are in the pre-update event anyway ;-)
  131.     //     }
  132.     // }
  133.     public function selectCustomLayout(GenericEvent $event)
  134.     {
  135.         $object $event->getArgument("object");
  136.         //Logger::debug($object->getId());
  137.         if ($object instanceof Prodotti) {
  138.             //get product type
  139.             $tipo_prodotto $object->getTipo_prodotto();
  140.             Logger::debug($tipo_prodotto);
  141.             if ($tipo_prodotto!="") {
  142.                 //data element that is send to Pimcore backend UI
  143.                 $dataEl $event->getArgument("data");
  144.                 $data $this->doModifyCustomLayouts($dataEl$object, (int)$tipo_prodotto, [0, -1]);
  145.                 $event->setArgument("data"$data);
  146.             }
  147.             // switch ($tipo_prodotto) {
  148.             //     case "Abbigliamento":
  149.             //         $data = $this->doModifyCustomLayouts($data, $object, 2, [0,-1]);
  150.             //         $event->setArgument("data", $data);
  151.             //         break;
  152.             //     case "Libro":
  153.             //         $data = $this->doModifyCustomLayouts($data, $object, 1, [0,-1]);
  154.             //         $event->setArgument("data", $data);
  155.             //         break;
  156.             //     case "Oggettistica":
  157.             //         $data = $this->doModifyCustomLayouts($data, $object, 3, [0,-1]);
  158.             //         $event->setArgument("data", $data);
  159.             //         break;
  160.             //     default:
  161.             //         break;
  162.             // }
  163.             //Logger::debug("layout id ".$data['currentLayoutId']);
  164.             //set tipo_prodotto with current layout
  165.             //$customLayout = CustomLayout::getById($data['currentLayoutId']);
  166.             // if (!is_null($customLayout)) {
  167.             //     Logger::debug(print_r($customLayout->getName(), true));
  168.             //     $object->setTipo_prodotto($customLayout->getName());
  169.             //     //$object->save();
  170.             //     //object.reload($object);
  171.             //     //
  172.             //     $objId = $object->getId();
  173.             //     $db = Db::get();
  174.             //     $sql = "UPDATE object_1 SET tipo_prodotto = '" . $customLayout->getName() . "' WHERE oo_id = '" . $object->getId() . "' AND oo_classId = '1' AND oo_className = 'Prodotti' ";
  175.             //     Logger::debug($sql);
  176.             //     $db->query($sql);
  177.             //     Cache::clearAll();
  178.             //     //$object->reload();
  179.             // }
  180.         }
  181.     }
  182.     /**
  183.      *
  184.      */
  185.     private function doModifyCustomLayouts($data$object$customLayoutToSelect null$layoutsToRemove = [])
  186.     {
  187.         if ($customLayoutToSelect != null) {
  188.             //set current layout to subcategory layout
  189.             $data['currentLayoutId'] = $customLayoutToSelect;
  190.             $customLayout CustomLayout::getById($customLayoutToSelect);
  191.             $data['layout'] = $customLayout->getLayoutDefinitions();
  192.             Service::enrichLayoutDefinition($data["layout"], $object);
  193.         }
  194.         if (!empty($layoutsToRemove)) {
  195.             //remove master layout from valid layouts
  196.             $validLayouts $data["validLayouts"];
  197.             foreach ($validLayouts as $key => $validLayout) {
  198.                 if (in_array($validLayout['id'], $layoutsToRemove)) {
  199.                     unset($validLayouts[$key]);
  200.                 }
  201.             }
  202.             $data["validLayouts"] = array_values($validLayouts);
  203.         }
  204.         return $data;
  205.     }
  206. }