<?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\Profile;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Events\User\ProfileWidgetAddEvent;
use App\Entity\Media\Photo;
use Twig\Environment;
use App\Entity\Profile\Travel;
use App\Entity\Profile;
use App\Entity\Profile\Feed;
use App\Entity\Profile\Note;
use App\Events\Core\HomepageWidgetAddEvent;
use App\Settings\SettingsManager;
class WidgetSubscriber implements EventSubscriberInterface
{
private $em;
private $twig;
private $settingsManager;
public function __construct(
Environment $twig,
SettingsManager $settingsManager,
EntityManagerInterface $em
) {
$this->em = $em;
$this->settingsManager = $settingsManager;
$this->twig = $twig;
}
public static function getSubscribedEvents(): array
{
return [
ProfileWidgetAddEvent::NAME => 'onProfileWidgetAddEvent',
HomepageWidgetAddEvent::NAME => 'onHomepageWidgetAddEvent',
];
}
public function onHomepageWidgetAddEvent(HomepageWidgetAddEvent $event): void
{
$profiles = $this->em->getRepository(Profile::class)->getNewProfiles(12);
if ($profiles) {
$event->addWidget(
'new_profiles',
$this->twig->render('profile/widget/new_profiles.html.twig', [
'profiles' => $profiles,
]),
'right',
2
);
}
$user = $event->getUser();
if (!$user) {
return;
}
/*$followers = $user->getFollowers();
if ($followers->count()) {
$event->addWidget(
'followers',
$this->twig->render('profile/widget/followers.html.twig', [
'followers' => $followers,
]),
'left',
1
);
}*/
$travels = $this->em->getRepository(Travel::class)->getNearTravels($user);
if ($travels) {
$event->addWidget(
'travels',
$this->twig->render('profile/widget/near_travels.html.twig', [
'travels' => $travels,
]),
'left',
3
);
}
$configs = $this->settingsManager->get([
SettingsManager::USER_FEED_COUNT,
SettingsManager::USER_FEED_COUNT_GROUP,
], true);
$limit = (int) $configs[SettingsManager::USER_FEED_COUNT];
if ($user->getGroups(json_decode($configs[SettingsManager::USER_FEED_COUNT_GROUP]))->count()) {
$limit = $user->getFeedLimit();
}
$feeds = $this->em->getRepository(Feed::class)->getFollowProfiles($user, $limit);
//if ($feeds) {
$event->addWidget(
'feeds',
$this->twig->render('profile/widget/feeds.html.twig', [
'feeds' => $feeds,
]),
'left',
2
);
//}
}
public function onProfileWidgetAddEvent(ProfileWidgetAddEvent $event): void
{
$profile = $event->getProfile();
if (!$profile) {
return;
}
$user = $event->getUser();
$owner = $user && in_array('ROLE_ADMIN', $user->getRoles()) || $user == $profile->getUser();
$configs = $this->settingsManager->get([
SettingsManager::PROFILE_TRAVELS,
SettingsManager::PROFILE_NOTES_GROUPS,
], true);
if ($profile->getDescription()) {
$event->addWidget(
'profile_description',
$this->twig->render('profile/widget/description.html.twig', [
'profile' => $profile,
'owner' => $owner,
]),
'left',
0
);
}
$event->addWidget(
'profile_info',
$this->twig->render('profile/widget/' . $profile->getType() . '_info.html.twig', [
'profile' => $profile,
'owner' => $owner,
]),
'right',
1
);
if (in_array($profile->getProfileType()->getId(), json_decode($configs[SettingsManager::PROFILE_TRAVELS], true))) {
$travels = $this->em->getRepository(Travel::class)->getCurrentProfileList($profile);
if ($travels || $owner) {
$event->addWidget(
'travels',
$this->twig->render('profile/widget/travels.html.twig', [
'travels' => $travels,
'profile' => $profile,
'owner' => $owner,
]),
'left',
4
);
}
}
if ($user && !$owner) {
$note = $this->em->getRepository(Note::class)->findOneBy([
'profile' => $profile,
'user' => $user,
]);
$event->addWidget(
'notes',
$this->twig->render('profile/widget/note.html.twig', [
'note' => $note ? $note->getContent() : '',
'profile' => $profile,
'enabled' => $user->getGroups(json_decode($configs[SettingsManager::PROFILE_NOTES_GROUPS], true))->count(),
]),
'left',
10
);
}
}
}