src/EventSubscriber/Profile/WidgetSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. /*
  3.  * @since 1.0.0
  4.  * @copyright Copyright (C) 2020 ArtMedia. All rights reserved.
  5.  * @website http://artmedia.biz.pl
  6.  * @author Arkadiusz Tobiasz
  7.  * @email kontakt@artmedia.biz.pl
  8.  */
  9. namespace App\EventSubscriber\Profile;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use App\Events\User\ProfileWidgetAddEvent;
  13. use App\Entity\Media\Photo;
  14. use Twig\Environment;
  15. use App\Entity\Profile\Travel;
  16. use App\Entity\Profile;
  17. use App\Entity\Profile\Feed;
  18. use App\Entity\Profile\Note;
  19. use App\Events\Core\HomepageWidgetAddEvent;
  20. use App\Settings\SettingsManager;
  21. class WidgetSubscriber implements EventSubscriberInterface
  22. {
  23.     private $em;
  24.     private $twig;
  25.     private $settingsManager;
  26.     public function __construct(
  27.         Environment $twig,
  28.         SettingsManager $settingsManager,
  29.         EntityManagerInterface $em
  30.     ) {
  31.         $this->em $em;
  32.         $this->settingsManager $settingsManager;
  33.         $this->twig $twig;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             ProfileWidgetAddEvent::NAME => 'onProfileWidgetAddEvent',
  39.             HomepageWidgetAddEvent::NAME => 'onHomepageWidgetAddEvent',
  40.         ];
  41.     }
  42.     public function onHomepageWidgetAddEvent(HomepageWidgetAddEvent $event): void
  43.     {
  44.         $profiles $this->em->getRepository(Profile::class)->getNewProfiles(12);
  45.         if ($profiles) {
  46.             $event->addWidget(
  47.                 'new_profiles',
  48.                 $this->twig->render('profile/widget/new_profiles.html.twig', [
  49.                     'profiles' => $profiles,
  50.                 ]),
  51.                 'right',
  52.                 2
  53.             );
  54.         }
  55.         $user $event->getUser();
  56.         if (!$user) {
  57.             return;
  58.         }
  59.         /*$followers = $user->getFollowers();
  60.         if ($followers->count()) {
  61.             $event->addWidget(
  62.                 'followers',
  63.                 $this->twig->render('profile/widget/followers.html.twig', [
  64.                     'followers' => $followers,
  65.                 ]),
  66.                 'left',
  67.                 1
  68.             );
  69.         }*/
  70.         $travels $this->em->getRepository(Travel::class)->getNearTravels($user);
  71.         if ($travels) {
  72.             $event->addWidget(
  73.                 'travels',
  74.                 $this->twig->render('profile/widget/near_travels.html.twig', [
  75.                     'travels' => $travels,
  76.                 ]),
  77.                 'left',
  78.                 3
  79.             );
  80.         }
  81.         $configs $this->settingsManager->get([
  82.             SettingsManager::USER_FEED_COUNT,
  83.             SettingsManager::USER_FEED_COUNT_GROUP,
  84.         ], true);
  85.         $limit = (int) $configs[SettingsManager::USER_FEED_COUNT];
  86.         if ($user->getGroups(json_decode($configs[SettingsManager::USER_FEED_COUNT_GROUP]))->count()) {
  87.             $limit $user->getFeedLimit();
  88.         }
  89.         $feeds $this->em->getRepository(Feed::class)->getFollowProfiles($user$limit);
  90.         //if ($feeds) {
  91.             $event->addWidget(
  92.                 'feeds',
  93.                 $this->twig->render('profile/widget/feeds.html.twig', [
  94.                     'feeds' => $feeds,
  95.                 ]),
  96.                 'left',
  97.                 2
  98.             );
  99.         //}
  100.     }
  101.     public function onProfileWidgetAddEvent(ProfileWidgetAddEvent $event): void
  102.     {
  103.         $profile $event->getProfile();
  104.         if (!$profile) {
  105.             return;
  106.         }
  107.         $user $event->getUser();
  108.         $owner $user && in_array('ROLE_ADMIN'$user->getRoles()) || $user == $profile->getUser();
  109.         $configs $this->settingsManager->get([
  110.             SettingsManager::PROFILE_TRAVELS,
  111.             SettingsManager::PROFILE_NOTES_GROUPS,
  112.         ], true);
  113.         if ($profile->getDescription()) {
  114.             $event->addWidget(
  115.                 'profile_description',
  116.                 $this->twig->render('profile/widget/description.html.twig', [
  117.                     'profile' => $profile,
  118.                     'owner' => $owner,
  119.                 ]),
  120.                 'left',
  121.                 0
  122.             );
  123.         }
  124.         $event->addWidget(
  125.             'profile_info',
  126.             $this->twig->render('profile/widget/' $profile->getType() . '_info.html.twig', [
  127.                 'profile' => $profile,
  128.                 'owner' => $owner,
  129.             ]),
  130.             'right',
  131.             1
  132.         );
  133.         if (in_array($profile->getProfileType()->getId(), json_decode($configs[SettingsManager::PROFILE_TRAVELS], true))) {
  134.             $travels $this->em->getRepository(Travel::class)->getCurrentProfileList($profile);
  135.             if ($travels || $owner) {
  136.                 $event->addWidget(
  137.                     'travels',
  138.                     $this->twig->render('profile/widget/travels.html.twig', [
  139.                     'travels' => $travels,
  140.                     'profile' => $profile,
  141.                     'owner' => $owner,
  142.                 ]),
  143.                     'left',
  144.                     4
  145.                 );
  146.             }
  147.         }
  148.         if ($user && !$owner) {
  149.             $note $this->em->getRepository(Note::class)->findOneBy([
  150.                 'profile' => $profile,
  151.                 'user' => $user,
  152.             ]);
  153.             $event->addWidget(
  154.                 'notes',
  155.                 $this->twig->render('profile/widget/note.html.twig', [
  156.                 'note' => $note $note->getContent() : '',
  157.                 'profile' => $profile,
  158.                 'enabled' => $user->getGroups(json_decode($configs[SettingsManager::PROFILE_NOTES_GROUPS], true))->count(),
  159.             ]),
  160.                 'left',
  161.                 10
  162.             );
  163.         }
  164.     }
  165. }