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

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.         //do not update export when auto save or only saving version
  36.         if (
  37.             ($e->hasArgument('isAutoSave') && $e->getArgument('isAutoSave')) ||
  38.             ($e->hasArgument('saveVersionOnly') && $e->getArgument('saveVersionOnly'))
  39.         ) {
  40.             return;
  41.         }
  42.         $element $e->getElement();
  43.         if ($element instanceof \Pimcore\Model\DataObject\Concrete) {
  44.             $configList Configuration::getList();
  45.             foreach ($configList as $config) {
  46.                 if ($config->isActive() && $config->getType() == static::ADAPTER_TYPE) {
  47.                     $configuration $config->getConfiguration();
  48.                     if (!$configuration['schema']['classId']) {
  49.                         continue;
  50.                     }
  51.                     $classDef null;
  52.                     try {
  53.                         $classDef \Pimcore\Model\DataObject\ClassDefinition::getById($configuration['schema']['classId']);
  54.                     } catch (\Exception $e) {
  55.                     }
  56.                     if ($classDef) {
  57.                         $inheritanceEnabled $classDef->getAllowInherit();
  58.                         $saveHookType $configuration['triggerForDelivery']['saveHookType'];
  59.                         $type $configuration['general']['type'];
  60.                         /**
  61.                          * @var \Pimcore\Bundle\DataHubFileExportBundle\Helper $helperClass
  62.                          */
  63.                         $helperClass = static::getHelperClass($config);
  64.                         $exporter $helperClass::getExporterService($config);
  65.                         $relevantItemIds $exporter->getRelevantItemIds();
  66.                         if ($inheritanceEnabled) {
  67.                             $db \Pimcore\Db::get();
  68.                             $query 'SELECT o_id FROM objects WHERE  (o_path LIKE ' $db->quote($element->getFullPath() . '/%') . ' AND o_classId="' $classDef->getId() . '" ) OR o_id=' $element->getId();
  69.                             $itemIdsToUpdate $db->fetchCol($query);
  70.                         } else {
  71.                             $itemIdsToUpdate = [$element->getId()];
  72.                         }
  73.                         if ($saveHookType == 'queue') {
  74.                             foreach ($itemIdsToUpdate as $id) {
  75.                                 if (in_array($id$relevantItemIds)) {
  76.                                     $item = new Model\QueueItem();
  77.                                     $item->setConfigName($config->getName())->setItemId($id)->setItemType(Service::getType($element))->save();
  78.                                 }
  79.                             }
  80.                         } else {
  81.                             Model\QueueItem\Dao::deleteByNameAndItemId($config->getName(), $relevantItemIds);
  82.                         }
  83.                         if ($saveHookType == 'directExport') {
  84.                             $data $exporter->getExportData($itemIdsToUpdate);
  85.                             if (!empty($data)) {
  86.                                 $exporter->execute($data);
  87.                             }
  88.                         }
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.     }
  94.     public static function preDelete(ElementEventInterface $e)
  95.     {
  96.         $element $e->getElement();
  97.         if ($element instanceof \Pimcore\Model\DataObject\Concrete) {
  98.             $configList Configuration::getList();
  99.             foreach ($configList as $config) {
  100.                 if ($config->isActive() && $config->getType() == static::ADAPTER_TYPE) {
  101.                     $configuration $config->getConfiguration();
  102.                     $classDef null;
  103.                     if ($configuration['schema']['classId']) {
  104.                         $classDef \Pimcore\Model\DataObject\ClassDefinition::getById($configuration['schema']['classId']);
  105.                     }
  106.                     if ($classDef) {
  107.                         $inheritanceEnabled $classDef->getAllowInherit();
  108.                         $saveHookType $configuration['triggerForDelivery']['saveHookType'];
  109.                         $type $configuration['general']['type'];
  110.                         /**
  111.                          * @var \Pimcore\Bundle\DataHubFileExportBundle\Helper $helperClass
  112.                          */
  113.                         $helperClass = static::getHelperClass($config);
  114.                         $exporter $helperClass::getExporterService($config);
  115.                         $relevantItemIds $exporter->getRelevantItemIds();
  116.                         if ($saveHookType == 'queue') {
  117.                             $id $element->getId();
  118.                             if (in_array($id$relevantItemIds)) {
  119.                                 $item = new Model\QueueItem();
  120.                                 $item->setConfigName($config->getName())->setItemId($id)->setItemType(Service::getType($element))->save();
  121.                             }
  122.                         }
  123.                         if ($saveHookType == 'directExport') {
  124.                             $index array_search($element->getId(), $relevantItemIds);
  125.                             if ($index !== false) {
  126.                                 unset($relevantItemIds[$index]);
  127.                             }
  128.                             $exporter->addIllegalItemId($element->getId());
  129.                             $exporter->execute([]);
  130.                         }
  131.                     }
  132.                 }
  133.             }
  134.         }
  135.     }
  136. }