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

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