src/Controller/CompanyController.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Company;
  4. use App\Entity\Country;
  5. use App\Form\CompanyType;
  6. use App\Repository\CompanyRepository;
  7. use App\Repository\SchoolRepository;
  8. use App\Repository\UserRepository;
  9. use App\Services\ActivityService;
  10. use App\Services\CompanyService;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. #[Route(path'/company')]
  21. #[Security("is_granted('ROLE_ADMIN') or 
  22.             is_granted('ROLE_LEGISLATEUR') or 
  23.             is_granted('ROLE_DIRECTEUR') or 
  24.             is_granted('ROLE_ADMIN_REGIONS') or 
  25.             is_granted('ROLE_ADMIN_PAYS') or 
  26.             is_granted('ROLE_ADMIN_VILLES') or 
  27.             is_granted('ROLE_PRINCIPAL')")]
  28. class CompanyController extends AbstractController {
  29.     private EntityManagerInterface $em;
  30.     private ActivityService $activityService;
  31.     private UserRepository $userRepository;
  32.     private CompanyRepository $companyRepository;
  33.     private SchoolRepository $schoolRepository;
  34.     private CompanyService $companyService;
  35.     private TranslatorInterface $translator;
  36.     public function __construct(
  37.         EntityManagerInterface $em,
  38.         ActivityService        $activityService,
  39.         UserRepository         $userRepository,
  40.         CompanyRepository      $companyRepository,
  41.         SchoolRepository       $schoolRepository,
  42.         CompanyService         $companyService,
  43.         TranslatorInterface    $translator
  44.     ) {
  45.         $this->em $em;
  46.         $this->activityService $activityService;
  47.         $this->userRepository $userRepository;
  48.         $this->companyRepository $companyRepository;
  49.         $this->schoolRepository $schoolRepository;
  50.         $this->companyService $companyService;
  51.         $this->translator $translator;
  52.     }
  53.     #[Route(path'/'name'company_index'methods: ['GET'])]
  54.     public function indexAction(Request $request): Response {
  55.         $userCountry $this->getUser()->getCountry();
  56.         $companies $this->companyRepository->findAll();
  57.         if ($userCountry) {
  58.             $companies $this->companyRepository->findByCountry($userCountry);
  59.         }
  60.         // adaptation for multi administrators
  61.         $userRegions = [];
  62.         $userCities = [];
  63.         if ($this->getUser()->hasRole('ROLE_ADMIN_REGIONS')) {
  64.             $userRegions =  $this->getUser()->getAdminRegions();
  65.         } else if ($this->getUser()->hasRole('ROLE_ADMIN_VILLES')) {
  66.             $userCities =  $this->getUser()->getAdminCities();
  67.         }
  68.         if(count($userRegions) > 0) {
  69.             $companies = [];
  70.             foreach ($userRegions as $region) {
  71.                 $companies array_merge($companies$this->companyRepository->findByRegion($region));
  72.             }
  73.         } else if(count($userCities) > 0) {
  74.             $companies = [];
  75.             foreach ($userCities as $city) {
  76.                 $companies array_merge($companies$this->companyRepository->findByCity($city));
  77.             }
  78.         }
  79.         //For principal Role
  80.         if($this->getUser()->getPrincipalSchool()) {
  81.             $school $this->schoolRepository->find($this->getUser()->getPrincipalSchool());
  82.             $companies $this->companyRepository->getBySchool($school);
  83.         }
  84.         return $this->render('company/index.html.twig', ['companies' => $companies]);
  85.     }
  86.     #[Route(path'/new'name'company_new'methods: ['GET''POST'])]
  87.     public function newAction(Request $request): RedirectResponse|Response {
  88.         $company = new Company();
  89.         $company->setLocationMode(true);
  90.         $form $this->createForm(CompanyType::class, $company);
  91.         $form->handleRequest($request);
  92.         $selectedCountry = new Country();
  93.         //adaptation for DBTA
  94.         $selectedRegion null;
  95.         if($_ENV['STRUCT_PROVINCE_COUNTRY_CITY'] == 'true') {
  96.             $selectedRegion = new Region();
  97.         }
  98.         if ($form->isSubmitted() && $form->isValid()) {
  99.             $company->setCreatedDate(new \DateTime());
  100.             $company->setUpdatedDate(new \DateTime());
  101.             $dnsServer $this->getParameter('dnsServer');
  102.             if ((php_uname('n') != $dnsServer)&&(php_uname('n') != null))
  103.                 $company->setClientUpdateDate(new \DateTime());
  104.             $this->em->persist($company);
  105.             $this->em->flush();
  106.             return $this->redirectToRoute('company_show', ['id' => $company->getId()]);
  107.         }
  108.         return $this->render('company/new.html.twig', [
  109.             'company' => $company,
  110.             'form' => $form->createView(),
  111.             'allActivities' => $this->activityService->getAllActivities(),
  112.             'selectedCountry' => $selectedCountry,
  113.             'selectedRegion' => $selectedRegion
  114.         ]);
  115.     }
  116.     #[Route(path'/{id}'name'company_show'methods: ['GET'])]
  117.     public function showAction(Company $company): Response {
  118.         return $this->render('company/show.html.twig', array(
  119.             'company' => $company
  120.         ));
  121.     }
  122.     #[Route(path'/{id}/edit'name'company_edit'methods: ['GET''POST'])]
  123.     public function editAction(Request $requestCompany $company): RedirectResponse|Response {
  124.         $createdDate $company->getCreatedDate();
  125.         $editForm $this->createForm(CompanyType::class, $company);
  126.         $editForm->handleRequest($request);
  127.         $selectedCountry $company->getCountry();
  128.         //adaptation for DBTA
  129.         $selectedRegion null;
  130.         if($_ENV['STRUCT_PROVINCE_COUNTRY_CITY'] == 'true') {
  131.             $selectedRegion $this->getUser()->getRegion();
  132.         }
  133.         if ($editForm->isSubmitted() && $editForm->isValid()) {
  134.             $currentUser $this->userRepository->getFromCompany($company->getId());
  135.             if (count($currentUser) > 0)
  136.                 $company->setUser($currentUser[0]);
  137.             $company->setCreatedDate($createdDate);
  138.             if ($company->getCreatedDate() == null) {
  139.                 if ($company->getUpdatedDate()) {
  140.                     $company->setCreatedDate($company->getUpdatedDate());
  141.                 } else {
  142.                     $company->setCreatedDate(new \DateTime());
  143.                 }
  144.             }
  145.             $company->setUpdatedDate(new \DateTime());
  146.             $dnsServer $this->getParameter('dnsServer');
  147.             if ((php_uname('n') != $dnsServer)&&(php_uname('n') != null))
  148.                 $company->setClientUpdateDate(new \DateTime());
  149.             $this->em->persist($company);
  150.             $this->em->flush();
  151.             return $this->redirectToRoute('company_show', array('id' => $company->getId()));
  152.         }
  153.         return $this->render('company/edit.html.twig', array(
  154.             'company' => $company,
  155.             'edit_form' => $editForm->createView(),
  156.             'allActivities' => $this->activityService->getAllActivities(),
  157.             'selectedCountry' => $selectedCountry,
  158.             'selectedRegion' => $selectedRegion
  159.         ));
  160.     }
  161.     #[Route(path'/delete/{id}'name'company_delete'methods: ['GET'])]
  162.     public function deleteElementAction(Request $request, ?Company $company): RedirectResponse {
  163.         if (array_key_exists('HTTP_REFERER'$request->server->all())) {
  164.             if ($company) {
  165.                 $user $company->getUser();
  166.                 if($user) {
  167.                     $this->companyService->removeRelations($user);
  168.                     $this->em->remove($user);
  169.                     $this->em->flush();
  170.                     $this->addFlash('success'$this->translator->trans('flashbag.the_deletion_of_the_user_is_done_with_success'));
  171.                 } else {
  172.                     $this->addFlash('warning'$this->translator->trans('flashbag.unable_to_delete_user'));
  173.                 }
  174.             } else {
  175.                 $this->addFlash('warning'$this->translator->trans('flashbag.unable_to_delete_company'));
  176.                 return $this->redirect($request->server->all()['HTTP_REFERER']);
  177.             }
  178.         }
  179.         return $this->redirectToRoute('company_index');
  180.     }
  181. }