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.                 }
  55. //                p_r($asset->getCustomSetting('plugin_assetmetdata_collections'));
  56.                 $collection Collections::getByAssetId($asset->getId());
  57.                 $dirty false;
  58.                 if (!$collection instanceof Collections) {
  59.                     $collection = new Collections();
  60.                     $collection->setAssetId($asset->getId());
  61.                     $dirty true;
  62.                 }
  63.                 $collections $collection->getCollections();
  64.                 if (!in_array("General"$collections)) {
  65.                     $collections[] = "General";
  66.                     $dirty true;
  67.                 }
  68.                 if ($asset instanceof Asset\Image && !in_array('ImageSpecific'$collections)) {
  69.                     $collections[] = "ImageSpecific";
  70.                     $dirty true;
  71.                 }
  72.                 if ($dirty) {
  73. //                    $collection->setCollections($collections);
  74. //                    $collection->applyToAsset();
  75.                     // can't use the normal functions since applyToAsset gets asset by id and would not add the data to the correct asset object
  76.                     $asset->setCustomSetting('plugin_assetmetdata_collections'$collections);
  77.                 }
  78.             }
  79.         }
  80.     }
  81. }