src/EventListener/AveryEventListener.php line 133

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Pimcore\Event\BundleManager\PathsEvent;
  4. use Pimcore\Event\Model\DataObjectEvent;
  5. use Pimcore\Event\Model\ElementEventInterface;
  6. use Pimcore\Log\ApplicationLogger;
  7. use Pimcore\Model\Asset;
  8. use Pimcore\Model\DataObject\AveryProdBase;
  9. use Pimcore\Model\DataObject\AverySalesChannel;
  10. use Pimcore\Model\DataObject\ExportProfile;
  11. use Pimcore\Model\DataObject\AveryExportProfile;
  12. // use Pimcore\Model\Notification\Service\NotificationService;
  13. // use Symfony\Contracts\Translation\TranslatorInterface;
  14. use App\Service\AverySettingsService;
  15. use Pimcore\Tool\Session;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. class AveryEventListener
  18. {
  19.     private $logger;
  20.     private $settingsService;
  21.     private RequestStack $requestStack;
  22.     public function __construct(
  23.         ApplicationLogger    $logger,
  24.         AverySettingsService $settingsService,
  25.         RequestStack         $requestStack
  26.     )
  27.     {
  28.         $this->logger $logger;
  29.         $this->settingsService $settingsService;
  30.         $this->requestStack $requestStack;
  31.     }
  32.     public function addCSSFiles(PathsEvent $event): void
  33.     {
  34.         $event->setPaths(
  35.             array_merge(
  36.                 $event->getPaths(),
  37.                 [
  38.                     '/admin-static/css/admin-style.css'
  39.                 ]
  40.             )
  41.         );
  42.         $leerFeatureEnabled false;
  43.         $user \Pimcore\Tool\Admin::getCurrentUser();
  44.         if ($user == null && $this->requestStack->getCurrentRequest()) {
  45.             try {
  46.                 $user Session::getSessionBag($this->requestStack->getSession())->get("user");
  47.             } catch (\Exception $e) {
  48.                 $this->logger->error("AVERY EVENT LISTENER | Could not get user session from session bag!", [$e]);
  49.             }
  50.         }
  51.         if ($user != null) {
  52.             $roles $user->getRoles();
  53.             foreach ($roles as $role) {
  54.                 $role \Pimcore\Model\User\Role::getById($role);
  55.                 if (strpos(strtolower($role->getName()), "meto") !== false) {
  56.                     $leerFeatureEnabled true;
  57.                     break;
  58.                 }
  59.             }
  60.         }
  61.         if ($leerFeatureEnabled) {
  62.             $event->setPaths(
  63.                 array_merge(
  64.                     $event->getPaths(),
  65.                     [
  66.                         '/admin-static/css/leerFeature.css',
  67.                     ]
  68.                 )
  69.             );
  70.         }
  71.     }
  72.     public function addJSFiles(PathsEvent $event): void
  73.     {
  74.         $event->setPaths(
  75.             array_merge(
  76.                 $event->getPaths(),
  77.                 [
  78.                     '/admin-static/js/startup.js',
  79.                     '/admin-static/js/metostartup.js',
  80.                     '/admin-static/js/transitions.js',
  81.                 ]
  82.             )
  83.         );
  84.         $leerFeatureEnabled false;
  85.         $user \Pimcore\Tool\Admin::getCurrentUser();
  86.         if ($user == null) {
  87.             try {
  88.                 $user Session::getSessionBag($this->requestStack->getSession())->get("user");
  89.             } catch (\Exception $e) {
  90.                 $this->logger->error("AVERY EVENT LISTENER | Could not get user session from session bag!", [$e]);
  91.             }
  92.         }
  93.         if ($user != null) {
  94.             if (strpos(strtolower($user->getEmail()), "@meto") !== false) {
  95.                 $leerFeatureEnabled true;
  96.             } else {
  97.                 $roles $user->getRoles();
  98.                 foreach ($roles as $role) {
  99.                     $role \Pimcore\Model\User\Role::getById($role);
  100.                     if (strpos(strtolower($role->getName()), "meto") !== false) {
  101.                         $leerFeatureEnabled true;
  102.                         break;
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.         if ($leerFeatureEnabled) {
  108.             $event->setPaths(
  109.                 array_merge(
  110.                     $event->getPaths(),
  111.                     [
  112.                         '/admin-static/js/leerFeature.js',
  113.                     ]
  114.                 )
  115.             );
  116.         }
  117.     }
  118.     public function onPreAdd(ElementEventInterface $e): void
  119.     {
  120.         if ($e instanceof DataObjectEvent) {
  121.             $newExportProfileObject $e->getObject();
  122.             // TODO: Abfrage für Meto Profile Klasse
  123.             if ($newExportProfileObject instanceof ExportProfile || $newExportProfileObject instanceof AveryExportProfile) {
  124.                 // TODO: An neue Ordnerstruktur anpassen
  125.                 // CR: done! (11 months later...)
  126.                 $defaultMappingSetting null;
  127.                 $defaultAttributesMapping null;
  128.                 if ($newExportProfileObject instanceof ExportProfile) {
  129.                     $averyAssDefaultMappingSetting $this->settingsService->getDataFromWebsiteSetting('averyAssDefaultMapping''de');
  130.                     if (!empty($averyAssDefaultMappingSetting) && !empty($averyAssDefaultMappingSetting->getData())) {
  131.                         $defaultMappingSetting $averyAssDefaultMappingSetting->getData();
  132.                     }
  133.                 } elseif ($newExportProfileObject instanceof AveryExportProfile) {
  134.                     $averyAssPhase2DefaultMapSetting $this->settingsService->getDataFromWebsiteSetting('averyAssPhase2DefaultMap''de');
  135.                     if (!empty($averyAssPhase2DefaultMapSetting) && !empty($averyAssPhase2DefaultMapSetting->getData())) {
  136.                         $defaultMappingSetting $averyAssPhase2DefaultMapSetting->getData();
  137.                     }
  138.                 }
  139.                 if (!empty($defaultMappingSetting)) {
  140.                     $defaultAttributesMapping Asset::getByPath($defaultMappingSetting);
  141.                 }
  142.                 $defaultLanguage "de";
  143.                 if ($defaultAttributesMapping) {
  144.                     $newExportProfileObject->setExportAttributesConfig($defaultAttributesMapping);
  145.                 }
  146.                 if ($defaultLanguage) {
  147.                     $newExportProfileObject->setExportLanguage($defaultLanguage);
  148.                 }
  149.                 // Set the default export assets, if any
  150.                 if (!empty($this->settingsService->getDataFromWebsiteSetting('averyTxtExportDefaultAssets''de'))) {
  151.                     $tmpSetting $this->settingsService->getDataFromWebsiteSetting('averyTxtExportDefaultAssets''de')->getData();
  152.                 }
  153.                 if (isset($tmpSetting)) {
  154.                     (array)$exportDefaultAssetsSetting explode(','$tmpSetting);
  155.                     if (isset($exportDefaultAssetsSetting)) {
  156.                         $newExportProfileObject->setExportAssetTypes($exportDefaultAssetsSetting);
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.     }
  162.     public function onObjectPreUpdate(ElementEventInterface $e): void
  163.     {
  164.         $object $e->getObject();
  165.         if (!$e->hasArgument("saveVersionOnly")) {
  166.             if ($object instanceof AverySalesChannel) {
  167.                 // Check for and remove offline products
  168.                 $salesChannelProducts $object->getSalesChannelProducts();
  169.                 // Fixing problem with changing count by unsetting products in temporarily duplicated array instead of actual iterated array
  170.                 $tempProds $salesChannelProducts;
  171.                 $changed false;
  172.                 if (!empty($salesChannelProducts)) {
  173.                     for ($i 0$i <= count($salesChannelProducts) - 1$i++) {
  174.                         if (!$this->checkIsOnline($salesChannelProducts[$i])) {
  175.                             unset($tempProds[$i]);
  176.                             $changed true;
  177.                         }
  178.                     }
  179.                 }
  180.                 if ($changed) {
  181.                     $salesChannelProducts $tempProds;
  182.                     $object->setSalesChannelProducts($salesChannelProducts);
  183.                 }
  184.             }
  185.         }
  186.     }
  187.     public function checkIsOnline(AveryProdBase $prod): bool
  188.     {
  189.         $result false;
  190.         if ($prod->getWorkflowState() == "online") {
  191.             $result true;
  192.         }
  193.         /*
  194.         if($prod->isPublished()){
  195.             $result = true;
  196.         }
  197.         */
  198.         return $result;
  199.     }
  200. }