<?php
namespace Jabber\GenericBundle\EventListener;
use Pimcore\Db;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\DataObject\Product;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class DataToParent {
private $params;
public function __construct(ParameterBagInterface $params) {
$this->params = $params;
}
public function onPreUpdate (ElementEventInterface $e) {
if (!$e->hasArgument('isAutoSave')) {
if ($e instanceof DataObjectEvent) {
$dataObject = $e->getObject();
if ($dataObject instanceof Product) {
// $WebthinkingAttributeData = $dataObject->getWebthinkingAttributeData();
// $hasClassificationChanges = $this->hasClassificationDataChanged($dataObject);
// $hasRelationalDataChanges = $this->hasRelationalDataChanged($dataObject);
// $hasLocalisedDataChanges = $this->hasLocalisedDataChanged($dataObject, 'en_GB');
// $hasLocalisedDataChangesUs = $this->hasLocalisedDataChanged($dataObject, 'en_US');
// $hasProductChanges = $this->hasProductChanged($dataObject);
}
if ($dataObject instanceof Product && $dataObject->getType() == "variant") {
$dataObject->setGetInheritedValues(false);
if ($dataObject->get('DataInheritanceOverride') !== true) {
$parentObject = null;
if (!empty($this->params->get('datatoparent'))) {
$user = $this->GetUserId($this->params);
$productLevel = $this->GetProductLevel($dataObject);
if (!empty($this->params->get('datatoparent')['properties'][$productLevel])) {
foreach ($this->params->get('datatoparent')['properties'][$productLevel] as $property) {
$propertyValue = $dataObject->get($property);
if (!empty($propertyValue)) {
$parentObject = $this->GetParentProduct($dataObject, $productLevel);
$parentObject->set($property, $propertyValue);
$parentObject->setUserModification($user);
$parentObject->save();
$dataObject->set($property, null);
}
}
}
}
}
}
}
}
}
private function hasProductChanged(Product $dataObject) {
$currentProduct = Db::getConnection()->fetchAssociative('SELECT Name, `SKU`, `Brand` FROM object_store_Product WHERE oo_id = ?', [$dataObject->getId()]);
$currentLocalisedProductGb = Db::getConnection()->fetchAssociative('SELECT `Description` FROM object_localized_query_Product_en_GB WHERE ooo_id = ?', [$dataObject->getId()]);
$currentLocalisedProductUs = Db::getConnection()->fetchAssociative('SELECT `Description` FROM object_localized_query_Product_en_Us WHERE ooo_id = ?', [$dataObject->getId()]);
}
private function hasLocalisedDataChanged(Product $dataObject, $locale) {
$fields = ['Description'];
$table = 'object_localized_query_Product_' . $locale;
$tableFields = implode(',', $fields);
$currentLocalisedProduct = Db::getConnection()->fetchAssociative("SELECT {$tableFields} FROM {$table} WHERE ooo_id = ?", [$dataObject->getId()]);
foreach ($fields as $field) {
if ($currentLocalisedProduct[$field] != $dataObject->get($field)) {
return true;
}
}
return false;
}
private function hasRelationalDataChanged(Product $dataObject) {
$currentRelations = Db::getConnection()->fetchAllAssociative('SELECT * FROM object_relations_Product WHERE (fieldname = ? OR fieldname = ? OR fieldname = ?) AND src_id = ?', ['PrimaryCategory', 'Categories', 'StyleRange', $dataObject->getId()]);
$primaryCategories = $dataObject->getPrimaryCategory();
$categories = $dataObject->getCategories();
$styleRanges = $dataObject->getStyleRange();
$currentRelationIds = [
'PrimaryCategory'=>[],
'Categories'=>[],
'StyleRange'=>[]
];
foreach ($currentRelations as $currentRelation) {
$currentRelationIds[$currentRelation['fieldname']][] = $currentRelation['dest_id'];
}
$newPrimaryCategoryIds = [];
$newCategoryIds = [];
$newStyleRangeIds = [];
foreach ($primaryCategories as $primaryCategory) {
$newPrimaryCategoryIds[] = $primaryCategory->getId();
}
if (count(array_merge(array_diff($newPrimaryCategoryIds, $currentRelationIds['PrimaryCategory']), array_diff($currentRelationIds['PrimaryCategory'], $newPrimaryCategoryIds))) > 0) {
return true;
}
foreach ($categories as $category) {
$newCategoryIds[] = $category->getId();
}
if (count(array_merge(array_diff($newCategoryIds, $currentRelationIds['Categories']), array_diff($currentRelationIds['Categories'], $newCategoryIds))) > 0) {
return true;
}
if (!empty($styleRanges)) {
foreach ($styleRanges as $styleRange) {
$newStyleRangeIds[] = $styleRange->getId();
}
}
if (count(array_merge(array_diff($newStyleRangeIds, $currentRelationIds['StyleRange']), array_diff($currentRelationIds['StyleRange'], $newStyleRangeIds))) > 0) {
return true;
}
return false;
}
private function hasClassificationDataChanged(Product $dataObject) {
$currentClassificationData = Db::getConnection()->fetchAllAssociative('SELECT * FROM object_classificationstore_data_Product WHERE o_id = ? AND fieldname = ?', [$dataObject->getId(), 'WebthinkingAttributeData']);
$newClassificationData = $dataObject->getWebthinkingAttributeData();
$currentClassificationDataArray = [];
foreach ($currentClassificationData as $currentData) {
$values = explode(",", $currentData['value']);
if (count($values) > 1 || $currentData['type'] == 'multiselect') {
$currentClassificationDataArray[$currentData['groupId']][$currentData['keyId']][$currentData['language']] = $values;
} else {
$currentClassificationDataArray[$currentData['groupId']][$currentData['keyId']][$currentData['language']] = $values[0];
}
}
foreach ($newClassificationData->getItems() as $groupId=>$keyList) {
foreach ($keyList as $keyId=>$values) {
foreach ($values as $valueKey => $actualValues) {
if (!isset($currentClassificationDataArray[$groupId][$keyId][$valueKey])) {
return true;
}
$checkValue = $currentClassificationDataArray[$groupId][$keyId][$valueKey];
if ($currentClassificationDataArray[$groupId][$keyId][$valueKey] != $actualValues) {
return true;
}
}
}
}
return false;
}
/**
* Gets the system user. Defaults to the administrator account if a parameter cannot be found
*
* @param ParameterBagInterface $params
* @return int
*/
private function GetUserId(ParameterBagInterface $params) {
if (!empty($params->get('datatoparent')['user'])) {
if (is_int($params->get('datatoparent')['user'])) {
return $params->get('datatoparent')['user'];
} else {
throw new \Exception("The user has not been correctly defined in your settings file. Please ensure the user ID has been used");
}
} else {
return 2;
}
}
private function GetProductLevel(Product $dataObject) {
if (!empty($dataObject->getProductLevel())) {
return strtolower($dataObject->getProductLevel());
} else {
if ($dataObject->getParent()->getType() == "parent") {
return "style";
} else if ($dataObject->getParent()->getType() == "variant") {
return "child";
} else {
throw new \Exception("Your products relationship is not setup correctly. Please fix the relationship of the product and then try again.");
}
}
}
private function GetParentProduct(Product $dataObject, $productLevel) {
if ($productLevel == "style") {
return $dataObject->getParent();
} else {
return $dataObject->getParent()->getParent();
}
}
}