vendor/pimcore/data-hub-file-export/src/EventSubscriber/DataObjectSubscriber.php line 101

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under following license:
  6.  * - Pimcore Commercial License (PCL)
  7.  *
  8.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  9.  *  @license    http://www.pimcore.org/license     PCL
  10.  */
  11. namespace Pimcore\Bundle\DataHubFileExportBundle\EventSubscriber;
  12. use Pimcore\Bundle\DataHubBundle\Configuration;
  13. use Pimcore\Bundle\DataHubFileExportBundle\Model;
  14. use Pimcore\Event\DataObjectEvents;
  15. use Pimcore\Event\Model\ElementEventInterface;
  16. use Pimcore\Model\Element\Service;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class DataObjectSubscriber implements EventSubscriberInterface
  19. {
  20.     const ADAPTER_TYPE 'fileExport';
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             DataObjectEvents::POST_UPDATE => 'doExport',
  25.             DataObjectEvents::POST_ADD => 'doExport',
  26.             DataObjectEvents::PRE_DELETE => 'preDelete',
  27.         ];
  28.     }
  29.     protected static function getHelperClass(\Pimcore\Bundle\DataHubBundle\Configuration $config)
  30.     {
  31.         return new \Pimcore\Bundle\DataHubFileExportBundle\Helper();
  32.     }
  33.     public static function doExport(ElementEventInterface $e)
  34.     {
  35.         $element $e->getElement();
  36.         if ($element instanceof \Pimcore\Model\DataObject\Concrete) {
  37.             $configList Configuration::getList();
  38.             foreach ($configList as $config) {
  39.                 if ($config->isActive() && $config->getType() == static::ADAPTER_TYPE) {
  40.                     $configuration $config->getConfiguration();
  41.                     if (!$configuration['schema']['classId']) {
  42.                         continue;
  43.                     }
  44.                     try {
  45.                         $classDef \Pimcore\Model\DataObject\ClassDefinition::getById($configuration['schema']['classId']);
  46.                     } catch (\Exception $e) {
  47.                     }
  48.                     if ($classDef) {
  49.                         $inheritanceEnabled $classDef->getAllowInherit();
  50.                         $saveHookType $configuration['triggerForDelivery']['saveHookType'];
  51.                         $type $configuration['general']['type'];
  52.                         /**
  53.                          * @var $helperClass \Pimcore\Bundle\DataHubFileExportBundle\Helper
  54.                          */
  55.                         $helperClass = static::getHelperClass($config);
  56.                         $exporter $helperClass::getExporterService($config);
  57.                         $relevantItemIds $exporter->getRelevantItemIds();
  58.                         if ($inheritanceEnabled) {
  59.                             $db \Pimcore\Db::get();
  60.                             $query 'SELECT o_id FROM objects WHERE  (o_path LIKE ' $db->quote($element->getFullPath() . '/%') . ' AND o_classId="' $classDef->getId() . '" ) OR o_id=' $element->getId();
  61.                             $itemIdsToUpdate $db->fetchCol($query);
  62.                         } else {
  63.                             $itemIdsToUpdate = [$element->getId()];
  64.                         }
  65.                         if ($saveHookType == 'queue') {
  66.                             foreach ($itemIdsToUpdate as $id) {
  67.                                 if (in_array($id$relevantItemIds)) {
  68.                                     $item = new Model\QueueItem();
  69.                                     $item->setConfigName($config->getName())->setItemId($id)->setItemType(Service::getType($element))->save();
  70.                                 }
  71.                             }
  72.                         } else {
  73.                             Model\QueueItem\Dao::deleteByNameAndItemId($config->getName(), $relevantItemIds);
  74.                         }
  75.                         if ($saveHookType == 'directExport') {
  76.                             // $data = $exporter->getExportData(array_intersect($itemIdsToUpdate, $relevantItemIds));
  77.                             $data $exporter->getExportData($itemIdsToUpdate);
  78.                             $exporter->execute($data);
  79.                         }
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.     }
  85.     public static function preDelete(ElementEventInterface $e)
  86.     {
  87.         $element $e->getElement();
  88.         if ($element instanceof \Pimcore\Model\DataObject\Concrete) {
  89.             $configList Configuration::getList();
  90.             foreach ($configList as $config) {
  91.                 if ($config->isActive() && $config->getType() == static::ADAPTER_TYPE) {
  92.                     $configuration $config->getConfiguration();
  93.                     if ($configuration['schema']['classId']) {
  94.                         $classDef \Pimcore\Model\DataObject\ClassDefinition::getById($configuration['schema']['classId']);
  95.                     }
  96.                     if ($classDef) {
  97.                         $inheritanceEnabled $classDef->getAllowInherit();
  98.                         $saveHookType $configuration['triggerForDelivery']['saveHookType'];
  99.                         $type $configuration['general']['type'];
  100.                         /**
  101.                          * @var $helperClass \Pimcore\Bundle\DataHubFileExportBundle\Helper
  102.                          */
  103.                         $helperClass = static::getHelperClass($config);
  104.                         $exporter $helperClass::getExporterService($config);
  105.                         $relevantItemIds $exporter->getRelevantItemIds();
  106.                         if ($saveHookType == 'queue') {
  107.                             $id $element->getId();
  108.                             if (in_array($id$relevantItemIds)) {
  109.                                 $item = new Model\QueueItem();
  110.                                 $item->setConfigName($config->getName())->setItemId($id)->setItemType(Service::getType($element))->save();
  111.                             }
  112.                         }
  113.                         if ($saveHookType == 'directExport') {
  114.                             $index array_search($element->getId(), $relevantItemIds);
  115.                             if ($index !== false) {
  116.                                 unset($relevantItemIds[$index]);
  117.                             }
  118.                             $exporter->addIllegalItemId($element->getId());
  119.                             $exporter->execute([]);
  120.                         }
  121.                     }
  122.                 }
  123.             }
  124.         }
  125.     }
  126. }