<?php
/**
* Copyright Blackbit digital Commerce GmbH <info@blackbit.de>
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
namespace Blackbit\DataDirectorBundle\EventListener;
use Blackbit\DataDirectorBundle\Controller\ImportController;
use Blackbit\DataDirectorBundle\lib\Pim\Helper;
use Blackbit\DataDirectorBundle\lib\Pim\Import\CallbackFunction;
use Blackbit\DataDirectorBundle\lib\Pim\Item\ImporterInterface;
use Blackbit\DataDirectorBundle\lib\Pim\Item\ItemMoldBuilder;
use Blackbit\DataDirectorBundle\lib\Pim\RawData\Importer;
use Blackbit\DataDirectorBundle\lib\Pim\Serializer;
use Blackbit\DataDirectorBundle\model\Dataport;
use Blackbit\DataDirectorBundle\model\DataportResource;
use Blackbit\DataDirectorBundle\model\Fieldmapping;
use Blackbit\DataDirectorBundle\model\ImportStatus;
use Blackbit\DataDirectorBundle\model\PimcoreDbRepository;
use Blackbit\DataDirectorBundle\model\Queue;
use Blackbit\DataDirectorBundle\model\RawItem;
use Blackbit\DataDirectorBundle\model\RawItemData;
use Blackbit\DataDirectorBundle\Tools\Installer;
use InvalidArgumentException;
use Pimcore\Db;
use Pimcore\Event\Model\AssetEvent;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Logger;
use Pimcore\Model\AbstractModel;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\Element\DirtyIndicatorInterface;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\Element\Service;
use Pimcore\Model\User;
use Pimcore\Model\Version;
use Pimcore\Tool;
use Blackbit\DataDirectorBundle\lib\Pim\Cli;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
class AutomaticImportListener implements LoggerAwareInterface
{
use LoggerAwareTrait;
private static $processCommandRegistered = [];
private static $dataports = null;
private static $hasAutomaticDataports = null;
/** @var ItemMoldBuilder */
private $itemMoldBuilder;
public function __construct(ItemMoldBuilder $itemMoldBuilder)
{
$this->itemMoldBuilder = $itemMoldBuilder;
}
private static function getDataports() {
if(self::$dataports === null) {
self::$dataports = Dataport::getInstance()->find([], 'name');
}
return self::$dataports;
}
private function hasAutomaticDataports() {
if(self::$hasAutomaticDataports === null) {
self::$hasAutomaticDataports = false;
foreach (self::getDataports() as $dataport) {
$sourceConfig = $dataport['sourceconfig'];
if (!empty($sourceConfig['autoImport'])) {
self::$hasAutomaticDataports = true;
break;
}
}
}
return self::$hasAutomaticDataports;
}
public function startImports(ElementEventInterface $e) {
try {
if ($e->getArgument('saveVersionOnly') && $e->getArgument('isAutoSave')) {
return;
}
} catch (\InvalidArgumentException $exception) {
}
if(!$this->hasAutomaticDataports()) {
return;
}
$object = $e->getElement();
$user = Helper::getUser();
$queue = Queue::getInstance();
$savedDraftVersion = false;
try {
if ($e->getArgument('saveVersionOnly')) {
$savedDraftVersion = true;
}
} catch (\InvalidArgumentException $exception) {
}
$queue->create([
'command' => 'data-director:start-automatic-dataports '.$object->getId().' '.Service::getElementType($object).' --user='.$user->getId().($savedDraftVersion ? ' --draft-version':''),
'triggered_by' => $object->getRealFullPath().' got saved'.(($user instanceof User) ? ' by '.$user->getUsername() : ''),
'worker_id' => 'check_automatic_run'
]);
}
public function deleteRawdata(ElementEventInterface $e)
{
if (\method_exists($e, 'getArgument')) {
try {
$saveVersionOnly = $e->getArgument('saveVersionOnly');
if ($saveVersionOnly) {
return;
}
} catch (\InvalidArgumentException $exception) {
}
}
$object = $e->getElement();
$objectType = Service::getElementType($object);
$commandPrefix = '"'.Cli::getPhpCli().'" '.realpath(PIMCORE_PROJECT_ROOT.DIRECTORY_SEPARATOR.'bin'.DIRECTORY_SEPARATOR.'console');
foreach (self::getDataports() as $dataport) {
$sourceConfig = $dataport['sourceconfig'];
$targetConfig = $dataport['targetconfig'];
if (!empty($sourceConfig['autoImport']) && empty($targetConfig['itemClass'])) {
$itemMold = $this->itemMoldBuilder->getItemMoldByClassId($sourceConfig['sourceClass'] ?? null);
if ($object instanceof $itemMold) {
Cli::exec($commandPrefix.' data-director:delete-rawdata --dataport='.$dataport['id'].' --object-id='.$object->getId().' --object-type='.$objectType);
}
}
}
}
}