<?php
/*
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential.
*
* @author Bilel AZRI <azri.bilel@gmail.com>
* @author Assma BEN SASSI <bensassiasma.bws@gmail.com>
*
* Bicking man (c) 2019-present.
*/
declare(strict_types=1);
namespace App\Controller\Back;
use App\Entity\Media\Media;
use App\Entity\User\User;
use App\Repository\Media\MediaRepository;
use App\Repository\User\UserRepository;
use App\Repository\Company\CompanyRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use App\Event\Media\MediaPreRemoveEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;
/**
* @Route("/media", name="media.")
*/
final class MediaController
{
private EntityManagerInterface $entityManager;
private MediaRepository $repository;
private UserRepository $userRepository;
private CompanyRepository $companyRepository;
private EventDispatcherInterface $dispatcher;
private Environment $twig;
private UrlGeneratorInterface $urlGenerator;
/// asmaaaaaa A terminer getUser from image ID
public function __construct(EntityManagerInterface $entityManager, EventDispatcherInterface $dispatcher,
MediaRepository $repository, UserRepository $userRepository, UrlGeneratorInterface $urlGenerator,
CompanyRepository $companyRepository, Environment $twig, FlashBagInterface $flashBag)
{
$this->entityManager = $entityManager;
$this->repository = $repository;
$this->userRepository = $userRepository;
$this->companyRepository = $companyRepository;
$this->dispatcher = $dispatcher;
$this->twig = $twig;
$this->flashBag = $flashBag;
$this->urlGenerator = $urlGenerator;
}
/**
* @Route("/", name="index", methods={"GET"}, options={"expose": true})
*/
public function index(Request $request): Response
{
$companies = $this->repository->findAll();
return new Response($this->twig->render('back/media/index.html.twig', ['companies' => $companies]));
}
/**
* @Route("/{id}", name="show", methods={"GET"}, options={"expose": true})
*/
public function show(Media $media): Response
{
$file = $media->getFile();
//var_dump($file->getPathname());exit();
if ($file instanceof File) {
return new BinaryFileResponse($file->getPathname());
}
throw new FileNotFoundException('File not found!');
}
/**
* @Route("/{id}", name="getUserPhoto", methods={"GET"}, options={"expose": true})
*/
public function getUserPhoto(Request $request): Response
{
//$file = $media->getFile();
//$documentsPending = $this->userRepository->findBy(['photo' => 2]);
//var_dump($request->get('id'));exit();
$user = $this->userRepository ->findOneByphoto($request->get('id'));
$company = $this->companyRepository ->findOneBy(['logo' => $request->get('id')]);
if($user != null){
$nameUser = $user->fullName();
return new Response($nameUser);
}elseif($company != null){
$nameUser = $company->getName();
return new Response($nameUser);
}else{
return new Response(null);
}
//throw new FileNotFoundException('File not found!');
}
/**
* @Route("/{id}/activate", name="activate", methods={"GET", "POST"})
*/
public function activate(Request $request, Media $media): Response
{
$media = $this->entityManager->getRepository(Media::class)->find($request->get('media'));
if (null != $media) {
if ($media->getValidate() == 0) {
$media->setValidate(true);
$this->entityManager->flush();
$this->flashBag->add('success', "L'image a été activé avec succès.");
} else {
$media->setValidate(false);
$this->entityManager->flush();
$this->flashBag->add('success', "L'image a été désactivé avec succès.");
}
} else {
$this->flashBag->add('warning', 'Oups! quelque chose a mal tourné ');
}
return new RedirectResponse($this->urlGenerator->generate('back.media.index'));
}
/**
* @Route("/{id}", name="deleteMedia", methods={"DELETE"})
*/
public function deleteMedia(Request $request, Media $media): Response
{
$event = new MediaPreRemoveEvent($media);
/** @psalm-suppress TooManyArguments */
$this->dispatcher->dispatch($event, MediaPreRemoveEvent::NAME);
$this->entityManager->remove($media);
$this->entityManager->flush();
$this->flashBag->add('success', 'Media supprimé avec succès!');
return new RedirectResponse($this->urlGenerator->generate('back.media.index'));
}
/**
* @Route("/getStatutImage/{id}", name="getStatutImage", methods={"GET"}, options={"expose": true})
*/
public function getStatutImage(Request $request): Response
{
$media = $this->entityManager->getRepository(Media::class)->find($request->get('id'));
$userPhoto = $this->entityManager->getRepository(User::class) ->findOneBy(['photo' => $request->get('id')]);
$userCover = $this->entityManager->getRepository(User::class) ->findOneBy(['coverPhoto' => $request->get('id')]);
if($userPhoto != null){
$media->setValidate(true);
$this->entityManager->flush();
}
if($userCover != null){
$media->setValidate(true);
$this->entityManager->flush();
}
$image = $this->repository ->find($request->get('id'));
if($image != null){
if($image->getValidate() == 1){
return new Response('validate');
}else{
return new Response(null);
}
}else{
return new Response(null);
}
}
/**
* @Route("/{id}/delete", name="delete", methods={"GET"}, options={"expose": true})
*/
public function delete(Media $media): JsonResponse
{
$event = new MediaPreRemoveEvent($media);
/** @psalm-suppress TooManyArguments */
$this->dispatcher->dispatch($event, MediaPreRemoveEvent::NAME);
$this->entityManager->remove($media);
$this->entityManager->flush();
return new JsonResponse(['status' => 'success', 'message' => 'media successffuly deleted!']);
}
}