<?php
namespace App\Controller;
use App\Entity\FilesUser;
use App\Form\FilesUserType;
use App\Repository\FilesUserRepository;
use App\Repository\InformationEntrepriseRepository;
use App\Services\XslService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/files/user')]
class FilesUserController extends AbstractController
{
public function __construct(private InformationEntrepriseRepository $informationEntrepriseRepository)
{
}
#[Route('/new', name: 'app_files_user_index', methods: ['GET', 'POST'])]
public function new(Request $request, FilesUserRepository $filesUserRepository): Response
{
$filesUser = new FilesUser();
$form = $this->createForm(FilesUserType::class, $filesUser);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$filesUser
->setUser($this->getUser())
->setIsPaid(false)
->setCreatedAt(new \DateTimeImmutable('now'))
->setIsReadyToDownload(false)
->setIsFileVerified(false)
->setHaveData(false);
$filesUserRepository->save($filesUser, true);
return $this->redirectToRoute('app_user_show', ['email' => $this->getUser()->getUserIdentifier()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('files_user/new.html.twig', [
'files_user' => $filesUser,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_files_user_show', methods: ['GET'])]
public function show(FilesUser $filesUser): Response
{
$data = $this->informationEntrepriseRepository->findByFilesUser($filesUser);
return $this->render('files_user/show.html.twig', [
'files_user' => $filesUser,
'data' => $data
]);
}
#[Route('/{id}/edit', name: 'app_files_user_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, FilesUser $filesUser, FilesUserRepository $filesUserRepository): Response
{
$form = $this->createForm(FilesUserType::class, $filesUser);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$filesUserRepository->save($filesUser, true);
return $this->redirectToRoute('app_files_user_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('files_user/edit.html.twig', [
'files_user' => $filesUser,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_files_user_delete', methods: ['POST'])]
public function delete(Request $request, FilesUser $filesUser, FilesUserRepository $filesUserRepository): Response
{
if ($this->isCsrfTokenValid('delete' . $filesUser->getId(), $request->request->get('_token'))) {
$filesUserRepository->remove($filesUser, true);
}
return $this->redirectToRoute('app_files_user_index', [], Response::HTTP_SEE_OTHER);
}
#[Route('/{id}/paid', name: 'app_files_user_paid')]
public function paid(FilesUser $filesUser, EntityManagerInterface $entityManager)
{
if ($filesUser) {
$filesUser->setIsPaid(true);
$entityManager->persist($filesUser);
$entityManager->flush();
}
# Redirect the user to their show page
return $this->redirectToRoute('app_user_show', ['email' => $this->getUser()->getUserIdentifier()]);
}
#[Route('/{id}/download', name: 'app_files_user_download')]
public function downloadXsl(int $id, FilesUserRepository $filesUserRepository, XslService $xslService)
{
set_time_limit(0);
$results = $this->informationEntrepriseRepository->getDataByFilesUser($filesUserRepository->find($id));
# Check if the file exists
if ($results) {
// # Prepare the data for the file
$preparedData = $xslService->prepareData($results);
# Create the file using the xslService and the prepared data
$xslService->createFile($preparedData);
}
# Redirect the user to their show page
return $this->redirectToRoute('app_user_show', ['email' => $this->getUser()->getUserIdentifier()]);
}
}