src/Controller/ThemesWebsite/Whileresume/Application/JobsController.php line 137

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ThemesWebsite\Whileresume\Application;
  3. use App\Entity\Core\Users;
  4. use App\Entity\Cvs\Candidates;
  5. use App\Entity\Cvs\CandidatesHasExperiences;
  6. use App\Entity\Cvs\CandidatesHasProjects;
  7. use App\Entity\Cvs\CandidatesHasServices;
  8. use App\Entity\Cvs\CandidatesHasSkills;
  9. use App\Entity\Cvs\Jobs;
  10. use App\Entity\Cvs\JobsHasLikes;
  11. use App\Form\Core\UsersType;
  12. use App\Form\Cvs\JobsForm;
  13. use App\Form\Cvs\JobsFrForm;
  14. use App\Services\Core\RequestData;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Symfony\Component\HttpFoundation\Cookie;
  24. use Symfony\Component\HttpFoundation\JsonResponse;
  25. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  26. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  27. use Doctrine\ORM\EntityManagerInterface;
  28. class JobsController extends AbstractController
  29. {
  30.     private $rd;
  31.     private $em;
  32.     public function __construct(RequestData $rd,
  33.                                 EntityManagerInterface $em
  34.     ) {
  35.         $this->rd $rd;
  36.         $this->em $em;
  37.     }
  38.     public function new(Request $request): Response
  39.     {
  40.         $locale $request->getLocale();
  41.         $user $this->getUser();
  42.         $jobClass JobsForm::class;
  43.         if($locale == "fr") {
  44.             $jobClass JobsFrForm::class;
  45.         }
  46.         $job = new Jobs();
  47.         $form $this->createForm($jobClass$job);
  48.         $form->handleRequest($request);
  49.         if ($form->isSubmitted() && $form->isValid()) {
  50.             $job->setSlug("0");
  51.             $job->setLocale($locale);
  52.             $job->setValidation(false);
  53.             $job->setVerification(false);
  54.             $job->setOnline(false);
  55.             $job->setEnterprise(null);
  56.             $job->setUser($user);
  57.             $this->em->persist($job);
  58.             $this->em->flush();
  59.             if($job->getSlug() != "0") {
  60.                 $job->setSlug($this->generateSlug($job->getId()));
  61.                 $this->em->persist($job);
  62.                 $this->em->flush();
  63.             }
  64.             if($locale !== "en") {
  65.                 return $this->redirectToRoute('locale_cvs_application_job_new_confirm',['_locale' => $locale]);
  66.             }
  67.             return $this->redirectToRoute('cvs_application_job_new_confirm');
  68.         }
  69.         return $this->render('application/whileresume/application/jobs/new_'.$locale.'.html.twig',[
  70.             'form' => $form->createView(),
  71.         ]);
  72.     }
  73.     private function generateSlug(int $id): string
  74.     {
  75.         $letters 'abcdefghijklmnopqrstuvwxyz';
  76.         $random '';
  77.         for ($i 0$i 6$i++) {
  78.             $random .= $letters[random_int(025)];
  79.         }
  80.         return $random '-' $id;
  81.     }
  82.     public function show(Request $request$slug): Response
  83.     {
  84.         $locale $request->getLocale();
  85.         $job $this->em->getRepository(Jobs::class)->findOneBy(['locale' => $locale'slug' => $slug'online' => true'validation' => true]);
  86.         if($job === null) {
  87.             throw $this->createNotFoundException('Page non trouvée');
  88.         }
  89.         $othersJobs null;
  90.         if($job->getEnterprise() != null) {
  91.             $othersJobs $this->em->getRepository(Jobs::class)->findBy(['enterprise' => $job->getEnterprise(), 'online' => true]);
  92.         }
  93.         // Offres similaires basées sur catégorie, compétences, ville, titre
  94.         $similarJobs $this->em->getRepository(Jobs::class)->findSimilarJobs($job5);
  95.         $count 0;
  96.         if($job->getViews() != null) {
  97.             $count $job->getViews() + 1;
  98.         }
  99.         $job->setViews($count);
  100.         $this->em->persist($job);
  101.         $this->em->flush();
  102.         return $this->render('application/whileresume/application/jobs/show.html.twig',[
  103.             'slug' => $slug,
  104.             'job' =>  $job,
  105.             'othersJobs' => $othersJobs,
  106.             'similarJobs' => $similarJobs
  107.         ]);
  108.     }
  109.     /**
  110.      * Retourne les détails d'une offre en JSON pour la fiche modale (AJAX).
  111.      * Si l'utilisateur n'est pas connecté, retourne uniquement les champs publics
  112.      * (résumé + meta) avec un flag "locked" sur les détails.
  113.      */
  114.     public function ajaxShow(Request $request$slug): JsonResponse
  115.     {
  116.         $locale $request->getLocale();
  117.         $user $this->getUser();
  118.         $job $this->em->getRepository(Jobs::class)->findOneBy([
  119.             'locale' => $locale,
  120.             'slug' => $slug,
  121.             'online' => true,
  122.             'validation' => true
  123.         ]);
  124.         if ($job === null) {
  125.             return new JsonResponse(['error' => 'not_found'], 404);
  126.         }
  127.         $isAuthenticated $user !== null;
  128.         $isCandidate $isAuthenticated && $user->getCandidate() !== null;
  129.         $enterpriseData null;
  130.         if ($job->getEnterprise() !== null) {
  131.             $enterpriseData = [
  132.                 'id' => $job->getEnterprise()->getId(),
  133.                 'slug' => $job->getEnterprise()->getSlug(),
  134.                 'name' => $job->getEnterprise()->getCompanyName(),
  135.                 'logo' => method_exists($job->getEnterprise(), 'getLogo') ? $job->getEnterprise()->getLogo() : null,
  136.                 'city' => method_exists($job->getEnterprise(), 'getCity') ? $job->getEnterprise()->getCity() : null,
  137.                 'country' => method_exists($job->getEnterprise(), 'getCountry') ? $job->getEnterprise()->getCountry() : null,
  138.             ];
  139.         }
  140.         // Données publiques (toujours visibles)
  141.         $payload = [
  142.             'id' => $job->getId(),
  143.             'slug' => $job->getSlug(),
  144.             'title' => $job->getJobTitle(),
  145.             'companyName' => $job->getCompanyName(),
  146.             'category' => $job->getCategory(),
  147.             'city' => $job->getCity(),
  148.             'country' => $job->getCountry(),
  149.             'employmentType' => $job->getEmploymentType(),
  150.             'remoteWork' => $job->getRemoteWork(),
  151.             'experienceLevel' => $job->getExperienceLevel(),
  152.             'salaryMin' => $job->getSalaryMin(),
  153.             'salaryMax' => $job->getSalaryMax(),
  154.             'salaryPeriod' => $job->getSalaryPeriod(),
  155.             'devise' => $job->getDevise(),
  156.             'jobSummary' => $job->getJobSummary(),
  157.             'verification' => $job->isVerification(),
  158.             'website' => method_exists($job'getWebsite') ? $job->getWebsite() : null,
  159.             'websearch' => method_exists($job'getWebsearch') ? $job->getWebsearch() : null,
  160.             'enterprise' => $enterpriseData,
  161.             'isAuthenticated' => $isAuthenticated,
  162.             'isCandidate' => $isCandidate,
  163.             'locked' => !$isAuthenticated,
  164.             'showUrl' => $this->generateUrl('locale_cvs_application_job_show', [
  165.                 '_locale' => $locale,
  166.                 'slug' => $slug
  167.             ]),
  168.             'likeUrl' => null,
  169.         ];
  170.         // Données détaillées (uniquement si connecté)
  171.         if ($isAuthenticated) {
  172.             $payload['keyResponsabilities'] = $job->getKeyResponsabilities();
  173.             $payload['requirements'] = $job->getRequirements();
  174.             $payload['benefits'] = $job->getBenefits();
  175.             if ($isCandidate) {
  176.                 $payload['likeUrl'] = $this->generateUrl('locale_cvs_application_job_like', [
  177.                     '_locale' => $locale,
  178.                     'slug' => $slug
  179.                 ]);
  180.             }
  181.         }
  182.         return new JsonResponse($payload);
  183.     }
  184.     /**
  185.      * API publique de recherche d'offres pour l'autocomplétion.
  186.      * GET /api/jobs/search?q=...&locale=...&limit=...
  187.      */
  188.     public function apiSearch(Request $request): JsonResponse
  189.     {
  190.         $query trim((string) $request->query->get('q'''));
  191.         $locale $request->query->get('locale'$request->getLocale());
  192.         $limit max(1min(20, (int) $request->query->get('limit'6)));
  193.         $offset max(0, (int) $request->query->get('offset'0));
  194.         if (mb_strlen($query) < 2) {
  195.             return new JsonResponse([
  196.                 'items' => [],
  197.                 'total' => 0,
  198.                 'query' => $query,
  199.             ]);
  200.         }
  201.         $repo $this->em->getRepository(Jobs::class);
  202.         $jobs $repo->searchJobs($query$locale$offset$limit);
  203.         $total $repo->countSearchJobs($query$locale);
  204.         $items = [];
  205.         foreach ($jobs as $job) {
  206.             $logo null;
  207.             if ($job->getEnterprise() !== null && method_exists($job->getEnterprise(), 'getLogo')) {
  208.                 $logo $job->getEnterprise()->getLogo();
  209.             }
  210.             $items[] = [
  211.                 'id' => $job->getId(),
  212.                 'slug' => $job->getSlug(),
  213.                 'title' => $job->getJobTitle(),
  214.                 'companyName' => $job->getCompanyName(),
  215.                 'category' => $job->getCategory(),
  216.                 'city' => $job->getCity(),
  217.                 'country' => $job->getCountry(),
  218.                 'employmentType' => $job->getEmploymentType(),
  219.                 'salaryMin' => $job->getSalaryMin(),
  220.                 'salaryMax' => $job->getSalaryMax(),
  221.                 'devise' => $job->getDevise(),
  222.                 'logo' => $logo,
  223.                 'url' => $this->generateUrl('locale_cvs_application_job_show', [
  224.                     '_locale' => $locale,
  225.                     'slug' => $job->getSlug(),
  226.                 ]),
  227.             ];
  228.         }
  229.         return new JsonResponse([
  230.             'items' => $items,
  231.             'total' => $total,
  232.             'query' => $query,
  233.             'limit' => $limit,
  234.             'offset' => $offset,
  235.         ]);
  236.     }
  237.     public function likeJob(Request $request$slug): Response
  238.     {
  239.         $locale $request->getLocale();
  240.         $user $this->getUser();
  241.         if($user == null) {
  242.             if($locale != "en") {
  243.                 return $this->redirectToRoute('locale_cvs_gestion_candidates_dashboard',['_locale' => $locale]);
  244.             }
  245.             return $this->redirectToRoute('cvs_gestion_candidates_dashboard');
  246.         }
  247.         $candidate  $user->getCandidate();
  248.         $job $this->em->getRepository(Jobs::class)->findOneBy(['slug' => $slug]);
  249.         $jhl $this->em->getRepository(JobsHasLikes::class)->findOneBy(['candidate' => $candidate'job' => $job]);
  250.         if($jhl == null) {
  251.             $jhl = new JobsHasLikes();
  252.             $jhl->setJob($job);
  253.             $jhl->setCandidate($candidate);
  254.             $jhl->setCreatedAt(new \DateTime("now"));
  255.             $jhl->setUpdatedAt(new \DateTime("now"));
  256.             $this->em->persist($jhl);
  257.             $this->em->flush();
  258.         }
  259.         if($locale != "en") {
  260.             return $this->redirectToRoute('locale_cvs_gestion_candidates_dashboard',['_locale' => $locale]);
  261.         }
  262.         return $this->redirectToRoute('cvs_gestion_candidates_dashboard');
  263.     }
  264. }