bundles/IndabaExtensionBundle/EventListener/DataObjectEventListener.php line 139

Open in your IDE?
  1. <?php
  2. /*
  3.  * To change this license header, choose License Headers in Project Properties.
  4.  * To change this template file, choose Tools | Templates
  5.  * and open the template in the editor.
  6.  */
  7. namespace IndabaExtensionBundle\EventListener;
  8. use Symfony\Component\EventDispatcher\GenericEvent;
  9. use Pimcore\Event\Model\DataObjectEvent;
  10. use Pimcore\Model\DataObject;
  11. /**
  12.  * Description of DataObjectEventListener
  13.  *
  14.  * @author PcCom
  15.  */
  16. /**
  17.  * object in array $objects
  18.  * [0] => Array
  19.  *      (
  20.  *           [id] => 13010
  21.  *           [idx] => 0
  22.  *           [key] => 0021-05
  23.  *           [sortBy] => key
  24.  *           [sortOrder] => ASC
  25.  *           [text] => 0021-05
  26.  *           [type] => variant
  27.  *           [path] => /genebre/products/genebre/000000-000999/0000/0021/0021-05
  28.  *           [basePath] => /genebre/products/genebre/000000-000999/0000/0021/
  29.  *           [elementType] => object
  30.  *           [locked] =>
  31.  *           [lockOwner] =>
  32.  *           [isTarget] => 1
  33.  *           [allowDrop] =>
  34.  *           [allowChildren] => 1
  35.  *           [leaf] =>
  36.  *           [cls] => pimcore_class_icon
  37.  *           [published] => 1
  38.  *           [className] => Product
  39.  *           [allowVariants] => 1
  40.  *           [icon] => /bundles/app/icons/valve-variant.svg
  41.  *           [iconCls] =>
  42.  *           [qtipCfg] => Array
  43.  *               (
  44.  *                   [title] => ID: 13010
  45.  *                   [text] => Type: Product
  46.  *               )
  47.  *
  48.  *           [expanded] => 1
  49.  *           [permissions] => Array
  50.  *               (
  51.  *                   [save] => 1
  52.  *                   [unpublish] => 1
  53.  *                   [lEdit] => 1
  54.  *                   [lView] => 1
  55.  *                   [layouts] => 1
  56.  *                   [list] => 1
  57.  *                   [view] => 1
  58.  *                   [publish] => 1
  59.  *                   [delete] => 1
  60.  *                   [rename] => 1
  61.  *                   [create] => 1
  62.  *                   [settings] => 1
  63.  *                   [versions] => 1
  64.  *                   [properties] => 1
  65.  *               )
  66.  *
  67.  *           [expandable] =>
  68.  *           [loaded] => 1
  69.  *       )
  70.  */
  71. class DataObjectEventListener
  72. {
  73.     public function onResolveElementData(GenericEvent $event)
  74.     {
  75.         $objects $event->getArgument('objects');
  76.         foreach ($objects as $key => $object) {
  77.             $objects[$key] = $this->setNodeConfig($object);
  78.         }
  79.         $event->setArgument('objects'$objects);
  80.     }
  81.     
  82.     public function onPostAdd(DataObjectEvent $event)
  83.     {
  84.         $obj $event->getObject(); 
  85.         $objectbrickConfig \Pimcore::getContainer()->getParameter("indabaExtension.dataObject.objectbricks");
  86.  
  87.         if (\is_array($objectbrickConfig) && \method_exists($obj"getClassname") && \array_key_exists($obj->getClassname(), $objectbrickConfig)) {
  88.             foreach($objectbrickConfig[$obj->getClassname()] as $obField) {
  89.                 if ($obField["addIfCreate"]) {
  90.                     $field $obField["field"];
  91.                     $this->addObjectbricks($obj->getId(), $field );
  92.                 }
  93.             }
  94.         }
  95.     }
  96.     
  97.      protected function addObjectbricks(int $objIdstring $field
  98.      {
  99.         $obj DataObject::getById($objId);
  100.         if ($obj) {
  101.             $allowedString $obj->getProperty("allowedObjectbricks");
  102.             $allowedList = array();
  103.             
  104.             if (!empty($allowedString)) {
  105.                 $allowedList explode(","str_replace(" """$allowedString));
  106.             }
  107.             $getter "get" ucfirst($field);
  108.             if (method_exists($obj$getter)) {
  109.                 foreach ($allowedList as $class) {
  110.                     $classname "\\Pimcore\\Model\\DataObject\\Objectbrick\\Data\\" $class;
  111.                     if (class_exists($classname)) {
  112.                         $objectbricks = new $classname($obj);
  113.                         $objectbricks->setFieldname($field);
  114.                         $objectbricks->save($obj);
  115.                         $setter "set" $class;
  116.                         $obj->$getter()->$setter($objectbricks);
  117.                     }
  118.                 }
  119.                 $obj->setOmitMandatoryCheck(true);
  120.                 $obj->save();
  121.             }
  122.         }
  123.     }
  124.     
  125.      /**
  126.      * Arguments:
  127.      *  - objectData | array | contains the export data of the object
  128.      *  - context | array | context information - default ['source' => 'pimcore-export']
  129.      *  - requestedLanguage | string | requested language
  130.      *  - helperDefinitions | array | containing the column definition from the grid view
  131.      *  - localeService | \Pimcore\Localization\LocaleService
  132.      *  - returnMappedFieldNames | bool | if "true" the objectData is an associative array, otherwise it is an indexed array
  133.      *
  134.      * @Event("Pimcore\Event\Model\DataObjectEvent")
  135.      */
  136.     public function onPostCsvItemExport(DataObjectEvent $event)
  137.     {
  138.         $object $event->getObject();
  139.         if ($event->getArgument('returnMappedFieldNames')) {
  140.             $objectData $this->correctCsvItem($object$event->getArgument('objectData'));
  141.             $event->setArgument('objectData'$objectData);
  142.         }
  143. //        \Pimcore\Log\Simple::log("DataObjectEventListener", "ObjectId: " . $object->getId());
  144. //        \Pimcore\Log\Simple::log("DataObjectEventListener", "ClassName: " . $object->getClassName());
  145. //        \Pimcore\Log\Simple::log("DataObjectEventListener", "**************** objectData ******************");
  146. //        \Pimcore\Log\Simple::log("DataObjectEventListener", print_r($event->getArgument('objectData'), true));
  147. //        \Pimcore\Log\Simple::log("DataObjectEventListener", "**************** context ******************");
  148. //        \Pimcore\Log\Simple::log("DataObjectEventListener", print_r($event->getArgument('context'), true));
  149. //        \Pimcore\Log\Simple::log("DataObjectEventListener", "RequestedLanguage: " . $event->getArgument('requestedLanguage'));
  150. //        \Pimcore\Log\Simple::log("DataObjectEventListener", "**************** helperDefinitions ******************");
  151. //        \Pimcore\Log\Simple::log("DataObjectEventListener", print_r($event->getArgument('helperDefinitions'), true));
  152. //        \Pimcore\Log\Simple::log("DataObjectEventListener", "****************returnMappedFieldNames ******************");
  153. //        \Pimcore\Log\Simple::log("DataObjectEventListener", print_r($event->getArgument('returnMappedFieldNames'), true));
  154.     }
  155.     
  156.     protected function setNodeConfig($object)
  157.     {
  158.         $child \Pimcore\Model\DataObject\AbstractObject::getById($object['id'])  ;
  159.         
  160.         // add from indabasolutions allowed classes inside this element
  161.         if (\Pimcore::getContainer()->getParameter("indabaExtension.dataObject.onlyAllowedClass")) {
  162.             $object['allowedClassIDS'] = $child->getProperty("allowedClass");
  163.         }
  164.         if ($child->getType() != 'folder') {
  165.             $object['published'] = $child->isPublished();
  166.             $object['className'] = $child->getClass()->getName();
  167.             if (!$child->isPublished()) {
  168.                 $object['cls'] .= 'pimcore_unpublished ';
  169.             }
  170.             $object['allowVariants'] = $child->getClass()->getAllowVariants();
  171.         }
  172.         return $object;
  173.     }
  174.     
  175.     protected function correctCsvItem($object$objectData)
  176.     {
  177.         foreach ($objectData as $key => $value) {
  178.             $fieldDefinition $object->getClass()->getFieldDefinition($key);
  179.             if ($fieldDefinition) {
  180.                 switch ($fieldDefinition->fieldtype) {
  181.                     case "inputQuantityValue":
  182.                     case "quantityValue":
  183.                         $fieldValue $fieldDefinition->getForCsvExport($object);
  184.                         $values explode("_"$fieldValue);
  185.                         $unitAbbreviation "";
  186.                         if (count($values) == 2) {
  187.                             $unit DataObject\QuantityValue\Unit::getById($values[1]);
  188.                             if ($unit) {
  189.                                 $unitAbbreviation $unit->getAbbreviation();
  190.                             }
  191.                         }
  192.                         $value trim($values[0] . ' ' $unitAbbreviation);
  193.                         break;
  194.                     case "select":
  195.                         $optionValue $fieldDefinition->getForCsvExport($object);
  196.                         foreach ($fieldDefinition->options as $option) {
  197.                             if ($option['value'] == $optionValue) {
  198.                                 $value =  $option['key'];
  199.                             }
  200.                         }
  201.                 }
  202.                 $objectData[$key] = $value ;
  203.             }
  204.         }
  205.         return $objectData;
  206.     }
  207. }