bundles/PfcExportBundle/EventListener/ThemeListener.php line 92

Open in your IDE?
  1. <?php
  2. namespace PfcExportBundle\EventListener;
  3. use Pimcore\Event\Model\DataObjectEvent;
  4. use Pimcore\Event\Model\ElementEventInterface;
  5. use Pimcore\Model\DataObject;
  6. use Pimcore\Model\DataObject\ClassDefinition\CustomLayout;
  7. use Pimcore\Model\DataObject\Service;
  8. use Pimcore\Model\Element\ValidationException;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\EventDispatcher\GenericEvent;
  11. use Symfony\Component\Yaml\Yaml;
  12. class ThemeListener implements EventSubscriberInterface
  13. {
  14.     const pttheme_layout PIMCORE_PROJECT_ROOT '/bundles/PfcExportBundle/Resources/config/pfcExport/export/config.yml';
  15.     /**
  16.      * @param GenericEvent $event
  17.      */
  18.     public function checkPermissions(GenericEvent $event): void
  19.     {
  20.         $object $event->getArgument("object");
  21.         if ($object instanceof DataObject\Theme) {
  22.             $parsedYaml Yaml::parse(file_get_contents(self::pttheme_layout));
  23.             //data element that is send to Pimcore backend UI
  24.             $data $event->getArgument("data");
  25.             $data['hasPreview'] = false;
  26.             if($object->getPtTheme() == "pttheme") {
  27. //                $currentLayout = explode(",", $parsedYaml["pfc_export"]['pt_theme_layout']);
  28.                 if (isset($parsedYaml["pfc_export"]['pt_theme_layout']) && !in_array($data['currentLayoutId'], $parsedYaml["pfc_export"]['pt_theme_layout'])) {
  29.                     // Set Model Layout when product is a Model and Sku Layout when it is variant (sku)
  30.                     $data $this->f_setLayout($object$data);
  31.                     $event->setArgument("data"$data);
  32.                 }
  33.             }
  34.         }
  35.     }
  36.     /**
  37.      * @param $object
  38.      * @param $data
  39.      * @return mixed
  40.      */
  41.     public function f_setLayout($object$data): array
  42.     {
  43.         $parsedYaml Yaml::parse(file_get_contents(self::pttheme_layout));
  44.         DataObject\Product::setGetInheritedValues(true);
  45.         if ($object->getPtTheme() == "pttheme") {
  46.             $customLayoutToSelect $parsedYaml["pfc_export"]['pt_theme_layout'];
  47.         } else {
  48.             $customLayoutToSelect "";
  49.         }
  50.         if ($customLayoutToSelect != null) {
  51.             //set current layout to subcategory layout
  52.             $data['currentLayoutId'] = $customLayoutToSelect;
  53.             $customLayout CustomLayout::getById($customLayoutToSelect);
  54.             $data['layout'] = $customLayout->getLayoutDefinitions();
  55.             Service::enrichLayoutDefinition($data["layout"], $object);
  56.         }
  57.         if (!empty($layoutsToRemove)) {
  58.             //remove master layout from valid layouts
  59.             $validLayouts $data["validLayouts"];
  60.             foreach ($validLayouts as $key => $validLayout) {
  61.                 if (in_array($validLayout['id'], $layoutsToRemove)) {
  62.                     unset($validLayouts[$key]);
  63.                 }
  64.             }
  65.             $data["validLayouts"] = array_values($validLayouts);
  66.         }
  67.         return $data;
  68.     }
  69.     public static function getSubscribedEvents(): array
  70.     {
  71.         return [
  72. //            'workflow.Product_Workflow.guard' => ['guardPublish'],
  73. //            'workflow.Product_Workflow.leave' => ['onLeave'],
  74.         ];
  75.     }
  76.     /**
  77.      * @param ElementEventInterface $event
  78.      *
  79.      * @throws \Pimcore\Model\Element\ValidationException
  80.      */
  81.     public function onPreUpdate(ElementEventInterface $event): void
  82.     {
  83.         if ($event instanceof DataObjectEvent) {
  84.             $objects $event->getObject();
  85.             $this->checkValidation($objects);
  86.         }
  87.     }
  88.     /**
  89.      * @param $objects
  90.      * @throws ValidationException
  91.      */
  92.     public function checkValidation($objects): void
  93.     {
  94.         if ($objects instanceof DataObject\Theme) {
  95.             $errors = [];
  96.             if ($objects->getPtTheme() == "pttheme") {
  97. //                if (null == $objects->getUniqueCode()) {
  98. //                    $message = 'UniqueCode is required.';
  99. //                    array_push($errors, $message);
  100. //                }
  101. //                if ($objects->getUniqueCode() == "" || $objects->getUniqueCode() == null) {
  102. //                if (null == $objects->getChannel()) {
  103. //                    $message = 'Channel is required.';
  104. //                    array_push($errors, $message);
  105. //                }
  106. //                if (empty($objects->getProducts())) {
  107. //                    $message = 'Product Model is required.';
  108. //                    array_push($errors, $message);
  109. //                }
  110.                 if ($objects->getHeroPFCollection() == '1' || $objects->getHeroPFCollection() == 1) {
  111.                     if ($objects->getUniqueHeroPfCollectionSortingId() == "" || $objects->getUniqueHeroPfCollectionSortingId() == null) {
  112.                         $message 'Hero PF Collection with Sorting Id cannto be empty and need to Unique.';
  113.                         array_push($errors$message);
  114.                     }
  115.                 }
  116.                 $this->getExceptionMessage($errors$objects);
  117.                 $objects->setOmitMandatoryCheck(true);
  118. //                $objects->setPublished(true);
  119. //                $objects->save();
  120. //                }
  121.             }
  122.         }
  123.     }
  124.     /**
  125.      * @param $themeMessages
  126.      * @param $object
  127.      * @throws ValidationException
  128.      */
  129.     public function getExceptionMessage($themeMessages$object): void
  130.     {
  131.         if (!empty($themeMessages)) {
  132.             $result null;
  133.             foreach ($themeMessages as $themeMessage) {
  134.                 $result .= $themeMessage "<span style='color: red;'>*</span>" '<br>';
  135.             }
  136.             throw new ValidationException($result403);
  137.         }
  138.     }
  139. }