<?php
/*
* @since 1.0.0
* @copyright Copyright (C) 2020 ArtMedia. All rights reserved.
* @website http://artmedia.biz.pl
* @author Arkadiusz Tobiasz
* @email kontakt@artmedia.biz.pl
*/
namespace App\EventSubscriber\Media;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Events\Core\DashboardWidgetAddEvent;
use App\Events\Core\HomepageWidgetAddEvent;
use App\Events\User\ProfileWidgetAddEvent;
use App\Entity\Media\Photo;
use Twig\Environment;
use App\Entity\Media\Album;
use App\Entity\Media\Media;
use App\Entity\Media\Credit;
use App\Entity\Profile;
use App\Settings\SettingsManager;
class WidgetSubscriber implements EventSubscriberInterface
{
private $em;
private $twig;
private $settingsManager;
public function __construct(
Environment $twig,
EntityManagerInterface $em,
SettingsManager $settingsManager
) {
$this->em = $em;
$this->twig = $twig;
$this->settingsManager = $settingsManager;
}
public static function getSubscribedEvents(): array
{
return [
DashboardWidgetAddEvent::NAME => 'onDashboardWidgetAddEvent',
HomepageWidgetAddEvent::NAME => 'onHomepageWidgetAddEvent',
ProfileWidgetAddEvent::NAME => 'onProfileWidgetAddEvent',
];
}
public function onProfileWidgetAddEvent(ProfileWidgetAddEvent $event): void
{
$profile = $event->getProfile();
if (!$profile) {
return;
}
$configs = $this->settingsManager->get([
SettingsManager::MEDIA_ALBUMS_GROUPS,
SettingsManager::MEDIA_PHOTOS_LIMIT,
SettingsManager::MEDIA_UNLIMITED_GROUPS,
SettingsManager::PROFILE_CREDITS_PROFILES,
], true);
$user = $event->getUser();
$owner = $user && (in_array('ROLE_ADMIN', $user->getRoles()) || $user == $profile->getUser());
$profiles = [];
$viewersProfiles = [];
$commonProfiles = [];
$moreLink = false;
$credits = $this->em->getRepository(Credit::class)->getWorkProfiles($profile);
if ($credits) {
$limit = 1; (int) $configs[SettingsManager::PROFILE_CREDITS_PROFILES];
$count = count($credits);
if ($count > $limit) {
$moreLink = true;
}
$i = 0;
foreach ($credits as $credit) {
$i++;
if ($i > $limit) {
break;
}
$profiles[$credit->getId()] = $credit;
}
}
if ($profiles) {
if ($user && !$owner) {
if ($user->getProfiles()) {
foreach ($user->getProfiles() as $viewerProfile) {
if (isset($profiles[$viewerProfile->getId()])) {
$viewersProfiles[] = $profiles[$viewerProfile->getId()];
unset($profiles[$viewerProfile->getId()]);
}
}
}
$viewersCredits = $this->em->getRepository(Profile::class)->getUserWorkProfiles($user);
if ($viewersCredits) {
foreach ($viewersCredits as $credit) {
if (isset($profiles[$credit->getId()])) {
$commonProfiles[] = $profiles[$credit->getId()];
unset($profiles[$credit->getId()]);
}
}
}
}
}
$event->addWidget(
'credits',
$this->twig->render('media/widget/credits.html.twig', [
'profile' => $profile,
'profiles' => $profiles,
'viewersProfiles' => $viewersProfiles,
'commonProfiles' => $commonProfiles,
'moreLink' => $moreLink,
]),
'left',
5
);
$limit = 0;
if ((int)$configs[SettingsManager::MEDIA_PHOTOS_LIMIT] &&
!$profile->getUser()->getGroups(json_decode($configs[SettingsManager::MEDIA_UNLIMITED_GROUPS], true))->count()) {
$limit = (int)$configs[SettingsManager::MEDIA_PHOTOS_LIMIT];
}
$media = $this->em->getRepository(Media::class)->getPortfolioList($profile, $owner, $limit);
$album = $this->em->getRepository(Album::class)->getPortfolio($profile);
$event->addWidget(
'portfolio',
$this->twig->render('media/widget/portfolio.html.twig', [
'media' => $media,
'profile' => $profile,
'owner' => $owner,
'album' => $album,
]),
'left',
1
);
$creditsAlbum = $this->em->getRepository(Profile::class)->creditsAlbumEnabled($profile);
$albums = [];
if ($profile->getUser()->getGroups(json_decode($configs[SettingsManager::MEDIA_ALBUMS_GROUPS], true))->count()) {
$albums = $this->em->getRepository(Album::class)->getProfileAlbums($profile);
}
if ($albums || $creditsAlbum) {
$event->addWidget(
'portfolio_albums',
$this->twig->render('media/widget/albums.html.twig', [
'albums' => $albums,
'profile' => $profile,
'owner' => $owner,
'creditsAlbum' => ($creditsAlbum ? true : false)
]),
'left',
2
);
}
}
public function onHomepageWidgetAddEvent(HomepageWidgetAddEvent $event): void
{
$media = $this->em->getRepository(Media::class)->getLatest(12);
if ($media) {
$event->addWidget(
'latest_media',
$this->twig->render('media/widget/latest_media.html.twig', [
'media' => $media,
]),
'right',
1
);
}
$tags = $this->em->getRepository(Media::class)->getTags(12);
if ($tags) {
$maxCount = 1;
$minCount = 1;
foreach ($tags as $tag) {
if ($tag['tagCount'] > $maxCount) {
$maxCount = $tag['tagCount'];
}
if ($tag['tagCount'] < $minCount) {
$minCount = $tag['tagCount'];
}
}
$event->addWidget(
'media_tags',
$this->twig->render('media/widget/tags.html.twig', [
'tags' => $tags,
'mediumCount' => (($maxCount + $minCount) / 2),
]),
'right',
3
);
}
$user = $event->getUser();
if (!$user || !$user->getProfiles()->count()) {
return;
}
foreach ($user->getProfiles() as $profile) {
$count = $this->em->getRepository(Photo::class)->countUnpublished($profile);
if ($count) {
$event->addWidget(
'unpublished_photos_' . $profile->getSlug(),
$this->twig->render('media/widget/unpublished_photos.html.twig', [
'count' => $count,
'profile' => $profile,
]),
'top',
1
);
}
}
}
public function onDashboardWidgetAddEvent(DashboardWidgetAddEvent $event): void
{
$user = $event->getUser();
if (!$user || !$user->getProfiles()->count()) {
return;
}
foreach ($user->getProfiles() as $profile) {
$count = $this->em->getRepository(Photo::class)->countUnpublished($profile);
if ($count) {
$event->addWidget(
'unpublished_photos_' . $profile->getSlug(),
$this->twig->render('media/widget/unpublished_photos.html.twig', [
'count' => $count,
'profile' => $profile,
]),
'top',
1
);
}
}
}
}