src/EventSubscriber/VideoGallerySubscriber.php line 177

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\ElTask;
  4. use App\Entity\Upload;
  5. use App\Entity\Session;
  6. use App\Service\EsCache;
  7. use App\Entity\Container;
  8. use App\Service\EsUpload;
  9. use App\Entity\VideoGallery;
  10. use App\Entity\CommonCategory;
  11. use App\Entity\VideoGalleryDoc;
  12. use App\Entity\VideoGalleryTag;
  13. use App\Entity\VideoGalleryChapter;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use App\Event\VideoGalleryPreUpdateEvent;
  16. use App\Event\VideoGalleryPrePersistEvent;
  17. use App\Event\VideoGalleryPostPersistEvent;
  18. use Symfony\Component\Security\Core\Security;
  19. use App\Entity\VideoGalleryChapterTranslation;
  20. use App\Entity\Interfaces\VideoGalleryCreatorInterface;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. class VideoGallerySubscriber implements EventSubscriberInterface
  23. {
  24.     public function __construct(
  25.         private EntityManagerInterface $_em,
  26.         private Security $security,
  27.         private EsCache $esCache,
  28.         private EsUpload $esUpload
  29.     ) {}
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             VideoGalleryPrePersistEvent::NAME => 'onPreCreate',
  34.             VideoGalleryPostPersistEvent::NAME => 'onPostCreate',
  35.             VideoGalleryPreUpdateEvent::NAME => 'onPreUpdate'
  36.         ];
  37.     }
  38.     public function onPreCreate(VideoGalleryPrePersistEvent $event)
  39.     {
  40.         $object $event->getObject();
  41.         $creator null;
  42.         if ($object->getType() === VideoGallery::VIDEOGALLERYTYPE_SESSION) {
  43.             $creator $this->_em->getRepository(Session::class)->find($object->getTypeId());
  44.         } else if ($object->getType() === VideoGallery::VIDEOGALLERYTYPE_TASK) {
  45.             $creator $this->_em->getRepository(ElTask::class)->findOneBy(['id' => $object->getTypeId(), 'taskType' => ElTask::TASKTYPE_VIDEO]);
  46.         }
  47.         if ($creator instanceof VideoGalleryCreatorInterface) {
  48.             $object->setMetadata($creator->getVideoGalleryMetadata());
  49.             $object->setStreamType($creator->getVideoGalleryStreamType());
  50.             $object->setIsCommentEnable($creator->getVideoGalleryCommentEnable());
  51.             foreach ($creator->getVideoGalleryTranslations() as $translation) {
  52.                 $object->addTranslation($translation);
  53.             }
  54.             /*
  55.             $chapters = $creator->getVideoGalleryChapters();
  56.             if ($chapters) {
  57.                 foreach ($chapters as $chapter) {
  58.                     $vgc = new VideoGalleryChapter();
  59.                     foreach ($chapter->getTranslations() as $t) {
  60.                         $newT = new VideoGalleryChapterTranslation();
  61.                         $newT
  62.                             ->setName($t->getName())
  63.                             ->setLocale($t->getLocale())
  64.                         ;
  65.                         $vgc->addTranslation($newT);
  66.                     }
  67.                     $vgc
  68.                         ->setStartTime($chapter->getStartTime())
  69.                         ->setImageName($chapter->getImageName())
  70.                         ->setOrd($chapter->getOrd())
  71.                     ;
  72.                     if ($vgc->getImageName()) {
  73.                         $newFileName = $this->esUpload->copy(
  74.                             $vgc->getImageName(),
  75.                             $vgc->getImageName(),
  76.                             Upload::FILETYPE_VIDEOGALLERY_CHAPTER_POSTER
  77.                         );
  78.                         $vgc->setImageName($newFileName);
  79.                     }
  80.                     $object->addVideoGalleryChapter($vgc);
  81.                 }
  82.             }
  83.             $docs = $creator->getVideoGalleryDocs();
  84.             if ($docs) {
  85.                 foreach ($docs as $doc) {
  86.                     $vgd = new VideoGalleryDoc();
  87.                     $vgd
  88.                         ->setName($doc->getName())
  89.                         ->setFileName($doc->getFileName())
  90.                     ;
  91.                     if ($vgd->getFileName()) {
  92.                         $newFileName = $this->esUpload->copy(
  93.                             $vgd->getFileName(),
  94.                             $vgd->getFileName(),
  95.                             Upload::FILETYPE_VIDEOGALLERY_DOC
  96.                         );
  97.                         $vgd->setFileName($newFileName);
  98.                     }
  99.                     $object->addVideoGalleryDoc($vgd);
  100.                 }
  101.             }
  102.             */
  103.             $userGroups $creator->getVideoGalleryUserGroups();
  104.             if ($userGroups) {
  105.                 foreach ($userGroups as $userGroup) {
  106.                     $object->addUserGroup($userGroup);
  107.                 }
  108.             }
  109.             $speakers $creator->getVideoGallerySpeakers();
  110.             if ($speakers) {
  111.                 foreach ($speakers as $speaker) {
  112.                     $object->addSpeaker($speaker);
  113.                 }
  114.             }
  115.             $moderators $creator->getVideoGalleryModerators();
  116.             if ($moderators) {
  117.                 foreach ($moderators as $moderator) {
  118.                     $object->addModerator($moderator);
  119.                 }
  120.             }
  121.             $container $this->esCache->getContainer();
  122.             $newTags $this->_em->getRepository(VideoGalleryTag::class)->getTagsFromExternalTags($creator->getVideoGalleryTags(), $container);
  123.             if ($newTags) {
  124.                 foreach ($newTags as $tag) {
  125.                     $object->addVideoGalleryTag($tag);
  126.                 }
  127.             }
  128.         }
  129.     }
  130.     public function onPostCreate(VideoGalleryPostPersistEvent $event)
  131.     {
  132.         $object $event->getObject();
  133.         $client $this->esCache->getClient();
  134.         $container $this->esCache->getContainer();
  135.         $categories $this->_em->getRepository(CommonCategory::class)->findBy(['client' => $client'type' => CommonCategory::CATEGORYTYPE_VIDEOGALLERY], ['ord' => 'asc']);
  136.         $videoIds = [];
  137.         foreach ($categories as $category) {
  138.              $vid $this->_em->getRepository(VideoGallery::class)->getIdsByCategory($category->getId());
  139.              if ($vid) {
  140.                 $videoIds = [...$videoIds, ...$vid];
  141.              }
  142.         }
  143.         $creator null;
  144.         if ($object->getType() === VideoGallery::VIDEOGALLERYTYPE_SESSION) {
  145.             $creator $this->_em->getRepository(Session::class)->find($object->getTypeId());
  146.         } else if ($object->getType() === VideoGallery::VIDEOGALLERYTYPE_TASK) {
  147.             $creator $this->_em->getRepository(ElTask::class)->findOneBy(['id' => $object->getTypeId(), 'taskType' => ElTask::TASKTYPE_VIDEO]);
  148.         }
  149.         if ($creator) {
  150.             $creator->setVideoGallery($object);
  151.             $this->_em->persist($creator);
  152.         }
  153.         $c $this->_em->getRepository(Container::class)->find($container->getId());
  154.         $c->setVideoGalleryIds($videoIds);
  155.         $this->_em->persist($c);
  156.         $this->_em->flush();
  157.     }
  158.     public function onPreUpdate(VideoGalleryPreUpdateEvent $event)
  159.     {
  160.     }
  161. }