<?php
namespace App\EventListener;
use Pimcore\AssetMetadataClassDefinitionsBundle\Model\Collections;
use Pimcore\Event\Model\AssetEvent;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject\Contact;
use Pimcore\Model\DataObject\Data\Hotspotimage;
use Pimcore\Model\DataObject\MediaCopyright;
use Pimcore\Model\DataObject\Service;
class AssetListener
{
public function preUpdateAction(ElementEventInterface $e) {
if ($e instanceof AssetEvent) {
$asset = $e->getAsset();
if ($asset instanceof Asset) {
if ($asset->getMetadata('General.author') == '' && $asset->getMetadata('General.authorAutocreate') != '') {
$name = $asset->getMetadata('General.authorAutocreate');
$author = Contact::getByName($name, 1);
if (!$author instanceof Contact) {
$author = new Contact();
$author->setPublished(true);
$author->setParent(Service::createFolderByPath('Press/MediaAuthor-autocreated'));
$author->setKey(Service::getValidKey($name . '-autocreated-' . time(), 'object'));
$author->setName($name);
$author->save();
}
$asset->addMetadata('General.author', 'object', $author);
$asset->addMetadata('General.authorAutocreate', 'input', null);
}
if ($asset->getMetadata('General.copyright') == '' && $asset->getMetadata('General.copyrightAutocreate') != '') {
$copyright = $asset->getMetadata('General.copyrightAutocreate');
$copyrightObject = MediaCopyright::getByText($copyright, 'de', 1);
if (!$copyrightObject instanceof MediaCopyright) {
$copyrightObject = new MediaCopyright();
$copyrightObject->setPublished(true);
$copyrightObject->setParent(Service::createFolderByPath('Press/MediaCopyright-autocreated'));
$copyrightObject->setKey(Service::getValidKey($copyright . '-autocreated-' . time() . '-' . $asset->getId(), 'object'));
$copyrightObject->setText($copyright, 'de');
$copyrightObject->setText($copyright, 'en');
$copyrightObject->save();
}
$asset->addMetadata('General.copyright', 'object', $copyrightObject);
$asset->addMetadata('General.copyrightAutocreate', 'input', null);
}
if($asset instanceof Asset\Image) {
if($asset->getHeight() > $asset->getWidth()) {
$asset->addMetadata('General.isPortrait', 'checkbox', true);
$asset->addMetadata('General.isLandscape', 'checkbox', false);
} else {
$asset->addMetadata('General.isLandscape', 'checkbox', true ); //==landscape
$asset->addMetadata('General.isPortrait', 'checkbox', false ); //==landscape
}
}
// p_r($asset->getCustomSetting('plugin_assetmetdata_collections'));
$collection = Collections::getByAssetId($asset->getId());
$dirty = false;
if (!$collection instanceof Collections) {
$collection = new Collections();
$collection->setAssetId($asset->getId());
$dirty = true;
}
$collections = $collection->getCollections();
if (!in_array("General", $collections)) {
$collections[] = "General";
$dirty = true;
}
if ($asset instanceof Asset\Image && !in_array('ImageSpecific', $collections)) {
$collections[] = "ImageSpecific";
$dirty = true;
}
if ($dirty) {
// $collection->setCollections($collections);
// $collection->applyToAsset();
// can't use the normal functions since applyToAsset gets asset by id and would not add the data to the correct asset object
$asset->setCustomSetting('plugin_assetmetdata_collections', $collections);
}
}
}
}
}