<?php
namespace Blackbit\DataDirectorBundle\EventListener;
use Blackbit\DataDirectorBundle\lib\Pim\Helper;
use Blackbit\DataDirectorBundle\lib\Pim\AssetNormalizer\NormalizeAction;
use Blackbit\DataDirectorBundle\lib\Pim\AssetNormalizer\NormalizeActionHistogram;
use Blackbit\DataDirectorBundle\lib\Pim\AssetNormalizer\NormalizeActionSquaredSize;
use Blackbit\DataDirectorBundle\lib\Pim\AssetNormalizer\Normalizer;
use Blackbit\DataDirectorBundle\lib\Pim\Item\Importer;
use Blackbit\DataDirectorBundle\model\Dataport;
use Blackbit\DataDirectorBundle\model\Fieldmapping;
use Blackbit\DataDirectorBundle\model\PimcoreDbRepository;
use Blackbit\DataDirectorBundle\Tools\Installer;
use InvalidArgumentException;
use Pimcore\Cache;
use Pimcore\Event\Model\AssetEvent;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\Asset\Folder;
use Pimcore\Model\Asset\Image;
use Pimcore\Model\Property;
class AssetHashListener
{
public function updateHash(AssetEvent $e)
{
if (\method_exists($e, 'getArgument')) {
try {
$saveVersionOnly = $e->getArgument('saveVersionOnly');
if ($saveVersionOnly) {
return;
}
} catch (\InvalidArgumentException $exception) {
}
}
$asset = $e->getAsset();
if($asset instanceof Folder) {
return;
}
$parentPath = $asset->getParent()->getFullPath(); // when asset gets moved realpath is not correct yet but only parent gets set
$fieldMappings = new Fieldmapping();
$calculateHash = false;
$dataports = new Dataport();
foreach($fieldMappings->find(['format LIKE ?' => '%"preventDuplicates";b:1%']) as $fieldMapping) {
$dataport = $dataports->get($fieldMapping['dataportId']);
$targetConfig = $dataport['targetconfig'];
if(strpos($asset->getRealPath(), $targetConfig['assetFolder']) === 0 || strpos($parentPath, $targetConfig['assetFolder']) === 0) {
$calculateHash = true;
break;
}
}
if(!$calculateHash) {
return;
}
try {
$asset->setProperty(Normalizer::HASH_PROPERTY, 'text', Normalizer::getHash($asset));
} catch (\Exception $e) {
return;
}
}
}