<?php
// created by: Cesar Bianchi
namespace IndabaSolutions\CSRepresentationBundle\EventListener;
use IndabaSolutions\CSRepresentationBundle\Service\ClassificationStoreElements\ClassificationStoreGroupService;
use IndabaSolutions\CSRepresentationBundle\Service\ClassificationStoreElements\ClassificationStoreKeyService;
use Pimcore\Event\Model\VersionEvent;
use Pimcore\Event\Model\DataObjectEvent;
use IndabaSolutions\CSRepresentationBundle\Model\DataObject\GenericParentClass;
class DataObjectVersionListener
{
protected ClassificationStoreGroupService $classificationStoreGroupService;
protected ClassificationStoreKeyService $classificationStoreKeyService;
protected $keyObjectClassName;
protected $deleteGroupOnDeleteGeneric;
protected $sincOnAddKeyObject;
protected $sincOnAddGeneric;
protected $deleteKeyOnDeleteKeyObject;
protected $addKeyFromUI;
protected $addGenericFromUI;
protected $deleteKeyFromUI;
protected $deleteGenericFromUI;
protected $deleteGenericFromCS;
public function __construct(
ClassificationStoreGroupService $classificationStoreGroupService,
ClassificationStoreKeyService $classificationStoreKeyService,
$keyObjectClassName,
$deleteGroupOnDeleteGeneric,
$sincOnAddKeyObject,
$sincOnAddGeneric,
$deleteKeyOnDeleteKeyObject,
$addKeyFromUI,
$addGenericFromUI,
$deleteKeyFromUI,
$deleteGenericFromUI,
$deleteGenericFromCS
)
{
$this->classificationStoreGroupService = $classificationStoreGroupService;
$this->classificationStoreKeyService = $classificationStoreKeyService;
$this->keyObjectClassName = $keyObjectClassName;
$this->deleteGroupOnDeleteGeneric = $deleteGroupOnDeleteGeneric;
$this->sincOnAddKeyObject = $sincOnAddKeyObject;
$this->sincOnAddGeneric = $sincOnAddGeneric;
$this->deleteKeyOnDeleteKeyObject = $deleteKeyOnDeleteKeyObject;
$this->addKeyFromUI = $addKeyFromUI;
$this->addGenericFromUI = $addGenericFromUI;
$this->deleteKeyFromUI = $deleteKeyFromUI;
$this->deleteGenericFromUI = $deleteGenericFromUI;
$this->deleteGenericFromCS = $deleteGenericFromCS;
}
private function getTraceArray()
{
$e = new \Exception();
$trace = $e->getTraceAsString();
return explode("\n", $trace);
}
private function IsInstanceOfKeyClass($dataObject)
{
$keyClass = "Pimcore\Model\DataObject\\$this->keyObjectClassName";
$keyClass = new $keyClass;
return $dataObject instanceof $keyClass;
}
public function onPreSave(VersionEvent $event)
{
$dataObject = $event->getVersion();
$dataObject = $dataObject->getData();
$explodedTrace = $this->getTraceArray();
$fromBundle = str_contains($explodedTrace[12], "csrepresentationbundle");
if ($this->IsInstanceOfKeyClass($dataObject)) {
if (! $this->addKeyFromUI && (! $fromBundle)) {
throw new \Exception("Cannot add Key from UI, please use Classification Store to edit.");
}
} elseif ($dataObject instanceof GenericParentClass) {
if (! $this->addGenericFromUI && ! $fromBundle) {
throw new \Exception("Cannot add Generic from UI, please use Classification Store to edit.");
}
}
}
public function onPreDelete(DataObjectEvent $event)
{
$dataObject = $event->getObject();
$explodedTrace = $this->getTraceArray();
$fromBundle = str_contains($explodedTrace[7], "csrepresentationbundle");
if ($this->IsInstanceOfKeyClass($dataObject)) {
if (! $this->deleteKeyFromUI && ! $fromBundle) {
throw new \Exception("Cannot delete Key from UI, please use Classification Store to edit.");
}
} elseif ($dataObject instanceof GenericParentClass) {
if (! $this->deleteGenericFromUI && ! $fromBundle) {
throw new \Exception("Cannot delete Generic from UI, please use Classification Store to edit.");
}
}
}
public function onPostSave(VersionEvent $event)
{
$dataObject = $event->getVersion();
$dataObject = $dataObject->getData();
$explodedTrace = $this->getTraceArray();
$fromBundle = str_contains($explodedTrace[7], "csrepresentationbundle") ||
str_contains($explodedTrace[12], "csrepresentationbundle");
if ($this->sincOnAddGeneric && !$fromBundle && $dataObject instanceof GenericParentClass) {
$this->classificationStoreGroupService->sincGroupObjectKeys($dataObject);
} elseif ($this->sincOnAddKeyObject && !$fromBundle && $this->IsInstanceOfKeyClass($dataObject)) {
$this->classificationStoreKeyService->sincKeyObjects($dataObject);
}
}
public function onPostDelete(DataObjectEvent $event)
{
$dataObject = $event->getObject();
if ($this->deleteGroupOnDeleteGeneric && $dataObject instanceof GenericParentClass) {
$this->classificationStoreGroupService->deleteGroupObject($dataObject);
}
if ($this->deleteKeyOnDeleteKeyObject && $this->IsInstanceOfKeyClass($dataObject)) {
$this->classificationStoreKeyService->deleteKeyObject($dataObject);
}
}
}