<?php
namespace Jabber\GenericBundle\EventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject\Photoshoot;
class Event {
public function onPreAdd (ElementEventInterface $e) {
if (!$e->hasArgument('isAutoSave')) {
if ($e instanceof DataObjectEvent) {
// do something with the object
$dataObject = $e->getObject();
if ($dataObject instanceof Photoshoot) {
if (empty($dataObject->getAccessKey())) {
$dataObject->setAccessKey(implode('', str_split(substr(strtolower(md5(microtime().rand(1000, 9999))), 0, 30), 6)));
}
}
}
}
}
public function onPreDelete(ElementEventInterface $e) {
if ($e instanceof DataObjectEvent) {
$eventObject = $e->getObject();
if ($eventObject instanceof Photoshoot) {
$assetPath = $eventObject->getDirectories();
foreach ($assetPath as $path) {
$assets = Asset::getById($path->getElementId());
$children = $assets->getChildren();
if (count($children) > 0) {
throw new \Exception("The event has assets stored against it. Cannot be deleted.");
} else {
$assets->delete();
}
}
}
}
}
}