<?php
namespace ExportBundle\EventListener;
use ExportBundle\Notifications\Heirachy;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\Department;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\Brand;
use Pimcore\Model\DataObject\Collections;
use Pimcore\Model\DataObject\ColourGroup;
use Pimcore\Model\DataObject\MainGroup;
use Pimcore\Model\DataObject\Season;
use Pimcore\Model\DataObject\SubGroup;
class HeirachyListener {
public function onObjectSave(ElementEventInterface $e) {
if($e instanceof DataObjectEvent) {
if (!$e->hasArgument('isAutoSave')) {
$dataObject = $e->getObject();
if (
$dataObject instanceof Department
|| $dataObject instanceof MainGroup
|| $dataObject instanceof SubGroup
|| $dataObject instanceof Collections
|| $dataObject instanceof Season
|| $dataObject instanceof ColourGroup
|| $dataObject instanceof Brand
) {
if ($this->CheckForChanges($dataObject)) {
$heirachyNotification = new Heirachy();
$heirachyNotification->SendMessage($dataObject);
}
}
}
}
}
public function onObjectCreate(ElementEventInterface $e) {
if($e instanceof DataObjectEvent) {
$dataObject = $e->getObject();
if ($dataObject instanceof Department) {
$heirachyNotification = new Heirachy();
$heirachyNotification->SendMessage($dataObject);
}
}
}
private function CheckForChanges(DataObject $dataObject) {
$type = substr(strrchr(get_class($dataObject), '\\'), 1);
if (method_exists($dataObject, 'get'.$type.'ID') && method_exists($dataObject, 'getName')) {
$function = 'get'.$type.'ID';
if (empty($dataObject->$function())) {
return false;
}
if ($dataObject->isPublished() == false) {
return false;
}
$versions = $dataObject->getVersions();
$previousVersion = $versions[count($versions)-2];
if (!empty($previousVersion)) {
$previousObject = $previousVersion->getData();
}
if (isset($previousObject) && !empty($previousObject)) {
$currentId = $dataObject->$function();
$currentName = $dataObject->getName();
$previousId = $previousObject->$function();
$previousName = $previousObject->getName();
$currentStatus = $dataObject->isPublished();
$previousStatus = $previousObject->isPublished();
if ($currentId != $previousId || $currentName != $previousName || $currentStatus != $previousStatus) {
return true;
}
} else {
return true;
}
return false;
}
}
}