src/EventListener/AssetListener.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Pimcore\AssetMetadataClassDefinitionsBundle\Model\Collections;
  4. use Pimcore\Event\Model\AssetEvent;
  5. use Pimcore\Event\Model\ElementEventInterface;
  6. use Pimcore\Model\Asset;
  7. use Pimcore\Model\DataObject\Contact;
  8. use Pimcore\Model\DataObject\Data\Hotspotimage;
  9. use Pimcore\Model\DataObject\MediaCopyright;
  10. use Pimcore\Model\DataObject\Service;
  11. class AssetListener
  12. {
  13.     public function preUpdateAction(ElementEventInterface $e) {
  14.         if ($e instanceof AssetEvent) {
  15.             $asset $e->getAsset();
  16.             if ($asset instanceof Asset) {
  17.                 if ($asset->getMetadata('General.author') == '' && $asset->getMetadata('General.authorAutocreate') != '') {
  18.                     $name $asset->getMetadata('General.authorAutocreate');
  19.                     $author Contact::getByName($name1);
  20.                     if (!$author instanceof Contact) {
  21.                         $author = new Contact();
  22.                         $author->setPublished(true);
  23.                         $author->setParent(Service::createFolderByPath('Press/MediaAuthor-autocreated'));
  24.                         $author->setKey(Service::getValidKey($name '-autocreated-' time(), 'object'));
  25.                         $author->setName($name);
  26.                         $author->save();
  27.                     }
  28.                     $asset->addMetadata('General.author''object'$author);
  29.                     $asset->addMetadata('General.authorAutocreate''input'null);
  30.                 }
  31.                 if ($asset->getMetadata('General.copyright') == '' && $asset->getMetadata('General.copyrightAutocreate') != '') {
  32.                     $copyright $asset->getMetadata('General.copyrightAutocreate');
  33.                     $copyrightObject MediaCopyright::getByText($copyright'de'1);
  34.                     if (!$copyrightObject instanceof MediaCopyright) {
  35.                         $copyrightObject = new MediaCopyright();
  36.                         $copyrightObject->setPublished(true);
  37.                         $copyrightObject->setParent(Service::createFolderByPath('Press/MediaCopyright-autocreated'));
  38.                         $copyrightObject->setKey(Service::getValidKey($copyright '-autocreated-' time() . '-' $asset->getId(), 'object'));
  39.                         $copyrightObject->setText($copyright'de');
  40.                         $copyrightObject->setText($copyright'en');
  41.                         $copyrightObject->save();
  42.                     }
  43.                     $asset->addMetadata('General.copyright''object'$copyrightObject);
  44.                     $asset->addMetadata('General.copyrightAutocreate''input'null);
  45.                 }
  46.                 if($asset instanceof Asset\Image) {
  47.                     if($asset->getHeight() > $asset->getWidth()) {
  48.                         $asset->addMetadata('General.isPortrait''checkbox'true);
  49.                         $asset->addMetadata('General.isLandscape''checkbox'false);
  50.                     } else {
  51.                         $asset->addMetadata('General.isLandscape''checkbox'true ); //==landscape
  52.                         $asset->addMetadata('General.isPortrait''checkbox'false ); //==landscape
  53.                     }
  54.                     $oldAsset Asset\Image::getById($asset->getId(), true);
  55.                     if($oldAsset instanceof Asset\Image) {
  56.                         if($oldAsset->getProperty('excludeFromMetadataSynchronisation') !== true && $asset->getProperty('excludeFromMetadataSynchronisation') !== true) {
  57.                             foreach (\Pimcore\Tool::getValidLanguages() as $language) {
  58.                                 // check if ai-status-title was review and if it is approved in the new version
  59.                                 if($oldAsset->getMetadata('ai-status-title'$languagetrue) === 'review' && $asset->getMetadata('ai-status-title'$languagetrue) === 'approved') {
  60.                                     $asset->addMetadata('General.title''input'$asset->getMetadata('title'$languagetrue), $language);
  61.                                 } elseif(
  62.                                     $asset->getMetadata('General.title'$languagetrue)
  63.                                     || $oldAsset->getMetadata('General.title'$languagetrue) !== $asset->getMetadata('General.title'$languagetrue)) { // check if the metadata is set in the new version or if it is different from the old version
  64.                                     $asset->addMetadata('title''input'$asset->getMetadata('General.title'$languagetrue), $language);
  65.                                 }
  66.                                 // check if ai-status-alt was review and if it is approved in the new version
  67.                                 if($oldAsset->getMetadata('ai-status-alt'$languagetrue) === 'review' && $asset->getMetadata('ai-status-alt'$languagetrue) === 'approved') {
  68.                                     $asset->addMetadata('General.alt''input'$asset->getMetadata('alt'$languagetrue), $language);
  69.                                 } elseif(
  70.                                     $asset->getMetadata('General.alt'$languagetrue)
  71.                                     || $oldAsset->getMetadata('General.alt'$languagetrue) !== $asset->getMetadata('General.alt'$languagetrue)) { // check if the metadata is set in the new version or if it is different from the old version
  72.                                     $asset->addMetadata('alt''input'$asset->getMetadata('General.alt'$languagetrue), $language);
  73.                                 }
  74.                             }
  75.                         }
  76.                     }
  77.                 }
  78.                 $collection Collections::getByAssetId($asset->getId());
  79.                 $dirty false;
  80.                 if (!$collection instanceof Collections) {
  81.                     $collection = new Collections();
  82.                     $collection->setAssetId($asset->getId());
  83.                     $dirty true;
  84.                 }
  85.                 $collections $collection->getCollections();
  86.                 if (!in_array("General"$collections)) {
  87.                     $collections[] = "General";
  88.                     $dirty true;
  89.                 }
  90.                 if ($asset instanceof Asset\Image && !in_array('ImageSpecific'$collections)) {
  91.                     $collections[] = "ImageSpecific";
  92.                     $dirty true;
  93.                 }
  94.                 if ($dirty) {
  95. //                    $collection->setCollections($collections);
  96. //                    $collection->applyToAsset();
  97.                     // can't use the normal functions since applyToAsset gets asset by id and would not add the data to the correct asset object
  98.                     $asset->setCustomSetting('plugin_assetmetdata_collections'$collections);
  99.                 }
  100.             }
  101.         }
  102.     }
  103. }