<?php
namespace App\Controller;
use App\Entity\Image;
use App\Entity\ImageLang;
use App\Repository\ImageLangRepository;
use App\Repository\ImageRepository;
use App\Repository\LangRepository;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\String\Slugger\SluggerInterface;
#[Route('/image', name: 'app_image')]
class ImageController extends AbstractController
{
private $em = null;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
#[Route('/{imageName}', name: '_display', methods:['GET'])]
public function display($imageName, Request $request, SluggerInterface $slugger, ImageRepository $imageRepository): Response
{
$image = $imageRepository->findOneBy(['name'=>$imageName]);
if($image->getProduct() != false){
$filename = $this->getParameter('general_image_directory') . "/products/" . $imageName;
}else{
$filename = $this->getParameter('wysiwyg_image_directory') . "/" . $imageName;
}
if (file_exists($filename)) {
//return a new BinaryFileResponse with the file name
return new BinaryFileResponse($filename);
} else {
return new JsonResponse(null, 404);
}
}
#[IsGranted('ROLE_CONTENT_MANAGER')]
#[Route('/add', name: '_add', methods:['POST'])]
public function add(Request $request, SluggerInterface $slugger, ImageRepository $imageRepository, LangRepository $langRepository, ImageLangRepository $imageLangRepository): Response
{
$file = $request->files->get('image');
if ($file) {
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
// this is needed to safely include the file name as part of the URL
$safeFilename = $slugger->slug($originalFilename);
$newFilename = $safeFilename.'-'.uniqid().'.'.$file->guessExtension();
// Move the file to the directory where product image are stored
try {
$file->move(
$this->getParameter('wysiwyg_image_directory'),
$newFilename
);
$image = new Image();
$image->setName($newFilename);
$image->setProduct(null);
$image->setCover(false);
$image->setPosition(0);
$langs = $langRepository->findAll();
foreach($langs as $lang){
$imageLang = new ImageLang();
$imageLang->setLang($lang);
$imageLang->setImage($image);
$imageLang->setDescription('');
$imageLangRepository->add($imageLang);
}
$imageRepository->add($image, true);
return $this->json(['code'=>200, 'success'=>1, 'file'=>['url'=>$this->generateUrl('app_image_display',['imageName'=>$newFilename])]]);
} catch (FileException $e) {
return new Response($e, Response::HTTP_INTERNAL_SERVER_ERROR);
}
}else{
return $this->json(['code'=>Response::HTTP_NO_CONTENT, 'success'=>0]);
}
}
#[IsGranted('ROLE_CONTENT_MANAGER')]
#[Route('/editposition', name: '_editposition', methods:['POST'])]
public function editPosition(Request $request, ImageRepository $imageRepository): Response
{
$data = $request->request->get('data');
$datas = json_decode($data);
foreach($datas as $key => $value){
$image = $imageRepository->find($value->imageId);
$image->setPosition($value->position);
$imageRepository->add($image);
}
$this->em->flush();
return $this->json(['code'=>200, 'success'=>1]);
}
}