src/Controller/AdminParamsSecteurController.php line 86

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Planning\PlanningParams;
  4. use App\Entity\Planning\Site;
  5. use App\Entity\Planning\User;
  6. use App\Entity\Vetoadom\ChatCanalPlanning;
  7. use App\Entity\Vetoadom\Document;
  8. use App\Entity\Vetoadom\DocumentType;
  9. use App\Entity\Vetoadom\Fonctionnement;
  10. use App\Entity\Vetoadom\JourType;
  11. use App\Entity\Vetoadom\Langue;
  12. use App\Entity\Vetoadom\Pays;
  13. use App\Entity\Vetoadom\Referant;
  14. use App\Entity\Vetoadom\Secteur;
  15. use App\Entity\Vetoadom\SecteurCategoriePoidsEspece;
  16. use App\Entity\Vetoadom\SecteurCliniqueSecondaire;
  17. use App\Entity\Vetoadom\SecteurDelaiDefaut;
  18. use App\Entity\Vetoadom\SecteurEspece;
  19. use App\Entity\Vetoadom\SecteurGammeTarif;
  20. use App\Entity\Vetoadom\SecteurPays;
  21. use App\Entity\Vetoadom\SecteurPlanningRegulation;
  22. use App\Entity\Vetoadom\SecteurZone;
  23. use App\Entity\Vetoadom\SousSecteur;
  24. use App\Entity\Vetoadom\Tarif;
  25. use App\Entity\Vetoadom\Variable;
  26. use App\Entity\Vetoadom\Ville;
  27. use App\Entity\Vetoadom\ZoneType;
  28. use App\Service\APIlocation;
  29. use App\Service\CustomJsonResponse;
  30. use App\Service\Misc;
  31. use App\Service\Translation;
  32. use Doctrine\ORM\EntityManagerInterface;
  33. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  34. use Symfony\Component\HttpFoundation\File\File;
  35. use Symfony\Component\HttpFoundation\Request;
  36. use Symfony\Component\HttpFoundation\Response;
  37. use Symfony\Component\Routing\Annotation\Route;
  38. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  39. use Symfony\Component\Validator\Validator\ValidatorInterface;
  40. use Symfony\Contracts\Translation\TranslatorInterface;
  41. class AdminParamsSecteurController extends AbstractController
  42. {
  43.     private $APIlocation;
  44.     private $customJsonResponse;
  45.     private $misc;
  46.     private $planningEntityManager;
  47.     private $vetoadomEntityManager;
  48.     private $translation;
  49.     private $translator;
  50.     private $urlGenerator;
  51.     private $validator;
  52.     public function __construct(
  53.         APIlocation $APIlocation,
  54.         CustomJsonResponse $customJsonResponse,
  55.         EntityManagerInterface $planningEntityManager,
  56.         EntityManagerInterface $vetoadomEntityManager,
  57.         Misc $misc,
  58.         Translation $translation,
  59.         TranslatorInterface $translator,
  60.         UrlGeneratorInterface $urlGenerator,
  61.         ValidatorInterface $validator
  62.     )
  63.     {
  64.         $this->APIlocation $APIlocation;
  65.         $this->customJsonResponse $customJsonResponse;
  66.         $this->planningEntityManager $planningEntityManager;
  67.         $this->vetoadomEntityManager $vetoadomEntityManager;
  68.         $this->misc $misc;
  69.         $this->translation $translation;
  70.         $this->translator $translator;
  71.         $this->urlGenerator $urlGenerator;
  72.         $this->validator $validator;
  73.     }
  74.     /**
  75.      * @Route("/admin-params-secteur/{secteurId}", name="app_admin_params_secteur", methods={"GET", "POST"})
  76.      * @param Request $request
  77.      * @param string $secteurId
  78.      * @return Response
  79.      * @throws \Exception
  80.      */
  81.     public function index(Request $requeststring $secteurId "0"): Response
  82.     {
  83.         /** @var User $user */
  84.         $user $this->getUser();
  85.         $secteurId = (int)$secteurId;
  86.         $secteur $secteurId $this->vetoadomEntityManager->getRepository(Secteur::class)->findOneBy(['id' => $secteurId]) : new Secteur();
  87.         if (!$secteur->getId()) {
  88.             $errorMessage $this->translator->trans('page_admin_params_secteur.secteur_unknown', [], 'validators');
  89.             $redirect $this->urlGenerator->generate('app_admin_params_secteurs');
  90.             if ($request->isXmlHttpRequest()) {
  91.                 $this->customJsonResponse->setForbidden(true);
  92.                 $this->customJsonResponse->setField('alert'$errorMessage);
  93.                 $this->customJsonResponse->setRedirect($redirect);
  94.                 return $this->customJsonResponse->sendResponse();
  95.             }
  96.             return $this->render('error.html.twig', [
  97.                 'user' => $user,
  98.                 'errorMessage' => $errorMessage,
  99.                 'redirect' => $redirect
  100.             ]);
  101.         }
  102.         $params = [];
  103.         $variableEmplacements = [];
  104.         $params['variables'] = [];
  105.         $params['restrictions'] = [
  106.             'langueDisabled' => $secteur->getLangue1() || $secteur->getLangue2(),
  107.             'paysDisabled' => (bool)$secteur->getSecteurPaysCollection()->count(),
  108.             'fonctionnementDisabled' => (bool)$secteur->getFonctionnement()
  109.         ];
  110.         // show section Planning regulation only when fonctionnement 'domicile','clinique'
  111.         $params['restrictions']['planningRegulationShow'] = $secteur->getFonctionnement()
  112.             && in_array($secteur->getFonctionnement()->getCode(),['domicile','clinique']);
  113.         // show section Zones only when langue/pays/fonctionnement filled
  114.         $params['restrictions']['zonesShow'] = $secteur->getFonctionnement()
  115.             && in_array($secteur->getFonctionnement()->getCode(),['domicile','clinique'])
  116.             && $params['restrictions']['langueDisabled']
  117.             && $params['restrictions']['paysDisabled']
  118.             && $params['restrictions']['fonctionnementDisabled'];
  119.         // show calcul in section Zones
  120.         $params['restrictions']['zonesCalculShow'] = $secteur->getFonctionnement()
  121.             && in_array($secteur->getFonctionnement()->getCode(),['domicile']);
  122.         // show section Categories poids especes only when fonctionnement 'domicile','clinique'
  123.         $params['restrictions']['categoriesPoidsEspecesShow'] = $secteur->getFonctionnement()
  124.             && in_array($secteur->getFonctionnement()->getCode(),['domicile','clinique']);
  125.         // show section Localisation clinique only when fonctionnement 'clinique' and pays filled
  126.         $params['restrictions']['localisationCliniqueShow'] = $secteur->getFonctionnement()
  127.             && $secteur->getFonctionnement()->getCode() == 'clinique'
  128.             && $params['restrictions']['paysDisabled'];
  129.         // show section Localisation cliniques secondaires only when fonctionnement 'clinique' and pays filled
  130.         $params['restrictions']['localisationCliniquesSecondairesShow'] = $secteur->getFonctionnement()
  131.             && $secteur->getFonctionnement()->getCode() == 'clinique'
  132.             && $params['restrictions']['paysDisabled'];
  133.         // show section Gamme de tarif pour chaque jour de la semaine only when fonctionnement 'domicile','clinique'
  134.         $params['restrictions']['gammeTarifShow'] = $secteur->getFonctionnement()
  135.             && in_array($secteur->getFonctionnement()->getCode(),['domicile','clinique']);
  136.         // show section TEXTES MONO/MULTI/SANS VETERINAIRE(S) only when fonctionnement 'domicile','clinique'
  137.         $params['restrictions']['textesShow'] = $secteur->getFonctionnement()
  138.             && in_array($secteur->getFonctionnement()->getCode(),['domicile','clinique']);
  139.         // show section SMS only when fonctionnement 'domicile','clinique'
  140.         $params['restrictions']['smsShow'] = $secteur->getFonctionnement()
  141.             && in_array($secteur->getFonctionnement()->getCode(),['domicile','clinique']);
  142.         // show section Chat
  143.         $params['restrictions']['chatShow'] = (bool)$secteur->getChatCanal();
  144.         // show section Autres parameters only when fonctionnement not 'medicphone'
  145.         $params['restrictions']['autresShow'] = $secteur->getFonctionnement()
  146.             && $secteur->getFonctionnement()->getCode() != 'medicphone';
  147.         $params['restrictions']['autresDelaiShow'] = $params['restrictions']['autresShow']
  148.             && $secteur->getFonctionnement()->getCode() == 'domicile';
  149.         $params['restrictions']['autresCentreMapShow'] = $params['restrictions']['autresShow']
  150.             && $secteur->getFonctionnement()->getCode() == 'domicile';
  151.         $params['restrictions']['autresReferantShow'] = $params['restrictions']['autresShow']
  152.             && $secteur->getFonctionnement()->getCode() != 'alvetis';
  153.         if ($request->getMethod() == 'GET') {
  154.             $formData = [
  155.                 'secteur' => $secteur->getId() ?? 0,
  156.                 'nom' => $secteur->getNom() ?? '',
  157.                 'nomRaccourci' => $secteur->getNomRaccourci() ?? '',
  158.                 'nomShort' => $secteur->getNomShort() ?? '',
  159.                 'emailFrom' => $secteur->getEmailFrom() ?? '',
  160.                 'emailToTest' => $secteur->getEmailToTest() ?? '',
  161.                 'couleur' => $secteur->getCouleur() ?? '#000000',
  162.                 'ordre' => $secteur->getOrdre(),
  163.                 'langues' => $secteur->getLanguesIdsArray(),
  164.                 'pays' => $secteur->getSecteurPaysIds(),
  165.                 'prefixPhones' => $secteur->getSecteurPaysPrefixPhonesArray(),
  166.                 'fonctionnement' => $secteur->getFonctionnement() ? $secteur->getFonctionnement()->getId() : 0,
  167.                 'planning1Id' => $secteur->getPlanning1Id() ?? 0,
  168.                 'planning2Id' => $secteur->getPlanning2Id() ?? 0,
  169.                 'sousSecteurs' => [],
  170.                 'supervisionPlanningId' => $secteur->getSupervisionPlanningId() ?? 0,
  171.                 'vetcoachPlanningId' => $secteur->getVetcoachPlanningId() ?? 0,
  172.                 'asvPlanningId' => $secteur->getAsvPlanningId() ?? 0,
  173.                 'astreinteChirurgiePlanningId' => $secteur->getAstreinteChirurgiePlanningId() ?? 0,
  174.             ];
  175.             foreach ($secteur->getSousSecteurCollection() as $sousSecteur) {
  176.                 $formData['sousSecteurs'][] = [
  177.                     'id' => $sousSecteur->getId(),
  178.                     'nom' => $sousSecteur->getNom(),
  179.                     'planning1' => $sousSecteur->getPlanning1(),
  180.                     'planning2' => $sousSecteur->getPlanning2(),
  181.                 ];
  182.             }
  183.             if (!$formData['sousSecteurs']) {
  184.                 $formData['sousSecteurs'][] = [
  185.                     'id' => 0,
  186.                     'nom' => '',
  187.                     'planning1' => false,
  188.                     'planning2' => false,
  189.                 ];
  190.             }
  191.             $params['secteurUrl'] = $this->urlGenerator->generate('app_admin_params_secteur');
  192.             $params['secteurs'] = [];
  193.             foreach ($this->vetoadomEntityManager->getRepository(Secteur::class)->findBy([],['nom' => 'ASC']) as $item) {
  194.                 $params['secteurs'][] = [
  195.                     'id' => $item->getId(),
  196.                     'nom' => $item->getNom(),
  197.                 ];
  198.             }
  199.             $params['langues'] = [];
  200.             foreach ($this->vetoadomEntityManager->getRepository(Langue::class)->findAll() as $langue) {
  201.                 $params['langues'][] = [
  202.                     'id' => $langue->getId(),
  203.                     'nom' => $langue->getNom(),
  204.                 ];
  205.             }
  206.             $params['pays'] = [];
  207.             foreach ($this->vetoadomEntityManager->getRepository(Pays::class)->findBy([],['nom' => 'ASC']) as $pays) {
  208.                 $params['pays'][] = [
  209.                     'id' => $pays->getId(),
  210.                     'nom' => $pays->getNom(),
  211.                     'fuseauHoraireGroupe' => $pays->getFuseauHoraire()->getGroupe(),
  212.                 ];
  213.             }
  214.             $params['fonctionnements'] = [];
  215.             foreach ($this->vetoadomEntityManager->getRepository(Fonctionnement::class)->findAll() as $fonctionnement) {
  216.                 $params['fonctionnements'][] = [
  217.                     'id' => $fonctionnement->getId(),
  218.                     'nom' => $fonctionnement->getNom(),
  219.                     'code' => $fonctionnement->getCode(),
  220.                     'planningTypeId' => $fonctionnement->getPlanningTypeId(),
  221.                 ];
  222.             }
  223.             $params['sites'] = [];
  224.             foreach ($this->planningEntityManager->getRepository(Site::class)->findBy([],['nom' => 'ASC']) as $site) {
  225.                 if (
  226.                     !$site->getPlanningParams()
  227.                     || !$site->getPlanningParams()->getFonction()
  228.                 ) continue;
  229.                 $params['sites'][] = [
  230.                     'id' => $site->getId(),
  231.                     'nom' => $site->getNom(),
  232.                     'type' => $site->getPlanningParams()->getType()->getId(),
  233.                     'fonction' => $site->getPlanningParams()->getFonction()->getId(),
  234.                 ];
  235.             }
  236.             // section planning regulation
  237.             if ($params['restrictions']['planningRegulationShow']) {
  238.                 $params['jourTypes'] = [];
  239.                 foreach ($this->vetoadomEntityManager->getRepository(JourType::class)->findAll() as $jourType) {
  240.                     $params['jourTypes'][] = [
  241.                         'id' => $jourType->getId(),
  242.                         'nom' => $this->translator->trans('jour.' $jourType->getCode(), [], 'messages'),
  243.                         'nomRaccourci' => $this->translator->trans('jour_raccourci.' $jourType->getCode(), [], 'messages')
  244.                     ];
  245.                 }
  246.                 $params['planningRegulationDefault'] = [
  247.                     'planningId' => 0,
  248.                     'jourType' => 0,
  249.                     'debutHeure' => '',
  250.                     'finHeure' => '',
  251.                 ];
  252.                 $params['secteurPlanningRegulation'] = [];
  253.                 foreach ($secteur->getSecteurPlanningRegulationCollection() as $secteurPlanningRegulation) {
  254.                     $params['secteurPlanningRegulation'][] = [
  255.                         'planningId' => $secteurPlanningRegulation->getPlanningId(),
  256.                         'jourType' => $secteurPlanningRegulation->getJourType()->getId(),
  257.                         'debutHeure' => $secteurPlanningRegulation->getDebutHeure()->format('H:i'),
  258.                         'finHeure' => $secteurPlanningRegulation->getFinHeure()->format('H:i'),
  259.                     ];
  260.                     $params['planningRegulationDefault']['planningId'] = $secteurPlanningRegulation->getPlanningId();
  261.                 }
  262.                 $params['planningRegulation'] = $params['planningRegulationDefault'];
  263.             }
  264.             $params['uploadUrl'] = $this->urlGenerator->generate('app_upload');
  265.             $params['documents'] = [];
  266.             // section zones
  267.             if ($params['restrictions']['zonesShow']) {
  268.                 foreach ($this->vetoadomEntityManager->getRepository(DocumentType::class)->findBy(['type' => 'logo_secteur_zone']) as $documentType) {
  269.                     $params['documents'][$documentType->getType()] = [
  270.                         'type' => $documentType->getType(),
  271.                         'size' => $documentType->getSize(),
  272.                         'mime' => implode(",",$documentType->getMime()),
  273.                         'extensions' => implode(",",$documentType->getExtensions()),
  274.                         'multiple' => $documentType->getMultiple()
  275.                     ];
  276.                 }
  277.                 $params['secteurZones'] = [];
  278.                 foreach ($secteur->getSecteurZoneCollection() as $secteurZone) {
  279.                     $logoFile null;
  280.                     foreach ($secteurZone->getDocumentsActiveByType('logo_secteur_zone') as $document) {
  281.                         $filePath sprintf("%s%s",
  282.                             $this->getParameter('documents_directory'),
  283.                             $document->getId()
  284.                         );
  285.                         if (file_exists($filePath)) {
  286.                             $fileInfo = new File($filePath);
  287.                             $logoFile = [
  288.                                 'name' => $document->getNom(),
  289.                                 'size' => $fileInfo->getSize(),
  290.                                 'type' => $fileInfo->getMimeType(),
  291.                                 'ext' => $fileInfo->guessExtension(),
  292.                                 'url' => $this->urlGenerator->generate('app_download', ['documentId' => $document->getId()])
  293.                             ];
  294.                         }
  295.                     }
  296.                     $zone = [
  297.                         'id' => $secteurZone->getId(),
  298.                         'pays' => $secteurZone->getPays()->getId(),
  299.                         'nom' => $secteurZone->getNom(),
  300.                         'textPriseAppelLangue1' => $secteurZone->getTextPriseAppelLangue1(),
  301.                         'textPriseAppelLangue2' => $secteurZone->getTextPriseAppelLangue2() ?? '',
  302.                         'siteProtocol' => $secteurZone->getSiteProtocol(),
  303.                         'siteInternet' => $secteurZone->getSiteHost(),
  304.                         'telephone' => $secteurZone->getTelephone(),
  305.                         'logoFile' => $logoFile,
  306.                         'zoneType' => $secteurZone->getZoneType() ? $secteurZone->getZoneType()->getId() : 0,
  307.                         'sousSecteur' => $secteurZone->getSousSecteur() ? $secteurZone->getSousSecteur()->getId() : 0,
  308.                         'formId' => (string)microtime(true)
  309.                     ];
  310.                     if ($params['restrictions']['zonesCalculShow']) {
  311.                         $zone['calculAdresse1'] = $secteurZone->getCalculAdresse1() ?? '';
  312.                         $zone['calculAdresse2'] = $secteurZone->getCalculAdresse2() ?? '';
  313.                         $zone['calculX'] = $secteurZone->getCalculX() ?? '';
  314.                         $zone['calculY'] = $secteurZone->getCalculY() ?? '';
  315.                         $zone['ville'] = $secteurZone->getCalculVille() ? $secteurZone->getCalculVille()->getId() : 0;
  316.                         $zone['villeCp'] = $secteurZone->getCalculVille() ? $secteurZone->getCalculVille()->getCp() : '';
  317.                         $zone['villeSearchStarted'] = false;
  318.                         $zone['villeSearchTimer'] = null;
  319.                         $zone['villes'] = [];
  320.                         if ($secteurZone->getCalculVille()) {
  321.                             $zone['villes'] = $this->vetoadomEntityManager->getRepository(Ville::class)->getVillesForApi(['pays' => $secteur->getSecteurPaysIds(), 'cp' => $secteurZone->getCalculVille()->getCp()]);
  322.                         }
  323.                     }
  324.                     $params['secteurZones'][] = $zone;
  325.                 }
  326.                 $params['secteurZoneDefault'] = [
  327.                     'id' => 0,
  328.                     'pays' => 0,
  329.                     'nom' => '',
  330.                     'textPriseAppelLangue1' => '',
  331.                     'textPriseAppelLangue2' => '',
  332.                     'siteProtocol' => 'https',
  333.                     'siteInternet' => '',
  334.                     'telephone' => '',
  335.                     'logoFile' => null,
  336.                     'zoneType' => 0,
  337.                     'sousSecteur' => 0,
  338.                     'formId' => ''
  339.                 ];
  340.                 if ($params['restrictions']['zonesCalculShow']) {
  341.                     $params['secteurZoneDefault']['calculAdresse1'] = '';
  342.                     $params['secteurZoneDefault']['calculAdresse2'] = '';
  343.                     $params['secteurZoneDefault']['calculX'] = '';
  344.                     $params['secteurZoneDefault']['calculY'] = '';
  345.                     $params['secteurZoneDefault']['ville'] = 0;
  346.                     $params['secteurZoneDefault']['villeCp'] = '';
  347.                     $params['secteurZoneDefault']['villeSearchStarted'] = false;
  348.                     $params['secteurZoneDefault']['villeSearchTimer'] = null;
  349.                     $params['secteurZoneDefault']['villes'] = [];
  350.                 }
  351.                 if (!$params['secteurZones']) {
  352.                     $params['secteurZones'][] = $params['secteurZoneDefault'];
  353.                 }
  354.                 $params['zoneTypes'] = [];
  355.                 foreach ($this->vetoadomEntityManager->getRepository(ZoneType::class)->findAll() as $zoneType) {
  356.                     $params['zoneTypes'][] = [
  357.                         'id' => $zoneType->getId(),
  358.                         'nom' => $this->translator->trans('zone_type.'.$zoneType->getCode(), [], 'messages'),
  359.                     ];
  360.                 }
  361.             }
  362.             // section categoriesPoidsEspeces
  363.             if ($params['restrictions']['categoriesPoidsEspecesShow']) {
  364.                 $params['secteurEspeces'] = [];
  365.                 foreach ($this->vetoadomEntityManager->getRepository(SecteurEspece::class)->findAll() as $secteurEspece) {
  366.                     $params['secteurEspeces'][] = [
  367.                         'id' => $secteurEspece->getId(),
  368.                         'nom' => $this->translator->trans('secteur_espece.'.$secteurEspece->getCode(), [], 'messages'),
  369.                     ];
  370.                 }
  371.                 $params['secteurCategoriePoidsEspeces'] = [];
  372.                 foreach ($secteur->getSecteurCategoriePoidsEspeceCollection() as $secteurCategoriePoidsEspece) {
  373.                     $params['secteurCategoriePoidsEspeces'][] = [
  374.                         'id' => $secteurCategoriePoidsEspece->getId(),
  375.                         'secteurEspece' => $secteurCategoriePoidsEspece->getSecteurEspece()->getId(),
  376.                         'min' => $secteurCategoriePoidsEspece->getMin(),
  377.                         'max' => $secteurCategoriePoidsEspece->getMax()
  378.                     ];
  379.                 }
  380.                 $params['secteurCategoriePoidsEspeceDefault'] = [
  381.                     'id' => 0,
  382.                     'secteurEspece' => 0,
  383.                     'min' => '',
  384.                     'max' => ''
  385.                 ];
  386.             }
  387.             // section localisation clinique
  388.             if ($params['restrictions']['localisationCliniqueShow']) {
  389.                 $params['localisationClinique'] = [
  390.                     'adresse1' => $secteur->getAdresse1() ?? '',
  391.                     'adresse2' => $secteur->getAdresse2() ?? '',
  392.                     'ville' => $secteur->getVille() ? $secteur->getVille()->getId() : 0,
  393.                     'villeCp' => $secteur->getVille() ? $secteur->getVille()->getCp() : '',
  394.                     'villeSearchStarted' => false,
  395.                     'villeSearchTimer' => null,
  396.                     'villes' => []
  397.                 ];
  398.                 if ($secteur->getVille()) {
  399.                     $params['localisationClinique']['villes'] = $this->vetoadomEntityManager->getRepository(Ville::class)->getVillesForApi(['pays' => $secteur->getSecteurPaysIds(), 'cp' => $secteur->getVille()->getCp()]);
  400.                 }
  401.             }
  402.             // section localisation cliniques secondaires
  403.             if ($params['restrictions']['localisationCliniquesSecondairesShow']) {
  404.                 $params['localisationCliniquesSecondaires'] = [];
  405.                 foreach ($secteur->getSecteurCliniqueSecondaireCollection() as $index => $secteurCliniqueSecondaire) {
  406.                     if (!$secteurCliniqueSecondaire->getActive()) continue;
  407.                     $params['localisationCliniquesSecondaires'][] = [
  408.                         'id' => $secteurCliniqueSecondaire->getId(),
  409.                         'nom' => $secteurCliniqueSecondaire->getNom(),
  410.                         'adresse1' => $secteurCliniqueSecondaire->getAdresse1(),
  411.                         'adresse2' => $secteurCliniqueSecondaire->getAdresse2() ?? '',
  412.                         'ville' => $secteurCliniqueSecondaire->getVille()->getId(),
  413.                         'villeCp' => $secteurCliniqueSecondaire->getVille()->getCp(),
  414.                         'villeSearchStarted' => false,
  415.                         'villeSearchTimer' => null,
  416.                         'villes' => $this->vetoadomEntityManager->getRepository(Ville::class)->getVillesForApi(['pays' => $secteur->getSecteurPaysIds(), 'cp' => $secteurCliniqueSecondaire->getVille()->getCp()])
  417.                     ];
  418.                 }
  419.                 $params['localisationCliniquesSecondairesDefault'] = [
  420.                     'id' => 0,
  421.                     'nom' => '',
  422.                     'adresse1' => '',
  423.                     'adresse2' => '',
  424.                     'ville' => 0,
  425.                     'villeCp' => '',
  426.                     'villeSearchStarted' => false,
  427.                     'villeSearchTimer' => null,
  428.                     'villes' => []
  429.                 ];
  430.                 if (!$params['localisationCliniquesSecondaires']) {
  431.                     $params['localisationCliniquesSecondaires'][] = $params['localisationCliniquesSecondairesDefault'];
  432.                 }
  433.             }
  434.             // section Gamme de tarif pour chaque jour de la semaine
  435.             if ($params['restrictions']['gammeTarifShow']) {
  436.                 if (!array_key_exists('jourTypes',$params)) {
  437.                     $params['jourTypes'] = [];
  438.                     foreach ($this->vetoadomEntityManager->getRepository(JourType::class)->findAll() as $jourType) {
  439.                         $params['jourTypes'][] = [
  440.                             'id' => $jourType->getId(),
  441.                             'nom' => $this->translator->trans('jour.' $jourType->getCode(), [], 'messages'),
  442.                             'nomRaccourci' => $this->translator->trans('jour_raccourci.' $jourType->getCode(), [], 'messages')
  443.                         ];
  444.                     }
  445.                 }
  446.                 $params['tarifs'] = [];
  447.                 foreach ($this->vetoadomEntityManager->getRepository(Tarif::class)->findBy(['active' => 1]) as $tarif) {
  448.                     $params['tarifs'][] = [
  449.                         'id' => $tarif->getId(),
  450.                         'nom' => $this->translator->trans('tarif.' $tarif->getCode(), [], 'messages'),
  451.                     ];
  452.                 }
  453.                 $params['secteurGammeTarif'] = [];
  454.                 foreach ($secteur->getSecteurGammeTarifCollection() as $secteurGammeTarif) {
  455.                     $params['secteurGammeTarif'][] = [
  456.                         'jourType' => $secteurGammeTarif->getJourType()->getId(),
  457.                         'debutHeure' => $secteurGammeTarif->getDebutHeure()->format('H:i'),
  458.                         'finHeure' => $secteurGammeTarif->getFinHeure()->format('H:i'),
  459.                         'tarif' => $secteurGammeTarif->getTarif()->getId(),
  460.                     ];
  461.                 }
  462.                 $params['gammeTarifDefault'] = [
  463.                     'jourType' => 0,
  464.                     'debutHeure' => '',
  465.                     'finHeure' => '',
  466.                     'tarif' => 0,
  467.                 ];
  468.                 $params['gammeTarif'] = $params['gammeTarifDefault'];
  469.             }
  470.             // section textes
  471.             if ($params['restrictions']['textesShow']) {
  472.                 $params['textes'] = [
  473.                     'texteMonoLangue1' => $secteur->getTexteMonoLangue1() ?? '',
  474.                     'texteMonoLangue2' => $secteur->getTexteMonoLangue2() ?? '',
  475.                     'texteMultiLangue1' => $secteur->getTexteMultiLangue1() ?? '',
  476.                     'texteMultiLangue2' => $secteur->getTexteMultiLangue2() ?? '',
  477.                     'texteSansLangue1' => $secteur->getTexteSansLangue1() ?? '',
  478.                     'texteSansLangue2' => $secteur->getTexteSansLangue2() ?? '',
  479.                 ];
  480.                 $variableEmplacements[] = 'secteur_text_mono_veterinaire';
  481.                 $variableEmplacements[] = 'secteur_text_multi_veterinaire';
  482.                 $variableEmplacements[] = 'secteur_text_sans_veterinaire';
  483.             }
  484.             // section sms
  485.             if ($params['restrictions']['smsShow']) {
  486.                 $params['sms'] = [
  487.                     'smsExpediteur' => $secteur->getSmsExpediteur() ?? '',
  488.                     'smsContenueLangue1' => $secteur->getSmsContenueLangue1() ?? '',
  489.                     'smsContenueLangue2' => $secteur->getSmsContenueLangue2() ?? '',
  490.                 ];
  491.                 $variableEmplacements[] = 'sms_clinique';
  492.                 $variableEmplacements[] = 'sms_domicile';
  493.             }
  494.             // section chat
  495.             if ($params['restrictions']['chatShow']) {
  496.                 $params['chat']['plannings'] = [];
  497.                 foreach ($this->planningEntityManager->getRepository(Site::class)->findBy([], ['nom' => 'ASC']) as $item) {
  498.                     $params['chat']['plannings'][] = [
  499.                         'id' => $item->getId(),
  500.                         'nom' => $item->getNom(),
  501.                     ];
  502.                 }
  503.                 $planningIds $this->vetoadomEntityManager->getRepository(ChatCanalPlanning::class)->getChatCanalPlanningIdsArray($secteur->getChatCanal());
  504.                 $params['chat']['planningsSelected'] = [];
  505.                 foreach ($params['chat']['plannings'] as $item) {
  506.                     if (in_array($item['id'], $planningIds)) {
  507.                         $params['chat']['planningsSelected'][] = $item;
  508.                     }
  509.                 }
  510.                 if ($secteur->getFonctionnement()->getCode() == 'domicile') {
  511.                     $params['chat']['chatConsultationAccesRestreint'] = $secteur->getChatConsultationAccesRestreint() ?? false;
  512.                 }
  513.                 $params['chat']['chatConsultationCreationMonoLangue1'] = $secteur->getChatConsultationCreationMonoLangue1() ?? '';
  514.                 $params['chat']['chatConsultationCreationMonoLangue2'] = $secteur->getChatConsultationCreationMonoLangue2() ?? '';
  515.                 $params['chat']['chatConsultationCreationMultiLangue1'] = $secteur->getChatConsultationCreationMultiLangue1() ?? '';
  516.                 $params['chat']['chatConsultationCreationMultiLangue2'] = $secteur->getChatConsultationCreationMultiLangue2() ?? '';
  517.                 $params['chat']['chatConsultationCreationSansLangue1'] = $secteur->getChatConsultationCreationSansLangue1() ?? '';
  518.                 $params['chat']['chatConsultationCreationSansLangue2'] = $secteur->getChatConsultationCreationSansLangue2() ?? '';
  519.                 $variableEmplacements[] = 'secteur_chat_consultation_creation_mono_veterinaire';
  520.                 $variableEmplacements[] = 'secteur_chat_consultation_creation_multi_veterinaire';
  521.                 $variableEmplacements[] = 'secteur_chat_consultation_creation_sans_veterinaire';
  522.             }
  523.             // section autre
  524.             if ($params['restrictions']['autresShow']) {
  525.                 $params['autres'] = [
  526.                     'coeffFeux' => $secteur->getCoeffFeux() ?? '',
  527.                 ];
  528.                 if ($params['restrictions']['autresReferantShow']) {
  529.                     $params['autres']['referant'] = $secteur->getReferant() ? $secteur->getReferant()->getId() : 0;
  530.                     $params['referants'] = [];
  531.                     foreach ($this->vetoadomEntityManager->getRepository(Referant::class)->findBy([], ['nom' => 'ASC']) as $referant) {
  532.                         $params['referants'][] = [
  533.                             'id' => $referant->getId(),
  534.                             'nom' => $referant->getNom()
  535.                         ];
  536.                     }
  537.                 }
  538.                 if ($params['restrictions']['autresDelaiShow']) {
  539.                     $params['autres']['secteurDelaiDefaut'] = $secteur->getSecteurDelaiDefaut() ? $secteur->getSecteurDelaiDefaut()->getId() : 0;
  540.                     $params['secteurDelaiDefaut'] = [];
  541.                     foreach ($this->vetoadomEntityManager->getRepository(SecteurDelaiDefaut::class)->findAll() as $secteurDelaiDefaut) {
  542.                         $params['secteurDelaiDefaut'][] = [
  543.                             'id' => $secteurDelaiDefaut->getId(),
  544.                             'nom' => $this->translator->trans('secteur_delai_default.' $secteurDelaiDefaut->getCode(), [], 'messages'),
  545.                         ];
  546.                     }
  547.                 }
  548.                 $params['autres']['emailManagerServiceRegulation'] = $secteur->getEmailManagerServiceRegulation() ?? '';
  549.                 if ($params['restrictions']['autresCentreMapShow']) {
  550.                     $params['centreMap'] = [
  551.                         'ville' => $secteur->getVille() ? $secteur->getVille()->getId() : 0,
  552.                         'villeCp' => $secteur->getVille() ? $secteur->getVille()->getCp() : '',
  553.                         'villeSearchStarted' => false,
  554.                         'villeSearchTimer' => null,
  555.                         'villes' => []
  556.                     ];
  557.                     if ($secteur->getVille()) {
  558.                         $params['centreMap']['villes'] = $this->vetoadomEntityManager->getRepository(Ville::class)->getVillesForApi(['pays' => $secteur->getSecteurPaysIds(), 'cp' => $secteur->getVille()->getCp()]);
  559.                     }
  560.                 }
  561.             }
  562.             if ($variableEmplacements) {
  563.                 foreach ($this->vetoadomEntityManager->getRepository(Variable::class)->getVariables($variableEmplacements) as $variable) {
  564.                     $variableCodes = [];
  565.                     foreach ($variable->getVariableEmplacement() as $variableEmplacement) {
  566.                         $variableCodes[] = $variableEmplacement->getCode();
  567.                     }
  568.                     $params['variables'][] = [
  569.                         'code' => $variable->getCode(),
  570.                         'nom' => $this->translator->trans('variable.' $variable->getCode(), [], 'messages'),
  571.                         'variableEmplacements' => $variableCodes
  572.                     ];
  573.                 }
  574.             }
  575.             // scripts translation
  576.             $domainContext = [
  577.                 'scripts' => [
  578.                     'alert_modal',
  579.                     'error_alert_modal'
  580.                 ],
  581.                 'validators' => [
  582.                     'common',
  583.                     'documents',
  584.                     'page_admin_params_secteur'
  585.                 ],
  586.             ];
  587.             $params['trans'] = $this->translation->getTranslation($domainContext);
  588.             return $this->render(
  589.                 'admin-params-secteur.html.twig',
  590.                 [
  591.                     'user' => $user,
  592.                     'formData' => $formData,
  593.                     'params' => $params
  594.                 ]
  595.             );
  596.         }
  597.         # POST
  598.         $action $request->get('action');
  599.         switch($action){
  600.             case 'saveSecteur':
  601.                 {
  602.                     $errorAlerts = [];
  603.                     $validators = ['nom','nomRaccourci','nomShort','emailFrom','couleur','ordre','langue1','fonctionnement','planning1Id'];
  604.                     $secteur->setNom($this->misc->toUpper($request->get('nom')));
  605.                     $secteur->setNomRaccourci($this->misc->toUpper($request->get('nomRaccourci')));
  606.                     $secteur->setNomShort($this->misc->toUpper($request->get('nomShort')));
  607.                     $secteur->setEmailFrom($this->misc->toLower($request->get('emailFrom')));
  608.                     $secteur->setEmailToTest($this->misc->toLower($request->get('emailToTest')));
  609.                     if ($secteur->getEmailToTest() !== null) {
  610.                         $validators[] = 'emailToTest';
  611.                     }
  612.                     $secteur->setCouleur(trim($request->get('couleur') ?? ''));
  613.                     $secteur->setOrdre((int)($request->get('ordre') ?? 0));
  614.                     if (!$params['restrictions']['langueDisabled']) {
  615.                         $langueIds $request->get('langues') ?? [];
  616.                         if (!is_array($langueIds)) $langueIds = [];
  617.                         $secteur->setLangue1(null);
  618.                         $secteur->setLangue2(null);
  619.                         $index 1;
  620.                         foreach ($langueIds as $langueId) {
  621.                             if ($index == 3) break;
  622.                             if ($langue $this->vetoadomEntityManager->getRepository(Langue::class)->findOneBy(['id' => (int)$langueId])) {
  623.                                 $secteur->{'setLangue' $index}($langue);
  624.                             }
  625.                             $index++;
  626.                         }
  627.                     }
  628.                     if (!$params['restrictions']['paysDisabled']) {
  629.                         $paysIds $request->get('pays') ?? [];
  630.                         if (!is_array($paysIds)) $paysIds = [];
  631.                         $prefixPhones $request->get('prefixPhones') ?? [];
  632.                         if (!is_array($prefixPhones)) $prefixPhones = [];
  633.                         // add pays
  634.                         foreach ($paysIds as $paysId) {
  635.                             $paysId = (int)$paysId;
  636.                             if ($secteur->hasPays($paysId)) continue;
  637.                             $pays $paysId $this->vetoadomEntityManager->getRepository(Pays::class)->findOneBy(['id' => $paysId]) : null;
  638.                             if (!$pays) continue;
  639.                             $secteurPays = new SecteurPays();
  640.                             $secteurPays->setSecteur($secteur);
  641.                             $secteurPays->setPays($pays);
  642.                             $secteurPays->setPrefixPhone($prefixPhones[$paysId] ?? '');
  643.                             $this->vetoadomEntityManager->persist($secteurPays);
  644.                             $secteur->getSecteurPaysCollection()->add($secteurPays);
  645.                         }
  646.                         // remove pays
  647.                         $fuseauHoraireGroupes = [];
  648.                         foreach ($secteur->getSecteurPaysCollection() as $secteurPays) {
  649.                             if (in_array($secteurPays->getPays()->getId(), $paysIds)) {
  650.                                 if (!in_array($secteurPays->getPays()->getFuseauHoraire()->getGroupe(), $fuseauHoraireGroupes)) {
  651.                                     $fuseauHoraireGroupes[] = $secteurPays->getPays()->getFuseauHoraire()->getGroupe();
  652.                                 }
  653.                                 continue;
  654.                             }
  655.                             $secteur->getSecteurPaysCollection()->removeElement($secteurPays);
  656.                             $this->vetoadomEntityManager->remove($secteurPays);
  657.                         }
  658.                         if (!$secteur->getSecteurPaysCollection()->count()) {
  659.                             $this->customJsonResponse->setField('pays'$this->translator->trans('common.blank', [], 'validators'));
  660.                         }
  661.                         else if (count($fuseauHoraireGroupes) > 1) {
  662.                             $this->customJsonResponse->setField('pays'$this->translator->trans('page_admin_params_secteur.pays_fuseau_horaire', [], 'validators'));
  663.                         }
  664.                     }
  665.                     else {
  666.                         $prefixPhones $request->get('prefixPhones') ?? [];
  667.                         if (!is_array($prefixPhones)) $prefixPhones = [];
  668.                         foreach ($secteur->getSecteurPaysCollection() as $secteurPays) {
  669.                             $secteurPays->setPrefixPhone($prefixPhones[$secteurPays->getPays()->getId()] ?? '');
  670.                         }
  671.                     }
  672.                     if (!$params['restrictions']['fonctionnementDisabled']) {
  673.                         $fonctionnement = (int)($request->get('fonctionnement') ?? 0);
  674.                         $secteur->setFonctionnement($fonctionnement $this->vetoadomEntityManager->getRepository(Fonctionnement::class)->findOneBy(['id' => $fonctionnement]) : null);
  675.                     }
  676.                     if ($secteur->getFonctionnement()) {
  677.                         $planning1Id $request->get('planning1Id') ?? 0;
  678.                         $planningParams $planning1Id $this->planningEntityManager->getRepository(PlanningParams::class)->findOneBy(['fonction' => 1'site' => $planning1Id'type' => $secteur->getFonctionnement()->getPlanningTypeId()]) : null;
  679.                         $secteur->setPlanning1Id($planningParams $planningParams->getSite()->getId() : null);
  680.                         $planning2Id $request->get('planning2Id') ?? 0;
  681.                         $planningParams $planning2Id $this->planningEntityManager->getRepository(PlanningParams::class)->findOneBy(['fonction' => 1'site' => $planning2Id'type' => $secteur->getFonctionnement()->getPlanningTypeId()]) : null;
  682.                         $secteur->setPlanning2Id($planningParams $planningParams->getSite()->getId() : null);
  683.                         if (
  684.                             $secteur->getPlanning1Id()
  685.                             && $secteur->getPlanning1Id() == $secteur->getPlanning2Id()
  686.                         ) {
  687.                             $secteur->setPlanning2Id(null);
  688.                         }
  689.                         if ($secteur->getFonctionnement()->getCode() != 'medicphone') {
  690.                             $validators[] = 'supervisionPlanningId';
  691.                             $validators[] = 'vetcoachPlanningId';
  692.                         }
  693.                         $supervisionPlanningId $request->get('supervisionPlanningId') ?? 0;
  694.                         $planningParams $supervisionPlanningId $this->planningEntityManager->getRepository(PlanningParams::class)->findOneBy(['fonction' => 4'site' => $supervisionPlanningId]) : null;
  695.                         $secteur->setSupervisionPlanningId($planningParams $planningParams->getSite()->getId() : null);
  696.                         $vetcoachPlanningId $request->get('vetcoachPlanningId') ?? 0;
  697.                         $planningParams $vetcoachPlanningId $this->planningEntityManager->getRepository(PlanningParams::class)->findOneBy(['fonction' => [4,5], 'site' => $vetcoachPlanningId]) : null;
  698.                         $secteur->setVetcoachPlanningId($planningParams $planningParams->getSite()->getId() : null);
  699.                         if ($secteur->getFonctionnement()->getCode() == 'clinique') {
  700.                             $validators[] = 'asvPlanningId';
  701.                             $asvPlanningId $request->get('asvPlanningId') ?? 0;
  702.                             $planningParams $asvPlanningId $this->planningEntityManager->getRepository(PlanningParams::class)->findOneBy(['fonction' => 2'site' => $asvPlanningId'type' => $secteur->getFonctionnement()->getPlanningTypeId()]) : null;
  703.                             $secteur->setAsvPlanningId($planningParams $planningParams->getSite()->getId() : null);
  704.                             $astreinteChirurgiePlanningId $request->get('astreinteChirurgiePlanningId') ?? 0;
  705.                             $planningParams $astreinteChirurgiePlanningId $this->planningEntityManager->getRepository(PlanningParams::class)->findOneBy(['fonction' => 1'site' => $astreinteChirurgiePlanningId'type' => $secteur->getFonctionnement()->getPlanningTypeId()]) : null;
  706.                             $secteur->setAstreinteChirurgiePlanningId($planningParams $planningParams->getSite()->getId() : null);
  707.                         }
  708.                         else {
  709.                             $secteur->setAsvPlanningId(null);
  710.                             $secteur->setAstreinteChirurgiePlanningId(null);
  711.                         }
  712.                     }
  713.                     # sousSecteurs
  714.                     $sousSecteurs $request->get('sousSecteurs') ?? [];
  715.                     if (!is_array($sousSecteurs)) $sousSecteurs = [];
  716.                     // add sousSecteurs
  717.                     $validatorsSousSecteur = ['nom'];
  718.                     foreach ($sousSecteurs as $index => $item) {
  719.                         $sousSecteurNom $this->misc->toUpper($item['nom'] ?? '');
  720.                         $sousSecteurs[$index]['nom'] = $sousSecteurNom;
  721.                         $sousSecteurPlanning1 = (bool)($item['planning1'] ?? false);
  722.                         if ($secteur->getPlanning2Id()) {
  723.                             $sousSecteurPlanning2 = (bool)($item['planning2'] ?? false);
  724.                         }
  725.                         else {
  726.                             $sousSecteurPlanning2 false;
  727.                         }
  728.                         if (
  729.                             $sousSecteurNom === ''
  730.                             && !$sousSecteurPlanning1
  731.                             && !$sousSecteurPlanning2
  732.                         ) continue;
  733.                         if (!$sousSecteurPlanning1 && !$sousSecteurPlanning2) {
  734.                             $this->customJsonResponse->setField("sousSecteur-planning{$index}",$this->translator->trans('common.blank', [], 'validators'));
  735.                         }
  736.                         if ($secteur->hasSousSecteurByNom($sousSecteurNom)) {
  737.                             $sousSecteur $secteur->getSousSecteurByNom($sousSecteurNom);
  738.                             $sousSecteur->setPlanning1($sousSecteurPlanning1);
  739.                             $sousSecteur->setPlanning2($sousSecteurPlanning2);
  740.                         }
  741.                         else {
  742.                             $sousSecteur = new SousSecteur();
  743.                             $sousSecteur->setSecteur($secteur);
  744.                             $sousSecteur->setNom($sousSecteurNom);
  745.                             $sousSecteur->setPlanning1($sousSecteurPlanning1);
  746.                             $sousSecteur->setPlanning2($sousSecteurPlanning2);
  747.                             $this->vetoadomEntityManager->persist($sousSecteur);
  748.                             $secteur->getSousSecteurCollection()->add($sousSecteur);
  749.                         }
  750.                         # secteur validation
  751.                         $errors $this->validator->validate($sousSecteur,null$validatorsSousSecteur);
  752.                         if ($errors->count()) {
  753.                             foreach ($errors as $key => $error) {
  754.                                 $propertyPath "sousSecteur-{$error->getPropertyPath()}{$index}";
  755.                                 if (!$this->customJsonResponse->hasField($propertyPath)) {
  756.                                     $this->customJsonResponse->setField($propertyPath$error->getMessage());
  757.                                 }
  758.                             }
  759.                         }
  760.                     }
  761.                     // remove sousSecteurs
  762.                     foreach ($secteur->getSousSecteurCollection() as $sousSecteur) {
  763.                         if (in_array($sousSecteur->getNom(),array_column($sousSecteurs,'nom')) !== false) {
  764.                             continue;
  765.                         }
  766.                         // check for exist secteurZone with this sousSecteur
  767.                         if ($sousSecteur->getId()) {
  768.                             $zonesNoms = [];
  769.                             foreach ($secteur->getSecteurZoneCollection() as $item) {
  770.                                 if ($item->getSecteur()->getId() != $sousSecteur->getId()) continue;
  771.                                 $zonesNoms[] = $item->getNom();
  772.                             }
  773.                             if ($zonesNoms) {
  774.                                 $errorAlerts[] = $this->translator->trans('page_admin_params_secteur.secteur_unknown',
  775.                                     ['{{sousSecteurNom}}' => $sousSecteur->getNom(), '{{zonesNoms}}' => implode(", ",$zonesNoms)],
  776.                                     'validators'
  777.                                 );
  778.                             }
  779.                         }
  780.                         $secteur->getSousSecteurCollection()->removeElement($sousSecteur);
  781.                         $this->vetoadomEntityManager->remove($sousSecteur);
  782.                     }
  783.                     # secteur validation
  784.                     $errors $this->validator->validate($secteur,null$validators);
  785.                     if ($errors->count()) {
  786.                         foreach ($errors as $key => $error) {
  787.                             if (!$this->customJsonResponse->hasField($error->getPropertyPath())) {
  788.                                 $this->customJsonResponse->setField($error->getPropertyPath(), $error->getMessage());
  789.                             }
  790.                         }
  791.                     }
  792.                     if ($errorAlerts) {
  793.                         $this->customJsonResponse->setField('alert'implode("<br>",$errorAlerts));
  794.                     }
  795.                     if ($this->customJsonResponse->hasAnyField()) {
  796.                         $this->customJsonResponse->setForbidden(true);
  797.                         return $this->customJsonResponse->sendResponse();
  798.                     }
  799.                     $this->vetoadomEntityManager->flush();
  800.                     $this->customJsonResponse->setRedirect($this->urlGenerator->generate('app_admin_params_secteur',['secteurId' => $secteur->getId()]));
  801.                     return $this->customJsonResponse->sendResponse();
  802.                 }
  803.                 break;
  804.             case 'savePlanningRegulation':
  805.                 {
  806.                     if (!$params['restrictions']['planningRegulationShow']) {
  807.                         $this->customJsonResponse->setForbidden(true);
  808.                         $this->customJsonResponse->setField('alert'$this->translator->trans('common.action_unknown', [], 'validators'));
  809.                         return $this->customJsonResponse->sendResponse();
  810.                     }
  811.                     $sites = [];
  812.                     foreach ($this->planningEntityManager->getRepository(Site::class)->findBy([],['nom' => 'ASC']) as $site) {
  813.                         if (
  814.                             !$site->getPlanningParams()
  815.                             || !$site->getPlanningParams()->getFonction()
  816.                             || !in_array($site->getPlanningParams()->getFonction()->getId(),[3,4])
  817.                         ) continue;
  818.                         $sites[$site->getId()] = $site;
  819.                     }
  820.                     $jourTypes = [];
  821.                     foreach ($this->vetoadomEntityManager->getRepository(JourType::class)->findAll() as $jourType) {
  822.                         $jourTypes[$jourType->getId()] = $jourType;
  823.                     }
  824.                     $secteurPlanningRegulations $request->get('secteurPlanningRegulations') ?? [];
  825.                     if (!is_array($secteurPlanningRegulations)) $secteurPlanningRegulations= [];
  826.                     // add secteurPlanningRegulation
  827.                     $secteurPlanningRegulationsAll = [];
  828.                     foreach ($secteurPlanningRegulations as $item) {
  829.                         $planningId = (int)($item['planningId'] ?? 0);
  830.                         if (!array_key_exists($planningId,$sites)) continue;
  831.                         $jourType = (int)($item['jourType'] ?? 0);
  832.                         if (!array_key_exists($jourType,$jourTypes)) continue;
  833.                         if (($debutHeure = \DateTime::createFromFormat("!H:i"$item['debutHeure'] ?? '')) === false) continue;
  834.                         if (($finHeure = \DateTime::createFromFormat("!H:i"$item['finHeure'] ?? '')) === false) continue;
  835.                         if ($finHeure->format('H:i') == '23:59') {
  836.                             $finHeure->add(new \DateInterval('PT59S'));
  837.                         }
  838.                         if ($debutHeure >= $finHeure) continue;
  839.                         // cross check through all plannings
  840.                         foreach ($secteurPlanningRegulationsAll as $spl) {
  841.                             if (
  842.                                 $spl['jourType'] == $jourType
  843.                                 &&
  844.                                 (
  845.                                     ($spl['debutHeure'] <= $debutHeure && $spl['finHeure'] >= $finHeure)
  846.                                     || ($spl['debutHeure'] > $debutHeure && $spl['debutHeure'] < $finHeure)
  847.                                     || ($spl['finHeure'] > $debutHeure && $spl['finHeure'] < $finHeure)
  848.                                 )
  849.                             ) continue(2);
  850.                         }
  851.                         $secteurPlanningRegulationsAll[] = [
  852.                             'planningId' => $planningId,
  853.                             'jourType' => $jourType,
  854.                             'debutHeure' => $debutHeure,
  855.                             'finHeure' => $finHeure
  856.                         ];
  857.                         if ($secteur->hasSecteurPlanningRegulation($planningId,$jourType,$debutHeure,$finHeure)) continue;
  858.                         $secteurPlanningRegulation = new SecteurPlanningRegulation();
  859.                         $secteurPlanningRegulation->setSecteur($secteur);
  860.                         $secteurPlanningRegulation->setPlanningId($planningId);
  861.                         $secteurPlanningRegulation->setJourType($jourTypes[$jourType]);
  862.                         $secteurPlanningRegulation->setDebutHeure($debutHeure);
  863.                         $secteurPlanningRegulation->setFinHeure($finHeure);
  864.                         $this->vetoadomEntityManager->persist($secteurPlanningRegulation);
  865.                         $secteur->getSecteurPlanningRegulationCollection()->add($secteurPlanningRegulation);
  866.                     }
  867.                     // remove secteurPlanningRegulation
  868.                     foreach ($secteur->getSecteurPlanningRegulationCollection() as $secteurPlanningRegulation) {
  869.                         $search = [
  870.                             'planningId' => $secteurPlanningRegulation->getPlanningId(),
  871.                             'jourType' => $secteurPlanningRegulation->getJourType()->getId(),
  872.                             'debutHeure' => $secteurPlanningRegulation->getDebutHeure(),
  873.                             'finHeure' => $secteurPlanningRegulation->getFinHeure()
  874.                         ];
  875.                         if (in_array($search,$secteurPlanningRegulationsAll)) continue;
  876.                         $secteur->getSecteurPlanningRegulationCollection()->removeElement($secteurPlanningRegulation);
  877.                         $this->vetoadomEntityManager->remove($secteurPlanningRegulation);
  878.                     }
  879.                     $this->vetoadomEntityManager->flush();
  880.                     $this->customJsonResponse->setRedirect($this->urlGenerator->generate('app_admin_params_secteur',['secteurId' => $secteur->getId()]));
  881.                     return $this->customJsonResponse->sendResponse();
  882.                 }
  883.                 break;
  884.             case 'saveSecteurZone':
  885.                 {
  886.                     if (!$params['restrictions']['zonesShow']) {
  887.                         $this->customJsonResponse->setForbidden(true);
  888.                         $this->customJsonResponse->setField('alert'$this->translator->trans('common.action_unknown', [], 'validators'));
  889.                         return $this->customJsonResponse->sendResponse();
  890.                     }
  891.                     $zoneTypes = [];
  892.                     foreach ($this->vetoadomEntityManager->getRepository(ZoneType::class)->findAll() as $zoneType) {
  893.                         $zoneTypes[$zoneType->getId()] = $zoneType;
  894.                     }
  895.                     $secteurZones $request->get('secteurZones') ?? [];
  896.                     if (!is_array($secteurZones)) $secteurZones = [];
  897.                     // add secteurZone
  898.                     $secteurZonesAll = [];
  899.                     $secteurZonesFormIds = [];
  900.                     foreach ($secteurZones as $index => $item) {
  901.                         $validators = ['nom','pays','textPriseAppelLangue1','telephone','siteInternet','zoneType','sousSecteur'];
  902.                         $id = (int)($item['id'] ?? 0);
  903.                         $secteurZone $id && $secteur->hasSecteurZoneById($id) ? $secteur->getSecteurZoneById($id) : new SecteurZone();
  904.                         $secteurZone->setSecteur($secteur);
  905.                         $paysId = (int)($item['pays'] ?? 0);
  906.                         $secteurZone->setPays($secteur->hasPays($paysId) ? $secteur->getSecteurPaysById($paysId)->getPays() : null);
  907.                         $secteurZone->setNom($this->misc->toUpper($item['nom']));
  908.                         $secteurZone->setTextPriseAppelLangue1(trim($item['textPriseAppelLangue1'] ?? ''));
  909.                         if ($secteur->getLangue2()) {
  910.                             $validators[] = 'textPriseAppelLangue2';
  911.                             $secteurZone->setTextPriseAppelLangue2(trim($item['textPriseAppelLangue2'] ?? ''));
  912.                         }
  913.                         $secteurZone->setTelephone($item['telephone'] ?? '');
  914.                         $siteProtocol trim($item['siteProtocol'] ?? '');
  915.                         $siteInternet trim($item['siteInternet'] ?? '');
  916.                         $siteInternet in_array($siteProtocol,['https','http']) && !empty($siteInternet) ? $siteProtocol '://' $siteInternet '';
  917.                         $secteurZone->setSiteInternet($siteInternet);
  918.                         if (array_key_exists($item['zoneType'] ?? 0,$zoneTypes)) {
  919.                             $secteurZone->setZoneType($zoneTypes[$item['zoneType']]);
  920.                         }
  921.                         else {
  922.                             $secteurZone->setZoneType(null);
  923.                         }
  924.                         $sousSecteurId = (int)($item['sousSecteur'] ?? 0);
  925.                         $secteurZone->setSousSecteur($secteur->hasSousSecteurById($sousSecteurId) ? $secteur->getSousSecteurById($sousSecteurId) : null);
  926.                         # hors_zone_proche
  927.                         # calcul fields
  928.                         if (
  929.                             $params['restrictions']['zonesCalculShow']
  930.                             && $secteurZone->getZoneType()
  931.                             && $secteurZone->getZoneType()->getId() == 2
  932.                         ) {
  933.                             $validators[] = 'calculAdresse1';
  934.                             $validators[] = 'calculAdresse2';
  935.                             $validators[] = 'calculVille';
  936.                             $validators[] = 'calculX';
  937.                             $validators[] = 'calculY';
  938.                             $secteurZone->setCalculAdresse1($this->misc->toUpper($item['calculAdresse1'] ?? ''));
  939.                             $secteurZone->setCalculAdresse2($this->misc->toUpper($item['calculAdresse2'] ?? ''));
  940.                             $villeId = (int)($item['ville'] ?? 0);
  941.                             $ville $villeId $this->vetoadomEntityManager->getRepository(Ville::class)->findOneBy(['id' => $villeId]) : null;
  942.                             if (
  943.                                 $ville
  944.                                 && $secteur->hasPays($ville->getPays()->getId())
  945.                             ) {
  946.                                 $secteurZone->setCalculVille($ville);
  947.                                 # set ville coordinates
  948.                                 $ville $this->APIlocation->setVilleCoordinates($ville);
  949.                             }
  950.                             else {
  951.                                 $secteurZone->setCalculVille(null);
  952.                             }
  953.                             $secteurZone->setCalculX((float)($item['calculX'] ?? ''));
  954.                             if ($secteurZone->getCalculX() == 0) {
  955.                                 $secteurZone->setCalculX(null);
  956.                             }
  957.                             $secteurZone->setCalculY((float)($item['calculY'] ?? ''));
  958.                             if ($secteurZone->getCalculY() == 0) {
  959.                                 $secteurZone->setCalculY(null);
  960.                             }
  961.                         }
  962.                         else {
  963.                             $secteurZone->setCalculAdresse1(null);
  964.                             $secteurZone->setCalculAdresse2(null);
  965.                             $secteurZone->setCalculVille(null);
  966.                             $secteurZone->setCalculX(null);
  967.                             $secteurZone->setCalculY(null);
  968.                         }
  969.                         # secteurZone validation
  970.                         $errors $this->validator->validate($secteurZone,null$validators);
  971.                         if ($errors->count()) {
  972.                             foreach ($errors as $key => $error) {
  973.                                 $property $error->getPropertyPath() == 'calculVille' 'ville' $error->getPropertyPath();
  974.                                 $propertyPath "secteurZones{$index}-{$property}";
  975.                                 if (!$this->customJsonResponse->hasField($propertyPath)) {
  976.                                     $this->customJsonResponse->setField($propertyPath$error->getMessage());
  977.                                 }
  978.                             }
  979.                             continue;
  980.                         }
  981.                         if (in_array($this->misc->removeDiacritics($this->misc->toUpper($secteurZone->getNom())),$secteurZonesAll)) {
  982.                             $this->customJsonResponse->setField("secteurZone-nom{$index}"$this->translator->trans('page_admin_params_secteur_section_zones.secteur_zone_nom_exist', [], 'validators'));
  983.                             continue;
  984.                         }
  985.                         if (!$secteurZone->getId()) {
  986.                             $this->vetoadomEntityManager->persist($secteurZone);
  987.                             $secteur->getSecteurZoneCollection()->add($secteurZone);
  988.                         }
  989.                         $secteurZonesAll[] = $this->misc->removeDiacritics($this->misc->toUpper($secteurZone->getNom()));
  990.                         $secteurZonesFormIds[(string)($item['formId'] ?? '')] = $secteurZone;
  991.                     }
  992.                     if (
  993.                         $secteur->getFonctionnement()->getCode() == 'clinique'
  994.                         && count($secteurZonesAll) > 1
  995.                     ) {
  996.                         $this->customJsonResponse->setField('alert'$this->translator->trans('common.action_unknown', [], 'validators'));
  997.                     }
  998.                     if ($this->customJsonResponse->hasAnyField()) {
  999.                         $this->customJsonResponse->setForbidden(true);
  1000.                         return $this->customJsonResponse->sendResponse();
  1001.                     }
  1002.                     // remove secteurZone
  1003.                     // remove not allowed
  1004.                     /*
  1005.                     foreach ($secteur->getSecteurZoneCollection() as $secteurZone) {
  1006.                         if (in_array($secteurZone->getNom(),$secteurZonesAll)) continue;
  1007.                         // delete all documents
  1008.                         foreach ($secteurZone->getDocumentsCollection() as $document) {
  1009.                             $documentFilePath = $this->getParameter("documents_directory") . $document->getId();
  1010.                             if (file_exists($documentFilePath)) {
  1011.                                 unlink($documentFilePath);
  1012.                             }
  1013.                         }
  1014.                         // secteur_zone_ville will be deleted cascade
  1015.                         // maybe need other blocking checks for linked tables
  1016.                         $secteur->getSecteurZoneCollection()->removeElement($secteurZone);
  1017.                         $this->vetoadomEntityManager->remove($secteurZone);
  1018.                     }
  1019.                     */
  1020.                     $this->vetoadomEntityManager->flush();
  1021.                     // logo
  1022.                     foreach ($this->vetoadomEntityManager->getRepository(DocumentType::class)->findBy(['type' => 'logo_secteur_zone']) as $documentType) {
  1023.                         foreach ($secteurZonesFormIds as $formId => $secteurZone) {
  1024.                             $uploads_directory sprintf("%s/%s/%s/%s/*",
  1025.                                 $this->getParameter('uploads_directory'),
  1026.                                 $request->getSession()->getId(),
  1027.                                 $formId,
  1028.                                 $documentType->getType()
  1029.                             );
  1030.                             if (($files glob($uploads_directory)) !== false) {
  1031.                                 if ($files) {
  1032.                                     foreach ($secteurZone->getDocumentsActiveByType($documentType->getType()) as $document) {
  1033.                                         $document->setActive(false);
  1034.                                     }
  1035.                                 }
  1036.                                 foreach ($files as $filePath) {
  1037.                                     $document = new Document();
  1038.                                     $document->setDocumentType($documentType);
  1039.                                     $document->setNom(pathinfo($filePathPATHINFO_BASENAME));
  1040.                                     $document->setSecteurZone($secteurZone);
  1041.                                     $this->vetoadomEntityManager->persist($document);
  1042.                                     $this->vetoadomEntityManager->flush();
  1043.                                     // move file from tmp uploads_directory
  1044.                                     $documentFilePath $this->getParameter("documents_directory");
  1045.                                     $file = new File($filePath);
  1046.                                     $file->move($documentFilePath$document->getId());
  1047.                                 }
  1048.                             }
  1049.                         }
  1050.                     }
  1051.                     $this->customJsonResponse->setRedirect($this->urlGenerator->generate('app_admin_params_secteur',['secteurId' => $secteur->getId()]));
  1052.                     return $this->customJsonResponse->sendResponse();
  1053.                 }
  1054.                 break;
  1055.             case 'saveSecteurCategoriePoidsEpece':
  1056.                 {
  1057.                     if (!$params['restrictions']['categoriesPoidsEspecesShow']) {
  1058.                         $this->customJsonResponse->setForbidden(true);
  1059.                         $this->customJsonResponse->setField('alert'$this->translator->trans('common.action_unknown', [], 'validators'));
  1060.                         return $this->customJsonResponse->sendResponse();
  1061.                     }
  1062.                     $secteurEspeces = [];
  1063.                     foreach ($this->vetoadomEntityManager->getRepository(SecteurEspece::class)->findAll() as $secteurEspece) {
  1064.                         $secteurEspeces[$secteurEspece->getId()] = $secteurEspece;
  1065.                     }
  1066.                     $secteurCategoriePoidsEpeces $request->get('secteurCategoriePoidsEspeces') ?? [];
  1067.                     if (!is_array($secteurCategoriePoidsEpeces)) $secteurCategoriePoidsEpeces = [];
  1068.                     // add secteurCategoriePoidsEpece
  1069.                     $secteurCategoriePoidsEpecesAll = [];
  1070.                     $validators = ['min','max'];
  1071.                     foreach ($secteurCategoriePoidsEpeces as $item) {
  1072.                         $id = (int)($item['id'] ?? 0);
  1073.                         $secteurCategoriePoidsEpece $id && $secteur->hasSecteurCategoriePoidsEspeceById($id) ? $secteur->getSecteurCategoriePoidsEspeceById($id) : new SecteurCategoriePoidsEspece();
  1074.                         $secteurCategoriePoidsEpece->setSecteur($secteur);
  1075.                         $secteurCategoriePoidsEpece->setMin($item['min'] ?? '');
  1076.                         $secteurCategoriePoidsEpece->setMax($item['max'] ?? '');
  1077.                         $secteurEspeceId = (int)($item['secteurEspece'] ?? 0);
  1078.                         if (array_key_exists($secteurEspeceId,$secteurEspeces)) {
  1079.                             $secteurCategoriePoidsEpece->setSecteurEspece($secteurEspeces[$secteurEspeceId]);
  1080.                         }
  1081.                         else {
  1082.                             continue;
  1083.                         }
  1084.                         $index array_key_exists($secteurEspeceId,$secteurCategoriePoidsEpecesAll) ? count($secteurCategoriePoidsEpecesAll[$secteurEspeceId]) : 0;
  1085.                         # secteurCategoriePoidsEpece validation
  1086.                         $errors $this->validator->validate($secteurCategoriePoidsEpece,null$validators);
  1087.                         if ($errors->count()) {
  1088.                             foreach ($errors as $key => $error) {
  1089.                                 $propertyPath "secteurCategoriePoidsEspece-{$error->getPropertyPath()}{$secteurCategoriePoidsEpece->getSecteurEspece()->getId()}-{$index}";
  1090.                                 if (!$this->customJsonResponse->hasField($propertyPath)) {
  1091.                                     $this->customJsonResponse->setField($propertyPath$error->getMessage());
  1092.                                 }
  1093.                             }
  1094.                             continue;
  1095.                         }
  1096.                         # min > max
  1097.                         if ($secteurCategoriePoidsEpece->getMin() > $secteurCategoriePoidsEpece->getMax()) {
  1098.                             $this->customJsonResponse->setField("secteurCategoriePoidsEspece-min{$secteurCategoriePoidsEpece->getSecteurEspece()->getId()}-{$index}"$this->translator->trans('page_admin_params_secteur_section_categories_poids_speces.min_max', [], 'validators'));
  1099.                             continue;
  1100.                         }
  1101.                         # overlap
  1102.                         if (array_key_exists($secteurEspeceId,$secteurCategoriePoidsEpecesAll)) {
  1103.                             $found false;
  1104.                             foreach ($secteurCategoriePoidsEpecesAll[$secteurEspeceId] as $scpe) {
  1105.                                 if (
  1106.                                     ($secteurCategoriePoidsEpece->getMin() >= $scpe['min'] && $secteurCategoriePoidsEpece->getMin() <= $scpe['max'])
  1107.                                     || ($secteurCategoriePoidsEpece->getMax() >= $scpe['min'] && $secteurCategoriePoidsEpece->getMax() <= $scpe['max'])
  1108.                                     || ($scpe['min'] >= $secteurCategoriePoidsEpece->getMin() && $scpe['min'] <= $secteurCategoriePoidsEpece->getMax())
  1109.                                     || ($scpe['max'] >= $secteurCategoriePoidsEpece->getmin() && $scpe['max'] <= $secteurCategoriePoidsEpece->getMax())
  1110.                                 ) {
  1111.                                     $this->customJsonResponse->setField("secteurCategoriePoidsEspece-min{$secteurCategoriePoidsEpece->getSecteurEspece()->getId()}-{$index}"$this->translator->trans('page_admin_params_secteur_section_categories_poids_speces.overlap', [], 'validators'));
  1112.                                     $found true;
  1113.                                     break;
  1114.                                 }
  1115.                             }
  1116.                             if ($found) continue;
  1117.                         }
  1118.                         if (!$secteurCategoriePoidsEpece->getId()) {
  1119.                             $this->vetoadomEntityManager->persist($secteurCategoriePoidsEpece);
  1120.                             $secteur->getSecteurCategoriePoidsEspeceCollection()->add($secteurCategoriePoidsEpece);
  1121.                         }
  1122.                         $secteurCategoriePoidsEpecesAll[$secteurCategoriePoidsEpece->getSecteurEspece()->getId()][] = [
  1123.                             'min' => $secteurCategoriePoidsEpece->getMin(),
  1124.                             'max' => $secteurCategoriePoidsEpece->getMax()
  1125.                         ];
  1126.                     }
  1127.                     // remove secteurCategoriePoidsEpece
  1128.                     foreach ($secteur->getSecteurCategoriePoidsEspeceCollection() as $secteurCategoriePoidsEpece) {
  1129.                         if (array_key_exists($secteurCategoriePoidsEpece->getSecteurEspece()->getId(),$secteurCategoriePoidsEpecesAll)) {
  1130.                             $found false;
  1131.                             foreach ($secteurCategoriePoidsEpecesAll[$secteurCategoriePoidsEpece->getSecteurEspece()->getId()] as $scpe) {
  1132.                                 if (
  1133.                                     $scpe['min'] != $secteurCategoriePoidsEpece->getMin()
  1134.                                     || $scpe['max'] != $secteurCategoriePoidsEpece->getMax()
  1135.                                 ) continue;
  1136.                                 $found true;
  1137.                                 break;
  1138.                             }
  1139.                             if ($found) continue;
  1140.                         }
  1141.                         $secteur->getSecteurCategoriePoidsEspeceCollection()->removeElement($secteurCategoriePoidsEpece);
  1142.                         $this->vetoadomEntityManager->remove($secteurCategoriePoidsEpece);
  1143.                     }
  1144.                     if ($this->customJsonResponse->hasAnyField()) {
  1145.                         $this->customJsonResponse->setForbidden(true);
  1146.                         return $this->customJsonResponse->sendResponse();
  1147.                     }
  1148.                     $this->vetoadomEntityManager->flush();
  1149.                     $this->customJsonResponse->setRedirect($this->urlGenerator->generate('app_admin_params_secteur',['secteurId' => $secteur->getId()]));
  1150.                     return $this->customJsonResponse->sendResponse();
  1151.                 }
  1152.                 break;
  1153.             case 'saveLocalisationClinique':
  1154.                 {
  1155.                     if (!$params['restrictions']['localisationCliniqueShow']) {
  1156.                         $this->customJsonResponse->setForbidden(true);
  1157.                         $this->customJsonResponse->setField('alert'$this->translator->trans('common.action_unknown', [], 'validators'));
  1158.                         return $this->customJsonResponse->sendResponse();
  1159.                     }
  1160.                     $validators = ['adresse1','adresse2','ville'];
  1161.                     $secteur->setAdresse1($this->misc->toUpper($request->get('adresse1') ?? ''));
  1162.                     $secteur->setAdresse2($this->misc->toUpper($request->get('adresse2') ?? ''));
  1163.                     $villeId = (int)($request->get('ville') ?? 0);
  1164.                     $ville $villeId $this->vetoadomEntityManager->getRepository(Ville::class)->findOneBy(['id' => $villeId]) : null;
  1165.                     if (
  1166.                         $ville
  1167.                         && $secteur->hasPays($ville->getPays()->getId())
  1168.                     ) {
  1169.                         $secteur->setVille($ville);
  1170.                     }
  1171.                     else {
  1172.                         $secteur->setVille(null);
  1173.                     }
  1174.                     # secteur validation
  1175.                     $errors $this->validator->validate($secteur,null$validators);
  1176.                     if ($errors->count()) {
  1177.                         foreach ($errors as $key => $error) {
  1178.                             $propertyPath "localisationClinique-{$error->getPropertyPath()}";
  1179.                             if (!$this->customJsonResponse->hasField($propertyPath)) {
  1180.                                 $this->customJsonResponse->setField($propertyPath$error->getMessage());
  1181.                             }
  1182.                         }
  1183.                     }
  1184.                     if ($this->customJsonResponse->hasAnyField()) {
  1185.                         $this->customJsonResponse->setForbidden(true);
  1186.                         return $this->customJsonResponse->sendResponse();
  1187.                     }
  1188.                     # set ville coordinates
  1189.                     $secteur->setVille($this->APIlocation->setVilleCoordinates($secteur->getVille()));
  1190.                     $this->vetoadomEntityManager->flush();
  1191.                     $this->customJsonResponse->setRedirect($this->urlGenerator->generate('app_admin_params_secteur',['secteurId' => $secteur->getId()]));
  1192.                     return $this->customJsonResponse->sendResponse();
  1193.                 }
  1194.                 break;
  1195.             case 'saveLocalisationCliniquesSecondaires':
  1196.                 {
  1197.                     if (!$params['restrictions']['localisationCliniquesSecondairesShow']) {
  1198.                         $this->customJsonResponse->setForbidden(true);
  1199.                         $this->customJsonResponse->setField('alert'$this->translator->trans('common.action_unknown', [], 'validators'));
  1200.                         return $this->customJsonResponse->sendResponse();
  1201.                     }
  1202.                     $localisationCliniquesSecondaires $request->get('localisationCliniquesSecondaires') ?? [];
  1203.                     if (!is_array($localisationCliniquesSecondaires)) $localisationCliniquesSecondaires = [];
  1204.                     // add localisationCliniquesSecondaires
  1205.                     $localisationCliniquesSecondairesNoms = [];
  1206.                     $validators = ['nom','adresse1','adresse2','ville'];
  1207.                     foreach ($localisationCliniquesSecondaires as $index => $item) {
  1208.                         $id = (int)($item['id'] ?? 0);
  1209.                         $secteurCliniqueSecondaire $id && $secteur->hasSecteurCliniqueSecondaireById($id) ? $secteur->getSecteurCliniqueSecondaireById($id) : new SecteurCliniqueSecondaire();
  1210.                         $secteurCliniqueSecondaire->setSecteur($secteur);
  1211.                         $secteurCliniqueSecondaire->setNom($this->misc->toUpper($item['nom'] ?? ''));
  1212.                         $secteurCliniqueSecondaire->setAdresse1($this->misc->toUpper($item['adresse1'] ?? ''));
  1213.                         $secteurCliniqueSecondaire->setAdresse2($this->misc->toUpper($item['adresse2'] ?? ''));
  1214.                         $villeId = (int)($item['ville'] ?? 0);
  1215.                         $ville $villeId $this->vetoadomEntityManager->getRepository(Ville::class)->findOneBy(['id' => $villeId]) : null;
  1216.                         if (
  1217.                             $ville
  1218.                             && $secteur->hasPays($ville->getPays()->getId())
  1219.                         ) {
  1220.                             $secteurCliniqueSecondaire->setVille($ville);
  1221.                         }
  1222.                         else {
  1223.                             $secteurCliniqueSecondaire->setVille(null);
  1224.                         }
  1225.                         # secteurCliniqueSecondaire validation
  1226.                         $errors $this->validator->validate($secteurCliniqueSecondaire,null$validators);
  1227.                         if ($errors->count()) {
  1228.                             foreach ($errors as $key => $error) {
  1229.                                 $propertyPath "localisationCliniquesSecondaires{$index}-{$error->getPropertyPath()}";
  1230.                                 if (!$this->customJsonResponse->hasField($propertyPath)) {
  1231.                                     $this->customJsonResponse->setField($propertyPath$error->getMessage());
  1232.                                 }
  1233.                             }
  1234.                             continue;
  1235.                         }
  1236.                         if (in_array($this->misc->removeDiacritics($this->misc->toUpper($secteurCliniqueSecondaire->getNom())),$localisationCliniquesSecondairesNoms)) {
  1237.                             $this->customJsonResponse->setField("localisationCliniquesSecondaires{$index}-nom"$this->translator->trans('page_admin_params_secteur_section_localisation_cliniques_secondaires.nom_exist', [], 'validators'));
  1238.                             continue;
  1239.                         }
  1240.                         # set ville coordinates
  1241.                         $secteurCliniqueSecondaire->setVille($this->APIlocation->setVilleCoordinates($secteurCliniqueSecondaire->getVille()));
  1242.                         if (!$secteurCliniqueSecondaire->getId()) {
  1243.                             $this->vetoadomEntityManager->persist($secteurCliniqueSecondaire);
  1244.                             $secteur->getSecteurCliniqueSecondaireCollection()->add($secteurCliniqueSecondaire);
  1245.                         }
  1246.                         $localisationCliniquesSecondairesNoms[] = $this->misc->removeDiacritics($this->misc->toUpper($secteurCliniqueSecondaire->getNom()));
  1247.                     }
  1248.                     // remove secteurCliniqueSecondaire
  1249.                     foreach ($secteur->getSecteurCliniqueSecondaireCollection() as $secteurCliniqueSecondaire) {
  1250.                         if (!$secteurCliniqueSecondaire->getActive()) continue;
  1251.                         if (in_array($this->misc->removeDiacritics($this->misc->toUpper($secteurCliniqueSecondaire->getNom())),$localisationCliniquesSecondairesNoms)) continue;
  1252.                         $secteurCliniqueSecondaire->setActive(false);
  1253.                     }
  1254.                     if ($this->customJsonResponse->hasAnyField()) {
  1255.                         $this->customJsonResponse->setForbidden(true);
  1256.                         return $this->customJsonResponse->sendResponse();
  1257.                     }
  1258.                     $this->vetoadomEntityManager->flush();
  1259.                     $this->customJsonResponse->setRedirect($this->urlGenerator->generate('app_admin_params_secteur',['secteurId' => $secteur->getId()]));
  1260.                     return $this->customJsonResponse->sendResponse();
  1261.                 }
  1262.                 break;
  1263.             case 'saveGammeTarif':
  1264.                 {
  1265.                     if (!$params['restrictions']['gammeTarifShow']) {
  1266.                         $this->customJsonResponse->setForbidden(true);
  1267.                         $this->customJsonResponse->setField('alert'$this->translator->trans('common.action_unknown', [], 'validators'));
  1268.                         return $this->customJsonResponse->sendResponse();
  1269.                     }
  1270.                     $jourTypes = [];
  1271.                     foreach ($this->vetoadomEntityManager->getRepository(JourType::class)->findAll() as $jourType) {
  1272.                         $jourTypes[$jourType->getId()] = $jourType;
  1273.                     }
  1274.                     $tarifs = [];
  1275.                     foreach ($this->vetoadomEntityManager->getRepository(Tarif::class)->findBy(['active' => 1]) as $tarif) {
  1276.                         $tarifs[$tarif->getId()] = $tarif;
  1277.                     }
  1278.                     $secteurGammeTarifs $request->get('secteurGammeTarifs') ?? [];
  1279.                     if (!is_array($secteurGammeTarifs)) $secteurGammeTarifs= [];
  1280.                     // add secteurGammeTarif
  1281.                     $secteurGammeTarifsAll = [];
  1282.                     foreach ($secteurGammeTarifs as $item) {
  1283.                         $tarif = (int)($item['tarif'] ?? 0);
  1284.                         if (!array_key_exists($tarif,$tarifs)) continue;
  1285.                         $jourType = (int)($item['jourType'] ?? 0);
  1286.                         if (!array_key_exists($jourType,$jourTypes)) continue;
  1287.                         if (($debutHeure = \DateTime::createFromFormat("!H:i"$item['debutHeure'] ?? '')) === false) continue;
  1288.                         if (($finHeure = \DateTime::createFromFormat("!H:i"$item['finHeure'] ?? '')) === false) continue;
  1289.                         if ($finHeure->format('H:i') == '23:59') {
  1290.                             $finHeure->add(new \DateInterval('PT59S'));
  1291.                         }
  1292.                         if ($debutHeure >= $finHeure) continue;
  1293.                         // cross check through all gamme tarifs
  1294.                         foreach ($secteurGammeTarifsAll as $sgt) {
  1295.                             if (
  1296.                                 $sgt['jourType'] == $jourType
  1297.                                 &&
  1298.                                 (
  1299.                                     ($sgt['debutHeure'] <= $debutHeure && $sgt['finHeure'] >= $finHeure)
  1300.                                     || ($sgt['debutHeure'] > $debutHeure && $sgt['debutHeure'] < $finHeure)
  1301.                                     || ($sgt['finHeure'] > $debutHeure && $sgt['finHeure'] < $finHeure)
  1302.                                 )
  1303.                             ) continue(2);
  1304.                         }
  1305.                         $secteurGammeTarifsAll[] = [
  1306.                             'jourType' => $jourType,
  1307.                             'debutHeure' => $debutHeure,
  1308.                             'finHeure' => $finHeure,
  1309.                             'tarif' => $tarif,
  1310.                         ];
  1311.                         if ($secteur->hasSecteurGammeTarif($jourType,$debutHeure,$finHeure,$tarif)) continue;
  1312.                         $secteurGammeTarif = new SecteurGammeTarif();
  1313.                         $secteurGammeTarif->setSecteur($secteur);
  1314.                         $secteurGammeTarif->setTarif($tarifs[$tarif]);
  1315.                         $secteurGammeTarif->setJourType($jourTypes[$jourType]);
  1316.                         $secteurGammeTarif->setDebutHeure($debutHeure);
  1317.                         $secteurGammeTarif->setFinHeure($finHeure);
  1318.                         $this->vetoadomEntityManager->persist($secteurGammeTarif);
  1319.                         $secteur->getSecteurGammeTarifCollection()->add($secteurGammeTarif);
  1320.                     }
  1321.                     // remove secteurGammeTarif
  1322.                     foreach ($secteur->getSecteurGammeTarifCollection() as $secteurGammeTarif) {
  1323.                         $search = [
  1324.                             'jourType' => $secteurGammeTarif->getJourType()->getId(),
  1325.                             'debutHeure' => $secteurGammeTarif->getDebutHeure(),
  1326.                             'finHeure' => $secteurGammeTarif->getFinHeure(),
  1327.                             'tarif' => $secteurGammeTarif->getTarif()->getId(),
  1328.                         ];
  1329.                         if (in_array($search,$secteurGammeTarifsAll)) continue;
  1330.                         $secteur->getSecteurGammeTarifCollection()->removeElement($secteurGammeTarif);
  1331.                         $this->vetoadomEntityManager->remove($secteurGammeTarif);
  1332.                     }
  1333.                     // check that filled all jourTypes 24/7
  1334.                     foreach ($jourTypes as $jourTypeId => $jourType) {
  1335.                         $total 0;
  1336.                         foreach ($secteur->getSecteurGammeTarifByJourType($jourTypeId) as $secteurGammeTarif) {
  1337.                             $total += $secteurGammeTarif->getFinHeure()->getTimestamp() - $secteurGammeTarif->getDebutHeure()->getTimestamp();
  1338.                         }
  1339.                         if ($total != 86399) {
  1340.                             $this->customJsonResponse->setForbidden(true);
  1341.                             $this->customJsonResponse->setField('alert'$this->translator->trans('page_admin_params_secteur_section_gamme_tarif.calendar_not_complete', [], 'validators'));
  1342.                             return $this->customJsonResponse->sendResponse();
  1343.                         }
  1344.                     }
  1345.                     $this->vetoadomEntityManager->flush();
  1346.                     $this->customJsonResponse->setRedirect($this->urlGenerator->generate('app_admin_params_secteur',['secteurId' => $secteur->getId()]));
  1347.                     return $this->customJsonResponse->sendResponse();
  1348.                 }
  1349.                 break;
  1350.             case 'saveTextes':
  1351.                 {
  1352.                     if (!$params['restrictions']['textesShow']) {
  1353.                         $this->customJsonResponse->setForbidden(true);
  1354.                         $this->customJsonResponse->setField('alert'$this->translator->trans('common.action_unknown', [], 'validators'));
  1355.                         return $this->customJsonResponse->sendResponse();
  1356.                     }
  1357.                     $validators = ['texteMonoLangue1','texteMultiLangue1','texteSansLangue1'];
  1358.                     $secteur->setTexteMonoLangue1(trim($request->get('texteMonoLangue1') ?? ''));
  1359.                     $secteur->setTexteMultiLangue1(trim($request->get('texteMultiLangue1') ?? ''));
  1360.                     $secteur->setTexteSansLangue1(trim($request->get('texteSansLangue1') ?? ''));
  1361.                     if ($secteur->getLangue2()) {
  1362.                         $validators[] = 'texteMonoLangue2';
  1363.                         $validators[] = 'texteMultiLangue2';
  1364.                         $validators[] = 'texteSansLangue2';
  1365.                         $secteur->setTexteMonoLangue2(trim($request->get('texteMonoLangue2') ?? ''));
  1366.                         $secteur->setTexteMultiLangue2(trim($request->get('texteMultiLangue2') ?? ''));
  1367.                         $secteur->setTexteSansLangue2(trim($request->get('texteSansLangue2') ?? ''));
  1368.                     }
  1369.                     # secteur validation
  1370.                     $errors $this->validator->validate($secteur,null$validators);
  1371.                     if ($errors->count()) {
  1372.                         foreach ($errors as $key => $error) {
  1373.                             $propertyPath "textes-{$error->getPropertyPath()}";
  1374.                             if (!$this->customJsonResponse->hasField($propertyPath)) {
  1375.                                 $this->customJsonResponse->setField($propertyPath$error->getMessage());
  1376.                             }
  1377.                         }
  1378.                     }
  1379.                     if ($this->customJsonResponse->hasAnyField()) {
  1380.                         $this->customJsonResponse->setForbidden(true);
  1381.                         return $this->customJsonResponse->sendResponse();
  1382.                     }
  1383.                     $this->vetoadomEntityManager->flush();
  1384.                     $this->customJsonResponse->setRedirect($this->urlGenerator->generate('app_admin_params_secteur',['secteurId' => $secteur->getId()]));
  1385.                     return $this->customJsonResponse->sendResponse();
  1386.                 }
  1387.                 break;
  1388.             case 'saveSms':
  1389.                 {
  1390.                     if (!$params['restrictions']['smsShow']) {
  1391.                         $this->customJsonResponse->setForbidden(true);
  1392.                         $this->customJsonResponse->setField('alert'$this->translator->trans('common.action_unknown', [], 'validators'));
  1393.                         return $this->customJsonResponse->sendResponse();
  1394.                     }
  1395.                     $validators = ['smsExpediteur','smsContenueLangue1'];
  1396.                     $secteur->setSmsExpediteur(trim($request->get('smsExpediteur') ?? ''));
  1397.                     $secteur->setSmsContenueLangue1(trim($request->get('smsContenueLangue1') ?? ''));
  1398.                     if ($secteur->getLangue2()) {
  1399.                         $validators[] = 'smsContenueLangue2';
  1400.                         $secteur->setSmsContenueLangue2(trim($request->get('smsContenueLangue2') ?? ''));
  1401.                     }
  1402.                     # secteur validation
  1403.                     $errors $this->validator->validate($secteur,null$validators);
  1404.                     if ($errors->count()) {
  1405.                         foreach ($errors as $key => $error) {
  1406.                             $propertyPath "sms-{$error->getPropertyPath()}";
  1407.                             if (!$this->customJsonResponse->hasField($propertyPath)) {
  1408.                                 $this->customJsonResponse->setField($propertyPath$error->getMessage());
  1409.                             }
  1410.                         }
  1411.                     }
  1412.                     if ($this->customJsonResponse->hasAnyField()) {
  1413.                         $this->customJsonResponse->setForbidden(true);
  1414.                         return $this->customJsonResponse->sendResponse();
  1415.                     }
  1416.                     $this->vetoadomEntityManager->flush();
  1417.                     $this->customJsonResponse->setRedirect($this->urlGenerator->generate('app_admin_params_secteur',['secteurId' => $secteur->getId()]));
  1418.                     return $this->customJsonResponse->sendResponse();
  1419.                 }
  1420.                 break;
  1421.             case 'saveChat':
  1422.                 {
  1423.                     if (!$params['restrictions']['chatShow']) {
  1424.                         $this->customJsonResponse->setForbidden(true);
  1425.                         $this->customJsonResponse->setField('alert'$this->translator->trans('common.action_unknown', [], 'validators'));
  1426.                         return $this->customJsonResponse->sendResponse();
  1427.                     }
  1428.                     $planningIds $request->get('planningIds') ?? [];
  1429.                     if (!is_array($planningIds)) $planningIds = [];
  1430.                     # array of exist chatCanalPlannings
  1431.                     $chatCanalPlannings = [];
  1432.                     foreach ($this->vetoadomEntityManager->getRepository(ChatCanalPlanning::class)->findBy(['chatCanal' => $secteur->getChatCanal()]) as $chatCanalPlanning) {
  1433.                         $chatCanalPlannings[$chatCanalPlanning->getPlanningId()] = $chatCanalPlanning;
  1434.                     }
  1435.                     # add new chatCanalPlannings
  1436.                     $plannings = [];
  1437.                     foreach ($this->planningEntityManager->getRepository(Site::class)->findAll() as $item) {
  1438.                         $plannings[] = $item->getId();
  1439.                     }
  1440.                     foreach ($planningIds as $planningId) {
  1441.                         $planningId = (int)$planningId;
  1442.                         if (!in_array($planningId$plannings)) continue;
  1443.                         if (array_key_exists($planningId,$chatCanalPlannings)) {
  1444.                             unset($chatCanalPlannings[$planningId]);
  1445.                             continue;
  1446.                         }
  1447.                         $chatCanalPlanning = new ChatCanalPlanning();
  1448.                         $chatCanalPlanning->setChatCanal($secteur->getChatCanal());
  1449.                         $chatCanalPlanning->setPlanningId($planningId);
  1450.                         $this->vetoadomEntityManager->persist($chatCanalPlanning);
  1451.                     }
  1452.                     # remove deleted chatCanalPlannings
  1453.                     foreach ($chatCanalPlannings as $planningId => $chatCanalPlanning) {
  1454.                         $this->vetoadomEntityManager->remove($chatCanalPlanning);
  1455.                         unset($chatCanalPlannings[$planningId]);
  1456.                     }
  1457.                     if ($secteur->getFonctionnement()->getCode() == 'domicile') {
  1458.                         $secteur->setChatConsultationAccesRestreint($request->get('chatConsultationAccesRestreint') ?? false);
  1459.                     }
  1460.                     $validators = [];
  1461.                     if ($chatCanalPlannings || $planningIds) {
  1462.                         $validators = ['chatConsultationCreationMonoLangue1''chatConsultationCreationMultiLangue1''chatConsultationCreationSansLangue1'];
  1463.                     }
  1464.                     $secteur->setChatConsultationCreationMonoLangue1(trim($request->get('chatConsultationCreationMonoLangue1') ?? ''));
  1465.                     $secteur->setChatConsultationCreationMultiLangue1(trim($request->get('chatConsultationCreationMultiLangue1') ?? ''));
  1466.                     $secteur->setChatConsultationCreationSansLangue1(trim($request->get('chatConsultationCreationSansLangue1') ?? ''));
  1467.                     if ($secteur->getLangue2()) {
  1468.                         if ($chatCanalPlannings) {
  1469.                             $validators[] = 'chatConsultationCreationMonoLangue2';
  1470.                             $validators[] = 'chatConsultationCreationMultiLangue2';
  1471.                             $validators[] = 'chatConsultationCreationSansLangue2';
  1472.                         }
  1473.                         $secteur->setChatConsultationCreationMonoLangue2(trim($request->get('chatConsultationCreationMonoLangue2') ?? ''));
  1474.                         $secteur->setChatConsultationCreationMultiLangue2(trim($request->get('chatConsultationCreationMultiLangue2') ?? ''));
  1475.                         $secteur->setChatConsultationCreationSansLangue2(trim($request->get('chatConsultationCreationSansLangue2') ?? ''));
  1476.                     }
  1477.                     # secteur validation
  1478.                     if ($validators) {
  1479.                         $errors $this->validator->validate($secteurnull$validators);
  1480.                         if ($errors->count()) {
  1481.                             foreach ($errors as $key => $error) {
  1482.                                 $propertyPath "chat-{$error->getPropertyPath()}";
  1483.                                 if (!$this->customJsonResponse->hasField($propertyPath)) {
  1484.                                     $this->customJsonResponse->setField($propertyPath$error->getMessage());
  1485.                                 }
  1486.                             }
  1487.                         }
  1488.                         if ($this->customJsonResponse->hasAnyField()) {
  1489.                             $this->customJsonResponse->setForbidden(true);
  1490.                             return $this->customJsonResponse->sendResponse();
  1491.                         }
  1492.                     }
  1493.                     $this->vetoadomEntityManager->flush();
  1494.                     $this->customJsonResponse->setRedirect($this->urlGenerator->generate('app_admin_params_secteur',['secteurId' => $secteur->getId()]));
  1495.                     return $this->customJsonResponse->sendResponse();
  1496.                 }
  1497.                 break;
  1498.             case 'saveAutres':
  1499.                 {
  1500.                     if (!$params['restrictions']['autresShow']) {
  1501.                         $this->customJsonResponse->setForbidden(true);
  1502.                         $this->customJsonResponse->setField('alert'$this->translator->trans('common.action_unknown', [], 'validators'));
  1503.                         return $this->customJsonResponse->sendResponse();
  1504.                     }
  1505.                     $validators = ['coeffFeux','emailManagerServiceRegulation'];
  1506.                     $secteur->setCoeffFeux((float)($request->get('coeffFeux') ?? ''));
  1507.                     if ($secteur->getCoeffFeux() == 0) {
  1508.                         $secteur->setCoeffFeux(null);
  1509.                     }
  1510.                     if ($params['restrictions']['autresReferantShow']) {
  1511.                         $validators[] = 'referant';
  1512.                         $referantId = (int)($request->get('referant') ?? 0);
  1513.                         $secteur->setReferant($referantId $this->vetoadomEntityManager->getRepository(Referant::class)->findOneBy(['id' => $referantId]) : null);
  1514.                     }
  1515.                     else {
  1516.                         $secteur->setReferant(null);
  1517.                     }
  1518.                     if ($params['restrictions']['autresDelaiShow']) {
  1519.                         $validators[] = 'secteurDelaiDefaut';
  1520.                         $secteurDelaiDefautId = (int)($request->get('secteurDelaiDefaut') ?? 0);
  1521.                         $secteur->setSecteurDelaiDefaut($secteurDelaiDefautId $this->vetoadomEntityManager->getRepository(SecteurDelaiDefaut::class)->findOneBy(['id' => $secteurDelaiDefautId]) : null);
  1522.                     }
  1523.                     else {
  1524.                         $secteur->setSecteurDelaiDefaut(null);
  1525.                     }
  1526.                     $secteur->setEmailManagerServiceRegulation($this->misc->toLower($request->get('emailManagerServiceRegulation')));
  1527.                     if ($params['restrictions']['autresCentreMapShow']) {
  1528.                         $validators[] = 'ville';
  1529.                         $villeId = (int)($request->get('ville') ?? 0);
  1530.                         $ville $villeId $this->vetoadomEntityManager->getRepository(Ville::class)->findOneBy(['id' => $villeId]) : null;
  1531.                         if (
  1532.                             $ville
  1533.                             && $secteur->hasPays($ville->getPays()->getId())
  1534.                         ) {
  1535.                             # set ville coordinates
  1536.                             $secteur->setVille($this->APIlocation->setVilleCoordinates($ville));
  1537.                         }
  1538.                         else {
  1539.                             $secteur->setVille(null);
  1540.                         }
  1541.                     }
  1542.                     # secteur validation
  1543.                     $errors $this->validator->validate($secteur,null$validators);
  1544.                     if ($errors->count()) {
  1545.                         foreach ($errors as $key => $error) {
  1546.                             if ($error->getPropertyPath() == 'ville') {
  1547.                                 $propertyPath "centreMap-{$error->getPropertyPath()}";
  1548.                             }
  1549.                             else {
  1550.                                 $propertyPath "autres-{$error->getPropertyPath()}";
  1551.                             }
  1552.                             if (!$this->customJsonResponse->hasField($propertyPath)) {
  1553.                                 $this->customJsonResponse->setField($propertyPath$error->getMessage());
  1554.                             }
  1555.                         }
  1556.                     }
  1557.                     if ($this->customJsonResponse->hasAnyField()) {
  1558.                         $this->customJsonResponse->setForbidden(true);
  1559.                         return $this->customJsonResponse->sendResponse();
  1560.                     }
  1561.                     $this->vetoadomEntityManager->flush();
  1562.                     $this->customJsonResponse->setRedirect($this->urlGenerator->generate('app_admin_params_secteur',['secteurId' => $secteur->getId()]));
  1563.                     return $this->customJsonResponse->sendResponse();
  1564.                 }
  1565.                 break;
  1566.             default:{
  1567.                 $this->customJsonResponse->setForbidden(true);
  1568.                 $this->customJsonResponse->setField('alert'$this->translator->trans('common.action_unknown', [], 'validators'));
  1569.                 return $this->customJsonResponse->sendResponse();
  1570.             }
  1571.         }
  1572.     }
  1573. }