src/ClientBundle/Controller/DevisController.php line 28

Open in your IDE?
  1. <?php
  2. namespace ClientBundle\Controller;
  3. use ClientBundle\Form\DevisType;
  4. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  6. use ClientBundle\Entity\Devis;
  7. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  8. use Symfony\Component\Form\Extension\Core\Type\DateType;
  9. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  10. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  11. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  12. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  13. use Symfony\Component\Form\Extension\Core\Type\TextType;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use libphonenumber\PhoneNumberFormat;
  17. use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
  18. use Symfony\Component\Validator\Constraints\NotBlank;
  19. class DevisController extends Controller
  20. {
  21.     /**
  22.      * @Route("/", name="generate_devis")
  23.      */
  24.     public function accueilAction(Request $request)
  25.     {
  26.         // create a task and give it some dummy data for this example
  27.         $devis = new Devis();
  28.         $devis->setLastUpdate(new \DateTime('now'));
  29.         $devis->setStatus(0);
  30.         $devis->setRecall(0);
  31.         $devis->setMsgStatus("La génération du devis entre en file d'attente");
  32.         $devis->setPourcentageStatus(0);
  33.         $em $this->getDoctrine()->getManager();
  34.         $form $this->createForm(DevisType::class, $devis, array(
  35.             'entity_manager' => $em,
  36.             'user' =>  $this->get('security.authorization_checker')
  37.         ));
  38.         $form->handleRequest($request);
  39.         if ($form->isSubmitted() && $form->isValid()) {
  40.             $devis $form->getData();
  41.             if ( $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
  42.                 $devis->setUser($this->getUser());
  43.             } else {
  44.                 $devis->setSendSms(1);
  45.             }
  46.             $city =  $em->getRepository("APIBundle:City")->findBy(array("id" => $devis->getCity()));
  47.             $devis->setCity($city[0]);
  48.             $token $this->giveMeToken("CustomID");
  49.             $devis->setCustomID($token);
  50.             $devis->setPrivateID($this->giveMeToken("PrivateID"));
  51.             $em->persist($devis);
  52.             $em->flush();
  53.             //return $this->redirectToRoute('show_devis', array('id' => $token));
  54.             return $this->redirect($this->generateUrl('show_devis',array('t'=> $token)));
  55.             //$this->get('session')->getFlashBag()->set('success', "Votre devis vous sera bientôt envoyé par mail ! <br /> et d'ici quelques minutes à ce <a href='$url'>lien</a>");
  56.         }
  57.         return $this->render('ClientBundle::accueil.html.twig', array(
  58.             'form' => $form->createView(),
  59.         ));
  60.     }
  61.     /**
  62.      * @Route("/show", name="show_devis")
  63.      */
  64.     public function showAction(Request $request)
  65.     {
  66.         $id $request->query->get('t');
  67.         $notdisplay $request->query->get('notdisplay');
  68.         $em $this->getDoctrine()->getManager();
  69.         $devis $em->getRepository("ClientBundle:Devis")->findOneBy(array("CustomID" => $id));
  70.         return $this->render('ClientBundle::show.html.twig', array(
  71.             'devis' => $devis,
  72.         ));
  73.     }
  74.     /**
  75.      * @Route("/admin/generate_devis", name="generate_devis_admin")
  76.      */
  77.     public function generateAction(Request $request)
  78.     {
  79.         return $this->render('ClientBundle::devis.html.twig');
  80.     }
  81.     private function giveMeToken($column){
  82.         $em $this->getDoctrine()->getManager();
  83.         $random str_shuffle(md5(rand(0,100000)));
  84.         $check $em->getRepository("ClientBundle:Devis")->findOneBy(array($column => $random));
  85.         if (!$check) {
  86.             return $random;
  87.         } else {
  88.             $this->giveMeToken();
  89.         }
  90.     }
  91.     /**
  92.      * @Route("/devis/{CustomID}/{PrivateID}", name="edit_devis")
  93.      */
  94.     public function editAction(Request $request$CustomID$PrivateID)
  95.     {
  96.         $em $this->getDoctrine()->getManager();
  97.         $devis $em->getRepository('ClientBundle:Devis')->findOneBy(array('CustomID' => $CustomID'PrivateID' => $PrivateID));
  98.         $session = new Session();
  99.         $session->start();
  100.         $session->set('firstname'$devis->getFirstname());
  101.         if (!$devis) {
  102.             return new Response("Le lien est erroné, contacter un administrateur ");
  103.         }
  104.         if(empty($devis->getMail())){
  105.             $clean true;
  106.         } else {
  107.             $clean false;
  108.         }
  109.         $editForm $this->createFormBuilder($devis)
  110.             ->add('LastName'TextType::class, array("label" => "Nom""constraints" => array( new NotBlank())))
  111.             ->add('FirstName'TextType::class, array("label" => "Prénom""constraints" => array( new NotBlank())))
  112.             ->add('Mail'EmailType::class, array("label" => "Adresse mail""error_bubbling" => false))
  113.             ->add('Phone'PhoneNumberType::class, array("label" => "Votre numéro de mobile""constraints" => array( new NotBlank()), 'format' => PhoneNumberFormat::INTERNATIONAL))
  114.             ->getForm();
  115.         $editForm->handleRequest($request);
  116.         if ($editForm->isSubmitted() && $editForm->isValid()) {
  117.             $em $this->getDoctrine()->getManager();
  118.             $body str_replace($session->get('firstname'), "Anonyme"$devis->getMailContent());
  119.             $devis->setMailContent($body);
  120.             $session->clear();
  121.             $em->persist($devis);
  122.             $em->flush();
  123.             $this->get('session')->getFlashBag()->add('success'"Mise à jour réussi !");
  124.             //return $this->redirectToRoute('mail', array('id' => $devis->getId()));
  125.         }
  126.         return $this->render('ClientBundle::consult.html.twig', array(
  127.             'devis' => $devis,
  128.             'clean' => $clean,
  129.             'form' => $editForm->createView(),
  130.         ));
  131.     }
  132. }