src/Controller/JobOfferController.php line 195

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.         // test if account is created for company and school
  81.         if($company==null && $school==null) {
  82.              if ($this->getUser()->hasRole('ROLE_ENTREPRISE')) {
  83.                  return $this->redirectToRoute('front_company_new');
  84.              } elseif ( $this->getUser()->hasRole('ROLE_ETABLISSEMENT')) {
  85.                  return $this->redirectToRoute('front_school_new');
  86.              }
  87.         }
  88.         return $this->render('jobOffer/index.html.twig', [
  89.             'jobOffers' => $jobOffers,
  90.             'othersJobs' => $othersJobOffers,
  91.             'jobApplieds' => $jobApplieds,
  92.         ]);
  93.     }
  94.     // #[IsGranted('ROLE_ADMIN')]
  95.     #[Route(path'/jobApplied'name'job_applied_index'methods: ['GET'])]
  96.     public function jobAppliedAction(): Response {
  97.         $jobApplieds $this->jobAppliedRepository->getAll();
  98.         $school null;
  99.         $company null;
  100.         if (($this->getUser()->hasRole('ROLE_ETABLISSEMENT')) || ($this->getUser()->hasRole('ROLE_PRINCIPAL'))) {
  101.             $school $this->schoolService->getSchool();
  102.             // only for Principal role
  103.             if (!$school) {
  104.                 $school $this->schoolRepository->find($this->getUser()->getPrincipalSchool());
  105.             }
  106.             $jobApplieds $this->jobAppliedRepository->getByUserSchool($school->getUser()->getId());
  107.         }
  108.         if ($this->getUser()->hasRole('ROLE_ENTREPRISE')) {
  109.             $company $this->companyService->getCompany();
  110.             $jobApplieds $this->jobAppliedRepository->getByUserCompany($company->getUser()->getId());
  111.         }
  112.         if ($this->getUser()->hasRole('ROLE_DIPLOME')) {
  113.             $jobApplieds $this->jobAppliedRepository->getByUserPersonDegree($this->getUser()->getId());
  114.         }
  115.         // test if account is created for company and school
  116.         if($company==null && $school==null) {
  117.             if ($this->getUser()->hasRole('ROLE_ENTREPRISE')) {
  118.                 return $this->redirectToRoute('front_company_new');
  119.             } elseif ( $this->getUser()->hasRole('ROLE_ETABLISSEMENT')) {
  120.                 return $this->redirectToRoute('front_school_new');
  121.             }
  122.         }
  123.         return $this->render('jobOffer/jobApplied.html.twig', [
  124.             'jobApplieds' => $jobApplieds
  125.         ]);
  126.     }
  127.     #[IsGranted('ROLE_USER')]
  128.     #[Route(path'/new'name'jobOffer_new'methods: ['GET''POST'])]
  129.     public function newAction(Request $request): RedirectResponse|Response {
  130.         $school null;
  131.         $company null;
  132.         $jobOffer = new JobOffer();
  133.         $form $this->createForm(JobOfferType::class, $jobOffer);
  134.         $form->handleRequest($request);
  135.         if ($this->getUser()->hasRole('ROLE_ENTREPRISE')) {
  136.             $company $this->companyService->getCompany();
  137.             $jobOffer->setCompany($company);
  138.         // } elseif ($this->getUser()->hasRole('ROLE_PRINCIPAL')) {
  139.         //     $school = $this->getUser()->getPrincipalSchool();
  140.         //     $jobOffer->setSchool($school);
  141.         } elseif ($this->getUser()->hasRole('ROLE_ETABLISSEMENT')) {
  142.             $school $this->schoolService->getSchool();
  143.             $jobOffer->setSchool($school);
  144.         }
  145.         // test if account is created for company and school
  146.         if($company==null && $school==null) {
  147.             if ($this->getUser()->hasRole('ROLE_ENTREPRISE')) {
  148.                 return $this->redirectToRoute('front_company_new');
  149.             } elseif ( $this->getUser()->hasRole('ROLE_ETABLISSEMENT')) {
  150.                 return $this->redirectToRoute('front_school_new');
  151.             }
  152.         }
  153.         if ($form->isSubmitted() && $form->isValid()) {
  154.             $offerDescription $form->get('file')->getData();
  155.             if ($offerDescription) {
  156.                 $offerDescriptionFileName $this->fileUploader->upload($offerDescription);
  157.                 $jobOffer->setFilename($offerDescriptionFileName);
  158.             }
  159.             $this->em->persist($jobOffer);
  160.             $this->em->flush();
  161.             return $this->redirectToRoute('jobOffer_show', ['id' => $jobOffer->getId()]);
  162.         }
  163.         return $this->render('jobOffer/new.html.twig', [
  164.             'school' => $school,
  165.             'company' => $company,
  166.             'jobOffer' => $jobOffer,
  167.             'form' => $form->createView(),
  168.         ]);
  169.     }
  170.     #[Route(path'/{id}'name'jobOffer_show'methods: ['GET'])]
  171.     public function showAction(JobOffer $jobOffer): Response {
  172.         $this->companyService->markJobOfferAsView($jobOffer->getId());
  173.         return $this->render('jobOffer/show.html.twig', [
  174.             'jobOffer' => $jobOffer,
  175.         ]);
  176.     }
  177.     #[IsGranted('ROLE_USER')]
  178.     #[Route(path'/{id}/edit'name'jobOffer_edit'methods: ['GET''POST'])]
  179.     public function editAction(Request $requestJobOffer $jobOffer): RedirectResponse|Response {
  180.         $school null;
  181.         $company null;
  182.         if ($this->getUser()->hasRole('ROLE_ENTREPRISE')) {
  183.             $company $this->companyService->getCompany();
  184.             if ($jobOffer->getCompany() !== $company)
  185.                 throw new NotFoundHttpException('Aucune offre trouvée');
  186.         } elseif ($this->getUser()->hasRole('ROLE_PRINCIPAL'))  {
  187.             $school $this->getUser()->getPrincipalSchool();
  188.             if ($jobOffer->getSchool() !== $school)
  189.                 throw new NotFoundHttpException('Aucune offre trouvée');
  190.         } elseif ($this->getUser()->hasRole('ROLE_ETABLISSEMENT')) {
  191.             $school $this->schoolService->getSchool();
  192.             if ($jobOffer->getSchool() !== $school)
  193.                 throw new NotFoundHttpException('Aucune offre trouvée');
  194.         }
  195.         $editForm $this->createForm(JobOfferType::class, $jobOffer);
  196.         $editForm->handleRequest($request);
  197.         if ($editForm->isSubmitted() && $editForm->isValid()) {
  198.             $offerDescription $editForm->get('file')->getData();
  199.             if ($offerDescription) {
  200.                 $offerDescriptionFileName $this->fileUploader->upload($offerDescription$jobOffer->getFilename());
  201.                 $jobOffer->setFilename($offerDescriptionFileName);
  202.             }
  203.             if ($this->getUser()->hasRole('ROLE_ENTREPRISE')) {
  204.                 $jobOffer->setCompany($company);
  205.             } elseif ($this->getUser()->hasRole('ROLE_PRINCIPAL') || $this->getUser()->hasRole('ROLE_ETABLISSEMENT')) {
  206.                 $jobOffer->setSchool($school);
  207.             }
  208.             $jobOffer->setUpdatedDate(new \DateTime());
  209.             $this->em->flush();
  210.             return $this->redirectToRoute('jobOffer_show', ['id' => $jobOffer->getId()]);
  211.         }
  212.         return $this->render('jobOffer/edit.html.twig', [
  213.             'school' => $school,
  214.             'company' => $company,
  215.             'jobOffer' => $jobOffer,
  216.             'edit_form' => $editForm->createView()
  217.         ]);
  218.     }
  219.     #[Security("is_granted('ROLE_ADMIN') or 
  220.             is_granted('ROLE_ETABLISSEMENT') or 
  221.             is_granted('ROLE_ENTREPRISE')")]
  222.     #[Route(path'/delete/{id}'name'jobOffer_delete'methods: ['GET'])]
  223.     public function deleteAction(Request $request, ?JobOffer $jobOffer): RedirectResponse {
  224.         if (array_key_exists('HTTP_REFERER'$request->server->all())) {
  225.             if ($jobOffer) {
  226.                 $this->fileUploader->removeOldFile($jobOffer->getFilename());
  227.                 $this->em->remove($jobOffer);
  228.                 $this->em->flush();
  229.                 $this->addFlash('success'$this->translator->trans('flashbag.the_deletion_is_done_successfully'));
  230.             } else {
  231.                 $this->addFlash('warning'$this->translator->trans('flashbag.unable_to_delete_offer'));
  232.                 return $this->redirect($request->server->all()['HTTP_REFERER']);
  233.             }
  234.         }
  235.         return $this->redirectToRoute('jobOffer_index');
  236.     }
  237. }