<?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\Helper;
use Pimcore\Bundle\DataHubFileExportBundle\Model;
use Pimcore\Event\DataObjectEvents;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\DataObject;
use Pimcore\Model\Element\Service;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DataObjectSubscriber implements EventSubscriberInterface
{
const ADAPTER_TYPE = 'fileExport';
/**
* @return string[]
*/
public static function getSubscribedEvents()
{
return [
DataObjectEvents::POST_UPDATE => 'doExport',
DataObjectEvents::POST_ADD => 'doExport',
DataObjectEvents::PRE_DELETE => 'preDelete',
];
}
/**
* @param Configuration $config
*
* @return Helper
*/
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();
$saveHookType = $configuration['triggerForDelivery']['saveHookType'];
if (!$configuration['schema']['classId'] || $saveHookType === '') {
continue;
}
$classDef = null;
try {
$classDef = \Pimcore\Model\DataObject\ClassDefinition::getById($configuration['schema']['classId']);
} catch (\Exception $e) {
}
if ($classDef) {
$inheritanceEnabled = $classDef->getAllowInherit();
$type = $configuration['general']['type'];
$helperClass = static::getHelperClass($config);
$exporter = $helperClass::getExporterService($config);
$relevantItemIds = $exporter->getRelevantItemIds();
if ($inheritanceEnabled) {
$db = \Pimcore\Db::get();
$query = sprintf('SELECT %s FROM objects WHERE (%s LIKE ' . $db->quote($element->getFullPath() . '/%%') . ' AND %s="' . $classDef->getId() . '" ) OR %s=' . $element->getId(),
$db->quoteIdentifier(DataObject\Service::getVersionDependentDatabaseColumnName('o_id')),
$db->quoteIdentifier(DataObject\Service::getVersionDependentDatabaseColumnName('o_path')),
$db->quoteIdentifier(DataObject\Service::getVersionDependentDatabaseColumnName('o_classId')),
$db->quoteIdentifier(DataObject\Service::getVersionDependentDatabaseColumnName('o_id')));
$itemIdsToUpdate = $db->fetchFirstColumn($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::getElementType($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'];
$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::getElementType($element))->save();
}
}
if ($saveHookType == 'directExport') {
$index = array_search($element->getId(), $relevantItemIds);
if ($index !== false) {
unset($relevantItemIds[$index]);
}
$exporter->addIllegalItemId($element->getId());
$exporter->execute([]);
}
}
}
}
}
}
}