src/Controller/Commercial/FicheController.php line 331

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Commercial;
  3. use App\Entity\Sessions;
  4. use App\Entity\Trainings;
  5. use App\Form\FicheType;
  6. use App\Form\SelectForm;
  7. use App\Repository\SessionsRepository;
  8. use App\Repository\TrainingsRepository;
  9. use App\Repository\UserRepository;
  10. use App\Service\Slugify;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. #[Route('/commercial/fiches')]
  16. class FicheController extends AbstractController
  17. {
  18.     #[Route('/select-training'name'select_training'methods: ['GET''POST'])]
  19.     public function selectTraining(
  20.         Request $request,
  21.         TrainingsRepository $trainingsRepository
  22.     ): Response
  23.     {
  24.         $form $this->createForm(SelectForm::class);
  25.         $form->handleRequest($request);
  26.         if ($form->isSubmitted() && $form->isValid()) {
  27.             return $this->redirectToRoute('fiches_new'$request->request->all(), Response::HTTP_SEE_OTHER);
  28.         }
  29.         $arrayTrainings = [];
  30.         $arrayTrainings['trainings'] = [];
  31.         foreach ($trainingsRepository->findAll() as $training){
  32.             $arrayTrainings['trainings'][] = ['id' => $training->getId(), 'title' => $training->getTitle(), 'isFiche' => $training->isIsFiche()];
  33.         }
  34.         return $this->renderForm('commercial/fiches/selectTraining.html.twig', [
  35.             'form' => $form,
  36.             'trainings' => $arrayTrainings,
  37.         ]);
  38.     }
  39.     #[Route('/'name'fiches_index'methods: ['GET'])]
  40.     public function index(
  41.         TrainingsRepository $fichesRepository
  42.     ): Response
  43.     {
  44.         return $this->render('commercial/fiches/index.html.twig', [
  45.             'fiches' => $fichesRepository->findBy(['owner' => $this->getUser(), 'isFiche' => true]),
  46.         ]);
  47.     }
  48.     #[Route('/users-fiches'name'other_fiches_index'methods: ['GET'])]
  49.     public function indexOtherFiches(
  50.         UserRepository $userRepository
  51.     ): Response
  52.     {
  53.         return $this->render('commercial/fiches/fichesByUsers.html.twig', [
  54.             'users' => $userRepository->findByRole('COMMERCIAL'$this->getUser()->getId()),
  55.         ]);
  56.     }
  57.     #[Route('/all-fiches'name'all_fiches_index'methods: ['GET'])]
  58.     public function indexAllFiches(
  59.         TrainingsRepository $trainingsRepository
  60.     ): Response
  61.     {
  62.         return $this->render('commercial/fiches/otherFichesNotFiltered.html.twig', [
  63.             'fiches' => $trainingsRepository->findBy(array('isFiche' => true)),
  64.         ]);
  65.     }
  66.     #[Route('/new'name'fiches_new'methods: ['GET''POST'])]
  67.     public function new(
  68.         Request $request,
  69.         TrainingsRepository $ficheRepository,
  70.         Slugify $slugify
  71.     ): Response
  72.     {
  73.         $fiches = array();
  74.         //set all fields from chosen trainings
  75.         if( ( //from select_training route
  76.                 $request->query->all() && //query empty
  77.                 in_array('select_form'array_keys($request->query->all())) //query has select_form parameter
  78.             ) || ( //from other_fiches_index route
  79.                 $request->request->all() && //request empty
  80.                 in_array('duplicate_form'array_keys($request->request->all())) && //request has select_form parameter
  81.                 !empty($request->request->all()['duplicate_form']['trainings']) //request has training id not empty
  82.             )
  83.         ){
  84.             if(!empty($request->query->all()) ){
  85.                 $trainingsForm $request->query->all()['select_form']; // when btn add more change this line
  86.                 foreach($trainingsForm as $key => $id){
  87.                     if(str_contains($key'trainings') && !empty($id)){
  88.                         $fiches[] = $ficheRepository->findOneBy(array('id' => $id));
  89.                     }
  90.                 }
  91.             }else{
  92.                 $id $request->request->all()['duplicate_form']['trainings'];
  93.                 $fiches[] = $ficheRepository->findOneBy(array('id' => $id));
  94.             }
  95.         }
  96. //        dd($fiches);
  97.         $fiche = new Trainings();
  98.         //only if there are trainings or fiches selected and doesn't redo the code after form validation
  99.         if(!empty($fiches) && !in_array('trainings'array_keys($request->request->all()))){
  100.             //default concat
  101.             //strings
  102.             $objectives $complementaryObjectives $pedagogicContent $pluses $prerequisites $termsOfAccess
  103.                 $alternatingRythm $teachingMethods $teachingMeans $assessmentMethods $companyMissions "";
  104.             //arrays
  105.             $pedagogicReferents $commercials $sessions = array();
  106.             $days null;
  107.             $hours null;
  108.             // concat from all the fiches
  109.             foreach($fiches as $key => $formerFiche){
  110. //                dd($formerFiche);
  111.                 $fichSeparator '';
  112.                 if($key != 0){
  113.                     $fichSeparator '<hr>';
  114.                 }
  115.                 if(!empty($formerFiche->getObjectives())){
  116.                     $objectives .= $fichSeparator.$formerFiche->getObjectives();
  117.                 }
  118.                 if(!empty($formerFiche->getComplementaryObjectives())){
  119.                     $complementaryObjectives .= $fichSeparator.$formerFiche->getComplementaryObjectives();
  120.                 }
  121.                 if(!empty($formerFiche->getPedagogicContent())){
  122.                     $pedagogicContent .= $fichSeparator.$formerFiche->getPedagogicContent();
  123.                 }
  124.                 if(!empty($formerFiche->getPluses())){
  125.                     $pluses .= $fichSeparator.$formerFiche->getPluses();
  126.                 }
  127.                 if(!empty($formerFiche->getPrerequisites())){
  128.                     $prerequisites .= $fichSeparator.$formerFiche->getPrerequisites();
  129.                 }
  130.                 if(!empty($formerFiche->getCompanyMissions())){
  131.                     $companyMissions .= $fichSeparator.$formerFiche->getCompanyMissions();
  132.                 }
  133.                 if(!empty($formerFiche->getTermsOfAccess())){
  134.                     $termsOfAccess .= $fichSeparator.$formerFiche->getTermsOfAccess();
  135.                 }
  136.                 if(!empty($formerFiche->getAlternatingRythm())){
  137.                     $alternatingRythm .= $fichSeparator.$formerFiche->getAlternatingRythm();
  138.                 }
  139.                 if(!empty($formerFiche->getTeachingMethods())){
  140.                     $teachingMethods .= $fichSeparator.$formerFiche->getTeachingMethods();
  141.                 }
  142.                 if(!empty($formerFiche->getTeachingMeans())){
  143.                     $teachingMeans .= $fichSeparator.$formerFiche->getTeachingMeans();
  144.                 }
  145.                 if(!empty($formerFiche->getAssessmentMethods())){
  146.                     $assessmentMethods .= $fichSeparator.$formerFiche->getAssessmentMethods();
  147.                 }
  148.                 foreach($formerFiche->getPedagogicalReferent() as $pedagogicalReferent){
  149.                     if(!in_array($pedagogicalReferent$pedagogicReferents)){
  150.                         $pedagogicReferents[] = $pedagogicalReferent;
  151.                     }
  152.                 }
  153.                 foreach($formerFiche->getCommercials() as $commercial){
  154.                     if(!in_array($commercial$commercials)){
  155.                         $commercials[] = $commercial;
  156.                     }
  157.                 }
  158.                 if($formerFiche->getDays()) {
  159.                     if($days === null){
  160.                         $days $formerFiche->getDays();
  161.                     } else {
  162.                         $days $days $formerFiche->getDays();
  163.                     }
  164.                 }
  165.                 if($formerFiche->getHours()) {
  166.                     if($hours === null){
  167.                         $hours $formerFiche->getHours();
  168.                     } else {
  169.                         $hours $hours $formerFiche->getHours();
  170.                     }
  171.                 }
  172. //                foreach($formerFiche->getSessions() as $session){
  173. //                    $newSession = new Sessions();
  174. //                    $newSession->setEndDate($session->getEndDate());
  175. //                    $newSession->setStartDate($session->getStartDate());
  176. //                    $newSession->setName($session->getName());
  177. //                    $newSession->setTrainings($fiche);
  178. //
  179. //                    $sessions[] = $newSession;
  180. //                }
  181.             }
  182.             //get first fiche selected
  183.             $first $fiches[0];
  184.             //set all properties of a fiche after concat
  185.             $fiche->setTitle($first->getTitle());
  186.             $fiche->setRncpOrRs($first->getRncpOrRs());
  187.             $fiche->setValidityDateRncp($first->getValidityDateRncp());
  188.             $fiche->setSubTitle($first->getSubTitle());
  189.             $fiche->setCode($first->getCode());
  190.             $fiche->setCutInBlocks($first->isCutInBlocks());
  191.             foreach($first->getDomain() as $domain){
  192.                 $fiche->addDomain($domain);
  193.             }
  194.             foreach($first->getSubDomain() as $subdomain){
  195.                 $fiche->addSubDomain($subdomain);
  196.             }
  197.             foreach($first->getKeywords() as $keyword){
  198.                 $fiche->addKeyword($keyword);
  199.             }
  200.             $fiche->setType($first->getType());
  201.             $fiche->setNew($first->isNew());
  202.             $fiche->setCustomizable($first->getCustomizable());
  203.             $fiche->setObjectives($objectives);
  204.             $fiche->setComplementaryObjectives($complementaryObjectives);
  205.             $fiche->setPedagogicContent($pedagogicContent);
  206.             $fiche->setCompanyMissions($companyMissions);
  207.             $fiche->setPluses($pluses);
  208.             $fiche->setConcernedPersons($first->getConcernedPersons());
  209.             $fiche->setPrerequisites($prerequisites);
  210.             $fiche->setPrice($first->getPrice());
  211.             $fiche->setTime($first->getTime());
  212.             $fiche->setDays($days);
  213.             $fiche->setHours($hours);
  214.             $fiche->setDate($first->getDate());
  215.             $fiche->setPlace($first->getPlace());
  216.             foreach($first->getDeviceAndTraining() as $deviceAndTraining){
  217.                 $fiche->addDeviceAndTraining($deviceAndTraining);
  218.             }
  219.             $fiche->setTermsOfAccess($termsOfAccess);
  220.             $fiche->setAlternatingRythm($alternatingRythm);
  221.             $fiche->setTeachingMethods($teachingMethods);
  222.             $fiche->setTeachingMeans($teachingMeans);
  223.             $fiche->setCertificationType($first->getCertificationType());
  224.             $fiche->setEducationLevel($first->getEducationLevel());
  225.             $fiche->setCertificationName($first->getCertificationName());
  226.             $fiche->setAssessmentMethods($assessmentMethods);
  227.             $fiche->setTargetFunctions($first->getTargetFunctions());
  228.             $fiche->setContinuingStudies($first->getContinuingStudies());
  229.             $fiche->setDisabledAccessibility($first->getDisabledAccessibility());
  230.             foreach($pedagogicReferents as $pedagogicReferent){
  231.                 $fiche->addPedagogicalReferent($pedagogicReferent);
  232.             }
  233.             foreach($commercials as $commercial){
  234.                 $fiche->addCommercial($commercial);
  235.             }
  236. //            foreach($sessions as $session){
  237. //                $newSession = new Sessions();
  238. //                $newSession->setEndDate($session->getEndDate());
  239. //                $newSession->setStartDate($session->getStartDate());
  240. //                $newSession->setName($session->getName());
  241. //                $newSession->setTrainings($fiche);
  242. //
  243. //                $fiche->addSession($newSession);
  244. //            }
  245.             $fiche->setTestimonials($first->getTestimonials());
  246.             $fiche->setAccessibilityText($first->getAccessibilityText());
  247.             $fiche->setLogoDisplay($first->isLogoDisplay());
  248.             $fiche->setWhichPublic($first->getWhichPublic());
  249.             $fiche->setSlug($first->getSlug());
  250.         }
  251.         $form $this->createForm(FicheType::class, $fiche);
  252.         $form->handleRequest($request);
  253.         if ($form->isSubmitted() && $form->isValid()) {
  254.             //set published to true for trainings
  255.             $fiche->setPublished(false);
  256.             //set new edit and create date
  257.             $dateImmutable = new \DateTime();
  258.             $fiche->setCreationDate($dateImmutable);
  259.             $fiche->setEditDate($dateImmutable);
  260.             $dateValidity = new \DateTime();
  261.             $fiche->setValidityDate($dateValidity->modify('+1 month'));
  262.             //set default owner
  263.             $fiche->setOwner($this->getUser());
  264.             $fiche->setIsFiche(true);
  265.             $fiche->setSlug($slugify->generate($fiche->getTitle()));
  266.             $ficheRepository->add($fichetrue);
  267.             return $this->redirectToRoute('fiches_index', [], Response::HTTP_SEE_OTHER);
  268.         }
  269.         return $this->renderForm('commercial/fiches/new.html.twig', [
  270.             'fiche' => $fiche,
  271.             'form' => $form,
  272.         ]);
  273.     }
  274.     #[Route('/{id}'name'fiches_show'methods: ['GET'])]
  275.     public function show(Trainings $fiche): Response
  276.     {
  277.         return $this->render('commercial/fiches/show.html.twig', [
  278.             'fiche' => $fiche,
  279.         ]);
  280.     }
  281.     #[Route('/{id}/edit'name'fiches_edit'methods: ['GET''POST'])]
  282.     public function edit(
  283.         Request $request,
  284.         Trainings $fiche,
  285.         TrainingsRepository $ficheRepository,
  286.         Slugify $slugify
  287.     ): Response
  288.     {
  289.         $oldValidityDateRncp $fiche->getValidityDateRncp();
  290.         $form $this->createForm(FicheType::class, $fiche);
  291.         $form->handleRequest($request);
  292.         if ($form->isSubmitted() && $form->isValid()) {
  293.             //set published to true for trainings
  294.             $fiche->setPublished(false);
  295.             //set new edit and create date
  296.             $dateImmutable = new \DateTime();
  297.             $fiche->setEditDate($dateImmutable);
  298.             $dateValidity = new \DateTime();
  299.             $fiche->setValidityDate($dateValidity->modify('+1 month'));
  300.             if($fiche->getValidityDateRncp() === $dateImmutable) {
  301.                 if($oldValidityDateRncp) {
  302.                     $fiche->setValidityDateRncp($oldValidityDateRncp);
  303.                 } else {
  304.                     $fiche->setValidityDateRncp(null);
  305.                 }
  306.             }
  307.             $fiche->setSlug($slugify->generate($fiche->getTitle()));
  308.             $ficheRepository->add($fichetrue);
  309.             return $this->redirectToRoute('fiches_index', [], Response::HTTP_SEE_OTHER);
  310.         }
  311.         return $this->renderForm('commercial/fiches/edit.html.twig', [
  312.             'fiche' => $fiche,
  313.             'form' => $form,
  314.         ]);
  315.     }
  316.     #[Route('/{id}'name'fiches_delete'methods: ['POST'])]
  317.     public function delete(
  318.         Request $request,
  319.         Trainings $fiche,
  320.         TrainingsRepository $ficheRepository
  321.     ): Response
  322.     {
  323.         if ($this->isCsrfTokenValid('delete'.$fiche->getId(), $request->request->get('_token'))) {
  324.             $ficheRepository->remove($fichetrue);
  325.         }
  326.         return $this->redirectToRoute('fiches_index', [], Response::HTTP_SEE_OTHER);
  327.     }
  328. }