<?phpnamespace IndabaPublicationBundle\EventListener;use Pimcore\Event\Model\ElementEventInterface;use Pimcore\Event\Model\DataObjectEvent;use Pimcore\Model\DataObject;use Pimcore\Event\Model\AssetEvent;use Pimcore\Event\Model\DocumentEvent;use Pimcore\Db;use IndabaPublicationBundle\Service\Exportconfig;use IndabaPublicationBundle\Service\Helper;/** * Description of RelationsUpdateListener * * @author PcCom */class RelationsUpdateListener{ public function onPreUpdate(ElementEventInterface $e) { if ($e instanceof DataObjectEvent) { try { $updateObject = $e->getObject(); $oid = $updateObject->getId(); $updateKeyValue = $updateObject->getKey(); $currentObject = DataObject::getById($oid, true); if (!$currentObject) { return 0; } if ($currentObject->getKey() !== $updateKeyValue) { $db = Db::get(); $result = $db->query("SELECT `id`, `name`, `structure_path` FROM `schrader_catalogue_structure` WHERE `object_id` = " . $oid . ";"); while ($row = $result->fetch()) { $nodeId = $row["id"]; $oldStructurePath = $row["structure_path"] . $row["name"] . "/"; $newStructurePath = $row["structure_path"] . $updateKeyValue . "/"; $db->query("UPDATE `schrader_catalogue_structure` SET `name` = '" . $updateKeyValue . "' WHERE `id` = " . $nodeId . " ;"); $db->query("UPDATE `schrader_catalogue_structure` SET `structure_path` = REPLACE (`structure_path`, '" . $oldStructurePath . "', '" . $newStructurePath . "') WHERE `structure_path` LIKE '" . $oldStructurePath . "%';"); } } if (method_exists($currentObject, "getNotforpublications")) { $cachedValue = $currentObject->getNotforpublications(); } if (method_exists($updateObject, "getNotforpublications")) { $newValue = $updateObject->getNotforpublications(); } if ($newValue && ($newValue !== $cachedValue)) { // $this->deleteObject($oid); } } catch (\Exception $e) { $error = $e->getMessage(); \Pimcore\Log\Simple::log("ObjectEventLog", "IndabaPublication preUpdate: " . $error); } } } public function onDataObjectDeleteInfo(ElementEventInterface $e) { if ($e instanceof DataObjectEvent) { $object = $e->getObject(); if ($this->inPublications($object)) { $e->setDeletionAllowed(false); $e->setReason("This object \"" . $object->getKey() . "\" or his children have any relations to publications. You must remove this relations before you can delete this object"); } } } public function onPreDelete(ElementEventInterface $e) { if ($e instanceof DataObjectEvent) { $object = $e->getObject(); if ($this->inPublications($object)) { throw new \Exception("This object or his children have any relations to publications. You must remove this relations before you can delete this object."); } } } private function inPublications($object) { $db = Db::get(); $query = "SELECT id FROM schrader_catalogue_structure WHERE object_id IN (SELECT o_id FROM objects WHERE o_id = " . $object->getId() . " OR o_path LIKE '" . $object->getFullPath() . "/%') LIMIT 1;"; $result = $db->query($query); return ($result->rowCount() > 0) ? true : false ; }}