bundles/Jabber/GenericBundle/EventListener/DataToParent.php line 19

Open in your IDE?
  1. <?php 
  2. namespace Jabber\GenericBundle\EventListener;
  3. use Pimcore\Db;
  4. use Pimcore\Event\Model\DataObjectEvent;
  5. use Pimcore\Event\Model\ElementEventInterface;
  6. use Pimcore\Model\DataObject\Product;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. class DataToParent {
  9.     private $params;
  10.     public function __construct(ParameterBagInterface $params) {
  11.         $this->params $params;
  12.     }
  13.     public function onPreUpdate (ElementEventInterface $e) {
  14.         if (!$e->hasArgument('isAutoSave')) {
  15.             if ($e instanceof DataObjectEvent) {
  16.                 $dataObject $e->getObject();
  17.                 if ($dataObject instanceof Product) {
  18.                     // $WebthinkingAttributeData = $dataObject->getWebthinkingAttributeData();
  19.                     // $hasClassificationChanges = $this->hasClassificationDataChanged($dataObject);
  20.                     // $hasRelationalDataChanges = $this->hasRelationalDataChanged($dataObject);
  21.                     // $hasLocalisedDataChanges = $this->hasLocalisedDataChanged($dataObject, 'en_GB');
  22.                     // $hasLocalisedDataChangesUs = $this->hasLocalisedDataChanged($dataObject, 'en_US');
  23.                     // $hasProductChanges = $this->hasProductChanged($dataObject);
  24.                 }
  25.                 if ($dataObject instanceof Product && $dataObject->getType() == "variant") {
  26.                     $dataObject->setGetInheritedValues(false);
  27.                     if ($dataObject->get('DataInheritanceOverride') !== true) {
  28.                         $parentObject null;
  29.                         if (!empty($this->params->get('datatoparent'))) {
  30.                             $user $this->GetUserId($this->params);
  31.                             $productLevel $this->GetProductLevel($dataObject);
  32.                             if (!empty($this->params->get('datatoparent')['properties'][$productLevel])) {
  33.                                 foreach ($this->params->get('datatoparent')['properties'][$productLevel] as $property) {
  34.                                     $propertyValue $dataObject->get($property);
  35.                                     if (!empty($propertyValue)) {
  36.                                         $parentObject $this->GetParentProduct($dataObject$productLevel);
  37.                                         $parentObject->set($property$propertyValue);
  38.                                         $parentObject->setUserModification($user);
  39.                                         $parentObject->save();
  40.                                         $dataObject->set($propertynull);
  41.                                     }
  42.                                 }
  43.                             }
  44.                         }
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.     }
  50.     private function hasProductChanged(Product $dataObject) {
  51.         
  52.         $currentProduct Db::getConnection()->fetchAssociative('SELECT Name, `SKU`, `Brand` FROM object_store_Product WHERE oo_id = ?', [$dataObject->getId()]);
  53.         $currentLocalisedProductGb Db::getConnection()->fetchAssociative('SELECT `Description` FROM object_localized_query_Product_en_GB WHERE ooo_id = ?', [$dataObject->getId()]);
  54.         $currentLocalisedProductUs Db::getConnection()->fetchAssociative('SELECT `Description` FROM object_localized_query_Product_en_Us WHERE ooo_id = ?', [$dataObject->getId()]);
  55.         
  56.         
  57.         
  58.     }
  59.     private function hasLocalisedDataChanged(Product $dataObject$locale) {
  60.         $fields = ['Description'];
  61.         $table 'object_localized_query_Product_' $locale;
  62.         $tableFields implode(','$fields);
  63.         $currentLocalisedProduct Db::getConnection()->fetchAssociative("SELECT {$tableFields} FROM {$table} WHERE ooo_id = ?", [$dataObject->getId()]);
  64.         foreach ($fields as $field) {
  65.             if ($currentLocalisedProduct[$field] != $dataObject->get($field)) {
  66.                 return true;
  67.             }
  68.         }
  69.         return false;
  70.     }
  71.     private function hasRelationalDataChanged(Product $dataObject) {
  72.         $currentRelations Db::getConnection()->fetchAllAssociative('SELECT * FROM object_relations_Product WHERE (fieldname = ? OR fieldname = ? OR fieldname = ?) AND src_id = ?', ['PrimaryCategory''Categories''StyleRange'$dataObject->getId()]);
  73.         $primaryCategories $dataObject->getPrimaryCategory();
  74.         $categories $dataObject->getCategories();
  75.         $styleRanges $dataObject->getStyleRange();
  76.         $currentRelationIds = [
  77.             'PrimaryCategory'=>[],
  78.             'Categories'=>[],
  79.             'StyleRange'=>[]
  80.         ];
  81.         foreach ($currentRelations as $currentRelation) {
  82.             $currentRelationIds[$currentRelation['fieldname']][] = $currentRelation['dest_id'];
  83.         }
  84.         $newPrimaryCategoryIds = [];
  85.         $newCategoryIds = [];
  86.         $newStyleRangeIds = [];
  87.         foreach ($primaryCategories as $primaryCategory) {
  88.             $newPrimaryCategoryIds[] = $primaryCategory->getId();
  89.         }
  90.         if (count(array_merge(array_diff($newPrimaryCategoryIds$currentRelationIds['PrimaryCategory']), array_diff($currentRelationIds['PrimaryCategory'], $newPrimaryCategoryIds))) > 0) {
  91.             return true;
  92.         }
  93.         foreach ($categories as $category) {
  94.             $newCategoryIds[] = $category->getId();
  95.         }
  96.         
  97.         if (count(array_merge(array_diff($newCategoryIds$currentRelationIds['Categories']), array_diff($currentRelationIds['Categories'], $newCategoryIds))) > 0) {
  98.             return true;
  99.         }
  100.         if (!empty($styleRanges)) {
  101.             foreach ($styleRanges as $styleRange) {
  102.                 $newStyleRangeIds[] = $styleRange->getId();
  103.             }
  104.         }
  105.         if (count(array_merge(array_diff($newStyleRangeIds$currentRelationIds['StyleRange']), array_diff($currentRelationIds['StyleRange'], $newStyleRangeIds))) > 0) {
  106.             return true;
  107.         }
  108.         return false;
  109.     }
  110.     private function hasClassificationDataChanged(Product $dataObject) {
  111.         $currentClassificationData Db::getConnection()->fetchAllAssociative('SELECT * FROM object_classificationstore_data_Product WHERE o_id = ? AND fieldname = ?', [$dataObject->getId(), 'WebthinkingAttributeData']);
  112.         $newClassificationData $dataObject->getWebthinkingAttributeData();
  113.         $currentClassificationDataArray = [];
  114.         foreach ($currentClassificationData as $currentData) {
  115.             
  116.             $values explode(","$currentData['value']);
  117.             if (count($values) > || $currentData['type'] == 'multiselect') {
  118.                 $currentClassificationDataArray[$currentData['groupId']][$currentData['keyId']][$currentData['language']] = $values;
  119.             } else {
  120.                 $currentClassificationDataArray[$currentData['groupId']][$currentData['keyId']][$currentData['language']] = $values[0];
  121.             }
  122.         }
  123.         foreach ($newClassificationData->getItems() as $groupId=>$keyList) {
  124.             foreach ($keyList as $keyId=>$values) {
  125.                 foreach ($values as $valueKey => $actualValues) {
  126.                     if (!isset($currentClassificationDataArray[$groupId][$keyId][$valueKey])) {
  127.                         return true;
  128.                     }
  129.                     $checkValue $currentClassificationDataArray[$groupId][$keyId][$valueKey];
  130.                     if ($currentClassificationDataArray[$groupId][$keyId][$valueKey] != $actualValues) {
  131.                         return true;
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.         return false;
  137.     }
  138.     /**
  139.      * Gets the system user. Defaults to the administrator account if a parameter cannot be found
  140.      *
  141.      * @param ParameterBagInterface $params
  142.      * @return int
  143.      */
  144.     private function GetUserId(ParameterBagInterface $params) {
  145.         if (!empty($params->get('datatoparent')['user'])) {
  146.             if (is_int($params->get('datatoparent')['user'])) {
  147.                 return $params->get('datatoparent')['user'];
  148.             } else {
  149.                 throw new \Exception("The user has not been correctly defined in your settings file. Please ensure the user ID has been used");
  150.             }
  151.         } else {
  152.             return 2;
  153.         }
  154.     }
  155.     private function GetProductLevel(Product $dataObject) {
  156.         if (!empty($dataObject->getProductLevel())) {
  157.             return strtolower($dataObject->getProductLevel());
  158.         } else {
  159.             if ($dataObject->getParent()->getType() == "parent") {
  160.                 return "style";
  161.             } else if ($dataObject->getParent()->getType() == "variant") {
  162.                 return "child";
  163.             } else {
  164.                 throw new \Exception("Your products relationship is not setup correctly. Please fix the relationship of the product and then try again.");
  165.             }
  166.         }
  167.     }
  168.     private function GetParentProduct(Product $dataObject$productLevel) {
  169.         if ($productLevel == "style") {
  170.             return $dataObject->getParent();
  171.         } else {
  172.             return $dataObject->getParent()->getParent();
  173.         }
  174.     }
  175. }