src/StartPlatz/Bundle/AlphaBundle/Controller/JobController.php line 30

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\StartPlatz\Bundle\AlphaBundle\Controller;
  3. use App\StartPlatz\Bundle\FeedbackBundle\FeedbackService;
  4. use App\StartPlatz\Bundle\UserBundle\Entity\User;
  5. use App\StartPlatz\Bundle\WebsiteBundle\Utility\Utility;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  9. use App\StartPlatz\Bundle\AlphaBundle\Entity\Job;
  10. use App\StartPlatz\Bundle\AlphaBundle\Form\JobType;
  11. use App\StartPlatz\Bundle\WebsiteBundle\MenuTranslationService;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Request;
  15. class JobController extends AbstractController
  16. {
  17.     public function __construct(
  18.         private readonly EntityManagerInterface $entityManager,
  19.         private readonly MenuTranslationService $menuTranslationService,
  20.         private readonly FeedbackService $feedbackService)
  21.     {
  22.     }
  23.     #[Route('x/jobs/home'name'x_jobs_home')]
  24.     #[Route('jobs/'name'extern_jobs_home')]
  25.     #[Route('en/jobs/'name'extern_jobs_home_english')]
  26.     public function indexAction(Request $request)
  27.     {
  28.                 if ($user $this->getUser()) {
  29.             $this->entityManager->getRepository(User::class)->writeActivity($user);
  30.         }
  31.         $templateVars $this->entityManager->getRepository(Job::class)->getTemplateVars($request);
  32.         $jobs $this->entityManager->getRepository(Job::class)->findAllCurrentlyOpen();
  33.         $total $this->entityManager->getRepository(Job::class)->countAll();
  34.         $companyNamearray_unique(array_map(fn ($item) => $item->getCompanyName(), $jobs));
  35.         $companyName array_filter($companyName);
  36.         asort($companyName);
  37.         $jobTypes array_unique(array_map(fn ($item) => $item->getJobType(), $jobs));
  38.         $jobTypes array_filter($jobTypes);
  39.         $companyTypes array_unique(array_map(fn ($item) => $item->getCompanyType(), $jobs));
  40.         $companyTypes array_filter($companyTypes);
  41.         $location array_unique(array_map(fn ($item) => $item->getLocation(), $jobs));
  42.         $location1 array_unique(array_map(fn ($item) => $item->getLocationOptional(), $jobs));
  43.         $locations array_merge($location$location1);
  44.         $locations array_filter($locations);
  45.         if ($request->get('companyName')) {
  46.             $filter =  $request->get('companyName');
  47.             $companyName array_filter($jobs, function ($item) use ($filter) {
  48.                 $companyName $item->getCompanyName();
  49.                 asort($companyName);
  50.                 if (!$companyName) {
  51.                     return false;
  52.                 }
  53.                 return strtolower($filter) === strtolower($companyName);
  54.             });
  55.             $total count($jobs);
  56.         }
  57.         if ($request->get('jobType')) {
  58.             $filter =  $request->get('jobType');
  59.             $jobs array_filter($jobs, function ($item) use ($filter) {
  60.                 $JobType $item->getJobType();
  61.                 if (!$JobType) {
  62.                     return false;
  63.                 }
  64.                 return strtolower($filter) === strtolower($JobType);
  65.             });
  66.             $total count($jobs);
  67.         }
  68.         if ($request->get('companyType')) {
  69.             $filter =  $request->get('companyType');
  70.             $jobs array_filter($jobs, function ($item) use ($filter) {
  71.                 $companyTypes $item->getCompanyType();
  72.                 if (!$companyTypes) {
  73.                     return false;
  74.                 }
  75.                 return strtolower($filter) === strtolower($companyTypes);
  76.             });
  77.             $total count($jobs);
  78.         }
  79.         if ($request->get('location')) {
  80.             $filter =  $request->get('location');
  81.             $jobs array_filter($jobs, function ($item) use ($filter) {
  82.                 if ($item->getLocation()) {
  83.                     $location $item->getLocation();
  84.                 } else {
  85.                     $location $item->getLocationOptional();
  86.                 }
  87.                 if (!$location) {
  88.                     return false;
  89.                 }
  90.                 return strtolower($filter) === strtolower($location);
  91.             });
  92.             $total count($jobs);
  93.         }
  94.         $location array_filter($location);
  95.         foreach ($jobs as $job) {
  96.             if($applyTarget $job->getApplyTarget()) {
  97.                 if (preg_match('/[a-z0-9!#$%&*\\.+?^_-]+(?:\\.[a-z0-9!#$%&*+?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i'$applyTarget)) {
  98.                     $job->applyTargetType "mail";
  99.                 } else {
  100.                     $job->applyTargetType "url";
  101.                 }
  102.             } else {
  103.                 $job->applyTargetType null;
  104.             }
  105.         }
  106.         $route $request->get('_route');
  107.         if ($route == 'x_jobs_home') {
  108.             if (!$this->getUser()) {
  109.                 return $this->redirect($this->generateUrl('extern_jobs_home'));
  110.             }
  111.             $view '@StartPlatzAlphaBundle/Job/Job_AB__index.twig';
  112.         } else {
  113.             $view '@StartPlatzAlphaBundle/Job/index.extern.html.twig';
  114.         }
  115.         return $this->render($view, [
  116.             'templateVars'        => $templateVars,
  117.             'companyName'         => $companyName,
  118.             'jobTypes'            => $jobTypes,
  119.             'locations'           => $locations,
  120.             'companyTypes'        => $companyTypes,
  121.             'jobs'                => $jobs,
  122.             'total'               => $total,
  123.             'selectedJobType'     => $request->get('jobType'),
  124.             'selectedCompanyType' => $request->get('companyType'),
  125.             'selectedLocation'    => $request->get('location'),
  126.             'redirectUrl'         => base64_encode(json_encode(['path' => 'x_jobs_home''parameters' => $templateVars ])),
  127.         ]);
  128.     }
  129.     #[Route('/website/jobs/home'name'website_jobs_home')]
  130.     public function indexWebsiteAction(Request $request)
  131.     {
  132.                 if ($user $this->getUser()) {
  133.             $this->entityManager->getRepository(User::class)->writeActivity($user);
  134.         }
  135.         $templateVars $this->entityManager->getRepository(Job::class)->getTemplateVars($request);
  136.         $jobs $this->entityManager->getRepository(Job::class)->findAll();
  137.         $total $this->entityManager->getRepository(Job::class)->countAll();
  138.         $companyNamearray_unique(array_map(fn ($item) => $item->getCompanyName(), $jobs));
  139.         $companyName array_filter($companyName);
  140.         asort($companyName);
  141.         $jobTypes array_unique(array_map(fn ($item) => $item->getJobType(), $jobs));
  142.         $jobTypes array_filter($jobTypes);
  143.         $companyTypes array_unique(array_map(fn ($item) => $item->getCompanyType(), $jobs));
  144.         $companyTypes array_filter($companyTypes);
  145.         $location array_unique(array_map(fn ($item) => $item->getLocation(), $jobs));
  146.         $location1 array_unique(array_map(fn ($item) => $item->getLocationOptional(), $jobs));
  147.         $location array_merge($location$location1);
  148.         if ($request->get('companyName')) {
  149.             $filter =  $request->get('companyName');
  150.             $companyName array_filter($jobs, function ($item) use ($filter) {
  151.                 $companyName $item->getCompanyName();
  152.                 asort($companyName);
  153.                 if (!$companyName) {
  154.                     return false;
  155.                 }
  156.                 return strtolower($filter) === strtolower($companyName);
  157.             });
  158.             $total count($jobs);
  159.         }
  160.         if ($request->get('jobType')) {
  161.             $filter =  $request->get('jobType');
  162.             $jobs array_filter($jobs, function ($item) use ($filter) {
  163.                 $JobType $item->getJobType();
  164.                 if (!$JobType) {
  165.                     return false;
  166.                 }
  167.                 return strtolower($filter) === strtolower($JobType);
  168.             });
  169.             $total count($jobs);
  170.         }
  171.         if ($request->get('companyType')) {
  172.             $filter =  $request->get('companyType');
  173.             $jobs array_filter($jobs, function ($item) use ($filter) {
  174.                 $companyTypes $item->getCompanyType();
  175.                 if (!$companyTypes) {
  176.                     return false;
  177.                 }
  178.                 return strtolower($filter) === strtolower($companyTypes);
  179.             });
  180.             $total count($jobs);
  181.         }
  182.         if ($request->get('location')) {
  183.             $filter =  $request->get('location');
  184.             $jobs array_filter($jobs, function ($item) use ($filter) {
  185.                 $location $item->getLocation();
  186.                 $location =$item->getLocationOptional();
  187.                 if (!$location) {
  188.                     return false;
  189.                 }
  190.                 return strtolower($filter) === strtolower($location);
  191.             });
  192.             $total count($jobs);
  193.         }
  194.         $location array_filter($location);
  195.         $lang 'DE';
  196.         if ($targetPath $request->server->get('REDIRECT_URL')) {
  197.             $lang $this->menuTranslationService->getLang($targetPath);
  198.         }
  199.         $menuLinksDe2En $this->menuTranslationService->getMenuLinksSwitcher();
  200.         return $this->render('@StartPlatzAlphaBundle/Job/index.website.html.twig', [
  201.             'templateVars' => $templateVars,
  202.             'companyName' => $companyName,
  203.             'jobTypes' => $jobTypes,
  204.             'location' =>$location,
  205.             'companyTypes' => $companyTypes,
  206.             'jobs'        => $jobs,
  207.             'total'        => $total,
  208.             'targetPath'    => $targetPath,
  209.             'menuLinksDe2En' => $menuLinksDe2En,
  210.             'menuLinksEn2De' => array_flip($menuLinksDe2En) ,
  211.             'menuPhrases'    => $this->menuTranslationService->getMenuPhrases($lang),
  212.             'menuLinks'      => $this->menuTranslationService->getMenuLinks($lang),
  213.             'footerPhrases'  => $this->menuTranslationService->getFooterPhrases($lang),
  214.             'footerLinks'    => $this->menuTranslationService->getFooterLinks($lang),
  215.             'lang'           => $lang,
  216.             'selectedJobType'     => $request->get('jobType'),
  217.             'selectedCompanyType'     => $request->get('companyType'),
  218.             'selectedLocation'     => $request->get('location'),
  219.             'redirectUrl'  => base64_encode(json_encode(['path' => 'x_jobs_home''parameters' => $templateVars ])),
  220.         ]);
  221.     }
  222.     #[Route('/allmeda/jobs/get/jobs/search'name'allmeda_api_search_jobs_standard')]
  223.     /**
  224.      * @IsGranted("ROLE_USER")
  225.      */
  226.     public function apiJobsSearchAction(Request $request)
  227.     {
  228.                 $location '';
  229.         if ($location $request->query->get('location')) {
  230.             $location = (!in_array($location, ['CGN''DUS''SP']) ? 'all' $location);
  231.         }
  232.         $jobs $this->entityManager->getRepository(Job::class)->find4ExternalAccess();
  233.         $response = new Response();
  234.         $response->setContent(json_encode($jobs));
  235.         $response->headers->set('Content-Type''application/json');
  236.         return $response;
  237.     }
  238.     #[Route('/allmeda/jobs/home'name'allmeda_jobs_home')]
  239.     /**
  240.      * @IsGranted("ROLE_ADMIN")
  241.      */
  242.     public function indexAllmedaAction(Request $request)
  243.     {
  244.                 $user $this->getUser();
  245.         $this->entityManager->getRepository(User::class)->writeActivity($user);
  246.         $templateVars $this->entityManager->getRepository(Job::class)->getTemplateVars($request);
  247.         $orderBy = ($templateVars['dimensions']) ? $templateVars['dimensions'] : ['lastModified' => 'DESC'];
  248.         $criteria   $templateVars['criteria'];
  249.         $jobs  $this->entityManager->getRepository(Job::class)->findBy($criteria$orderBy);
  250.         $total $this->entityManager->getRepository(Job::class)->countByCriteria($criteria);
  251.         foreach ($jobs as $job) {
  252.             if($applyTarget $job->getApplyTarget()) {
  253.                 if (preg_match('/[a-z0-9!#$%&*\\.+?^_-]+(?:\\.[a-z0-9!#$%&*+?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i'$applyTarget)) {
  254.                     $job->applyTargetType "mail";
  255.                 } else {
  256.                     $job->applyTargetType "url";
  257.                 }
  258.             } else {
  259.                 $job->applyTargetType null;
  260.             }
  261.         }
  262.         return $this->render('@StartPlatzAlphaBundle/Job/index.allmeda.v3.html.twig', [
  263.             'templateVars' => $templateVars,
  264.             'jobs'         => $jobs,
  265.             'total'        => $total,
  266.             'redirectUrl'  => base64_encode(json_encode(['path' => 'allmeda_jobs_home''parameters' => $templateVars ])),
  267.         ]);
  268.     }
  269.     #[Route('/allmeda/jobs/filter/single'name'allmeda_jobs_filter_single')]
  270.     /**
  271.      * @IsGranted("ROLE_ADMIN")
  272.      */
  273.     public function selectFilterSingleAjaxAction(Request $request)
  274.     {
  275.                 $redirect Utility::getRedirectTarget($request);
  276.         $templateVars $this->entityManager->getRepository(Job::class)->getTemplateVars($request);
  277.         $criteria $templateVars['criteria'];
  278.         $user $this->getUser();
  279.         $group $request->get('filter');
  280.         $aggregates $this->entityManager->getRepository(Job::class)->getAggregateByGroup($group, []);
  281.         $group2 "";
  282.         $aggregates2 = [];
  283.         $targetPath "allmeda_jobs_home";
  284.         return $this->render('@StartPlatzStartupBundle/Admin/listAggregates.ajax.html.twig', [
  285.             'title'        => "Set Filter on {$group}",
  286.             'aggregates'   => $aggregates,
  287.             'btnClass'     => "btn-outline-info",
  288.             'group'        => $group,
  289.             'group2'       => $group2,
  290.             'aggregates2'  => $aggregates2,
  291.             'targetPath'   => $targetPath,
  292.             'templateVars' => $templateVars,
  293.             'redirect'     => $redirect,
  294.             'redirectUrl'  => $request->get('redirect'),
  295.         ]);
  296.     }
  297.     #[Route('/allmeda/jobs/show/raw/{id}'name'allmeda_jobs_show_raw_ajax')]
  298.     /**
  299.      * @IsGranted("ROLE_ADMIN")
  300.      */
  301.     public function showRawAjaxAction(Request $request$id)
  302.     {
  303.         $redirect Utility::getRedirectTarget($request);
  304.                 /** @var Job $job */
  305.         if (!$job $this->entityManager->getRepository(Job::class)->findOneBy(['id' => $id])) {
  306.             $this->addFlash('notice''ERROR Job not found');
  307.             return $this->redirect($this->generateUrl('allmeda_home'));
  308.         }
  309.         return $this->render('@StartPlatzAlpha/Job/showRawAjax.html.twig', [
  310.             'job' => $job,
  311.             'methods' => $this->feedbackService->getEntityMethodsArray($job, []),
  312.         ]);
  313.     }
  314.     #[Route('/allmeda/jobs/filter/single/v3'name'allmeda_jobs_filter_single_v3')]
  315.     /**
  316.      * @IsGranted("ROLE_ADMIN")
  317.      */
  318.     public function selectFilterSingleV3Action(Request $request)
  319.     {
  320.         $templateVars $this->entityManager->getRepository(Job::class)->getTemplateVars($request);
  321.         $group $request->get('filter');
  322.         $aggregates $this->entityManager->getRepository(Job::class)->getAggregateByGroup($group, []);
  323.         return $this->render('@StartPlatzStartupBundle/Admin/listAggregates.ajax.v3.html.twig', [
  324.             'title'        => "Set Filter on {$group}",
  325.             'aggregates'   => $aggregates,
  326.             'group'        => $group,
  327.             'targetPath'   => 'allmeda_jobs_home',
  328.             'templateVars' => $templateVars,
  329.         ]);
  330.     }
  331.     #[Route('/allmeda/jobs/show/raw/{id}/v3'name'allmeda_jobs_show_raw_ajax_v3')]
  332.     /**
  333.      * @IsGranted("ROLE_ADMIN")
  334.      */
  335.     public function showRawAjaxV3Action(Request $request$id)
  336.     {
  337.         if (!$job $this->entityManager->getRepository(Job::class)->findOneBy(['id' => $id])) {
  338.             return new Response('Job not found'404);
  339.         }
  340.         return $this->render('@StartPlatzAlpha/Job/showRawAjax.v3.html.twig', [
  341.             'job'     => $job,
  342.             'methods' => $this->feedbackService->getEntityMethodsArray($job, []),
  343.         ]);
  344.     }
  345.     #[Route('/allmeda/jobs/edit/{id}'name'allmeda_jobs_edit')]
  346.     /**
  347.      * @IsGranted("ROLE_ADMIN")
  348.      */
  349.     public function editAction(Request $request$id)
  350.     {
  351.                 $user $this->getUser();
  352.         $this->entityManager->getRepository(User::class)->writeActivity($user);
  353.         $templateVars $this->entityManager->getRepository(Job::class)->getTemplateVars($request);
  354.         $redirect Utility::getRedirectTarget($request);
  355.         if ($id == 'create') {
  356.             $job = new Job();
  357.             $now = new \DateTime();
  358.             $job->setCreatedAt($now);
  359.         } else {
  360.             if (!$job $this->entityManager->getRepository(Job::class)->findOneBy(['id' => $id])) {
  361.                 $this->addFlash('notice''ERROR: no job found');
  362.                 return $this->redirect($this->generateUrl($redirect->path, (array) $redirect->parameters));
  363.             }
  364.         }
  365.         $fields     Utility::getEntityFieldsArray($job, []);
  366.         $storedData Utility::fillDataByObject($job$fields);
  367.         $form $this->createForm(JobType::class, $job, [
  368.             'action' => $this->generateUrl('allmeda_jobs_edit', ['id' => $id'redirect' => $request->get('redirect')] + $templateVars),
  369.         ]);
  370.         $form->handleRequest($request);
  371.         if ($form->isSubmitted() && $form->isValid()) {
  372.             $now = new \DateTime();
  373.             $job $this->entityManager->getRepository(Job::class)->setEndDate($job);
  374.             $tags explode(','str_replace(' ''', (string) $job->getTags()));
  375.             $job->setTags(json_encode(array_values($tags)));
  376.             $updatedData Utility::fillDataByObject($job$fields);
  377.             $differences array_diff_assoc($updatedData$storedData);
  378.             $job->setLastModified($now);
  379.             $job->setLastChangeUser($this->getUser()->getEmail());
  380.             $logText "";
  381.             if ($differences) {
  382.                 foreach ($differences as $field => $value) {
  383.                     $logText .= "{$field}$value";
  384.                 }
  385.                 $job->setHistory('==== 'date("Y-m-d") . ' by ' $job->getLastChangeUser() .' ==='PHP_EOL $logText   PHP_EOL $job->getHistory());
  386.             }
  387.             $this->entityManager->persist($job);
  388.             $this->entityManager->flush();
  389.             $this->addFlash('notice''SUCCESS Daten wurden gespeichert');
  390.             return $this->redirect($this->generateUrl($redirect->path, (array) $redirect->parameters));
  391.         } else {
  392.             $errorString $form->getErrors(truefalse);
  393.             // @todo: $errorString ist ein Array -> StringCast in der nächsten Zeile anders behandeln
  394.             if (str_contains((string) $errorString'ERROR:')) {
  395.                 $this->addFlash('notice''ERROR - Data have not been saved' $errorString);
  396.                 return $this->redirect($this->generateUrl($redirect->path, (array) $redirect->parameters));
  397.             }
  398.         }
  399.         return $this->render('@StartPlatzAlphaBundle/Job/edit.v3.html.twig', [
  400.             'job'         => $job,
  401.             'form'         => $form->createView(),
  402.             'templateVars' => $templateVars,
  403.             'redirectUrl'  => $this->redirect($this->generateUrl($redirect->path, (array) $redirect->parameters)),
  404.         ]);
  405.     }
  406.     #[Route('/allmeda/jobs/delete/{id}'name'allmeda_jobs_delete')]
  407.     /**
  408.      * @IsGranted("ROLE_ADMIN")
  409.      */
  410.     public function deleteAction($idRequest $request)
  411.     {
  412.                 $user $this->getUser();
  413.         $this->entityManager->getRepository(User::class)->writeActivity($user);
  414.         $templateVars $this->entityManager->getRepository(Job::class)->getTemplateVars($request);
  415.         $redirect Utility::getRedirectTarget($request);
  416.         if (!$job $this->entityManager->getRepository(Job::class)->findOneBy(['id' => $id])) {
  417.             $this->addFlash('notice''ERROR: no Job found');
  418.             return $this->redirect($this->generateUrl($redirect->path, (array) $redirect->parameters));
  419.         }
  420.         $this->entityManager->remove($job);
  421.         $this->entityManager->flush();
  422.         $this->addFlash('notice''SUCCESSJob has been deleted');
  423.         return $this->redirect($this->generateUrl($redirect->path, (array) $redirect->parameters));
  424.     }
  425. }