<?php
namespace App\BackendBundle\Helper;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use App\BackendBundle\Helper\AddressHelper;
use App\BackendBundle\Helper\MediaHelper;
use App\BackendBundle\Helper\VideoHelper;
use App\BackendBundle\Model\GraphQL\Address as AddressGQL;
use App\BackendBundle\Model\GraphQL\AddressCity as AddressCityGQL;
use App\BackendBundle\Model\GraphQL\AddressDistrict as AddressDistrictGQL;
use App\BackendBundle\Model\GraphQL\AddressLatLon as AddressLatLonGQL;
use App\BackendBundle\Model\GraphQL\Contact as ContactGQL;
use App\BackendBundle\Model\GraphQL\ContactGallery as ContactGalleryGQL;
use App\BackendBundle\Model\GraphQL\ContactGalleryEntry as ContactGalleryEntryGQL;
use App\BackendBundle\Model\GraphQL\Communication as CommunicationGQL;
use App\BackendBundle\Model\GraphQL\CommunicationEntry as CommunicationEntryGQL;
use App\BackendBundle\Model\GraphQL\CommunicationType as CommunicationTypeGQL;
use App\BackendBundle\Model\GraphQL\Document as DocumentGQL;
use App\BackendBundle\Model\GraphQL\EventTag as EventTagGQL;
use App\BackendBundle\Model\GraphQL\Gallery as GalleryGQL;
use App\BackendBundle\Model\GraphQL\Image;
use App\BackendBundle\Model\GraphQL\ImageData;
use App\BackendBundle\Model\GraphQL\ImageInfo;
use App\BackendBundle\Model\GraphQL\Media as MediaGQL;
use App\BackendBundle\Model\GraphQL\Person as PersonGQL;
use App\BackendBundle\Model\GraphQL\ProfileLink as ProfileLinkGQL;
use App\BackendBundle\Model\GraphQL\ProfileLinkEntry as ProfileLinkEntryGQL;
use App\BackendBundle\Model\GraphQL\ProfileType as ProfileTypeGQL;
use App\BackendBundle\Model\GraphQL\Title as TitleGQL;
use App\BackendBundle\Interfaces\ProfileInterface;
use App\Entity\Address;
use App\Entity\AddressCity;
use App\Entity\AddressDistrict;
use App\Entity\AddressLatLon;
use App\Entity\Communication;
use App\Entity\CommunicationEntry;
use App\Entity\CommunicationType;
use App\Entity\Contact;
use App\Entity\ContactGallery;
use App\Entity\ContactGalleryEntry;
use App\Entity\Event;
use App\Entity\EventState;
use App\Entity\EventTagMapping;
use App\Entity\EventTag;
use App\Entity\Gallery;
use App\Entity\GalleryHasMedia;
use App\Entity\Job;
use App\Entity\JobDailyRoutine;
use App\Entity\Media;
use App\Entity\Person;
use App\Entity\ProfileLink;
use App\Entity\ProfileLinkEntry;
use App\Entity\ProfileLinkType;
use App\Entity\ProfileType;
use App\Entity\Title;
class GraphQlCommonHelper {
private EntityManagerInterface $em;
private AddressHelper $addressHelper;
private CacheManager $imagineCacheManager;
private MediaHelper $mediaHelper;
private VideoHelper $videoHelper;
private $imageDomain;
public function __construct(EntityManagerInterface $em, $imageDomain, AddressHelper $addressHelper, CacheManager $imagineCacheManager,
MediaHelper $mediaHelper, VideoHelper $videoHelper) {
$this->em = $em;
$this->addressHelper = $addressHelper;
$this->imageDomain = $imageDomain;
$this->imagineCacheManager = $imagineCacheManager;
$this->mediaHelper = $mediaHelper;
$this->videoHelper = $videoHelper;
}
public function getAddress(Address $address = null) {
if (empty($address)) {
return null;
}
$addressGQL = new AddressGQL();
$addressGQL->setId($address->getId());
$addressGQL->setStreet($address->getStreet());
$addressGQL->setNumber($address->getNumber());
$addressGQL->setFloor($address->getFloor());
$addressGQL->setStair($address->getStair());
/* @var $addressCity AddressCity */
$addressCity = $address->getAddressCity();
if (!empty($addressCity)) {
$addressCityGQL = $this->getAddressCity($addressCity);
$addressGQL->setAddressCity($addressCityGQL);
}
$this->setAddressLatLon($addressGQL, $address);
return $addressGQL;
}
public function getAddressCity(AddressCity $addressCity) {
$addressCityGQL = new AddressCityGQL();
$addressCityGQL->setId($addressCity->getId());
$addressCityGQL->setName($addressCity->getName());
$addressCityGQL->setZip($addressCity->getZip());
/* @var $addressDistrict AddressDistrict */
$addressDistrict = $addressCity->getAddressDistrict();
if (!empty($addressDistrict)) {
$addressDistrictGQL = $this->getAddressDistrict($addressDistrict);
$addressCityGQL->setAddressDistrict($addressDistrictGQL);
}
return $addressCityGQL;
}
public function getAddressDistrict(AddressDistrict $addressDistrict) {
$addressDistrictGQL = new AddressDistrictGQL();
$addressDistrictGQL->setId($addressDistrict->getId());
$addressDistrictGQL->setBkz($addressDistrict->getBkz());
$addressDistrictGQL->setName($addressDistrict->getName());
return $addressDistrictGQL;
}
private function setAddressLatLon(AddressGQL $addressGQL, Address $address) {
/* @var $addressLatLon AddressLatLon */
$addressLatLon = $address->getAddressLatLon();
if (empty($addressLatLon)) {
return;
}
$addressLatLonGQL = new AddressLatLonGQL();
$addressLatLonGQL->setId($addressLatLon->getId());
$addressLatLonGQL->setLat($addressLatLon->getLat());
$addressLatLonGQL->setLon($addressLatLon->getLon());
$addressGQL->setAddressLatLon($addressLatLonGQL);
}
public function getCommunication(Communication $comm = null) {
if (empty($comm)) {
return null;
}
$commGQL = new CommunicationGQL();
$commGQL->setId($comm->getId());
$entriesSrc = $comm->getCommunicationEntries();
foreach ($entriesSrc as $entrySrc) {
$entry = $this->getCommuncationEntry($entrySrc);
$commGQL->addEntry($entry);
}
return $commGQL;
}
public function getCommuncationEntry(CommunicationEntry $commEntry = null) {
if (empty($commEntry)) {
return null;
}
$commEntryGQL = new CommunicationEntryGQL();
$commEntryGQL->setId($commEntry->getId());
$commEntryGQL->setEntryValue($commEntry->getEntryValue());
$commType = $this->getCommunicationType($commEntry->getCommunicationType());
$commEntryGQL->setCommunicationType($commType);
return $commEntryGQL;
}
public function getCommunicationType(CommunicationType $commType = null) {
if (empty($commType)) {
return null;
}
$commTypeGQL = new CommunicationTypeGQL();
$commTypeGQL->setName($commType->getName());
$commTypeGQL->setShortName($commType->getShortName());
return $commTypeGQL;
}
public function getContactGallery(ContactGallery $contactGallery = null, $showAllContacts = false) {
if (empty($contactGallery)) {
return null;
}
$contactGalleryGQL = new ContactGalleryGQL();
$contactGalleryGQL->setId($contactGallery->getId());
$this->addContactGalleryEntries($contactGalleryGQL, $contactGallery, $showAllContacts);
return $contactGalleryGQL;
}
public function addContactGalleryEntries(ContactGalleryGQL $contactGalleryGQL, ContactGallery $contactGallery, $showAllContacts) {
$contactGalleryEntries = $contactGallery->getGalleryEntries();
/* @var $contactGalleryEntry ContactGalleryEntry */
foreach ($contactGalleryEntries as $contactGalleryEntry) {
$isEnabled = $contactGalleryEntry->getEnabled();
if (!$showAllContacts && !$isEnabled) {
continue;
}
$contactGalleryEntryGQL = $this->getContactGalleryEntry($contactGalleryEntry);
$contactGalleryGQL->addGalleryEntry($contactGalleryEntryGQL);
}
}
public function getContactGalleryEntry(ContactGalleryEntry $contactGalleryEntry) {
$contactGalleryEntryGQL = new ContactGalleryEntryGQL();
$contactGalleryEntryGQL->setId($contactGalleryEntry->getId());
$contactGalleryEntryGQL->setEnabled($contactGalleryEntry->getEnabled());
$contact = $contactGalleryEntry->getContact();
$contactGQL = $this->getContact($contact);
$contactGalleryEntryGQL->setContact($contactGQL);
return $contactGalleryEntryGQL;
}
public function getContact(Contact $contact = null) {
if (empty($contact)) {
return null;
}
$contactGQL = new ContactGQL();
$contactGQL->setId($contact->getId());
$contactGQL->setEmployeePosition($contact->getEmployeePosition());
$personGQL = $this->getPerson($contact->getPerson());
$contactGQL->setPerson($personGQL);
$picture = $this->generateThumbnails($contact->getContactProfilePicture());
$contactGQL->setPicture($picture);
$commSrc = $contact->getCommunication();
$communication = $this->getCommunication($commSrc);
$contactGQL->setCommunication($communication);
return $contactGQL;
}
public function getDateString(DateTime $dateTime = null) {
if (empty($dateTime)) {
return '';
}
return $dateTime->format("d.m.Y");
}
public function getEventState(EventState $eventState = null) {
if (empty($eventState)) {
return null;
}
$stateID = $eventState->getId();
switch ($stateID) {
case 1:
return 'ACTIVE';
case 2:
return 'INACTIVE';
case 3:
return 'DELETED';
default:
return null;
}
}
public function getEventTag(EventTagMapping $eventTagMapping = null) {
if (empty($eventTagMapping)) {
return null;
}
/* @var $eventTag EventTag */
$eventTag = $eventTagMapping->getEventTag();
$eventTagGQL = new EventTagGQL();
$eventTagGQL->setTagName($eventTag->getName());
return $eventTagGQL;
}
private function getVideoUrl(Job $job) {
$jobDailyRoutines = $this->jobHelper->getJobDailyRoutinesByJob($job);
if (count($jobDailyRoutines) == 0) {
return null;
}
/* @var $jobDailyRoutine JobDailyRoutine */
$jobDailyRoutine = $jobDailyRoutines[0];
return $jobDailyRoutine->getVideoUrl();
}
public function getImageByID($mediaID) {
$image = new Image();
$media = $this->mediaHelper->getMediaById($mediaID);
if (empty($media)) {
return $image;
}
$imagePath = $this->mediaHelper->getPath($media);
$pathMedium = $this->imagineCacheManager->getBrowserPath($imagePath, 'tn_medium');
$image->setMedium($pathMedium);
return $image;
}
public function generateThumbnailFilter(Media $media = null, $liipFilter) {
$image = new Image();
if (empty('media')) {
return $image;
}
$imagePath = $this->mediaHelper->getPath($media);
$this->imagineCacheManager->getBrowserPath($imagePath, $liipFilter);
return $image;
}
public function generateThumbnailsByID($mediaID) {
$media = $this->mediaHelper->getMediaById($mediaID);
if (empty($media)) {
$image = new Image();
return $image;
}
return $this->generateThumbnails($media);
}
public function generateThumbnails(Media $media = null) {
if (empty($media)) {
return null;
}
$image = new Image();
$image->setId($media->getId());
$imagePath = $this->mediaHelper->getPath($media);
/* set image info */
$imageInfo = new ImageInfo();
$imageInfo->setCaption($media->getName());
$copyright = $media->getCopyright();
$imageInfo->setCopyright($copyright);
$image->setInfo($imageInfo);
/* set image data */
$image->setMedium($this->getImageData($imagePath, 'tn_medium', 527));
$image->setPortrait($this->getImageData($imagePath, 'tn_portrait', 1000));
$image->setLandscape($this->getImageData($imagePath, 'tn_landscape', 1240));
$image->setLarge($this->getImageData($imagePath, 'tn_large', 1560));
$image->setXlarge($this->getImageData($imagePath, 'tn_xlarge', 1900));
$image->setHuge($this->getImageData($imagePath, 'tn_huge', 2800));
return $image;
}
private function getImageData($imagePath, $filter, $width) {
/* Generate path for the filter */
$relativePath = $this->imagineCacheManager->generateUrl($imagePath, $filter, [], null, UrlGeneratorInterface::ABSOLUTE_PATH);
$mediaPath = $this->imageDomain . $relativePath;
$imageData = new ImageData();
$imageData->setUrl($mediaPath);
$imageData->setWidth($width);
return $imageData;
}
public function getMediaDocument(Media $media = null) {
if (empty($media)) {
return null;
}
$mediaID = $media->getId();
$mediaName = $media->getName();
$mediaGQL = new MediaGQL();
$mediaGQL->setId($mediaID);
$mediaGQL->setContentType("DOCUMENT");
$mediaGQL->setName($mediaName);
$document = new DocumentGQL();
$document->setId($mediaID);
$document->setName($mediaName);
$mediaLink = $this->mediaHelper->getMediaLink($media);
$document->setFilePath($mediaLink);
$mediaGQL->setDocument($document);
return $mediaGQL;
}
public function getMediaImage(Media $media = null) {
if (empty($media)) {
return null;
}
$mediaID = $media->getId();
$mediaGQL = new MediaGQL();
$mediaGQL->setId($mediaID);
$mediaGQL->setContentType("IMAGE");
$image = $this->generateThumbnails($media);
$mediaGQL->setImage($image);
return $mediaGQL;
}
public function getGalleryHasMediaImage(GalleryHasMedia $galleryHasMedia = null) {
if (empty($galleryHasMedia)) {
return null;
}
$media = $galleryHasMedia->getMedia();
if (empty($media)) {
return null;
}
$galleryHasMediaID = $galleryHasMedia->getId();
$mediaGQL = new MediaGQL();
$mediaGQL->setContentType("IMAGE");
$mediaGQL->setId($galleryHasMediaID);
$mediaGQL->setPosition($galleryHasMedia->getPosition());
$mediaGQL->setVisible($galleryHasMedia->getEnabled());
$image = $this->generateThumbnails($media);
$mediaGQL->setImage($image);
return $mediaGQL;
}
public function getMediaVideo(Media $media = null) {
if (empty($media)) {
return null;
}
$mediaGQL = new MediaGQL();
$mediaGQL->setContentType("VIDEO");
$mediaGQL->setId($media->getId());
$video = $this->videoHelper->getVideoByMedia($media);
$mediaGQL->setVideo($video);
return $mediaGQL;
}
public function getGalleryHasMediaVideo(GalleryHasMedia $galleryHasMedia = null) {
if (empty($galleryHasMedia)) {
return null;
}
$media = $galleryHasMedia->getMedia();
if (empty($media)) {
return null;
}
$galleryHasMediaID = $galleryHasMedia->getId();
$mediaGQL = new MediaGQL();
$mediaGQL->setContentType("VIDEO");
$mediaGQL->setId($galleryHasMediaID);
$mediaGQL->setPosition($galleryHasMedia->getPosition());
$mediaGQL->setVisible($galleryHasMedia->getEnabled());
$video = $this->videoHelper->getVideoByMedia($media);
$mediaGQL->setVideo($video);
return $mediaGQL;
}
public function getGalleryHasMediaDocument(GalleryHasMedia $galleryHasMedia = null) {
if (empty($galleryHasMedia)) {
return null;
}
$media = $galleryHasMedia->getMedia();
if (empty($media)) {
return null;
}
$mediaGQL = new MediaGQL();
$mediaGQL->setContentType("DOCUMENT");
$mediaGQL->setId($galleryHasMedia->getId());
$mediaGQL->setPosition($galleryHasMedia->getPosition());
$mediaGQL->setVisible($galleryHasMedia->getEnabled());
$document = $this->getMediaDocument($media);
$mediaGQL->setDocument($document);
return $mediaGQL;
}
public function getPerson(Person $person = null) {
if (empty($person)) {
return null;
}
$personGQL = new PersonGQL();
$personGQL->setId($person->getId());
$personGQL->setFirstname($person->getFirstname());
$personGQL->setLastname($person->getLastname());
$personGQL->setSex($person->getSex());
$titlesSrc = $person->getTitles();
/* @var $titleSrc Title */
foreach ($titlesSrc as $titleSrc) {
$title = new TitleGQL();
$title->setId($titleSrc->getId());
$title->setShortName($titleSrc->getShortName());
$title->setDescription($titleSrc->getDescription());
$personGQL->addTitle($title);
}
return $personGQL;
}
public function getProfileLink(ProfileInterface $profileSrc) {
/* @var $profileLinkSrc ProfileLink */
$profileLinkSrc = $profileSrc->getProfileLink();
if (empty($profileLinkSrc)) {
return null;
}
$profileLink = new ProfileLinkGQL();
/* @var $createdAt DateTime */
$createdAt = $profileLinkSrc->getCreatedAt();
$profileLink->setCreatedAt($createdAt->format("d.m.Y H:m"));
$entries = $profileLinkSrc->getProfileLinkEntries();
foreach ($entries as $entry) {
$this->addProfileLinkEntry($profileLink, $entry);
}
return $profileLink;
}
private function addProfileLinkEntry(ProfileLinkGQL $profileLinkGQL, ProfileLinkEntry $profileLinkEntry) {
$linkValue = $profileLinkEntry->getLinkValue();
// ignore empty link values -> can happen, if a previously present link is removed
if (empty($linkValue)) {
return;
}
/* @var $linkType ProfileLinkType */
$linkType = $profileLinkEntry->getLinkType();
$typePosition = $linkType->getPosition();
$profileLinkEntryGQL = new ProfileLinkEntryGQL();
$profileLinkEntryGQL->setId($profileLinkEntry->getId());
$profileLinkEntryGQL->setLinkType($linkType->getTypeName());
$profileLinkEntryGQL->setLinkValue($linkValue);
$profileLinkGQL->addEntry($profileLinkEntryGQL, $typePosition);
}
public function getProfileMediaGallery(ProfileInterface $profile, $showAllGalleryItems = false) {
$galleryImages = $profile->getGallery();
$galleryVideos = $profile->getVideos();
$mediaGalleryGQL = $this->getMediaGallery($galleryImages, $galleryVideos, $showAllGalleryItems);
/* sort items can only be done after all media items have been added -
* media gallery contains items from different galleries pictures and videos */
$mediaGalleryGQL->sortItems();
return $mediaGalleryGQL;
}
public function getProfileType(ProfileType $profileType) {
$profileTypeGQL = new ProfileTypeGQL();
$profileTypeGQL->setId($profileType->getId());
$profileTypeGQL->setName($profileType->getName());
$profileTypeGQL->setShortName($profileType->getShortName());
$profileTypeGQL->setPosition($profileType->getPosition());
return $profileTypeGQL;
}
public function getProfileTypeByID($id) {
$profileTypeGQL = new ProfileTypeGQL();
/* @var $profileType ProfileType */
$profileType = $this->em->getRepository(ProfileType::class)->find($id);
if (empty($profileType)) {
return $profileTypeGQL;
}
$profileTypeGQL->setId($profileType->getId());
$profileTypeGQL->setName($profileType->getName());
$profileTypeGQL->setShortName($profileType->getShortName());
$profileTypeGQL->setPosition($profileType->getPosition());
return $profileTypeGQL;
}
public function getProfileDocumentGallery(ProfileInterface $profile, $showAllGalleryItems = false) {
$documents = $profile->getDocuments();
return $this->getDocumentGallery($documents, $showAllGalleryItems);
}
public function getEventMediaGallery(Event $event) {
$galleryImages = $event->getGallery();
$galleryVideos = $event->getVideos();
return $this->getMediaGallery($galleryImages, $galleryVideos);
}
public function getDocumentGallery($documents = null, $showAllGalleryItems = false) {
$documentGalleryGQL = new GalleryGQL();
$documentGalleryGQL->setId(0);
$documentGalleryGQL->setName('documents');
if (empty($documents)) {
return $documentGalleryGQL;
}
$documentGalleryGQL->setId($documents->getId());
$documentGalleryGQL->setName($documents->getName());
$galleryHasMedias = $documents->getGalleryItems();
/* @var $galleryHasMedia GalleryHasMedia */
foreach ($galleryHasMedias as $galleryHasMedia) {
$enabled = $galleryHasMedia->getEnabled();
if (!$showAllGalleryItems && !$enabled) {
continue;
}
$document = $this->getGalleryHasMediaDocument($galleryHasMedia);
$documentGalleryGQL->addGalleryItem($document);
}
return $documentGalleryGQL;
}
private function getMediaGallery($galleryImages, $galleryVideos, $showAllGalleryItems = false) {
$mediaGallery = new GalleryGQL();
$mediaGallery->setName("mixed");
if (!empty($galleryImages)) {
$this->addMediaImage($mediaGallery, $galleryImages, $showAllGalleryItems);
}
if (!empty($galleryVideos)) {
$this->addMediaVideo($mediaGallery, $galleryVideos, $showAllGalleryItems);
}
return $mediaGallery;
}
private function addMediaImage(GalleryGQL $mediaGallery, Gallery $galleryImages, $showAllGalleryItems) {
$imageItems = $galleryImages->getGalleryItems();
/* @var $galleryHasMedia GalleryHasMedia */
foreach ($imageItems as $galleryHasMedia) {
$enabled = $galleryHasMedia->getEnabled();
if (!$showAllGalleryItems && !$enabled) {
continue;
}
$media = $this->getGalleryHasMediaImage($galleryHasMedia);
if (empty($media)) {
continue;
}
$mediaGallery->addGalleryItem($media);
}
}
private function addMediaVideo(GalleryGQL $mediaGallery, Gallery $galleryVideos, $showAllGalleryItems) {
$videoItems = $galleryVideos->getGalleryItems();
/* @var $galleryHasMedia GalleryHasMedia */
foreach ($videoItems as $galleryHasMedia) {
$enabled = $galleryHasMedia->getEnabled();
if (!$showAllGalleryItems && !$enabled) {
continue;
}
$media = $this->getGalleryHasMediaVideo($galleryHasMedia);
$mediaGallery->addGalleryItem($media);
}
}
}