src/EventSubscriber/Media/WidgetSubscriber.php line 164

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\Media;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use App\Events\Core\DashboardWidgetAddEvent;
  13. use App\Events\Core\HomepageWidgetAddEvent;
  14. use App\Events\User\ProfileWidgetAddEvent;
  15. use App\Entity\Media\Photo;
  16. use Twig\Environment;
  17. use App\Entity\Media\Album;
  18. use App\Entity\Media\Media;
  19. use App\Entity\Media\Credit;
  20. use App\Entity\Profile;
  21. use App\Settings\SettingsManager;
  22. class WidgetSubscriber implements EventSubscriberInterface
  23. {
  24.     private $em;
  25.     private $twig;
  26.     private $settingsManager;
  27.     public function __construct(
  28.         Environment $twig,
  29.         EntityManagerInterface $em,
  30.         SettingsManager $settingsManager
  31.     ) {
  32.         $this->em $em;
  33.         $this->twig $twig;
  34.         $this->settingsManager $settingsManager;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             DashboardWidgetAddEvent::NAME => 'onDashboardWidgetAddEvent',
  40.             HomepageWidgetAddEvent::NAME => 'onHomepageWidgetAddEvent',
  41.             ProfileWidgetAddEvent::NAME => 'onProfileWidgetAddEvent',
  42.         ];
  43.     }
  44.     public function onProfileWidgetAddEvent(ProfileWidgetAddEvent $event): void
  45.     {
  46.         $profile $event->getProfile();
  47.         if (!$profile) {
  48.             return;
  49.         }
  50.         $configs $this->settingsManager->get([
  51.             SettingsManager::MEDIA_ALBUMS_GROUPS,
  52.             SettingsManager::MEDIA_PHOTOS_LIMIT,
  53.             SettingsManager::MEDIA_UNLIMITED_GROUPS,
  54.             SettingsManager::PROFILE_CREDITS_PROFILES,
  55.         ], true);
  56.         $user $event->getUser();
  57.         $owner $user && (in_array('ROLE_ADMIN'$user->getRoles()) || $user == $profile->getUser());
  58.         $profiles = [];
  59.         $viewersProfiles = [];
  60.         $commonProfiles = [];
  61.         $moreLink false;
  62.         $credits $this->em->getRepository(Credit::class)->getWorkProfiles($profile);
  63.         if ($credits) {
  64.             $limit 1; (int) $configs[SettingsManager::PROFILE_CREDITS_PROFILES];
  65.             $count count($credits);
  66.             if ($count $limit) {
  67.                 $moreLink true;
  68.             }
  69.             $i 0;
  70.             foreach ($credits as $credit) {
  71.                 $i++;
  72.                 if ($i $limit) {
  73.                     break;
  74.                 }
  75.                 $profiles[$credit->getId()] = $credit;
  76.             }
  77.         }
  78.         if ($profiles) {
  79.             if ($user && !$owner) {
  80.                 if ($user->getProfiles()) {
  81.                     foreach ($user->getProfiles() as $viewerProfile) {
  82.                         if (isset($profiles[$viewerProfile->getId()])) {
  83.                             $viewersProfiles[] = $profiles[$viewerProfile->getId()];
  84.                             unset($profiles[$viewerProfile->getId()]);
  85.                         }
  86.                     }
  87.                 }
  88.                 $viewersCredits $this->em->getRepository(Profile::class)->getUserWorkProfiles($user);
  89.                 if ($viewersCredits) {
  90.                     foreach ($viewersCredits as $credit) {
  91.                         if (isset($profiles[$credit->getId()])) {
  92.                             $commonProfiles[] = $profiles[$credit->getId()];
  93.                             unset($profiles[$credit->getId()]);
  94.                         }
  95.                     }
  96.                 }
  97.             }
  98.         }
  99.         $event->addWidget(
  100.             'credits',
  101.             $this->twig->render('media/widget/credits.html.twig', [
  102.                 'profile' => $profile,
  103.                 'profiles' => $profiles,
  104.                 'viewersProfiles' => $viewersProfiles,
  105.                 'commonProfiles' => $commonProfiles,
  106.                 'moreLink' => $moreLink,
  107.             ]),
  108.             'left',
  109.             5
  110.         );
  111.         $limit 0;
  112.         if ((int)$configs[SettingsManager::MEDIA_PHOTOS_LIMIT] && 
  113.             !$profile->getUser()->getGroups(json_decode($configs[SettingsManager::MEDIA_UNLIMITED_GROUPS], true))->count()) {
  114.                 $limit = (int)$configs[SettingsManager::MEDIA_PHOTOS_LIMIT];
  115.         }
  116.         $media $this->em->getRepository(Media::class)->getPortfolioList($profile$owner$limit);
  117.         $album $this->em->getRepository(Album::class)->getPortfolio($profile);
  118.         $event->addWidget(
  119.             'portfolio',
  120.             $this->twig->render('media/widget/portfolio.html.twig', [
  121.                 'media' => $media,
  122.                 'profile' => $profile,
  123.                 'owner' => $owner,
  124.                 'album' => $album,
  125.             ]),
  126.             'left',
  127.             1
  128.         );
  129.         $creditsAlbum $this->em->getRepository(Profile::class)->creditsAlbumEnabled($profile);
  130.         $albums = [];
  131.         if ($profile->getUser()->getGroups(json_decode($configs[SettingsManager::MEDIA_ALBUMS_GROUPS], true))->count()) {
  132.             $albums $this->em->getRepository(Album::class)->getProfileAlbums($profile);
  133.         }
  134.         if ($albums || $creditsAlbum) {
  135.             $event->addWidget(
  136.                 'portfolio_albums',
  137.                 $this->twig->render('media/widget/albums.html.twig', [
  138.                     'albums' => $albums,
  139.                     'profile' => $profile,
  140.                     'owner' => $owner,
  141.                     'creditsAlbum' => ($creditsAlbum true false)
  142.                 ]),
  143.                 'left',
  144.                 2
  145.             );
  146.         }
  147.     }
  148.     public function onHomepageWidgetAddEvent(HomepageWidgetAddEvent $event): void
  149.     {
  150.         $media $this->em->getRepository(Media::class)->getLatest(12);
  151.         if ($media) {
  152.             $event->addWidget(
  153.                 'latest_media',
  154.                 $this->twig->render('media/widget/latest_media.html.twig', [
  155.                     'media' => $media,
  156.                 ]),
  157.                 'right',
  158.                 1
  159.             );
  160.         }
  161.         $tags $this->em->getRepository(Media::class)->getTags(12);
  162.         if ($tags) {
  163.             $maxCount 1;
  164.             $minCount 1;
  165.             foreach ($tags as $tag) {
  166.                 if ($tag['tagCount'] > $maxCount) {
  167.                     $maxCount $tag['tagCount'];
  168.                 }
  169.                 if ($tag['tagCount'] < $minCount) {
  170.                     $minCount $tag['tagCount'];
  171.                 }
  172.             }
  173.             $event->addWidget(
  174.                 'media_tags',
  175.                 $this->twig->render('media/widget/tags.html.twig', [
  176.                     'tags' => $tags,
  177.                     'mediumCount' => (($maxCount $minCount) / 2),
  178.                 ]),
  179.                 'right',
  180.                 3
  181.             );
  182.         }
  183.         $user $event->getUser();
  184.         if (!$user || !$user->getProfiles()->count()) {
  185.             return;
  186.         }
  187.         foreach ($user->getProfiles() as $profile) {
  188.             $count $this->em->getRepository(Photo::class)->countUnpublished($profile);
  189.             if ($count) {
  190.                 $event->addWidget(
  191.                     'unpublished_photos_' $profile->getSlug(),
  192.                     $this->twig->render('media/widget/unpublished_photos.html.twig', [
  193.                         'count' => $count,
  194.                         'profile' => $profile,
  195.                     ]),
  196.                     'top',
  197.                     1
  198.                 );
  199.             }
  200.         }
  201.     }
  202.     public function onDashboardWidgetAddEvent(DashboardWidgetAddEvent $event): void
  203.     {
  204.         $user $event->getUser();
  205.         if (!$user || !$user->getProfiles()->count()) {
  206.             return;
  207.         }
  208.         foreach ($user->getProfiles() as $profile) {
  209.             $count $this->em->getRepository(Photo::class)->countUnpublished($profile);
  210.             if ($count) {
  211.                 $event->addWidget(
  212.                     'unpublished_photos_' $profile->getSlug(),
  213.                     $this->twig->render('media/widget/unpublished_photos.html.twig', [
  214.                     'count' => $count,
  215.                     'profile' => $profile,
  216.                 ]),
  217.                     'top',
  218.                     1
  219.                 );
  220.             }
  221.         }
  222.     }
  223. }