<?php
namespace App\EventSubscriber;
use App\Entity\ElTask;
use App\Entity\Upload;
use App\Entity\Session;
use App\Service\EsCache;
use App\Entity\Container;
use App\Service\EsUpload;
use App\Entity\VideoGallery;
use App\Entity\CommonCategory;
use App\Entity\VideoGalleryDoc;
use App\Entity\VideoGalleryTag;
use App\Entity\VideoGalleryChapter;
use Doctrine\ORM\EntityManagerInterface;
use App\Event\VideoGalleryPreUpdateEvent;
use App\Event\VideoGalleryPrePersistEvent;
use App\Event\VideoGalleryPostPersistEvent;
use Symfony\Component\Security\Core\Security;
use App\Entity\VideoGalleryChapterTranslation;
use App\Entity\Interfaces\VideoGalleryCreatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class VideoGallerySubscriber implements EventSubscriberInterface
{
public function __construct(
private EntityManagerInterface $_em,
private Security $security,
private EsCache $esCache,
private EsUpload $esUpload
) {}
public static function getSubscribedEvents(): array
{
return [
VideoGalleryPrePersistEvent::NAME => 'onPreCreate',
VideoGalleryPostPersistEvent::NAME => 'onPostCreate',
VideoGalleryPreUpdateEvent::NAME => 'onPreUpdate'
];
}
public function onPreCreate(VideoGalleryPrePersistEvent $event)
{
$object = $event->getObject();
$creator = null;
if ($object->getType() === VideoGallery::VIDEOGALLERYTYPE_SESSION) {
$creator = $this->_em->getRepository(Session::class)->find($object->getTypeId());
} else if ($object->getType() === VideoGallery::VIDEOGALLERYTYPE_TASK) {
$creator = $this->_em->getRepository(ElTask::class)->findOneBy(['id' => $object->getTypeId(), 'taskType' => ElTask::TASKTYPE_VIDEO]);
}
if ($creator instanceof VideoGalleryCreatorInterface) {
$object->setMetadata($creator->getVideoGalleryMetadata());
$object->setStreamType($creator->getVideoGalleryStreamType());
$object->setIsCommentEnable($creator->getVideoGalleryCommentEnable());
foreach ($creator->getVideoGalleryTranslations() as $translation) {
$object->addTranslation($translation);
}
/*
$chapters = $creator->getVideoGalleryChapters();
if ($chapters) {
foreach ($chapters as $chapter) {
$vgc = new VideoGalleryChapter();
foreach ($chapter->getTranslations() as $t) {
$newT = new VideoGalleryChapterTranslation();
$newT
->setName($t->getName())
->setLocale($t->getLocale())
;
$vgc->addTranslation($newT);
}
$vgc
->setStartTime($chapter->getStartTime())
->setImageName($chapter->getImageName())
->setOrd($chapter->getOrd())
;
if ($vgc->getImageName()) {
$newFileName = $this->esUpload->copy(
$vgc->getImageName(),
$vgc->getImageName(),
Upload::FILETYPE_VIDEOGALLERY_CHAPTER_POSTER
);
$vgc->setImageName($newFileName);
}
$object->addVideoGalleryChapter($vgc);
}
}
$docs = $creator->getVideoGalleryDocs();
if ($docs) {
foreach ($docs as $doc) {
$vgd = new VideoGalleryDoc();
$vgd
->setName($doc->getName())
->setFileName($doc->getFileName())
;
if ($vgd->getFileName()) {
$newFileName = $this->esUpload->copy(
$vgd->getFileName(),
$vgd->getFileName(),
Upload::FILETYPE_VIDEOGALLERY_DOC
);
$vgd->setFileName($newFileName);
}
$object->addVideoGalleryDoc($vgd);
}
}
*/
$userGroups = $creator->getVideoGalleryUserGroups();
if ($userGroups) {
foreach ($userGroups as $userGroup) {
$object->addUserGroup($userGroup);
}
}
$speakers = $creator->getVideoGallerySpeakers();
if ($speakers) {
foreach ($speakers as $speaker) {
$object->addSpeaker($speaker);
}
}
$moderators = $creator->getVideoGalleryModerators();
if ($moderators) {
foreach ($moderators as $moderator) {
$object->addModerator($moderator);
}
}
$container = $this->esCache->getContainer();
$newTags = $this->_em->getRepository(VideoGalleryTag::class)->getTagsFromExternalTags($creator->getVideoGalleryTags(), $container);
if ($newTags) {
foreach ($newTags as $tag) {
$object->addVideoGalleryTag($tag);
}
}
}
}
public function onPostCreate(VideoGalleryPostPersistEvent $event)
{
$object = $event->getObject();
$client = $this->esCache->getClient();
$container = $this->esCache->getContainer();
$categories = $this->_em->getRepository(CommonCategory::class)->findBy(['client' => $client, 'type' => CommonCategory::CATEGORYTYPE_VIDEOGALLERY], ['ord' => 'asc']);
$videoIds = [];
foreach ($categories as $category) {
$vid = $this->_em->getRepository(VideoGallery::class)->getIdsByCategory($category->getId());
if ($vid) {
$videoIds = [...$videoIds, ...$vid];
}
}
$creator = null;
if ($object->getType() === VideoGallery::VIDEOGALLERYTYPE_SESSION) {
$creator = $this->_em->getRepository(Session::class)->find($object->getTypeId());
} else if ($object->getType() === VideoGallery::VIDEOGALLERYTYPE_TASK) {
$creator = $this->_em->getRepository(ElTask::class)->findOneBy(['id' => $object->getTypeId(), 'taskType' => ElTask::TASKTYPE_VIDEO]);
}
if ($creator) {
$creator->setVideoGallery($object);
$this->_em->persist($creator);
}
$c = $this->_em->getRepository(Container::class)->find($container->getId());
$c->setVideoGalleryIds($videoIds);
$this->_em->persist($c);
$this->_em->flush();
}
public function onPreUpdate(VideoGalleryPreUpdateEvent $event)
{
}
}