src/AppBundle/EventListener/DataObjectListener.php line 18

Open in your IDE?
  1. <?php
  2. namespace AppBundle\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\DataObject\Classificationstore;
  8. use Pimcore\Model\DataObject\ParentModel as DataObjectParentModel;
  9. use Pimcore\Model\DataObject\Objectbrick\Definition;
  10. use Pimcore\Model\DataObject\Data\QuantityValue;
  11. use Pimcore\Model\DataObject\ClassDefinition\Data\Input;
  12. class DataObjectListener
  13. {
  14.     public function onPostAdd(ElementEventInterface $e)
  15.     {
  16.         if ($e instanceof AssetEvent) {
  17.             // do something with the asset
  18.             // $foo = $e->getAsset();
  19.         } else if ($e instanceof DocumentEvent) {
  20.             // do something with the document
  21.             // $foo = $e->getDocument();
  22.         } else if ($e instanceof DataObjectEvent) {
  23.             // do something with the object
  24.             $object $e->getObject();
  25.             $defaultValuesArray $this->getDefaultValues();
  26.             if (!empty($defaultValuesArray)) {
  27.                 $this->setDefaultValues($object$defaultValuesArray);
  28.             }
  29.         }
  30.     }
  31.     // The method returns array of default values from the parent models
  32.     // Return: array with default values for the attributes or empty array
  33.     protected function getDefaultValues()
  34.     {
  35.         // Result array example
  36.         // $resArray: [
  37.         //     "LVTDefaultValues" => array:10 [
  38.         //       "fiveGILocking" => false
  39.         //       "adhesive" => "V-88"
  40.         //       "antiMicrobialTechnology" => "Yes"
  41.         //       "attachedPad" => "Yes"
  42.         //       "backingBrand" => "Foam"
  43.         //       "cartonWeight" => "3"
  44.         //       "cartonsPerPallet" => "3"
  45.         //       "commercialWarranty" => 3
  46.         //       "core" => "Poplar/Gum"
  47.         //       "patternType" => "12" Encaustic Tile"
  48.         //     ]
  49.         //     "HardwoodDefaultValues" => array:6 [
  50.         //       "backingBrand" => "Wood"
  51.         //       "cartonWeight" => "12"
  52.         //       "cartonsPerPallet" => "1"
  53.         //       "commercialWarranty" => 3
  54.         //       "core" => "HDF"
  55.         //       "patternType" => "6" Stone"
  56.         //     ]
  57.         //   ]
  58.         $parentModels = new DataObjectParentModel\Listing();
  59.         $parentModels->load();
  60.         $resArray = [];
  61.         foreach ($parentModels as $parentModel) {
  62.             $isParentModelPublished $parentModel->getPublished();
  63.             // LVTDefaultValues, HardwoodDefaultValues, LaminateDefaultValues, VinylSheetDefaultValues, CarepetBroadloomDefaultValues, CarpetTileDefaultValues
  64.             $parentModelKey $parentModel->getKey();
  65.             $parentModelClassificationStore $parentModel->getParentModelClassificationStore();
  66.             $defaultValueItems $parentModelClassificationStore->getItems();
  67.             if ($isParentModelPublished) {
  68.                 foreach ($defaultValueItems as $key => $group) {
  69.                     // $groupId = $key;
  70.                     // $csGroupConfig = Classificationstore\GroupConfig::getById($groupId);
  71.                     // $groupName = $csGroupConfig->getName();
  72.                     foreach ($group as $key => $value) {
  73.                         $csKeyConfig Classificationstore\KeyConfig::getById($key);
  74.                         $attributeName $csKeyConfig->getName();
  75.                         if (is_array($value['default'])) {
  76.                             $resArray[$parentModelKey][$attributeName] = $value['default'][0];
  77.                         } else {
  78.                             $resArray[$parentModelKey][$attributeName] = $value['default'];
  79.                         }
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.         return $resArray;
  85.     }
  86.     // The method mapping the array default values to the object bricks 
  87.     // Parameter: array, default values
  88.     // Return: void
  89.     protected function setDefaultValues($object$defaultValuesArray)
  90.     {
  91.         $objectBrickName '';
  92.         foreach ($defaultValuesArray as $classificationStoreGroupName => $defaultAttributesHashArray) {
  93.             if ($classificationStoreGroupName == 'LVTDefaultValues') {
  94.                 $objectBrickName 'lvtAttributes';
  95.                 $this->setDefaultValuesToObjectBrick($object$objectBrickName$defaultAttributesHashArray);
  96.             } elseif ($classificationStoreGroupName == 'HardwoodDefaultValues') {
  97.                 $objectBrickName 'hardwoodAttributes';
  98.                 $this->setDefaultValuesToObjectBrick($object$objectBrickName$defaultAttributesHashArray);
  99.             } elseif ($classificationStoreGroupName == 'LaminateDefaultValues') {
  100.                 $objectBrickName 'laminateAttributes';
  101.                 $this->setDefaultValuesToObjectBrick($object$objectBrickName$defaultAttributesHashArray);
  102.             } elseif ($classificationStoreGroupName == 'VinylSheetDefaultValues') {
  103.                 $objectBrickName 'vinylSheetAttributes';
  104.                 $this->setDefaultValuesToObjectBrick($object$objectBrickName$defaultAttributesHashArray);
  105.             } elseif ($classificationStoreGroupName == 'CarepetBroadloomDefaultValues') {
  106.                 $objectBrickName 'carpetBroadloomAttributes';
  107.                 $this->setDefaultValuesToObjectBrick($object$objectBrickName$defaultAttributesHashArray);
  108.             } elseif ($classificationStoreGroupName == 'CarpetTileDefaultValues') {
  109.                 $objectBrickName 'carpetTileAttributes';
  110.                 $this->setDefaultValuesToObjectBrick($object$objectBrickName$defaultAttributesHashArray);
  111.             }
  112.         }
  113.     }
  114.     // The method set default values to the specified object brick from the passed hash array of the default values
  115.     // Parameter: string, name of the object brick
  116.     // Parameter: array, the hash array with the attribute name (key) and default attribute value (value)
  117.     // Return: void
  118.     protected function setDefaultValuesToObjectBrick($object$objectBrickName$defaultAttributesHashArray)
  119.     {
  120.         $objectBrickDefinition Definition::getByKey($objectBrickName);
  121.         $objectBrickFields $objectBrickDefinition->getFieldDefinitions();
  122.         // dd($objectBrickFields);
  123.         foreach ($objectBrickFields as $objectBrickFieldName => $objectBrickFieldDefinition) {
  124.             foreach ($defaultAttributesHashArray as $attributeName => $attributeDefaultValue) {
  125.                 if ($objectBrickFieldName == $attributeName) {
  126.                     if ($objectBrickFieldName == 'adhesive') {
  127.                         // dd($attributeDefaultValue);
  128.                         // $objectBricksAttributesArray = $object->getProductInfo();
  129.                         // $object->setValues(['adhesive' => 'V-88']);
  130.                         // dd($objectBricksAttributesArray);
  131.                     }
  132.                     if ($attributeDefaultValue instanceof QuantityValue) {
  133.                         $quantityValue $attributeDefaultValue->getValue();
  134.                         $objectBrickFieldDefinition->defaultValue $quantityValue;
  135.                         // dd($objectBrickFieldDefinition);
  136.                     } elseif ($objectBrickFieldDefinition instanceof Input) {
  137.                         // $objectBrickFieldDefinition->setDefaultValue($attributeDefaultValue);
  138.                         // $objectBrickFieldDefinition->defaultValue = $inputValue;
  139.                         $objectBrickFieldDefinition->defaultValue $attributeDefaultValue;
  140.                         // dd($objectBrickFieldDefinition);
  141.                     } else {
  142.                         $objectBrickFieldDefinition->defaultValue $attributeDefaultValue;
  143.                     }
  144.                 }
  145.             }
  146.         }
  147.         $objectBrickDefinition->save();
  148.     }
  149. }