src/Controller/SchoolController.php line 199

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