<?php
namespace App\Controller;
use App\Entity\Demande;
use App\Entity\DemandeJustificationProtection;
use App\Entity\DemandeNote;
use App\Entity\FormUser;
use App\Entity\Meeting;
use App\Entity\User;
use App\Form\DemandeNoteType;
use App\Form\DemandeType;
use App\Form\MeetingType;
use App\Repository\MeetingRepository;
use App\Repository\UserRepository;
use App\Security\EmailVerifier;
use App\Service\NotificationManager;
use Knp\Snappy\Pdf;
use Kricha\DoctrineAuditBundle\Reader\AuditReader;
use Symfony\Component\Mime\Address;
use App\Entity\DemandePhotoBatiment;
use App\Entity\JustifiantApprobationSpontane;
use App\Entity\JustifiantPropriete;
use App\Repository\DemandeRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\String\Slugger\SluggerInterface;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
#[Route('/demande')]
class DemandeController extends BaseController
{
private EmailVerifier $emailVerifier;
public function __construct(EmailVerifier $emailVerifier)
{
$this->emailVerifier = $emailVerifier;
}
#[Route('/', name: 'app_demande_index', methods: ['GET'])]
public function index(DemandeRepository $demandeRepository): Response
{
$demandes = [];
$isAdmin = false;
if ($this->isGranted('ROLE_ADMIN')) {
$demandes = $demandeRepository->findBy([], ['id' => 'DESC']);
$isAdmin = true;
} else if ($this->isGranted('ROLE_USER')) {
$demandes = $this->getUser()->getDemandes();
$isAdmin = false;
}
return $this->render('demande/index.html.twig', [
'demandes' => $demandes,
'isAdmin' => $isAdmin,
'status_array' => array_flip(FormUser::STATUS_ARRAY),
]);
}
#[Route('/new', name: 'app_demande_new', methods: ['GET', 'POST'])]
public function new(
Request $request,
EntityManagerInterface $entityManager,
SluggerInterface $slugger,
NotificationManager $notificationManager,
): Response
{
$demande = new Demande();
$form = $this->createForm(DemandeType::class, $demande);
$form->handleRequest($request);
if ($form->isSubmitted()/* && $form->isValid() */) {
//dd($form->get('JustificationApprobationSpontane')->getData());
if (!file_exists($this->getParameter('demande_directory'))) {
mkdir($this->getParameter('demande_directory'), 777, true);
}
$demande->setUser($this->getUser());
$demande->setTypeEtude(explode(',',$request->request->get('TypeEtudes')));
$JustificationApprobationSpontane = $form->get('JustificationApprobationSpontane')->getData();
foreach ($JustificationApprobationSpontane as $file) {
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$sourceFileName = $slugger->slug($originalFilename) . '-' . uniqid() . '.' . $file->guessExtension();
try {
copy($file, $this->getParameter('demande_directory') . $sourceFileName);
$JustifiantApprobationSpontane = new JustifiantApprobationSpontane();
$JustifiantApprobationSpontane->setDemande($demande);
$JustifiantApprobationSpontane->setSource($sourceFileName);
$entityManager->persist($JustifiantApprobationSpontane);
} catch (FileException $e) {
dd($e);
}
}
$JustificationDuPropriete = $form->get('JustificationDuPropriete')->getData();
foreach ($JustificationDuPropriete as $file) {
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$sourceFileName = $slugger->slug($originalFilename) . '-' . uniqid() . '.' . $file->guessExtension();
try {
copy($file, $this->getParameter('demande_directory') . $sourceFileName);
$JustifiantPropriete = new JustifiantPropriete();
$JustifiantPropriete->setDemande($demande);
$JustifiantPropriete->setSource($sourceFileName);
$entityManager->persist($JustifiantPropriete);
} catch (FileException $e) {
dd($e);
}
}
$PhotoBatiment = $form->get('PhotoBatiment')->getData();
foreach ($PhotoBatiment as $file) {
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$sourceFileName = $slugger->slug($originalFilename) . '-' . uniqid() . '.' . $file->guessExtension();
try {
copy($file, $this->getParameter('demande_directory') . $sourceFileName);
$DemandePhoto = new DemandePhotoBatiment();
$DemandePhoto->setDemande($demande);
$DemandePhoto->setSource($sourceFileName);
$entityManager->persist($DemandePhoto);
} catch (FileException $e) {
dd($e);
}
}
$JustificationTypeProtection = $form->get('JustificationTypeProtection')->getData();
foreach ($JustificationTypeProtection as $file) {
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$sourceFileName = $slugger->slug($originalFilename) . '-' . uniqid() . '.' . $file->guessExtension();
try {
copy($file, $this->getParameter('demande_directory') . $sourceFileName);
$JustificationProtection = new DemandeJustificationProtection();
$JustificationProtection->setDemande($demande);
$JustificationProtection->setSource($sourceFileName);
$entityManager->persist($JustificationProtection);
} catch (FileException $e) {
dd($e);
}
}
$demande->setDateCreation(new \DateTime());
$entityManager->persist($demande);
$entityManager->flush();
// Message informatif lors de la réception d’une nouvelle demande SuperAdmin
$edit_demande_url = $this->generateUrl('app_redirect_email', array( 'id' => $demande->getId(), 'type' => 'demande' ), UrlGenerator::ABSOLUTE_URL );
$this->emailVerifier->sendEmailConfirmation(
'app_verify_email',
$demande->getUser(),
(new TemplatedEmail())
->from(new Address($this->getParameter('EmailAdmin'), 'IGPPP'))
->to($this->getParameter('EmailNotification'))
->subject('Une nouvelle demande a été ajoutée !')
->htmlTemplate('email/email_nouvelle_demande.html.twig')
->context([
'User' => $demande->getUser()->getEmail(),
'demande_url' => $edit_demande_url,
'demande_id' => $demande->getId(),
])
);
$admins = $entityManager->getRepository(User::class)->findByRole('ROLE_ADMIN');
foreach ( $admins as $admin){
$notification_message = 'Une nouvelle demande a été ajoutée !' . $demande->getId() . ' du formulaire de demande d’assistance technique de l’IGPPP';
$notificationManager->pushMessage('Une nouvelle demande a été ajoutée !', $notification_message, $admin, $edit_demande_url);
}
// Confirmation de réception de la demande Demandeur
$show_demande_url = $this->generateUrl('app_demande_show', array( 'id' => $demande->getId() ), UrlGenerator::ABSOLUTE_URL );
$this->emailVerifier->sendEmailConfirmation(
'app_verify_email',
$demande->getUser(),
(new TemplatedEmail())
->from(new Address($this->getParameter('EmailAdmin'), 'IGPPP'))
->to($demande->getUser()->getEmail())
->subject('Confirmation de réception de la demande !')
->htmlTemplate('email/email_confirmation_creation_demande.html.twig')
->context([
'User' => $demande->getUser()->getEmail(),
'demande_url' => $show_demande_url,
'demande_id' => $demande->getId(),
])
);
$notification_message = 'Confirmation de réception de la demande !' . $demande->getId() . ' du formulaire de demande d’assistance technique de l’IGPPP ';
$notificationManager->pushMessage('Confirmation de réception de la demande !', $notification_message, $demande->getUser(), $show_demande_url);
return $this->redirectToRoute('app_demande_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('demande/new.html.twig', [
'demande' => $demande,
'form' => $form,
'ModeModif' => false
]);
}
#[Route('/{id}', name: 'app_demande_show', methods: ['GET'])]
public function show($id, Request $request, Demande $demande, UserRepository $UserRepository, AuditReader $auditReader, MeetingRepository $meetingRepository): Response
{
$note = new DemandeNote();
$meeting = new Meeting();
$typeProtection = [
1 => 'Classe',
2 => 'Protégé',
3 => 'Aucun type de protection',
0 => 'Autre type de protection'
];
$TypeEtude = [
1 => 'Relévé géometre',
2 => 'Diagnostic',
3 => 'Etudes architecturales',
4 => 'Etudes techniques',
0 => 'Autre'
];
$SecteurProjet = [
1 => 'Culturel',
2 => 'Touristique',
0 => 'Autre'
];
$users = $UserRepository->findByRole('ROLE_RD');
$formNote = $this->createForm(DemandeNoteType::class, $note);
$formMeeting = $this->createForm(MeetingType::class, $meeting);
$demande_audit = $auditReader->getAudits('App\Entity\Demande', $demande->getId() );
if ( $request->query->get('meeting_id') ){
$meeting_demande = $meetingRepository->find( $request->query->get('meeting_id') );
}else{
$meeting_demande = null;
}
return $this->render('demande/show.html.twig', [
'demande' => $demande,
'typeProtection' => $typeProtection,
'TypeEtude' => $TypeEtude,
'SecteurProjet' => $SecteurProjet,
'demande_audit' => $demande_audit,
'users' => $users,
'status_array' => array_flip(Demande::STATUS_ARRAY),
'formNote' => $formNote->createView(),
'formMeeting' => $formMeeting->createView(),
'meeting_demande' => $meeting_demande
]);
}
#[Route('/{id}/edit', name: 'app_demande_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Demande $demande, DemandeRepository $demandeRepository): Response
{
return $this->redirectToRoute('app_demande_index');
$form = $this->createForm(DemandeType::class, $demande);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$demandeRepository->add($demande);
return $this->redirectToRoute('app_demande_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('demande/edit.html.twig', [
'demande' => $demande,
'form' => $form,
'ModeModif' => true
]);
}
#[Route('/{id}', name: 'app_demande_delete', methods: ['POST'])]
public function delete(Request $request, Demande $demande, DemandeRepository $demandeRepository): Response
{
return $this->redirectToRoute('app_demande_index');
if ($this->isCsrfTokenValid('delete' . $demande->getId(), $request->request->get('_token'))) {
/*unlink($this->getParameter('demande_directory') . $demande->getJustificationApprobationSpontane());
unlink($this->getParameter('demande_directory') . $demande->getJustificationDuPropriete());
unlink($this->getParameter('demande_directory') . $demande->getPhotoBatiment());
unlink($this->getParameter('demande_directory') . $demande->getJustificationTypeProtection());
*/
$demandeRepository->remove($demande);
}
return $this->redirectToRoute('app_demande_index', [], Response::HTTP_SEE_OTHER);
}
}