src/Controller/ProjetController.php line 145

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Projet;
  4. use App\Form\ProjetType;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\String\Slugger\SluggerInterface;
  12. class ProjetController extends AbstractController
  13. {
  14.     public function __construct(EntityManagerInterface $manager)
  15.     {
  16.         $this->manager $manager;
  17.     }
  18.     /**
  19.      * @Route("/projet", name="app_projet")
  20.      */
  21.     public function index(Request $requestSluggerInterface $sluger): Response
  22.     {
  23.         // Creation de l'instance Projet
  24.         $projet = new Projet();
  25.         // Création du formulaire à partir d'une classe issu du fichier ProjetType.php
  26.         $formProjet $this->createForm(ProjetType::class, $projet);
  27.         // Traitement du formulaire
  28.         $formProjet->handleRequest($request);
  29.         // Si le formulaire est soumis et validé , alors...
  30.         if ($formProjet->isSubmitted() && $formProjet->isValid()) {
  31.             // On recupere dans une variable $photoProjet le contenu de l'image stocké 
  32.             $photoProjet $formProjet->get('lien')->getData();
  33.             if ($photoProjet) {
  34.                 $originalFilename pathinfo($photoProjet->getClientOriginalName(), PATHINFO_FILENAME);
  35.                 $safeFilename $sluger->slug($originalFilename);
  36.                 $newFilename $safeFilename '-' uniqid() . '.' $photoProjet->guessExtension();
  37.                 try {
  38.                     $photoProjet->move(
  39.                         $this->getParameter('lien'),
  40.                         $newFilename
  41.                     );
  42.                 } catch (FileException $e) {
  43.                 }
  44.                 $projet->setLien($newFilename);
  45.             } else {
  46.                 dd('Aucune photo de projet disponible');
  47.             }
  48.             // $projet->setPublication(new \datetime);
  49.             // $projet->setAuteur($this->getUser()->getNomComplet());
  50.             // On persiste le projet
  51.             $this->manager->persist($projet);
  52.             // On le flush
  53.             $this->manager->flush();
  54.         }
  55.         return $this->render('projet/index.html.twig', [
  56.             // Matérialise l'affichage du formulaire
  57.             'myProject' => $formProjet->createView(),
  58.         ]);
  59.     }
  60.     /**
  61.      * @Route("/projet/delete/{id}", name="app_projet_delete")
  62.      */
  63.     public function projetDelete(Projet $projetRequest $request): Response
  64.     {
  65.         // On efface le projet
  66.         $this->manager->remove($projet);
  67.         // On flush 
  68.         $this->manager->flush();
  69.         return $this->redirectToRoute('app_home');
  70.     }
  71.     /* ------------------ Modifier le projet ---------------- */
  72.     /**
  73.      * @Route("/projet/edit/{id}", name="app_projet_edit")
  74.      */
  75.     public function projetEdit(Projet $projetModifSluggerInterface $sluggerRequest $request): Response
  76.     {
  77.         $projetFormModif $this->createForm(ProjetType::class, $projetModif);
  78.         // Traitement du formulaire
  79.         $projetFormModif->handleRequest($request);
  80.         // Si le formulaire est soumis et validé , alors...
  81.         if ($projetFormModif->isSubmitted() && $projetFormModif->isValid()) {
  82.             //  On recupere dans une variable $photoProjet le contenu de l'image stocké 
  83.             $photoProjet $projetFormModif->get('lien')->getData();
  84.             if ($photoProjet) {
  85.                 $originalFilename pathinfo($photoProjet->getClientOriginalName(), PATHINFO_FILENAME);
  86.                 $safeFilename $slugger->slug($originalFilename);
  87.                 $newFilename $safeFilename '-' uniqid() . '.' $photoProjet->guessExtension();
  88.                 try {
  89.                     $photoProjet->move(
  90.                         $this->getParameter('lien'),
  91.                         $newFilename
  92.                     );
  93.                 } catch (FileException $e) {
  94.                 }
  95.                 $projetModif->setLien($newFilename);
  96.             } else {
  97.                 dd('Aucune photo de projet disponible');
  98.             }
  99.             //$contactModif->setPublication(new \datetime);
  100.             // On persiste le projet modifié
  101.             $this->manager->persist($projetModif);
  102.             // On le flush 
  103.             $this->manager->flush();
  104.             // On redirige ensuite à la page qui liste les projets
  105.             return $this->redirectToRoute('app_all_projets');
  106.         }
  107.         return $this->render('projet/editProjet.html.twig', [
  108.             // Matérialise l'affichage du formulaire
  109.             'myEditProjet' => $projetFormModif->createView(),
  110.         ]);
  111.     }
  112.     /*------------------ Lister tous les projets ---------------------*/
  113.     /**
  114.      * @Route("/all/projets", name="app_all_projets")
  115.      */
  116.     public function allProjets(): Response
  117.     {
  118.         $projets $this->manager->getRepository(Projet::class)->findAll();
  119.         // Verifier que la variable a bien recu les données cherchés dans la BDD
  120.         // dd($projets);
  121.         return $this->render('projet/allProjets.html.twig', [
  122.             // Matérialise l'affichage du formulaire
  123.             'projets' => $projets,
  124.         ]);
  125.     }
  126.     /**
  127.      * @Route("/single/projet/{id}", name="app_view_projet")
  128.      */
  129.     public function singleProjet(Projet $projet): Response
  130.     {
  131.         return $this->render('projet/singleProjet.html.twig', [
  132.             'projet' => $projet,
  133.         ]);
  134.     }
  135. }