<?php
/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/
namespace Pimcore\Bundle\DataHubFileExportBundle\EventSubscriber;
use Pimcore\Bundle\DataHubBundle\Configuration;
use Pimcore\Bundle\DataHubFileExportBundle\Model;
use Pimcore\Event\DataObjectEvents;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\Element\Service;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DataObjectSubscriber implements EventSubscriberInterface
{
const ADAPTER_TYPE = 'fileExport';
public static function getSubscribedEvents()
{
return [
DataObjectEvents::POST_UPDATE => 'doExport',
DataObjectEvents::POST_ADD => 'doExport',
DataObjectEvents::PRE_DELETE => 'preDelete',
];
}
protected static function getHelperClass(\Pimcore\Bundle\DataHubBundle\Configuration $config)
{
return new \Pimcore\Bundle\DataHubFileExportBundle\Helper();
}
public static function doExport(ElementEventInterface $e)
{
//do not update export when auto save or only saving version
if (
($e->hasArgument('isAutoSave') && $e->getArgument('isAutoSave')) ||
($e->hasArgument('saveVersionOnly') && $e->getArgument('saveVersionOnly'))
) {
return;
}
$element = $e->getElement();
if ($element instanceof \Pimcore\Model\DataObject\Concrete) {
$configList = Configuration::getList();
foreach ($configList as $config) {
if ($config->isActive() && $config->getType() == static::ADAPTER_TYPE) {
$configuration = $config->getConfiguration();
if (!$configuration['schema']['classId']) {
continue;
}
$classDef = null;
try {
$classDef = \Pimcore\Model\DataObject\ClassDefinition::getById($configuration['schema']['classId']);
} catch (\Exception $e) {
}
if ($classDef) {
$inheritanceEnabled = $classDef->getAllowInherit();
$saveHookType = $configuration['triggerForDelivery']['saveHookType'];
$type = $configuration['general']['type'];
/**
* @var \Pimcore\Bundle\DataHubFileExportBundle\Helper $helperClass
*/
$helperClass = static::getHelperClass($config);
$exporter = $helperClass::getExporterService($config);
$relevantItemIds = $exporter->getRelevantItemIds();
if ($inheritanceEnabled) {
$db = \Pimcore\Db::get();
$query = 'SELECT o_id FROM objects WHERE (o_path LIKE ' . $db->quote($element->getFullPath() . '/%') . ' AND o_classId="' . $classDef->getId() . '" ) OR o_id=' . $element->getId();
$itemIdsToUpdate = $db->fetchCol($query);
} else {
$itemIdsToUpdate = [$element->getId()];
}
if ($saveHookType == 'queue') {
foreach ($itemIdsToUpdate as $id) {
if (in_array($id, $relevantItemIds)) {
$item = new Model\QueueItem();
$item->setConfigName($config->getName())->setItemId($id)->setItemType(Service::getType($element))->save();
}
}
} else {
Model\QueueItem\Dao::deleteByNameAndItemId($config->getName(), $relevantItemIds);
}
if ($saveHookType == 'directExport') {
$data = $exporter->getExportData($itemIdsToUpdate);
if (!empty($data)) {
$exporter->execute($data);
}
}
}
}
}
}
}
public static function preDelete(ElementEventInterface $e)
{
$element = $e->getElement();
if ($element instanceof \Pimcore\Model\DataObject\Concrete) {
$configList = Configuration::getList();
foreach ($configList as $config) {
if ($config->isActive() && $config->getType() == static::ADAPTER_TYPE) {
$configuration = $config->getConfiguration();
$classDef = null;
if ($configuration['schema']['classId']) {
$classDef = \Pimcore\Model\DataObject\ClassDefinition::getById($configuration['schema']['classId']);
}
if ($classDef) {
$inheritanceEnabled = $classDef->getAllowInherit();
$saveHookType = $configuration['triggerForDelivery']['saveHookType'];
$type = $configuration['general']['type'];
/**
* @var \Pimcore\Bundle\DataHubFileExportBundle\Helper $helperClass
*/
$helperClass = static::getHelperClass($config);
$exporter = $helperClass::getExporterService($config);
$relevantItemIds = $exporter->getRelevantItemIds();
if ($saveHookType == 'queue') {
$id = $element->getId();
if (in_array($id, $relevantItemIds)) {
$item = new Model\QueueItem();
$item->setConfigName($config->getName())->setItemId($id)->setItemType(Service::getType($element))->save();
}
}
if ($saveHookType == 'directExport') {
$index = array_search($element->getId(), $relevantItemIds);
if ($index !== false) {
unset($relevantItemIds[$index]);
}
$exporter->addIllegalItemId($element->getId());
$exporter->execute([]);
}
}
}
}
}
}
}