<?php
namespace AppBundle\EventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\AssetEvent;
use Pimcore\Event\Model\DocumentEvent;
use Pimcore\Model\DataObject\Classificationstore;
use Pimcore\Model\DataObject\ParentModel as DataObjectParentModel;
use Pimcore\Model\DataObject\Objectbrick\Definition;
use Pimcore\Model\DataObject\Data\QuantityValue;
use Pimcore\Model\DataObject\ClassDefinition\Data\Input;
class DataObjectListener
{
public function onPostAdd(ElementEventInterface $e)
{
if ($e instanceof AssetEvent) {
// do something with the asset
// $foo = $e->getAsset();
} else if ($e instanceof DocumentEvent) {
// do something with the document
// $foo = $e->getDocument();
} else if ($e instanceof DataObjectEvent) {
// do something with the object
$object = $e->getObject();
$defaultValuesArray = $this->getDefaultValues();
if (!empty($defaultValuesArray)) {
$this->setDefaultValues($object, $defaultValuesArray);
}
}
}
// The method returns array of default values from the parent models
// Return: array with default values for the attributes or empty array
protected function getDefaultValues()
{
// Result array example
// $resArray: [
// "LVTDefaultValues" => array:10 [
// "fiveGILocking" => false
// "adhesive" => "V-88"
// "antiMicrobialTechnology" => "Yes"
// "attachedPad" => "Yes"
// "backingBrand" => "Foam"
// "cartonWeight" => "3"
// "cartonsPerPallet" => "3"
// "commercialWarranty" => 3
// "core" => "Poplar/Gum"
// "patternType" => "12" Encaustic Tile"
// ]
// "HardwoodDefaultValues" => array:6 [
// "backingBrand" => "Wood"
// "cartonWeight" => "12"
// "cartonsPerPallet" => "1"
// "commercialWarranty" => 3
// "core" => "HDF"
// "patternType" => "6" Stone"
// ]
// ]
$parentModels = new DataObjectParentModel\Listing();
$parentModels->load();
$resArray = [];
foreach ($parentModels as $parentModel) {
$isParentModelPublished = $parentModel->getPublished();
// LVTDefaultValues, HardwoodDefaultValues, LaminateDefaultValues, VinylSheetDefaultValues, CarepetBroadloomDefaultValues, CarpetTileDefaultValues
$parentModelKey = $parentModel->getKey();
$parentModelClassificationStore = $parentModel->getParentModelClassificationStore();
$defaultValueItems = $parentModelClassificationStore->getItems();
if ($isParentModelPublished) {
foreach ($defaultValueItems as $key => $group) {
// $groupId = $key;
// $csGroupConfig = Classificationstore\GroupConfig::getById($groupId);
// $groupName = $csGroupConfig->getName();
foreach ($group as $key => $value) {
$csKeyConfig = Classificationstore\KeyConfig::getById($key);
$attributeName = $csKeyConfig->getName();
if (is_array($value['default'])) {
$resArray[$parentModelKey][$attributeName] = $value['default'][0];
} else {
$resArray[$parentModelKey][$attributeName] = $value['default'];
}
}
}
}
}
return $resArray;
}
// The method mapping the array default values to the object bricks
// Parameter: array, default values
// Return: void
protected function setDefaultValues($object, $defaultValuesArray)
{
$objectBrickName = '';
foreach ($defaultValuesArray as $classificationStoreGroupName => $defaultAttributesHashArray) {
if ($classificationStoreGroupName == 'LVTDefaultValues') {
$objectBrickName = 'lvtAttributes';
$this->setDefaultValuesToObjectBrick($object, $objectBrickName, $defaultAttributesHashArray);
} elseif ($classificationStoreGroupName == 'HardwoodDefaultValues') {
$objectBrickName = 'hardwoodAttributes';
$this->setDefaultValuesToObjectBrick($object, $objectBrickName, $defaultAttributesHashArray);
} elseif ($classificationStoreGroupName == 'LaminateDefaultValues') {
$objectBrickName = 'laminateAttributes';
$this->setDefaultValuesToObjectBrick($object, $objectBrickName, $defaultAttributesHashArray);
} elseif ($classificationStoreGroupName == 'VinylSheetDefaultValues') {
$objectBrickName = 'vinylSheetAttributes';
$this->setDefaultValuesToObjectBrick($object, $objectBrickName, $defaultAttributesHashArray);
} elseif ($classificationStoreGroupName == 'CarepetBroadloomDefaultValues') {
$objectBrickName = 'carpetBroadloomAttributes';
$this->setDefaultValuesToObjectBrick($object, $objectBrickName, $defaultAttributesHashArray);
} elseif ($classificationStoreGroupName == 'CarpetTileDefaultValues') {
$objectBrickName = 'carpetTileAttributes';
$this->setDefaultValuesToObjectBrick($object, $objectBrickName, $defaultAttributesHashArray);
}
}
}
// The method set default values to the specified object brick from the passed hash array of the default values
// Parameter: string, name of the object brick
// Parameter: array, the hash array with the attribute name (key) and default attribute value (value)
// Return: void
protected function setDefaultValuesToObjectBrick($object, $objectBrickName, $defaultAttributesHashArray)
{
$objectBrickDefinition = Definition::getByKey($objectBrickName);
$objectBrickFields = $objectBrickDefinition->getFieldDefinitions();
// dd($objectBrickFields);
foreach ($objectBrickFields as $objectBrickFieldName => $objectBrickFieldDefinition) {
foreach ($defaultAttributesHashArray as $attributeName => $attributeDefaultValue) {
if ($objectBrickFieldName == $attributeName) {
if ($objectBrickFieldName == 'adhesive') {
// dd($attributeDefaultValue);
// $objectBricksAttributesArray = $object->getProductInfo();
// $object->setValues(['adhesive' => 'V-88']);
// dd($objectBricksAttributesArray);
}
if ($attributeDefaultValue instanceof QuantityValue) {
$quantityValue = $attributeDefaultValue->getValue();
$objectBrickFieldDefinition->defaultValue = $quantityValue;
// dd($objectBrickFieldDefinition);
} elseif ($objectBrickFieldDefinition instanceof Input) {
// $objectBrickFieldDefinition->setDefaultValue($attributeDefaultValue);
// $objectBrickFieldDefinition->defaultValue = $inputValue;
$objectBrickFieldDefinition->defaultValue = $attributeDefaultValue;
// dd($objectBrickFieldDefinition);
} else {
$objectBrickFieldDefinition->defaultValue = $attributeDefaultValue;
}
}
}
}
$objectBrickDefinition->save();
}
}