src/Controller/Media/PortfolioController.php line 288

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\Controller\Media;
  10. use App\Controller\AbstractController;
  11. use App\Entity\Core\Tag;
  12. use App\Entity\Media\Album;
  13. use App\Entity\Media\AlbumMedia;
  14. use App\Entity\Media\Photo;
  15. use App\Entity\Media\Video;
  16. use App\Entity\Media\Media;
  17. use App\Entity\Profile;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Translation\TranslatableMessage;
  23. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  24. use Symfony\Component\HttpFoundation\JsonResponse;
  25. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  26. use App\Utils\Core\Image;
  27. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  28. use App\Settings\SettingsManager;
  29. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  30. use Symfony\Component\HttpFoundation\Cookie;
  31. /**
  32.  * 
  33.  * @Route("/{slug}/portfolio", priority="-1")
  34.  */
  35. class PortfolioController extends AbstractController
  36. {
  37.     /**
  38.      * @Route("/", priority="-1", name="portfolio", methods="GET|POST")
  39.      */
  40.     public function index(
  41.         Request $request
  42.         Profile $profile,
  43.         SettingsManager $settingsManager
  44.     ): Response
  45.     {
  46.         $configs $settingsManager->get([
  47.             SettingsManager::MEDIA_ALBUMS_GROUPS,
  48.             SettingsManager::MEDIA_PHOTOS_LIMIT,
  49.             SettingsManager::MEDIA_UNLIMITED_GROUPS,
  50.         ], true);
  51.         $limit 0;
  52.         if ((int)$configs[SettingsManager::MEDIA_PHOTOS_LIMIT] && 
  53.             !$profile->getUser()->getGroups(json_decode($configs[SettingsManager::MEDIA_UNLIMITED_GROUPS], true))->count()) {
  54.                 $limit = (int)$configs[SettingsManager::MEDIA_PHOTOS_LIMIT];
  55.         }
  56.         $owner $this->isGranted('ROLE_ADMIN') || $this->getUser() == $profile->getUser();
  57.         $media $this->getDoctrine()->getRepository(Media::class)->getPortfolioList($profile$owner$limit);
  58.         $albums = [];
  59.         if ($profile->getUser()->getGroups(json_decode($configs[SettingsManager::MEDIA_ALBUMS_GROUPS], true))->count()) {
  60.             $albums $this->getDoctrine()->getRepository(Album::class)->getProfileAlbums($profile);
  61.         }
  62.         $album $this->getDoctrine()->getRepository(Album::class)->getPortfolio($profile);
  63.         return $this->renderTemplate('media/portfolio/index.html.twig', [
  64.             'media' => $media,
  65.             'albums' => $albums,
  66.             'profile' => $profile,
  67.             'owner' => $owner,
  68.             'album' => $album,
  69.         ], [
  70.             '%profile%' => $profile->getName(),
  71.         ]);
  72.     }
  73.     /**
  74.      * @Route("/album/{album}", priority="-1", name="portfolio_album", methods="GET|POST")
  75.      * @ParamConverter("album", class="App\Entity\Media\Album", options={"mapping": {"album": "slug"}})
  76.      */
  77.     public function album(
  78.         Request $request
  79.         Profile $profile,
  80.         Album $album,
  81.         SettingsManager $settingsManager
  82.     ): Response
  83.     {
  84.         $configs $settingsManager->get([
  85.             SettingsManager::MEDIA_ALBUMS_GROUPS,
  86.         ], true);
  87.         if (!$profile->getUser()->getGroups(json_decode($configs[SettingsManager::MEDIA_ALBUMS_GROUPS], true))->count()) {
  88.             throw new AccessDeniedHttpException($this->translator->trans('You can\'t view album, because user can\'t create it', [], 'media_album'));
  89.         }
  90.         $owner $this->isGranted('ROLE_ADMIN') || $this->getUser() == $profile->getUser();
  91.         $media $this->getDoctrine()->getRepository(Media::class)->getAlbumList($album$owner);
  92.         return $this->renderTemplate('media/portfolio/album.html.twig', [
  93.             'media' => $media,
  94.             'profile' => $profile,
  95.             'owner' => $owner,
  96.             'album' => $album,
  97.         ], [
  98.             '%profile%' => $profile->getName(),
  99.             '%album%' => $album->getTitle(),
  100.         ]);
  101.     }
  102.     /**
  103.      * @Route("/credited", priority="-1", name="portfolio_credited", methods="GET|POST")
  104.      */
  105.     public function credited(
  106.         Request $request
  107.         Profile $profile
  108.     ): Response
  109.     {
  110.         $owner $this->isGranted('ROLE_ADMIN') || $this->getUser() == $profile->getUser();
  111.         $media $this->getDoctrine()->getRepository(Media::class)->getCreditedList($profile);
  112.         return $this->renderTemplate('media/portfolio/credited.html.twig', [
  113.             'media' => $media,
  114.             'profile' => $profile,
  115.             'owner' => $owner,
  116.         ], [
  117.             '%profile%' => $profile->getName(),
  118.         ]);
  119.     }
  120.     /**
  121.      * @Route("/video/{media}", priority="-1", name="portfolio_video", methods="GET|POST")
  122.      * @ParamConverter("video", class="App\Entity\Media\Video", options={"mapping": {"media": "hash"}})
  123.      */
  124.     public function video(
  125.         Request $request
  126.         Profile $profile,
  127.         Video $video
  128.     ): Response
  129.     {
  130.         $user $this->getUser();
  131.         $owner $user == $video->getProfile()->getUser();
  132.         if (!$owner && !$video->isActive()) {
  133.             throw new AccessDeniedHttpException($this->translator->trans('You can\'t view this video', [], 'media_portfolio'));
  134.         }
  135.         $albumId $request->get('album'null);
  136.         $type $request->get('type'null);
  137.         $prev null;
  138.         $album null;
  139.         $next null;
  140.         $params = [];
  141.         if ($albumId) {
  142.             $album $this->getDoctrine()->getRepository(Album::class)->find($albumId);
  143.             if ($album) {
  144.                 $sortOrder null;
  145.                 $media $album->getMedia();
  146.                 $count count($media);
  147.                 if ($count 1) {
  148.                     foreach ($media as $sort => $item) {
  149.                         if ($item == $video) {
  150.                             $sortOrder $sort;
  151.                             break;
  152.                         }
  153.                     }
  154.                     if ($sortOrder) {
  155.                         if ($sortOrder == 1) {
  156.                             $prev $media[$count];
  157.                         } else {
  158.                             $prev $media[$sortOrder 1];
  159.                         }
  160.                         if ($sortOrder == $count) {
  161.                             $next $media[1];
  162.                         } else {
  163.                             $next $media[$sortOrder 1];
  164.                         }
  165.                     }
  166.                 }
  167.             }
  168.         } elseif ($type) {
  169.             switch ($type) {
  170.                 case 'tags':
  171.                     $tagId $request->get('tag'null);
  172.                     $tag $this->getDoctrine()->getRepository(Tag::class)->find($tagId);
  173.                     if ($tag) {
  174.                         $params['tag'] = $tag->getId();
  175.                         $mediaRepo $this->getDoctrine()->getRepository(Media::class);
  176.                         $prev $mediaRepo->findPrevTagMedia($video$tag);
  177.                         if (!$prev) {
  178.                             $prev $mediaRepo->findLastTagMedia($tag);
  179.                         }
  180.                         $next $mediaRepo->findNextTagMedia($video$tag);
  181.                         if (!$next) {
  182.                             $next $mediaRepo->findFirstTagMedia($tag);
  183.                         }
  184.                     }
  185.                 break;
  186.                 case 'credited':
  187.                     $profileId $request->get('profile'null);
  188.                     $creditedProfile $this->getDoctrine()->getRepository(Profile::class)->find($profileId);
  189.                     if ($creditedProfile) {
  190.                         $params['profile'] = $creditedProfile->getId();
  191.                         $mediaRepo $this->getDoctrine()->getRepository(Media::class);
  192.                         $prev $mediaRepo->findPrevCreditedMedia($video$creditedProfile);
  193.                         if (!$prev) {
  194.                             $prev $mediaRepo->findLastCreditedMedia($creditedProfile);
  195.                         }
  196.                         $next $mediaRepo->findNextCreditedMedia($video$creditedProfile);
  197.                         if (!$next) {
  198.                             $next $mediaRepo->findFirstCreditedMedia($creditedProfile);
  199.                         }
  200.                     }
  201.                 break;
  202.                 default:
  203.                     $mediaRepo $this->getDoctrine()->getRepository(Media::class);
  204.                     $prev $mediaRepo->findPrevLatestMedia($video);
  205.                     if (!$prev) {
  206.                         $prev $mediaRepo->findLastLatestMedia();
  207.                     }
  208.                     $next $mediaRepo->findNextLatestMedia($video);
  209.                     if (!$next) {
  210.                         $next $mediaRepo->findFirstLatestMedia();
  211.                     }
  212.                 break;
  213.             }
  214.         }
  215.         $name Media::TYPE_VIDEO '-' $video->getId();
  216.         if ($request->cookies->get($name) == null) {
  217.             $video->setViews($video->getViews() + 1);
  218.             $this->getDoctrine()->getManager()->flush();
  219.             $expiration = new \DateTimeImmutable();
  220.             $expiration $expiration->modify('+31 days');
  221.             $cookie Cookie::create($name)
  222.                 ->withValue(true)
  223.                 ->withExpires($expiration)
  224.                 ->withSecure(true);
  225.             $res = new Response();
  226.             $res->headers->setCookie($cookie);
  227.             $res->send();
  228.         }
  229.         $safeMode true;
  230.         if ($user) {
  231.             $safeMode $user->isSafeMode();
  232.         } else {
  233.             if ($request->cookies->get('safeMode')) {
  234.                 $safeMode false;
  235.             }
  236.         }
  237.         $credited $user $this->getDoctrine()->getRepository(Profile::class)->checkCredits($video$user) : [];
  238.         return $this->renderTemplate('media/portfolio/video.html.twig', [
  239.             'video' => $video,
  240.             'profile' => $profile,
  241.             'owner' => $owner,
  242.             'prev' => $prev,
  243.             'next' => $next,
  244.             'album' => $album,
  245.             'safeMode' => $safeMode,
  246.             'type' => $type,
  247.             'routeParams' => $params,
  248.             'credited' => $credited,
  249.         ], [
  250.             '%profile%' => $profile->getName(),
  251.         ], [
  252.             'og:image' => $video->getThumb(),
  253.         ]);
  254.     }
  255.     /**
  256.      * @Route("/photo/{media}", priority="-1", name="portfolio_photo", methods="GET|POST")
  257.      * @ParamConverter("photo", class="App\Entity\Media\Photo", options={"mapping": {"media": "hash"}})
  258.      */
  259.     public function photo(
  260.         Request $request
  261.         Profile $profile,
  262.         Photo $photo
  263.     ): Response
  264.     {
  265.         $user $this->getUser();
  266.         $owner $user == $photo->getProfile()->getUser();
  267.         if (!$owner && !$photo->isActive()) {
  268.             throw new AccessDeniedHttpException($this->translator->trans('You can\'t view this photo', [], 'media_portfolio'));
  269.         }
  270.         $albumId $request->get('album'null);
  271.         $type $request->get('type'null);
  272.         $prev null;
  273.         $album null;
  274.         $next null;
  275.         $params = [];
  276.         if ($albumId) {
  277.             $album $this->getDoctrine()->getRepository(Album::class)->find($albumId);
  278.             if ($album) {
  279.                 $sortOrder null;
  280.                 $media $album->getMedia();
  281.                 $count count($media);
  282.                 if ($count 1) {
  283.                     foreach ($media as $sort => $item) {
  284.                         if ($item == $photo) {
  285.                             $sortOrder $sort;
  286.                             break;
  287.                         }
  288.                     }
  289.                     if ($sortOrder) {
  290.                         if ($sortOrder == 1) {
  291.                             $prev $media[$count];
  292.                         } else {
  293.                             $prev $media[$sortOrder 1];
  294.                         }
  295.                         if ($sortOrder == $count) {
  296.                             $next $media[1];
  297.                         } else {
  298.                             $next $media[$sortOrder 1];
  299.                         }
  300.                     }
  301.                 }
  302.             }
  303.         } elseif ($type) {
  304.             switch ($type) {
  305.                 case 'tags':
  306.                     $tagId $request->get('tag'null);
  307.                     $tag $this->getDoctrine()->getRepository(Tag::class)->find($tagId);
  308.                     if ($tag) {
  309.                         $params['tag'] = $tag->getId();
  310.                         $mediaRepo $this->getDoctrine()->getRepository(Media::class);
  311.                         $prev $mediaRepo->findPrevTagMedia($photo$tag);
  312.                         if (!$prev) {
  313.                             $prev $mediaRepo->findLastTagMedia($tag);
  314.                         }
  315.                         $next $mediaRepo->findNextTagMedia($photo$tag);
  316.                         if (!$next) {
  317.                             $next $mediaRepo->findFirstTagMedia($tag);
  318.                         }
  319.                     }
  320.                 break;
  321.                 case 'credited':
  322.                     $profileId $request->get('profile'null);
  323.                     $creditedProfile $this->getDoctrine()->getRepository(Profile::class)->find($profileId);
  324.                     if ($creditedProfile) {
  325.                         $params['profile'] = $creditedProfile->getId();
  326.                         $mediaRepo $this->getDoctrine()->getRepository(Media::class);
  327.                         $prev $mediaRepo->findPrevCreditedMedia($photo$creditedProfile);
  328.                         if (!$prev) {
  329.                             $prev $mediaRepo->findLastCreditedMedia($creditedProfile);
  330.                         }
  331.                         $next $mediaRepo->findNextCreditedMedia($photo$creditedProfile);
  332.                         if (!$next) {
  333.                             $next $mediaRepo->findFirstCreditedMedia($creditedProfile);
  334.                         }
  335.                     }
  336.                 break;
  337.                 default:
  338.                     $mediaRepo $this->getDoctrine()->getRepository(Media::class);
  339.                     $prev $mediaRepo->findPrevLatestMedia($photo);
  340.                     if (!$prev) {
  341.                         $prev $mediaRepo->findLastLatestMedia();
  342.                     }
  343.                     $next $mediaRepo->findNextLatestMedia($photo);
  344.                     if (!$next) {
  345.                         $next $mediaRepo->findFirstLatestMedia();
  346.                     }
  347.                 break;
  348.             }
  349.         }
  350.         $name Media::TYPE_PHOTO '-' $photo->getId();
  351.         if ($request->cookies->get($name) == null) {
  352.             $photo->setViews($photo->getViews() + 1);
  353.             $this->getDoctrine()->getManager()->flush();
  354.             $expiration = new \DateTimeImmutable();
  355.             $expiration $expiration->modify('+31 days');
  356.             $cookie Cookie::create($name)
  357.                 ->withValue(true)
  358.                 ->withExpires($expiration)
  359.                 ->withSecure(true);
  360.             $res = new Response();
  361.             $res->headers->setCookie($cookie);
  362.             $res->send();
  363.         }
  364.         $safeMode true;
  365.         if ($user) {
  366.             $safeMode $user->isSafeMode();
  367.         } else {
  368.             if ($request->cookies->get('safeMode')) {
  369.                 $safeMode false;
  370.             }
  371.         }
  372.         $credited $user $this->getDoctrine()->getRepository(Profile::class)->checkCredits($photo$user) : [];
  373.         $image $request->getSchemeAndHttpHost() . '/uploads/portfolio/photos/' Image::getDirPath($photo->getId()) . '/' $photo->getFilename();
  374.         return $this->renderTemplate('media/portfolio/photo.html.twig', [
  375.             'photo' => $photo,
  376.             'profile' => $profile,
  377.             'owner' => $owner,
  378.             'prev' => $prev,
  379.             'next' => $next,
  380.             'album' => $album,
  381.             'safeMode' => $safeMode,
  382.             'type' => $type,
  383.             'routeParams' => $params,
  384.             'credited' => $credited,
  385.         ], [
  386.             '%profile%' => $profile->getName(),
  387.         ], [
  388.             'og:image' => $image,
  389.         ]);
  390.     }
  391. }