<?php
namespace PfcExportBundle\EventListener;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\ClassDefinition\CustomLayout;
use Pimcore\Model\DataObject\Service;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Yaml\Yaml;
class TemplateListener implements EventSubscriberInterface
{
const TEMPLATE_LAYOUT = PIMCORE_PROJECT_ROOT . '/bundles/PfcExportBundle/Resources/config/pfcExport/export/config.yml';
/**
* @param GenericEvent $event
*/
public function checkPermissions(GenericEvent $event): void
{
$object = $event->getArgument("object");
//data element that is send to Pimcore backend UI
$data = $event->getArgument("data");
$data['hasPreview'] = false;
if ($object instanceof DataObject\Template) {
$parsedYaml = Yaml::parse(file_get_contents(self::TEMPLATE_LAYOUT));
$currentLayout = explode(",", $parsedYaml["pfc_export"]['template_layout']);
if(!in_array($data['currentLayoutId'], $currentLayout)) {
// Set Model Layout when product is a Model and Sku Layout when it is variant (sku)
$data = $this->f_setLayout($object, $data);
$event->setArgument("data", $data);
}
}
}
/**
* @param $object
* @param $data
* @return mixed
*/
public function f_setLayout($object, $data): array
{
$parsedYaml = Yaml::parse(file_get_contents(self::TEMPLATE_LAYOUT));
DataObject\Template::setGetInheritedValues(true);
if ($object->getTemplateType() == "leadTime") {
$customLayoutToSelect = $parsedYaml["pfc_export"]['template_layout'];
} else {
$customLayoutToSelect = "";
}
if ($customLayoutToSelect != null) {
//set current layout to subcategory layout
$data['currentLayoutId'] = $customLayoutToSelect;
$customLayout = CustomLayout::getById($customLayoutToSelect);
$data['layout'] = $customLayout->getLayoutDefinitions();
Service::enrichLayoutDefinition($data["layout"], $object);
}
if (!empty($layoutsToRemove)) {
//remove master layout from valid layouts
$validLayouts = $data["validLayouts"];
foreach ($validLayouts as $key => $validLayout) {
if (in_array($validLayout['id'], $layoutsToRemove)) {
unset($validLayouts[$key]);
}
}
$data["validLayouts"] = array_values($validLayouts);
}
return $data;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
// 'workflow.Product_Workflow.guard' => ['guardPublish'],
// 'workflow.Product_Workflow.leave' => ['onLeave'],
];
}
}