src/Controller/JobOfferController.php line 186

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Company;
  4. use App\Entity\JobOffer;
  5. use App\Entity\School;
  6. use App\Form\JobOfferType;
  7. use App\Repository\JobOfferRepository;
  8. use App\Repository\JobAppliedRepository;
  9. use App\Repository\SchoolRepository;
  10. use App\Services\SchoolService;
  11. use App\Services\CompanyService;
  12. use App\Services\FileUploader;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Symfony\Contracts\Translation\TranslatorInterface;
  24. #[Route('/jobOffer')]
  25. class JobOfferController extends AbstractController {
  26.     private EntityManagerInterface $em;
  27.     private JobOfferRepository $jobOfferRepository;
  28.     private JobAppliedRepository $jobAppliedRepository;
  29.     private SchoolRepository $schoolRepository;
  30.     private CompanyService $companyService;
  31.     private SchoolService $schoolService;
  32.     private FileUploader $fileUploader;
  33.     private TranslatorInterface $translator;
  34.     public function __construct(
  35.         EntityManagerInterface $em,
  36.         JobOfferRepository $jobOfferRepository,
  37.         JobAppliedRepository $jobAppliedRepository,
  38.         SchoolRepository $schoolRepository,
  39.         CompanyService $companyService,
  40.         SchoolService $schoolService,
  41.         FileUploader $fileUploader,
  42.         TranslatorInterface $translator
  43.     ) {
  44.         $this->em $em;
  45.         $this->jobOfferRepository $jobOfferRepository;
  46.         $this->jobAppliedRepository $jobAppliedRepository;
  47.         $this->schoolRepository $schoolRepository;
  48.         $this->companyService $companyService;
  49.         $this->schoolService $schoolService;
  50.         $this->fileUploader $fileUploader;
  51.         $this->translator $translator;
  52.     }
  53.     #[IsGranted('ROLE_USER')]
  54.     #[Route(path'/'name'jobOffer_index'methods: ['GET'])]
  55.     public function indexAction(): Response {
  56.         $company null;
  57.         $school null;
  58.         $jobOffers $this->jobOfferRepository->getAllJobOffer();
  59.         $jobApplieds $this->jobAppliedRepository->getAll();
  60.         $othersJobOffers null;
  61.         if ($this->getUser()->hasRole('ROLE_DIPLOME')) {
  62.             $jobApplieds $this->jobAppliedRepository->getByUserPersonDegree($this->getUser()->getId());
  63.         }
  64.         if ($this->getUser()->hasRole('ROLE_ENTREPRISE')) {
  65.             $company $this->companyService->getCompany();
  66.             $jobOffers $this->jobOfferRepository->findBy(['company' => $company], ['id' => 'DESC']);
  67.             $othersJobOffers $this->jobOfferRepository->othersDifferentOfId($company->getId());
  68.         }
  69.         if (($this->getUser()->hasRole('ROLE_ETABLISSEMENT')) || ($this->getUser()->hasRole('ROLE_PRINCIPAL'))) {
  70.             $school $this->schoolService->getSchool();
  71.             //only for Principal role
  72.             if (!$school) {
  73.                 $school $this->schoolRepository->find($this->getUser()->getPrincipalSchool());
  74.             }
  75.             if ($school) {
  76.                 $jobOffers $this->jobOfferRepository->findBySchool($school);
  77.                 $othersJobOffers $this->jobOfferRepository->othersDifferentOfId($school->getId());
  78.             }
  79.         }
  80.         if ($company == null && $school == null) {
  81.             if ($this->getUser()->hasRole('ROLE_ENTREPRISE')) {
  82.                 return $this->redirectToRoute('front_company_new');
  83.             } elseif ($this->getUser()->hasRole('ROLE_ETABLISSEMENT')) {
  84.                 return $this->redirectToRoute('front_school_new');
  85.             }
  86.         }
  87.         return $this->render('jobOffer/index.html.twig', [
  88.             'jobOffers' => $jobOffers,
  89.             'othersJobs' => $othersJobOffers,
  90.             'jobApplieds' => $jobApplieds,
  91.         ]);
  92.     }
  93.     #[Route(path'/jobApplied'name'job_applied_index'methods: ['GET'])]
  94.     public function jobAppliedAction(): Response {
  95.         $jobApplieds $this->jobAppliedRepository->getAll();
  96.         $school null;
  97.         $company null;
  98.         if (($this->getUser()->hasRole('ROLE_ETABLISSEMENT')) || ($this->getUser()->hasRole('ROLE_PRINCIPAL'))) {
  99.             $school $this->schoolService->getSchool();
  100.             if (!$school) {
  101.                 $school $this->schoolRepository->find($this->getUser()->getPrincipalSchool());
  102.             }
  103.             $jobApplieds $this->jobAppliedRepository->getByUserSchool($school->getUser()->getId());
  104.         }
  105.         if ($this->getUser()->hasRole('ROLE_ENTREPRISE')) {
  106.             $company $this->companyService->getCompany();
  107.             $jobApplieds $this->jobAppliedRepository->getByUserCompany($company->getUser()->getId());
  108.         }
  109.         if ($this->getUser()->hasRole('ROLE_DIPLOME')) {
  110.             $jobApplieds $this->jobAppliedRepository->getByUserPersonDegree($this->getUser()->getId());
  111.         }
  112.         if ($company == null && $school == null) {
  113.             if ($this->getUser()->hasRole('ROLE_ENTREPRISE')) {
  114.                 return $this->redirectToRoute('front_company_new');
  115.             } elseif ($this->getUser()->hasRole('ROLE_ETABLISSEMENT')) {
  116.                 return $this->redirectToRoute('front_school_new');
  117.             }
  118.         }
  119.         return $this->render('jobOffer/jobApplied.html.twig', [
  120.             'jobApplieds' => $jobApplieds
  121.         ]);
  122.     }
  123.     #[IsGranted('ROLE_USER')]
  124.     #[Route(path'/new'name'jobOffer_new'methods: ['GET''POST'])]
  125.     public function newAction(Request $request): RedirectResponse|Response {
  126.         $school null;
  127.         $company null;
  128.         $jobOffer = new JobOffer();
  129.         $form $this->createForm(JobOfferType::class, $jobOffer);
  130.         $form->handleRequest($request);
  131.         if ($this->getUser()->hasRole('ROLE_ENTREPRISE')) {
  132.             $company $this->companyService->getCompany();
  133.             $jobOffer->setCompany($company);
  134.         } elseif ($this->getUser()->hasRole('ROLE_ETABLISSEMENT')) {
  135.             $school $this->schoolService->getSchool();
  136.             $jobOffer->setSchool($school);
  137.         }
  138.         // test if account is created for company and school
  139.         if ($company == null && $school == null) {
  140.             if ($this->getUser()->hasRole('ROLE_ENTREPRISE')) {
  141.                 return $this->redirectToRoute('front_company_new');
  142.             } elseif ($this->getUser()->hasRole('ROLE_ETABLISSEMENT')) {
  143.                 return $this->redirectToRoute('front_school_new');
  144.             }
  145.         }
  146.         if ($form->isSubmitted() && $form->isValid()) {
  147.             $offerDescription $form->get('file')->getData();
  148.             if ($offerDescription) {
  149.                 $offerDescriptionFileName $this->fileUploader->upload($offerDescription);
  150.                 $jobOffer->setFilename($offerDescriptionFileName);
  151.             }
  152.             $this->em->persist($jobOffer);
  153.             $this->em->flush();
  154.             return $this->redirectToRoute('jobOffer_show', ['id' => $jobOffer->getId()]);
  155.         }
  156.         return $this->render('jobOffer/new.html.twig', [
  157.             'school' => $school,
  158.             'company' => $company,
  159.             'jobOffer' => $jobOffer,
  160.             'form' => $form->createView(),
  161.         ]);
  162.     }
  163.     #[Route(path'/{id}'name'jobOffer_show'methods: ['GET'])]
  164.     public function showAction(JobOffer $jobOffer): Response {
  165.         $this->companyService->markJobOfferAsView($jobOffer->getId());
  166.         return $this->render('jobOffer/show.html.twig', [
  167.             'jobOffer' => $jobOffer,
  168.         ]);
  169.     }
  170.     #[IsGranted('ROLE_USER')]
  171.     #[Route(path'/{id}/edit'name'jobOffer_edit'methods: ['GET''POST'])]
  172.     public function editAction(Request $requestJobOffer $jobOffer): RedirectResponse|Response {
  173.         $school null;
  174.         $company null;
  175.         if ($this->getUser()->hasRole('ROLE_ENTREPRISE')) {
  176.             $company $this->companyService->getCompany();
  177.             if ($jobOffer->getCompany() !== $company)
  178.                 throw new NotFoundHttpException('Aucune offre trouvée');
  179.         } elseif ($this->getUser()->hasRole('ROLE_PRINCIPAL')) {
  180.             $school $this->getUser()->getPrincipalSchool();
  181.             if ($jobOffer->getSchool() !== $school)
  182.                 throw new NotFoundHttpException('Aucune offre trouvée');
  183.         } elseif ($this->getUser()->hasRole('ROLE_ETABLISSEMENT')) {
  184.             $school $this->schoolService->getSchool();
  185.             if ($jobOffer->getSchool() !== $school)
  186.                 throw new NotFoundHttpException('Aucune offre trouvée');
  187.         }
  188.         $editForm $this->createForm(JobOfferType::class, $jobOffer);
  189.         $editForm->handleRequest($request);
  190.         if ($editForm->isSubmitted() && $editForm->isValid()) {
  191.             $offerDescription $editForm->get('file')->getData();
  192.             if ($offerDescription) {
  193.                 $offerDescriptionFileName $this->fileUploader->upload($offerDescription$jobOffer->getFilename());
  194.                 $jobOffer->setFilename($offerDescriptionFileName);
  195.             }
  196.             if ($this->getUser()->hasRole('ROLE_ENTREPRISE')) {
  197.                 $jobOffer->setCompany($company);
  198.             } elseif ($this->getUser()->hasRole('ROLE_PRINCIPAL') || $this->getUser()->hasRole('ROLE_ETABLISSEMENT')) {
  199.                 $jobOffer->setSchool($school);
  200.             }
  201.             $jobOffer->setUpdatedDate(new \DateTime());
  202.             $this->em->flush();
  203.             return $this->redirectToRoute('jobOffer_show', ['id' => $jobOffer->getId()]);
  204.         }
  205.         return $this->render('jobOffer/edit.html.twig', [
  206.             'school' => $school,
  207.             'company' => $company,
  208.             'jobOffer' => $jobOffer,
  209.             'edit_form' => $editForm->createView()
  210.         ]);
  211.     }
  212.     #[Security("is_granted('ROLE_ADMIN') or 
  213.             is_granted('ROLE_ETABLISSEMENT') or 
  214.             is_granted('ROLE_ENTREPRISE')")]
  215.     #[Route(path'/delete/{id}'name'jobOffer_delete'methods: ['GET'])]
  216.     public function deleteAction(Request $request, ?JobOffer $jobOffer): RedirectResponse {
  217.         if (array_key_exists('HTTP_REFERER'$request->server->all())) {
  218.             if ($jobOffer) {
  219.                 $this->fileUploader->removeOldFile($jobOffer->getFilename());
  220.                 $this->em->remove($jobOffer);
  221.                 $this->em->flush();
  222.                 $this->addFlash('success'$this->translator->trans('flashbag.the_deletion_is_done_successfully'));
  223.             } else {
  224.                 $this->addFlash('warning'$this->translator->trans('flashbag.unable_to_delete_offer'));
  225.                 return $this->redirect($request->server->all()['HTTP_REFERER']);
  226.             }
  227.         }
  228.         return $this->redirectToRoute('jobOffer_index');
  229.     }
  230. }