<?php
namespace App\Controller;
use App\Entity\DamFile;
use App\Entity\Product;
use App\Repository\DamCategoryRepository;
use App\Repository\DamFileRepository;
use App\Repository\DamGroupRepository;
use App\Repository\ProductLangRepository;
use App\Repository\ProductRepository;
use App\Services\ZipService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Security\Core\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\String\Slugger\SluggerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
#[Route('/dam/file', name: 'app_dam_file')]
#[IsGranted('ROLE_CONTENT_MANAGER')]
class DamFileController extends AbstractController
{
private array $breadcrumb = [];
private array $twigData = [];
private TranslatorInterface $translator;
private Security $security;
public function __construct(Security $security, TranslatorInterface $translator)
{
$this->twigData = [];
$this->translator = $translator;
$this->security = $security;
$this->twigData = [
'user' => $security->getUser(),
];
}
#[Route('/', name: '_index')]
public function index(DamFileRepository $damFileRepository): Response
{
$this->breadcrumb[0]['name'] = $this->translator->trans('page.dam.Manage');
$this->breadcrumb[0]['link'] = $this->generateUrl('app_dam_manage');
$this->breadcrumb[0]['last'] = false;
$this->breadcrumb[1]['name'] = $this->translator->trans('page.damFile.Files');
$this->breadcrumb[1]['last'] = true;
$this->twigData['breadcrumb'] = $this->breadcrumb;
$this->twigData['controller_name'] = "dam";
$files = $damFileRepository->findAll();
$this->twigData['files'] = $files;
return $this->render('dam/file/index.html.twig', $this->twigData);
}
#[Route('/new', name: '_new', methods: ['GET', 'POST'])]
public function new(SluggerInterface $slugger, Request $request, DamFileRepository $damFileRepository, DamGroupRepository $damGroupRepository, DamCategoryRepository $damCategoryRepository): Response
{
$this->breadcrumb[0]['name'] = $this->translator->trans('page.dam.Manage');
$this->breadcrumb[0]['link'] = $this->generateUrl('app_dam_index');
$this->breadcrumb[0]['last'] = false;
$this->breadcrumb[1]['name'] = $this->translator->trans('page.damFile.Files');
$this->breadcrumb[1]['link'] = $this->generateUrl('app_dam_file_index');
$this->breadcrumb[1]['last'] = false;
$this->breadcrumb[2]['name'] = $this->translator->trans('page.damFile.new');
$this->breadcrumb[2]['last'] = true;
$this->twigData['breadcrumb'] = $this->breadcrumb;
$this->twigData['controller_name'] = "dam";
$categories = $damCategoryRepository->findAll();
$categoriesSelect = [];
foreach ($categories as $category) {
$groups = $damGroupRepository->findBy(['damCategory' => $category]);
if(count($groups) > 0) {
$categoriesSelect[$category->getId()]['categories']['name'] = $category->getName();
$categoriesSelect[$category->getId()]['categories']['id'] = $category->getId();
$categoriesSelect[$category->getId()]['groups'] = [];
foreach ($groups as $group) {
$categoriesSelect[$category->getId()]['groups'][$group->getId()]['id'] = $group->getId();
$categoriesSelect[$category->getId()]['groups'][$group->getId()]['name'] = $group->getName();
}
}
}
$this->twigData['categoriesSelect'] = $categoriesSelect;
$this->twigData['categories'] = $categories;
$submit = $request->get("submit");
if ($submit == 1) {
$file = $request->files->get('file');
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 file are stored
try {
$file->move(
$this->getParameter('dam_file_directory'),
$newFilename
);
$fileNameDisplay = $request->request->get('name');
$fileFormDatas = $request->request->all();
$damFile = new DamFile();
$damFile->setName($newFilename);
$damFile->setDisplayName($fileNameDisplay);
$damFile->setDateAdd(new \DateTime());
$damFile->setDateUpd(new \DateTime());
$damGroupIds = $fileFormDatas['damGroups'];
foreach ($damGroupIds as $key => $damGroupId) {
$damGroup = $damGroupRepository->find($damGroupId);
$damFile->addDamGroup($damGroup);
}
$damFileRepository->add($damFile, true);
} catch (FileException $e) {
return new Response($e, Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
return $this->redirectToRoute('app_dam_file_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('dam/file/new.html.twig', $this->twigData);
}
#[Route('/new-multiple', name: '_new_multiple', methods: ['GET', 'POST'])]
public function newMultiple(SluggerInterface $slugger, DamCategoryRepository $damCategoryRepository, DamGroupRepository $damGroupRepository, Request $request, DamFileRepository $damFileRepository): Response
{
$this->breadcrumb[0]['name'] = $this->translator->trans('page.dam.Manage');
$this->breadcrumb[0]['link'] = $this->generateUrl('app_dam_index');
$this->breadcrumb[0]['last'] = false;
$this->breadcrumb[1]['name'] = $this->translator->trans('page.damFile.Files');
$this->breadcrumb[1]['link'] = $this->generateUrl('app_dam_file_index');
$this->breadcrumb[1]['last'] = false;
$this->breadcrumb[2]['name'] = $this->translator->trans('page.damFile.new multiple');
$this->breadcrumb[2]['last'] = true;
$this->twigData['breadcrumb'] = $this->breadcrumb;
$this->twigData['controller_name'] = "dam";
$categories = $damCategoryRepository->findAll();
$categoriesSelect = [];
foreach ($categories as $category) {
$groups = $damGroupRepository->findBy(['damCategory' => $category]);
if(count($groups) > 0) {
$categoriesSelect[$category->getId()]['categories']['name'] = $category->getName();
$categoriesSelect[$category->getId()]['categories']['id'] = $category->getId();
$categoriesSelect[$category->getId()]['groups'] = [];
foreach ($groups as $group) {
$categoriesSelect[$category->getId()]['groups'][$group->getId()]['id'] = $group->getId();
$categoriesSelect[$category->getId()]['groups'][$group->getId()]['name'] = $group->getName();
}
}
}
$this->twigData['categoriesSelect'] = $categoriesSelect;
$this->twigData['categories'] = $categories;
$submit = $request->get("submit");
if ($submit == 1) {
$files = $request->files->all();
$files = $files['files'];
foreach($files as $keyFile => $file) {
$clientFileName = $file->getClientOriginalName();
$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 file are stored
try {
$file->move(
$this->getParameter('dam_file_directory'),
$newFilename
);
$fileNameDisplay = $request->request->get('name');
$fileFormDatas = $request->request->all();
$damFile = new DamFile();
$damFile->setName($newFilename);
$damFile->setDisplayName($clientFileName);
$damFile->setDateAdd(new \DateTime());
$damFile->setDateUpd(new \DateTime());
$damGroupIds = $fileFormDatas['damGroups'];
foreach ($damGroupIds as $key => $damGroupId) {
$damGroup = $damGroupRepository->find($damGroupId);
$damFile->addDamGroup($damGroup);
}
$damFileRepository->add($damFile, true);
} catch (FileException $e) {
return new Response($e, Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
}
return $this->render('dam/file/new-multiple.html.twig', $this->twigData);
}
#[Route('/{id}/edit', name: '_edit', methods: ['GET', 'POST'])]
public function edit(DamFile $damFile, Request $request, SluggerInterface $slugger, DamGroupRepository $damGroupRepository, DamFileRepository $damFileRepository, DamCategoryRepository $damCategoryRepository): Response
{
$this->breadcrumb[0]['name'] = $this->translator->trans('page.dam.Manage');
$this->breadcrumb[0]['link'] = $this->generateUrl('app_dam_manage');
$this->breadcrumb[0]['last'] = false;
$this->breadcrumb[1]['name'] = $this->translator->trans('page.damFile.Files');
$this->breadcrumb[1]['link'] = $this->generateUrl('app_dam_file_index');
$this->breadcrumb[1]['last'] = false;
$this->breadcrumb[2]['name'] = $this->translator->trans('page.damFile.edit');
$this->breadcrumb[2]['last'] = true;
$this->twigData['breadcrumb'] = $this->breadcrumb;
$this->twigData['controller_name'] = "dam";
$categories = $damCategoryRepository->findAll();
$this->twigData['categories'] = $categories;
$this->twigData['file'] = $damFile;
$categories = $damCategoryRepository->findAll();
$categoriesSelect = [];
foreach ($categories as $category) {
$groups = $damGroupRepository->findBy(['damCategory' => $category]);
if(count($groups) > 0) {
$categoriesSelect[$category->getId()]['categories']['name'] = $category->getName();
$categoriesSelect[$category->getId()]['categories']['id'] = $category->getId();
$categoriesSelect[$category->getId()]['groups'] = [];
foreach ($groups as $group) {
$categoriesSelect[$category->getId()]['groups'][$group->getId()]['id'] = $group->getId();
$categoriesSelect[$category->getId()]['groups'][$group->getId()]['name'] = $group->getName();
//check if group is selected among the groups of the file
if($damFile->getDamGroups()->contains($group)) {
$categoriesSelect[$category->getId()]['groups'][$group->getId()]['selected'] = true;
} else {
$categoriesSelect[$category->getId()]['groups'][$group->getId()]['selected'] = false;
}
}
}
}
$this->twigData['categoriesSelect'] = $categoriesSelect;
$submit = $request->get("submit");
if ($submit == 1) {
$file = $request->files->get('file');
$fileNameDisplay = $request->request->get('name');
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 file are stored
try {
$fileName = $damFile->getName();
$filePath = $this->getParameter('dam_file_directory') . '/' . $fileName;
try {
unlink($filePath);
} catch (\Throwable $th) {
dd($th);
}
$file->move(
$this->getParameter('dam_file_directory'),
$newFilename
);
$damFile->setName($newFilename);
$damFile->setDisplayName($fileNameDisplay);
} catch (FileException $e) {
return new Response($e, Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
$fileFormDatas = $request->request->all();
$damFile->setDateUpd(new \DateTime());
$damFile->getDamGroups()->clear();
$damGroupIds = $fileFormDatas['damGroups'];
foreach ($damGroupIds as $key => $damGroupId) {
$damGroup = $damGroupRepository->find($damGroupId);
$damFile->addDamGroup($damGroup);
}
$damFileRepository->add($damFile, true);
return $this->redirectToRoute('app_dam_file_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('dam/file/edit.html.twig', $this->twigData);
}
#[Route('/{id}', name: '_delete', methods: ['POST'])]
public function delete(DamFile $damFile, DamFileRepository $damFileRepository, Request $request): Response
{
if ($this->isCsrfTokenValid('delete'.$damFile->getId(), $request->request->get('_token'))) {
$fileName = $damFile->getName();
$damFileRepository->remove($damFile, true);
$filePath = $this->getParameter('dam_file_directory') . '/' . $fileName;
try {
unlink($filePath);
} catch (\Throwable $th) {
dd($th);
}
}
return $this->redirectToRoute('app_dam_file_index', [], Response::HTTP_SEE_OTHER);
}
}