<?php
namespace IndabaSolutions\WebToPrintDownloadBundle\EventListener;
use IndabaSolutions\WebToPrintDownloadBundle\Repository\DataObjectRepository;
use IndabaSolutions\WebToPrintDownloadBundle\Service\TemplateProcessorService;
use Pimcore\Event\Model\DataObjectEvent;
/**
* UpdateTemplateListener
*/
class UpdateTemplateListener
{
/**
* @var TemplateProcessorService $templateProcessorService
*/
protected TemplateProcessorService $templateProcessorService;
/**
* __construct
*
* @param mixed $templateProcessorService
* @return void
*/
public function __construct(TemplateProcessorService $templateProcessorService)
{
$this->templateProcessorService = $templateProcessorService;
}
/**
* setResult
*
* @param DataObjectEvent $event
* @return void
*/
protected function setResult(DataObjectEvent $event): void
{
$element = $event->getElement();
$templateClass = $this->templateProcessorService->getTemplateClass();
$componentClass = $this->templateProcessorService->getComponentClass();
if ($element instanceof $templateClass) {
$html = $this->templateProcessorService->renderTemplateObject($element);
$element->setResult($html);
}
if ($element instanceof $componentClass) {
$templateClass = $templateClass;
try {
$relatedTemplates = DataObjectRepository::getRelatedFrom(
$templateClass, 'components', $element->getId()
);
foreach ($relatedTemplates as $templateObject) {
$templateObject->setResult(null);
}
} catch (\Exception $e) {}
}
}
/**
* onUpdateDo
*
* @param DataObjectEvent $event
* @return void
*/
protected function onUpdateDo(DataObjectEvent $event): void
{
$this->setResult($event);
}
/**
* onObjectPreUpdate
*
* @param DataObjectEvent $event
* @return void
*/
public function onObjectPreUpdate(DataObjectEvent $event): void
{
$this->onUpdateDo($event);
}
}