src/Controller/AjaxController.php line 311

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Knp\Component\Pager\PaginatorInterface;
  12. use App\Entity\Entreprise;
  13. use App\Entity\Reseau;
  14. use App\Entity\ReseauDivision;
  15. use App\Entity\Commercial;
  16. use App\Entity\Dossier;
  17. use App\Entity\Offre;
  18. use App\Entity\Garantie;
  19. use App\Entity\StructureCotisation;
  20. use App\Entity\Activite;
  21. class AjaxController extends AbstractController
  22. {
  23.     private $paginator;
  24.     private $em;
  25.     private $translator;
  26.     public function __construct(
  27.         PaginatorInterface $paginator,
  28.         EntityManagerInterface $em,
  29.         TranslatorInterface $translator
  30.     ) {
  31.         $this->paginator $paginator;
  32.         $this->em $em;
  33.         $this->translator $translator;
  34.     }
  35.     /**
  36.      * @param Request $request
  37.      *
  38.      * @Route("/autocomplete/activites", name="activite_autocomplete")
  39.      *
  40.      * @return JsonResponse
  41.      */
  42.     public function autocompleteAction(Request $request)
  43.     {
  44.         // Check security etc. if needed
  45.         $search $request->query->get('q');
  46.         $page_limit $request->query->get('page_limit');
  47.         $page $request->query->getInt('page'1);
  48.         $query $this->em->getRepository(Activite::class)->search($search);
  49.         $pagination $this->paginator->paginate(
  50.             $query/* query NOT result */
  51.             $page/*page number*/
  52.             $page_limit /*limit per page*/
  53.         );
  54.         
  55.         $response = [];
  56.         foreach($pagination as $activite) {
  57.             $response[] = [
  58.                 'id' => $activite->getId(),
  59.                 'text' => $activite->__toString()
  60.             ];
  61.         }
  62.         return new JsonResponse($response);
  63.     }
  64.     /**
  65.      * @param Request $request
  66.      *
  67.      * @Route("/autocomplete/entreprise/{id}/reseau", name="reseau_ajax")
  68.      * @Entity("entreprise", expr="repository.find(id)")
  69.      *
  70.      * @return JsonResponse
  71.      */
  72.     public function EntrepriseReseauChangeAction(Request $requestEntreprise $entreprise)
  73.     {
  74.         $default_reseau_division_id $entreprise->getReseauDivision()->getId();
  75.         $default_commercial_id is_null($entreprise->getCommercial()) ? $entreprise->getCommercial()->getId();
  76.         
  77.         $reseau_id $request->query->get('reseau_id');
  78.         $reseau $this->em->getRepository(Reseau::class)->find($reseau_id);
  79.         // On récupère les reseauDivisions avec éventuellement l'option sélectionnée pour l'entreprise
  80.         $reseauDivisions $this->em->getRepository(ReseauDivision::class)->findByReseau($reseau_id);
  81.         $rdArray = [];
  82.         $defautSelected false;
  83.         foreach($reseauDivisions as $reseauDivision) {
  84.             $selected false;
  85.             if($default_reseau_division_id == $reseauDivision->getId()) {
  86.                 $selected true;
  87.                 $defautSelected true;
  88.             }
  89.             $rdArray[] = [
  90.                 'id' => $reseauDivision->getId(),
  91.                 'text' => $reseauDivision->getNom(),
  92.                 'selected' => $selected
  93.             ];
  94.         }
  95.         if(!$defautSelected) {
  96.             $rdArray[0]['selected'] = true;
  97.             $default_reseau_division_id 0;
  98.             if(array_key_exists('id'$rdArray[0])) {
  99.                 $default_reseau_division_id $rdArray[0]['id'];
  100.             }
  101.         }
  102.         // On récupère les commerciaux de reseauDivision par défaut ou sélectionné, avec éventuellement le commercial sélectionné pour l'entreprise
  103.         $commerciaux $this->em->getRepository(Commercial::class)->findByReseauDivision($default_reseau_division_id);
  104.         $cArray = [];
  105.         $selected false;
  106.         foreach($commerciaux as $commercial) {
  107.             $selected false;
  108.             if($default_commercial_id == $commercial->getId()) {
  109.                 $selected true;
  110.             }
  111.             $cArray[] = [
  112.                 'id' => $commercial->getId(),
  113.                 'text' => $commercial->__toString(),
  114.                 'selected' => $selected
  115.             ];
  116.         }
  117.         if(!$selected) {
  118.             $cArray[0]['selected'] = true;
  119.         }
  120.         $response = [
  121.             'type_reseau' => $entreprise->getReseau()->getType(),
  122.             'reseauDivisionLabel' => $this->translator->trans('reseau.type.' $reseau->getType() . '.reseau_division.label', [], 'app'),
  123.             'reseauDivisions' => $rdArray,
  124.             'commercialLabel' => $this->translator->trans('reseau.type.' $reseau->getType() . '.commercial.label', [], 'app'),
  125.             'commerciaux' => $cArray,
  126.             'saisieProducteur' =>$reseau->getSaisieProducteur(),
  127.             'producteurCode' => $entreprise->getProducteurCodeIsWaiting(),
  128.             'producteurCodeIsWaiting' => $entreprise->getProducteurCode(),
  129.             'producteurNom' => $entreprise->getProducteurNom(),
  130.             'producteurPrenom' => $entreprise->getProducteurPrenom(),
  131.             'producteurEmail' => $entreprise->getProducteurEmail(),
  132.             'producteurTelephone' => $entreprise->getProducteurTelephone(),
  133.         ];
  134.         return new JsonResponse($response);
  135.     }
  136.     /**
  137.      * @param Request $request
  138.      *
  139.      * @Route("/autocomplete/entreprise/{id}/reseauDivision", name="reseau_division_ajax")
  140.      * @Entity("entreprise", expr="repository.find(id)")
  141.      *
  142.      * @return JsonResponse
  143.      */
  144.     public function EntrepriseReseauDivisionChangeAction(Request $requestEntreprise $entreprise)
  145.     {
  146.         $default_commercial_id is_null($entreprise->getCommercial()) ? $entreprise->getCommercial()->getId();
  147.         $reseau_division_id $request->query->get('reseau_division_id');
  148.         $reseauDivision $this->em->getRepository(reseauDivision::class)->find($reseau_division_id);
  149.         // On récupère les commerciaux avec éventuellement le commercial sélectionné pour l'entreprise
  150.         $commerciaux $this->em->getRepository(Commercial::class)->findByReseauDivision($reseau_division_id);
  151.         $cArray = [];
  152.         $selected false;
  153.         foreach($commerciaux as $commercial) {
  154.             $selected false;
  155.             if($default_commercial_id == $commercial->getId()) {
  156.                 $selected true;
  157.             }
  158.             $cArray[] = [
  159.                 'id' => $commercial->getId(),
  160.                 'text' => $commercial->__toString(),
  161.                 'selected' => $selected
  162.             ];
  163.         }
  164.         if(!$selected) {
  165.             $cArray[0]['selected'] = true;
  166.         }
  167.         $response = [
  168.             'type_reseau' => $entreprise->getReseau()->getType(),
  169.             'commercialLabel' => $this->translator->trans('reseau.type.' $reseauDivision->getReseau()->getType() . '.commercial.label', [], 'app'),
  170.             'commerciaux' => $cArray
  171.         ];
  172.         return new JsonResponse($response);
  173.     }
  174.     
  175.     /**
  176.      * @Route("/setDocuments", name="dossierMajDocuments")
  177.      */
  178.     public function setDocuments(Request $request): JsonResponse
  179.     {
  180.         if ($request->isXmlHttpRequest()) {
  181.             $dossier_id $request->request->get('dossier_id');
  182.             $checkedDocuments false;
  183.             if($request->request->has('documents') && count($request->request->get('documents')) > 0) {
  184.                 $checkedDocuments $request->request->get('documents');
  185.             }
  186.             
  187.             $dossier $this->em->getRepository(Dossier::class)->find($dossier_id );
  188.             if(!$dossier instanceof Dossier) {
  189.                 return new JsonResponse('Dossier ' $dossier_id ' introuvable'Response::HTTP_NOT_FOUND); // constant for 404
  190.             }
  191.             $tempDocuments = [];
  192.             // Initialisation du tableau de document du dossier à false
  193.             $documents $dossier->getDocuments();
  194.             if(is_countable($documents) && count($documents) > 0) {
  195.                 foreach($documents as $key => $value) {
  196.                     $tempDocuments[$key] = false;
  197.                 }
  198.                 // Mise à jour des documents cochés pour le dossier
  199.                 if($checkedDocuments) {
  200.                     foreach($checkedDocuments as $document) {
  201.                         $tempDocuments[$document['value']] = true;
  202.                     }
  203.                 }
  204.                 $dossier->setDocuments($tempDocuments);
  205.                 
  206.             } else {
  207.                 $dossier->setDocuments(null);
  208.             }
  209.             $this->em->persist($dossier);
  210.             $this->em->flush();
  211.             return  new JsonResponse($tempDocuments);
  212.         } 
  213.         return new Response('Interdit'Response::HTTP_NOT_FOUND);
  214.     }
  215.     /**
  216.      * @Route("/setControles", name="dossierMajControles")
  217.      */
  218.     public function setControles(Request $request): JsonResponse
  219.     {
  220.         if ($request->isXmlHttpRequest()) {
  221.             $dossier_id $request->request->get('dossier_id');
  222.             $checkedControles $request->request->get('controles');
  223.             $tempControles = [];
  224.             $dossier $this->em->getRepository(Dossier::class)->find($dossier_id );
  225.             if(!$dossier instanceof Dossier) {
  226.                 return new JsonResponse('Dossier introuvable'Response::HTTP_NOT_FOUND); // constant for 404
  227.             }
  228.             // Initialisation du tableau de document du dossier à false
  229.             $controles $dossier->getControles();
  230.             if(!is_null($controles)) {
  231.                 foreach($controles as $key => $value) {
  232.                     $tempControles[$key] = false;
  233.                 }
  234.             }
  235.             // Mise à jour des documents cochés pour le dossier
  236.             if(!is_null($checkedControles)) {
  237.                 foreach($checkedControles as $controle) {
  238.                     $tempControles[$controle['value']] = true;
  239.                 }
  240.             }
  241.             $dossier->setControles($tempControles);
  242.             $this->em->persist($dossier);
  243.             $this->em->flush();
  244.             return  new JsonResponse($tempControles);
  245.         }
  246.         return new Response('Interdit'Response::HTTP_NOT_FOUND); // constant for 404
  247.     }
  248.     /**
  249.      * @Route("/getGaranties", name="offreGetGaranties")
  250.      */
  251.     public function getGaranties(Request $request): JsonResponse
  252.     {
  253.         $offre_id $request->request->get('offre_id');
  254.         $offre $this->em->getRepository(Offre::class)->find($offre_id );
  255.         $garanties $this->em->getRepository(Garantie::class)->findByOffre($offre );
  256.         $garantieArray = [];
  257.         foreach($garanties as $garantie) {
  258.             $garantieArray[] = [
  259.                 'id' => $garantie->getId(),
  260.                 'text' => $garantie->getNom()
  261.             ];
  262.         }
  263.         return new JsonResponse(['garanties' => $garantieArray]);
  264.     }
  265.     /**
  266.      * @Route("/getStructureCotisations", name="offreGetStructureCotisations")
  267.      */
  268.     public function getStructureCotisations(Request $request): JsonResponse
  269.     {
  270.         $offre_id $request->request->get('offre_id');
  271.         $offre $this->em->getRepository(Offre::class)->find($offre_id );
  272.         $structureCotisations $this->em->getRepository(StructureCotisation::class)->findByOffre($offre );
  273.         $structureCotisationArray = [];
  274.         foreach($structureCotisations as $structureCotisation) {
  275.             $structureCotisationArray[] = [
  276.                 'id' => $structureCotisation->getId(),
  277.                 'text' => $structureCotisation->getNom()
  278.             ];
  279.         }
  280.         return new JsonResponse(['structureCotisations' => $structureCotisationArray]);
  281.     }
  282.     /**
  283.      * @param Request $request
  284.      *
  285.      * @Route("/dossier/{id}/motif/setVariables", name="setDossierMotifVariables")
  286.      * @Entity("dossier", expr="repository.find(id)")
  287.      *
  288.      * @return JsonResponse
  289.      */
  290.     public function setDossierMotifVariables(Request $requestDossier $dossier)
  291.     {
  292.         if ($request->isXmlHttpRequest()) {
  293.             $form $request->request->get('form');
  294.             $motif_id $request->request->get('motif_id');
  295.             parse_str($form$vars);
  296.             $vars = [$motif_id => $vars];
  297.            
  298.             $dossierVars $dossier->getVars();
  299.             if(!empty($dossierVars))
  300.             {
  301.                $variables array_replace($dossierVars$vars);
  302.             } else {
  303.                 $variables $vars;
  304.             }
  305.             $dossier->setVars($variables);
  306.             $this->em->persist($dossier);
  307.             $this->em->flush();
  308.             return  new JsonResponse($variables);
  309.         }
  310.         return new Response('Interdit'Response::HTTP_NOT_FOUND); // constant for 404
  311.     }
  312.     
  313. }