<?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\Controller\Media;
use App\Controller\AbstractController;
use App\Entity\Core\Tag;
use App\Entity\Media\Album;
use App\Entity\Media\AlbumMedia;
use App\Entity\Media\Photo;
use App\Entity\Media\Video;
use App\Entity\Media\Media;
use App\Entity\Profile;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\TranslatableMessage;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\JsonResponse;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use App\Utils\Core\Image;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use App\Settings\SettingsManager;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpFoundation\Cookie;
/**
*
* @Route("/{slug}/portfolio", priority="-1")
*/
class PortfolioController extends AbstractController
{
/**
* @Route("/", priority="-1", name="portfolio", methods="GET|POST")
*/
public function index(
Request $request,
Profile $profile,
SettingsManager $settingsManager
): Response
{
$configs = $settingsManager->get([
SettingsManager::MEDIA_ALBUMS_GROUPS,
SettingsManager::MEDIA_PHOTOS_LIMIT,
SettingsManager::MEDIA_UNLIMITED_GROUPS,
], true);
$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];
}
$owner = $this->isGranted('ROLE_ADMIN') || $this->getUser() == $profile->getUser();
$media = $this->getDoctrine()->getRepository(Media::class)->getPortfolioList($profile, $owner, $limit);
$albums = [];
if ($profile->getUser()->getGroups(json_decode($configs[SettingsManager::MEDIA_ALBUMS_GROUPS], true))->count()) {
$albums = $this->getDoctrine()->getRepository(Album::class)->getProfileAlbums($profile);
}
$album = $this->getDoctrine()->getRepository(Album::class)->getPortfolio($profile);
return $this->renderTemplate('media/portfolio/index.html.twig', [
'media' => $media,
'albums' => $albums,
'profile' => $profile,
'owner' => $owner,
'album' => $album,
], [
'%profile%' => $profile->getName(),
]);
}
/**
* @Route("/album/{album}", priority="-1", name="portfolio_album", methods="GET|POST")
* @ParamConverter("album", class="App\Entity\Media\Album", options={"mapping": {"album": "slug"}})
*/
public function album(
Request $request,
Profile $profile,
Album $album,
SettingsManager $settingsManager
): Response
{
$configs = $settingsManager->get([
SettingsManager::MEDIA_ALBUMS_GROUPS,
], true);
if (!$profile->getUser()->getGroups(json_decode($configs[SettingsManager::MEDIA_ALBUMS_GROUPS], true))->count()) {
throw new AccessDeniedHttpException($this->translator->trans('You can\'t view album, because user can\'t create it', [], 'media_album'));
}
$owner = $this->isGranted('ROLE_ADMIN') || $this->getUser() == $profile->getUser();
$media = $this->getDoctrine()->getRepository(Media::class)->getAlbumList($album, $owner);
return $this->renderTemplate('media/portfolio/album.html.twig', [
'media' => $media,
'profile' => $profile,
'owner' => $owner,
'album' => $album,
], [
'%profile%' => $profile->getName(),
'%album%' => $album->getTitle(),
]);
}
/**
* @Route("/credited", priority="-1", name="portfolio_credited", methods="GET|POST")
*/
public function credited(
Request $request,
Profile $profile
): Response
{
$owner = $this->isGranted('ROLE_ADMIN') || $this->getUser() == $profile->getUser();
$media = $this->getDoctrine()->getRepository(Media::class)->getCreditedList($profile);
return $this->renderTemplate('media/portfolio/credited.html.twig', [
'media' => $media,
'profile' => $profile,
'owner' => $owner,
], [
'%profile%' => $profile->getName(),
]);
}
/**
* @Route("/video/{media}", priority="-1", name="portfolio_video", methods="GET|POST")
* @ParamConverter("video", class="App\Entity\Media\Video", options={"mapping": {"media": "hash"}})
*/
public function video(
Request $request,
Profile $profile,
Video $video
): Response
{
$user = $this->getUser();
$owner = $user == $video->getProfile()->getUser();
if (!$owner && !$video->isActive()) {
throw new AccessDeniedHttpException($this->translator->trans('You can\'t view this video', [], 'media_portfolio'));
}
$albumId = $request->get('album', null);
$type = $request->get('type', null);
$prev = null;
$album = null;
$next = null;
$params = [];
if ($albumId) {
$album = $this->getDoctrine()->getRepository(Album::class)->find($albumId);
if ($album) {
$sortOrder = null;
$media = $album->getMedia();
$count = count($media);
if ($count > 1) {
foreach ($media as $sort => $item) {
if ($item == $video) {
$sortOrder = $sort;
break;
}
}
if ($sortOrder) {
if ($sortOrder == 1) {
$prev = $media[$count];
} else {
$prev = $media[$sortOrder - 1];
}
if ($sortOrder == $count) {
$next = $media[1];
} else {
$next = $media[$sortOrder + 1];
}
}
}
}
} elseif ($type) {
switch ($type) {
case 'tags':
$tagId = $request->get('tag', null);
$tag = $this->getDoctrine()->getRepository(Tag::class)->find($tagId);
if ($tag) {
$params['tag'] = $tag->getId();
$mediaRepo = $this->getDoctrine()->getRepository(Media::class);
$prev = $mediaRepo->findPrevTagMedia($video, $tag);
if (!$prev) {
$prev = $mediaRepo->findLastTagMedia($tag);
}
$next = $mediaRepo->findNextTagMedia($video, $tag);
if (!$next) {
$next = $mediaRepo->findFirstTagMedia($tag);
}
}
break;
case 'credited':
$profileId = $request->get('profile', null);
$creditedProfile = $this->getDoctrine()->getRepository(Profile::class)->find($profileId);
if ($creditedProfile) {
$params['profile'] = $creditedProfile->getId();
$mediaRepo = $this->getDoctrine()->getRepository(Media::class);
$prev = $mediaRepo->findPrevCreditedMedia($video, $creditedProfile);
if (!$prev) {
$prev = $mediaRepo->findLastCreditedMedia($creditedProfile);
}
$next = $mediaRepo->findNextCreditedMedia($video, $creditedProfile);
if (!$next) {
$next = $mediaRepo->findFirstCreditedMedia($creditedProfile);
}
}
break;
default:
$mediaRepo = $this->getDoctrine()->getRepository(Media::class);
$prev = $mediaRepo->findPrevLatestMedia($video);
if (!$prev) {
$prev = $mediaRepo->findLastLatestMedia();
}
$next = $mediaRepo->findNextLatestMedia($video);
if (!$next) {
$next = $mediaRepo->findFirstLatestMedia();
}
break;
}
}
$name = Media::TYPE_VIDEO . '-' . $video->getId();
if ($request->cookies->get($name) == null) {
$video->setViews($video->getViews() + 1);
$this->getDoctrine()->getManager()->flush();
$expiration = new \DateTimeImmutable();
$expiration = $expiration->modify('+31 days');
$cookie = Cookie::create($name)
->withValue(true)
->withExpires($expiration)
->withSecure(true);
$res = new Response();
$res->headers->setCookie($cookie);
$res->send();
}
$safeMode = true;
if ($user) {
$safeMode = $user->isSafeMode();
} else {
if ($request->cookies->get('safeMode')) {
$safeMode = false;
}
}
$credited = $user ? $this->getDoctrine()->getRepository(Profile::class)->checkCredits($video, $user) : [];
return $this->renderTemplate('media/portfolio/video.html.twig', [
'video' => $video,
'profile' => $profile,
'owner' => $owner,
'prev' => $prev,
'next' => $next,
'album' => $album,
'safeMode' => $safeMode,
'type' => $type,
'routeParams' => $params,
'credited' => $credited,
], [
'%profile%' => $profile->getName(),
], [
'og:image' => $video->getThumb(),
]);
}
/**
* @Route("/photo/{media}", priority="-1", name="portfolio_photo", methods="GET|POST")
* @ParamConverter("photo", class="App\Entity\Media\Photo", options={"mapping": {"media": "hash"}})
*/
public function photo(
Request $request,
Profile $profile,
Photo $photo
): Response
{
$user = $this->getUser();
$owner = $user == $photo->getProfile()->getUser();
if (!$owner && !$photo->isActive()) {
throw new AccessDeniedHttpException($this->translator->trans('You can\'t view this photo', [], 'media_portfolio'));
}
$albumId = $request->get('album', null);
$type = $request->get('type', null);
$prev = null;
$album = null;
$next = null;
$params = [];
if ($albumId) {
$album = $this->getDoctrine()->getRepository(Album::class)->find($albumId);
if ($album) {
$sortOrder = null;
$media = $album->getMedia();
$count = count($media);
if ($count > 1) {
foreach ($media as $sort => $item) {
if ($item == $photo) {
$sortOrder = $sort;
break;
}
}
if ($sortOrder) {
if ($sortOrder == 1) {
$prev = $media[$count];
} else {
$prev = $media[$sortOrder - 1];
}
if ($sortOrder == $count) {
$next = $media[1];
} else {
$next = $media[$sortOrder + 1];
}
}
}
}
} elseif ($type) {
switch ($type) {
case 'tags':
$tagId = $request->get('tag', null);
$tag = $this->getDoctrine()->getRepository(Tag::class)->find($tagId);
if ($tag) {
$params['tag'] = $tag->getId();
$mediaRepo = $this->getDoctrine()->getRepository(Media::class);
$prev = $mediaRepo->findPrevTagMedia($photo, $tag);
if (!$prev) {
$prev = $mediaRepo->findLastTagMedia($tag);
}
$next = $mediaRepo->findNextTagMedia($photo, $tag);
if (!$next) {
$next = $mediaRepo->findFirstTagMedia($tag);
}
}
break;
case 'credited':
$profileId = $request->get('profile', null);
$creditedProfile = $this->getDoctrine()->getRepository(Profile::class)->find($profileId);
if ($creditedProfile) {
$params['profile'] = $creditedProfile->getId();
$mediaRepo = $this->getDoctrine()->getRepository(Media::class);
$prev = $mediaRepo->findPrevCreditedMedia($photo, $creditedProfile);
if (!$prev) {
$prev = $mediaRepo->findLastCreditedMedia($creditedProfile);
}
$next = $mediaRepo->findNextCreditedMedia($photo, $creditedProfile);
if (!$next) {
$next = $mediaRepo->findFirstCreditedMedia($creditedProfile);
}
}
break;
default:
$mediaRepo = $this->getDoctrine()->getRepository(Media::class);
$prev = $mediaRepo->findPrevLatestMedia($photo);
if (!$prev) {
$prev = $mediaRepo->findLastLatestMedia();
}
$next = $mediaRepo->findNextLatestMedia($photo);
if (!$next) {
$next = $mediaRepo->findFirstLatestMedia();
}
break;
}
}
$name = Media::TYPE_PHOTO . '-' . $photo->getId();
if ($request->cookies->get($name) == null) {
$photo->setViews($photo->getViews() + 1);
$this->getDoctrine()->getManager()->flush();
$expiration = new \DateTimeImmutable();
$expiration = $expiration->modify('+31 days');
$cookie = Cookie::create($name)
->withValue(true)
->withExpires($expiration)
->withSecure(true);
$res = new Response();
$res->headers->setCookie($cookie);
$res->send();
}
$safeMode = true;
if ($user) {
$safeMode = $user->isSafeMode();
} else {
if ($request->cookies->get('safeMode')) {
$safeMode = false;
}
}
$credited = $user ? $this->getDoctrine()->getRepository(Profile::class)->checkCredits($photo, $user) : [];
$image = $request->getSchemeAndHttpHost() . '/uploads/portfolio/photos/' . Image::getDirPath($photo->getId()) . '/' . $photo->getFilename();
return $this->renderTemplate('media/portfolio/photo.html.twig', [
'photo' => $photo,
'profile' => $profile,
'owner' => $owner,
'prev' => $prev,
'next' => $next,
'album' => $album,
'safeMode' => $safeMode,
'type' => $type,
'routeParams' => $params,
'credited' => $credited,
], [
'%profile%' => $profile->getName(),
], [
'og:image' => $image,
]);
}
}