src/Controller/CompanyController.php line 99

  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use App\Entity\Company;
  9. use App\Entity\Contact;
  10. use App\Entity\ProspectFile;
  11. use App\Entity\Hardware;
  12. use App\Entity\Feature;
  13. use App\Entity\FeatureModel;
  14. use App\Entity\CompanyType;
  15. use App\Entity\CompanyLead;
  16. use App\Form\CompanyForm;
  17. use App\Form\ContactForm;
  18. use App\Form\HardwareForm;
  19. use App\Repository\CompanyRepository;
  20. use App\Repository\ContactRepository;
  21. use App\Repository\HardwareTypeRepository;
  22. use App\Repository\HardwareRepository;
  23. use App\Repository\ProspectFileRepository;
  24. use App\Repository\FeatureModelRepository;
  25. use App\Repository\UserRepository;
  26. use App\Repository\CompanyTypeRepository;
  27. use App\Repository\CompanyLeadRepository;
  28. class CompanyController extends AbstractController
  29. {
  30.     
  31.     #[Route(path'/companies'name'companies')]
  32.     public function companies(CompanyRepository $companyRepository): Response
  33.     {
  34.         $company = new Company();
  35.         $form $this->createForm(CompanyForm::class, $company , [
  36.             'user' => $this->getUser() ,
  37.         ]);
  38.         
  39.         
  40.         if ( in_array("MANAGER"$this->getUser()->getRoles() ) ) {
  41.             $companies $companyRepository->findBy( ['userGroup' => $this->getUser()->getUserGroup()] );
  42.         }else{
  43.             $companies $companyRepository->findByUser$this->getUser()->getId() );
  44.         }
  45.         return $this->render('company/companies.html.twig', ["companies" => $companies"form" => $form ]);
  46.     }
  47.     
  48.     #[Route(path'/addCompany'name'add_company')]
  49.     public function addCompany(CompanyRepository $companyRepositoryRequest $requestManagerRegistry $doctrine): Response
  50.     {
  51.         if( $request->get('company_form_id') == "" ){
  52.             $company = new Company();
  53.         }else{
  54.             $company $companyRepository->find$request->get('company_form_id') );
  55.         }
  56.         $form $this->createForm(CompanyForm::class, $company, [
  57.             'user' => $this->getUser() ,
  58.         ]);
  59.         $form->handleRequest($request);
  60.         if ($form->isSubmitted() && $form->isValid()) {
  61.             $company $form->getData();
  62.             $company->setUserGroup$this->getUser()->getUserGroup() );
  63.             $company->addUser$this->getUser() );
  64.             $em $doctrine->getManager();
  65.             $em->persist($company);
  66.             $em->flush();
  67.             return $this->redirectToRoute('companies');
  68.         }
  69.         return $this->redirectToRoute('companies');
  70.     }
  71.     #[Route(path'/deleteCompany/{id}'name'delete_company')]
  72.     public function deleteCompany(Request $requestCompany $companyManagerRegistry $doctrine ): Response
  73.     {
  74.         
  75.         $em $doctrine->getManager();
  76.         $em->remove($company);
  77.         $em->flush();
  78.         return $this->redirectToRoute('companies');
  79.     }
  80.     
  81.     #[Route(path'/infoCompany/{id}/{navlink?}'name'info_company')]
  82.     public function infoCompany(Request $requestCompany $company$navlink null 
  83.     HardwareTypeRepository $hardwareTypeRepositoryContactRepository $contactRepository): Response
  84.     {
  85.         
  86.         $contact = new Contact();
  87.         $contactForm $this->createForm(ContactForm::class, $contact);
  88.         $hardwareTypes $hardwareTypeRepository->findAll();
  89.         
  90.         return $this->render('company/infoCompany.html.twig', ["company" => $company,
  91.         "hardwareTypes" => $hardwareTypes"contactForm" => $contactForm"navlink" => $navlink]);
  92.     }
  93.     
  94.     #[Route('/addContact'name'add_contact')]
  95.     public function addContact(Request $requestManagerRegistry $doctrine
  96.     HardwareRepository $hardwareRepositoryCompanyRepository $companyRepositoryContactRepository $contactRepository): Response
  97.     {
  98.         $contact $request->get('contactId') == "" ? new Contact() :  $contactRepository->find$request->get('contactId') );
  99.         $company $companyRepository->find$request->get('companyId') );
  100.         $contactForm $this->createForm(ContactForm::class, $contact);
  101.         $contactForm->handleRequest($request);
  102.         
  103.         if ($contactForm->isSubmitted() && $contactForm->isValid()) {
  104.             
  105.             $contact $contactForm->getData();
  106.             $contact->setCompany($company);
  107.             $em $doctrine->getManager();
  108.             $em->persist($contact);
  109.             $em->flush();
  110.         }
  111.         return $this->redirectToRoute('info_company', ['id' =>  $company->getId(), 'navlink'=> 'contacts']);
  112.     }
  113.     
  114.     
  115.     #[Route('/newHardware'name'new_hardware')]
  116.     public function newHardware(Request $requestManagerRegistry $doctrineCompanyRepository $companyRepository
  117.     FeatureModelRepository $featureModelRepository,HardwareTypeRepository $hardwareTypeRepository): Response
  118.     {
  119.         $hardware = new Hardware();
  120.         $company $companyRepository->find$request->get('company_id') );
  121.         $hardwareType $hardwareTypeRepository->find$request->get('type_id') );
  122.         
  123.         $hardware->setCompany($company);
  124.         $hardware->setHardwareType($hardwareType);
  125.         $featureModels $featureModelRepository->findBy(
  126.             ['userGroup' => $this->getUser()->getUserGroup()],
  127.         );
  128.         $hardwareForm $this->createForm(HardwareForm::class, $hardware);
  129.         
  130.         $contact = new Contact();
  131.         $contactForm $this->createForm(ContactForm::class, $contact);
  132.         $typeId $request->get('type_id');
  133.         $hardwareTypes $hardwareTypeRepository->findAll();
  134.         return $this->render('company/hardwareForm.html.twig', ['hardwareForm' =>  $hardwareForm'contactForm' =>  $contactForm
  135.         'hardware' =>  $hardware"hardwareTypes" => $hardwareTypes 'featureModels' =>  $featureModels'navlink'=> 'editHardware']);
  136.     }
  137.     
  138.     #[Route('/editHardware/{id}'name'edit_hardware')]
  139.     public function editHardware(Request $requestManagerRegistry $doctrineCompanyRepository $companyRepository ,
  140.     Hardware $hardwareFeatureModelRepository $featureModelRepository,HardwareTypeRepository $hardwareTypeRepository ): Response
  141.     {
  142.         
  143.         $hardwareForm $this->createForm(HardwareForm::class, $hardware);
  144.         $featureModels $featureModelRepository->findBy(
  145.             ['userGroup' => $this->getUser()->getUserGroup()],
  146.         );
  147.         $company $hardware->getCompany();
  148.         
  149.         $contact = new Contact();
  150.         $contactForm $this->createForm(ContactForm::class, $contact);
  151.         
  152.         $hardwareForm->handleRequest($request);
  153.         if ($hardwareForm->isSubmitted() && $hardwareForm->isValid()) {
  154.             $hardware $hardwareForm->getData();
  155.             $em $doctrine->getManager();
  156.             foreach ( $request->get('feature') as $key => $value) {
  157.                 $feature $hardware->getFeatureByName($key);
  158.                 $feature->setValue($value);
  159.                 // ------- CHECK NEW MODEL ---------
  160.                 $key str_contains($key'-MARQUE')? "MARQUE"$key;
  161.                 $newFeature 1;
  162.                 foreach ($featureModels as $featureModel) {
  163.                     if( ( ($featureModel->getName() === $key) && ($featureModel->getValue() === $value)  )|| str_contains($key'DATE') ){
  164.                         $newFeature 0;
  165.                     }
  166.                 }
  167.                 if($newFeature){
  168.                     $featureModel = new FeatureModel();
  169.                     $featureModel->setName($key)->setValue($value);
  170.                     $featureModel->setUserGroup$this->getUser()->getUserGroup() );
  171.                     $value == "" "" :$em->persist($featureModel);
  172.                 }
  173.             }
  174.             $em->persist($hardware);
  175.             $em->flush();
  176.             return $this->redirectToRoute('info_company', ['id' =>  $company->getId(), 'navlink'=> 'hardwares']);
  177.         }
  178.         $hardwareTypes $hardwareTypeRepository->findAll();
  179.         return $this->render('company/hardwareForm.html.twig', ['hardwareForm' =>  $hardwareForm'contactForm' =>  $contactForm
  180.         'hardware' =>  $hardware"hardwareTypes" => $hardwareTypes'featureModels' =>  $featureModels'navlink'=> 'editHardware']);
  181.     }
  182.     
  183.     #[Route('/addHardware'name'add_hardware')]
  184.     public function addHardware(Request $requestManagerRegistry $doctrineCompanyRepository $companyRepository,
  185.      HardwareTypeRepository $hardwareTypeRepository): Response
  186.     {
  187.         $hardware = new Hardware();
  188.         $company $companyRepository->find$request->get('companyId') );
  189.         
  190.         $hardwareForm $this->createForm(HardwareForm::class, $hardware);
  191.         $hardwareForm->handleRequest($request);
  192.         
  193.         $hardware->setCompany($company);
  194.         
  195.         $hardwareType $hardwareTypeRepository->find$request->get('hardwareTypeId')  );
  196.         $hardware->setHardwareType$hardwareType );
  197.         if ($hardwareForm->isSubmitted() && $hardwareForm->isValid()) {
  198.             $hardware $hardwareForm->getData();
  199.             $em $doctrine->getManager();
  200.             foreach ( $request->get('feature') as $key => $value) {
  201.                 
  202.                 $feature = new Feature();
  203.                 $feature->setHardware($hardware);
  204.                 $feature->setName($key);
  205.                 $feature->setValue($value);
  206.                 $em->persist($feature);
  207.             }
  208.             $em->persist($hardware);
  209.             $em->flush();
  210.         }
  211.         
  212.         return $this->redirectToRoute('info_company', ['id' =>  $company->getId(), 'navlink'=> 'hardwares']);
  213.     }
  214.     #[Route('/addCompanyUser'name'add_company_user'methods:"POST")]
  215.     public function addCompanyUser(Request $requestManagerRegistry $doctrineCompanyRepository $companyRepositoryUserRepository $userRepository): Response
  216.     {
  217.         $company $companyRepository->find$request->get('companyId') );
  218.         $user $userRepository->find$request->get('companyUserSelect') );
  219.         $company->addUser($user);
  220.         
  221.         $em $doctrine->getManager();
  222.         $em->persist($company);
  223.         $em->flush();
  224.         return $this->redirectToRoute('info_company', ['id' =>  $company->getId()]);
  225.     }
  226.     #[Route('/deleteCompanyUser'name'delete_company_user'methods:"POST")]
  227.     public function deleteCompanyUser(Request $requestManagerRegistry $doctrineCompanyRepository $companyRepositoryUserRepository $userRepository): Response
  228.     {
  229.         
  230.         $company $companyRepository->find$request->get('companyId') );
  231.         $user $userRepository->find$request->get('companyUserSelect') );
  232.         
  233.         $company->removeUser($user);
  234.         $em $doctrine->getManager();
  235.         $em->persist($company);
  236.         $em->flush();
  237.         return $this->redirectToRoute('info_company', ['id' =>  $company->getId()]);
  238.     }
  239.     #[Route('/addCompanyType'name'add_companyType'methods:"POST")]
  240.     public function addCompanyType(Request $requestManagerRegistry $doctrine): Response
  241.     {
  242.         $companyType = new CompanyType();
  243.         $companyType->setName$request->get('companyTypeName') );
  244.         $companyType->setUserGroup$this->getUser()->getUserGroup() );
  245.         
  246.         $em $doctrine->getManager();
  247.         $em->persist$companyType );
  248.         $em->flush();
  249.         
  250.         return $this->redirectToRoute("settings");
  251.     }
  252.     
  253.     #[Route('/deleteCompanyType'name'delete_companyType'methods:"POST")]
  254.     public function deleteCompanyType(Request $requestCompanyTypeRepository $companyTypeRepositoryManagerRegistry $doctrine): Response
  255.     {
  256.         $companyType $companyTypeRepository->find$request->get('companyTypeId') );
  257.         if( $companyType->getUserGroup() !== $this->getUser()->getUserGroup() ){
  258.             return $this->redirectToRoute("settings");
  259.         }
  260.         if( $companyType->getCompanies()->count() == ){
  261.             $em $doctrine->getManager();
  262.             $em->remove($companyType);
  263.             $em->flush();
  264.         }else{
  265.             $this->addFlash("warning""Des entreprises sont reliées. Impossible de supprimer");
  266.         }
  267.         return $this->redirectToRoute("settings");
  268.     }
  269.     
  270.     #[Route('/addCompanyLead'name'add_companyLead'methods:"POST")]
  271.     public function addCompanyLead(Request $requestManagerRegistry $doctrine): Response
  272.     {
  273.         $companyLead = new CompanyLead();
  274.         $companyLead->setName$request->get('companyLeadName') );
  275.         $companyLead->setUserGroup$this->getUser()->getUserGroup() );
  276.         
  277.         $em $doctrine->getManager();
  278.         $em->persist$companyLead );
  279.         $em->flush();
  280.         return $this->redirectToRoute("settings");
  281.     }
  282.     
  283.     #[Route('/deleteCompanyLead'name'delete_companyLead'methods:"POST")]
  284.     public function deleteCompanyLead(Request $requestCompanyLeadRepository $companyLeadRepositoryManagerRegistry $doctrine): Response
  285.     {
  286.         $companyLead $companyLeadRepository->find$request->get('companyLeadId') );
  287.         if( $companyLead->getUserGroup() !== $this->getUser()->getUserGroup() ){
  288.             return $this->redirectToRoute("settings");
  289.         }
  290.         if( $companyLead->getCompanies()->count() == ){
  291.             $em $doctrine->getManager();
  292.             $em->remove($companyLead);
  293.             $em->flush();
  294.         }else{
  295.             $this->addFlash("warning""Des entreprises sont reliées. Impossible de supprimer");
  296.         }
  297.         return $this->redirectToRoute("settings");
  298.     }
  299. }