<?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 Pimcore\Model\Element\ValidationException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Yaml\Yaml;
class ThemeListener implements EventSubscriberInterface
{
const pttheme_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");
if ($object instanceof DataObject\Theme) {
$parsedYaml = Yaml::parse(file_get_contents(self::pttheme_layout));
//data element that is send to Pimcore backend UI
$data = $event->getArgument("data");
$data['hasPreview'] = false;
if($object->getPtTheme() == "pttheme") {
// $currentLayout = explode(",", $parsedYaml["pfc_export"]['pt_theme_layout']);
if (isset($parsedYaml["pfc_export"]['pt_theme_layout']) && !in_array($data['currentLayoutId'], $parsedYaml["pfc_export"]['pt_theme_layout'])) {
// 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::pttheme_layout));
DataObject\Product::setGetInheritedValues(true);
if ($object->getPtTheme() == "pttheme") {
$customLayoutToSelect = $parsedYaml["pfc_export"]['pt_theme_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;
}
public static function getSubscribedEvents(): array
{
return [
// 'workflow.Product_Workflow.guard' => ['guardPublish'],
// 'workflow.Product_Workflow.leave' => ['onLeave'],
];
}
/**
* @param ElementEventInterface $event
*
* @throws \Pimcore\Model\Element\ValidationException
*/
public function onPreUpdate(ElementEventInterface $event): void
{
if ($event instanceof DataObjectEvent) {
$objects = $event->getObject();
$this->checkValidation($objects);
}
}
/**
* @param $objects
* @throws ValidationException
*/
public function checkValidation($objects): void
{
if ($objects instanceof DataObject\Theme) {
$errors = [];
if ($objects->getPtTheme() == "pttheme") {
// if (null == $objects->getUniqueCode()) {
// $message = 'UniqueCode is required.';
// array_push($errors, $message);
// }
// if ($objects->getUniqueCode() == "" || $objects->getUniqueCode() == null) {
// if (null == $objects->getChannel()) {
// $message = 'Channel is required.';
// array_push($errors, $message);
// }
// if (empty($objects->getProducts())) {
// $message = 'Product Model is required.';
// array_push($errors, $message);
// }
if ($objects->getHeroPFCollection() == '1' || $objects->getHeroPFCollection() == 1) {
if ($objects->getUniqueHeroPfCollectionSortingId() == "" || $objects->getUniqueHeroPfCollectionSortingId() == null) {
$message = 'Hero PF Collection with Sorting Id cannto be empty and need to Unique.';
array_push($errors, $message);
}
}
$this->getExceptionMessage($errors, $objects);
$objects->setOmitMandatoryCheck(true);
// $objects->setPublished(true);
// $objects->save();
// }
}
}
}
/**
* @param $themeMessages
* @param $object
* @throws ValidationException
*/
public function getExceptionMessage($themeMessages, $object): void
{
if (!empty($themeMessages)) {
$result = null;
foreach ($themeMessages as $themeMessage) {
$result .= $themeMessage . "<span style='color: red;'>*</span>" . '<br>';
}
throw new ValidationException($result, 403);
}
}
}