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

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