<?php
namespace Jabber\GenericBundle\EventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\Colour;
use Pimcore\Model\DataObject\Product;
class RelationListener {
public function onPreUpdateAndSave(ElementEventInterface $e) {
if (!$e->hasArgument('isAutoSave')) {
if ($e instanceof DataObjectEvent) {
// do something with the object
$dataObject = $e->getObject();
if ($dataObject instanceof Product) {
if ($dataObject->getType() == "variant" && $dataObject->getProductLevel() == "STYLE") {
if (method_exists($dataObject, 'getColourRelation') && method_exists($dataObject, 'getColour')) {
$dataObject = $this->ChangeColourRelation($dataObject, $dataObject->getColour());
}
}
}
}
}
}
private function GetPreviousVersion(Product $dataObject) {
$versions = $dataObject->getVersions();
$previousVersion = $versions[count($versions)-1];
$previousObject = $previousVersion->getData();
return $previousObject;
}
private function ChangeColourRelation(Product $dataObject, $colourCode) {
$colourObject = Colour::getByColourID($colourCode);
if ($colourObject instanceof Colour\Listing && !empty($colourObject->current())) {
$colourValue = $colourObject->current();
$dataObject->setColourRelation($colourValue);
}
return $dataObject;
}
}