bundles/Jabber/WebthinkingBundle/EventListener/PreUpdateListener.php line 19

Open in your IDE?
  1. <?php 
  2. namespace Jabber\WebthinkingBundle\EventListener;
  3. use Jabber\WebthinkingBundle\Helpers\StringHelper;
  4. use Jabber\WebthinkingBundle\Provider\CategoryProvider;
  5. use Pimcore\Event\Model\ElementEventInterface;
  6. use Pimcore\Event\Model\DataObjectEvent;
  7. use Pimcore\Model\DataObject\Product;
  8. use Pimcore\Model\DataObject\Classificationstore;
  9. class PreUpdateListener {
  10.     public function __construct(public array $styleAttributes, public array $classificationGroupMapping, public int $systemUser) {
  11.     }
  12.     public function onPreUpdate(ElementEventInterface $e) {
  13.         if (!$e->hasArgument('isAutoSave')) {
  14.             if ($e instanceof DataObjectEvent) {
  15.                 $object $e->getObject();
  16.                 if ($object instanceof Product) {
  17.                     if ($object->getUserModification() != $this->systemUser) {
  18.                         if ($object->getProductLevel() == 'STYLE') {
  19.                             $activeGroups $this->getActiveGroups($object);
  20.                             $this->setClassificationValues($object$activeGroupstrue);
  21.                             $this->setClassificationValues($object$activeGroupsfalse);
  22.                             $this->setCategoryValues($object);
  23.                             
  24.                         }
  25.                         $this->sortCategoryOrder($object);
  26.                     }
  27.                 }
  28.             }
  29.         }
  30.     }
  31.     /**
  32.      * Sorts the categories into alphabetic order based on the path
  33.      * 
  34.      * Category order doesn't matter but it is much easier to read if they are in some sort of order
  35.      *
  36.      * @param Product $object
  37.      * @return void
  38.      */
  39.     private function sortCategoryOrder(Product $object) {
  40.         $items $object->getCategories();
  41.         if ($items) {
  42.             // Sorting items alphabetically by their path
  43.             usort($items, function ($a$b) {
  44.                 return strcmp($a->getFullPath(), $b->getFullPath());
  45.             });
  46.             $object->setCategories($items);
  47.         }
  48.     }
  49.     private function setCategoryValues(Product $object) {
  50.         $existingCategories $object->getCategories();
  51.         // Categories shouldn't be added to products where categories are already visible. This may be subject to change
  52.         foreach ($existingCategories as $category) {
  53.             if (str_starts_with(strtolower($category->getSource()), 'webthinking')) {
  54.                 return;
  55.             }
  56.         }
  57.         $provider = new CategoryProvider();
  58.         
  59.         $object->setCategories($provider->getCategories($object));
  60.         
  61.     }
  62.     private function getKeyGroupRelations($groupIdList): Classificationstore\KeyGroupRelation\Listing 
  63.     {
  64.         $keyConfigRelations = new Classificationstore\KeyGroupRelation\Listing();
  65.         return $keyConfigRelations->setCondition('groupId IN (' implode(","$groupIdList) . ')');
  66.     }
  67.     /**
  68.      * Sets classification store values, handling both direct product settings
  69.      * and inheritance from parent products.
  70.      *
  71.      * @param Product $object The product object to process.
  72.      */
  73.     public function setClassificationValues(Product $object, array $activeGroupsbool $isParentContext true) {
  74.         $changes false;
  75.         $contextObject $isParentContext $object->getParent() : $object;
  76.         $classificationStore $contextObject->getWebthinkingAttributeData();
  77.         $keyConfigRelations $this->getKeyGroupRelations($activeGroups); 
  78.         $originalInheritance $object->getGetInheritedValues();
  79.         $object->setGetInheritedValues(true);
  80.         foreach ($keyConfigRelations as $relation) {
  81.             if ($relation->getKeyId() == 137) {
  82.                 $foo "bar";
  83.             }
  84.             $keyConfig Classificationstore\KeyConfig::getById($relation->getKeyId());
  85.             $keyDefinition $keyConfig->getDefinition();
  86.             $keyDefinitionObject json_decode($keyDefinition);
  87.             $webthinkingAttributeId $keyDefinitionObject->optionsProviderData;
  88.             
  89.             if ($isParentContext) {
  90.                 if (in_array($webthinkingAttributeId$this->styleAttributes)) {
  91.                     continue;
  92.                 }
  93.             } else {
  94.                 if (!in_array($webthinkingAttributeId$this->styleAttributes)) {
  95.                     continue;
  96.                 }
  97.             }
  98.             
  99.             $groupId $relation->getGroupId();
  100.             $keyId $relation->getKeyId();
  101.             if (!empty($keyDefinitionObject->defaultValueGenerator) || !empty($keyDefinitionObject->defaultValue) || $keyConfig->getType() == 'multiselect') {
  102.                 $currentValue $classificationStore->getLocalizedKeyValue($groupId$keyId);
  103.                 if (empty($currentValue)) {
  104.                     $defaultValue $this->getDefaultValueForKey($keyDefinition$keyConfig->getType(), $contextObject);
  105.                     if (!empty($defaultValue)) {
  106.                         $changes true;
  107.                         $classificationStore->setLocalizedKeyValue($groupId$keyId$defaultValue);
  108.                     }
  109.                 }
  110.             }
  111.         }
  112.         //Return object to its original settings
  113.         $object->setGetInheritedValues($originalInheritance);
  114.         if ($changes == true) {
  115.             if ($isParentContext) {
  116.                 $contextObject->setWebthinkingAttributeData($classificationStore)->save();
  117.             } else {
  118.                 $object->setWebthinkingAttributeData($classificationStore);
  119.             }
  120.         }
  121.     }
  122.     private function getDefaultValueForKey($keyDefinition$type$dataObject) {
  123.         $fieldDefinitionClass '\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\' ucfirst($type);
  124.         if (!empty($keyDefinition) && ($dataObject->getProductLevel() == 'PARENT' || $dataObject->getProductLevel() == 'STYLE')) {
  125.             $keyDefinitionObject json_decode($keyDefinition);
  126.             if (!empty($keyDefinitionObject->defaultValue)) {
  127.                 return $keyDefinitionObject->defaultValue;
  128.             }
  129.             if (!empty($keyDefinitionObject->optionsProviderClass) && !empty($keyDefinitionObject->optionsProviderData)) {
  130.                 $fieldDefinitionObject = new $fieldDefinitionClass();
  131.                 $fieldDefinitionObject->setName($keyDefinitionObject->name);
  132.                 $fieldDefinitionObject->setOptionsProviderClass($keyDefinitionObject->optionsProviderClass);
  133.                 $fieldDefinitionObject->setOptionsProviderData($keyDefinitionObject->optionsProviderData);
  134.                 $fieldDefinitionObject->enrichFieldDefinition(['class'=>$keyDefinition]);
  135.                 if (!empty($keyDefinitionObject->defaultValueGenerator)) {
  136.                     $providerClass $keyDefinitionObject->defaultValueGenerator;
  137.                 } else {
  138.                     //assume multiselect in which case defaults don't exist
  139.                     $providerClass '\\Jabber\\WebthinkingBundle\\Provider\\Defaults\\Multiselect\\' StringHelper::toPascal($keyDefinitionObject->name) . 'Provider';
  140.                 }
  141.                 if (class_exists($providerClass)) {
  142.                     $provider = new $providerClass();
  143.                     return $provider->getValue($dataObject$fieldDefinitionObject, []);
  144.                 }
  145.             }
  146.         }
  147.     }
  148.     private function getActiveGroups(Product $object) {
  149.         $originalInheritance $object->getGetInheritedValues();
  150.         $object->setGetInheritedValues(true);
  151.         $classificationStore $object->getWebthinkingAttributeData();
  152.         $activeGroups = [];
  153.         $groupNames = [];
  154.         foreach ($this->classificationGroupMapping as $pimField=>$groups) {
  155.             if ($pimField == 'all') {
  156.                 foreach ($groups as $grp) {
  157.                     $groupNames[] = $grp;
  158.                 }
  159.                 continue;
  160.             }
  161.             if ($pimField == 'style' && $object->getProductLevel() == 'STYLE') {
  162.                 foreach ($groups as $grp) {
  163.                     $groupNames[] = $grp;
  164.                 }
  165.                 continue;
  166.             }
  167.             
  168.             $pimFieldValue strtolower($object->get($pimField));
  169.             if (in_array($pimFieldValue$groups)) {
  170.                 $groupNames[] = $pimFieldValue;
  171.             }
  172.         }
  173.         $fieldDefinition $object->getClass()->getFieldDefinition('WebthinkingAttributeData');
  174.         $storeId $fieldDefinition->getStoreId();
  175.         foreach ($groupNames as $groupName) {
  176.             $groupObject Classificationstore\GroupConfig::getByName(strtoupper($groupName), $storeId);
  177.             if (!empty($groupObject)) {
  178.                 $activeGroups[] = $groupObject->getId();
  179.             }
  180.         }
  181.         $activeGroupArray = [];
  182.         foreach ($activeGroups as $activeGroup) {
  183.             $activeGroupArray[$activeGroup] = true;
  184.         }
  185.         $classificationStore->setActiveGroups($activeGroupArray);
  186.         $object->setWebthinkingAttributeData($classificationStore);
  187.         $object->setGetInheritedValues($originalInheritance);
  188.         return $activeGroups;
  189.     }
  190. }