<?php
/**
* Created by PhpStorm.
* User: javra
* Date: 8/30/20
* Time: 5:21 PM
*/
namespace PfcPublicationBundle\EventListener;
use App\Entity\Site;
use Cocur\Slugify\Slugify;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\DataObject;
use Pimcore\Model\Element\ValidationException;
use Symfony\Component\Workflow\Registry;
class WorkflowListener
{
private $workflowRegistry;
/**
* @param ElementEventInterface $event
*/
public function onPreUpdate(ElementEventInterface $event): void
{
if ($event instanceof DataObjectEvent) {
$objects = $event->getObject();
// if ($objects->getClassName() == "Publication") {
$this->checkValidation($objects);
// }
}
}
/**
* @param $objects
* @throws ValidationException
*/
public function checkValidation($objects): void
{
if ($objects instanceof DataObject\Publication) {
if (null == $objects->getPublicationName()) {
$message = 'Publication Name is required.';
$result = $message . "<span style='color: red;'>*</span>" . '<br>';
throw new ValidationException($result, 403);
}
if ($objects->getPublicationName() != null) {
$this->getPublicationData($objects);
}
}
}
/**
* @param $objects
*/
public function getPublicationData($objects): void
{
$this->slugify = new Slugify();
$keyValue = $this->slugify->slugify($objects->getPublicationName());
if ($keyValue != $objects->getKey()) {
$objects->setKey($keyValue);
}
$publicationObject = DataObject::getByPath('/' . $objects->getParent()->getFullPath());
$objects->setParentId($objects->getParentId());
$objects->setPath($publicationObject);
}
}