src/Controller/JobsController.php line 44

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Elements.at New Media Solutions GmbH
  4.  *
  5.  */
  6. namespace App\Controller;
  7. use App\Twig\ConfigHelper;
  8. use App\Twig\SimpleHelper;
  9. use Carbon\Carbon;
  10. use Elements\Bundle\CmsToolsBundle\Tool\Helper\MailHelper;
  11. use Elements\Bundle\HashCashBundle\Service\HashCashService;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use Pimcore\Log\ApplicationLogger;
  14. use Pimcore\Mail;
  15. use Pimcore\Model\DataObject\Departments;
  16. use Pimcore\Model\DataObject\Team;
  17. use Pimcore\Model\DataObject\Job;
  18. use Pimcore\Model\DataObject\Location;
  19. use Pimcore\Model\DataObject\SiteConfig;
  20. use Pimcore\Model\Document\Email;
  21. use Pimcore\Model\Document\Page;
  22. use Symfony\Component\HttpFoundation\File\UploadedFile;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Contracts\Translation\TranslatorInterface;
  26. class JobsController extends AbstractController
  27. {
  28.     protected $maxUploadLimitContest 10;
  29.     public function __construct(private HashCashService $hashCashService, private TranslatorInterface $translator, private ConfigHelper $configHelper, private SimpleHelper $simpleHelper, private ApplicationLogger $applicationLogger, private PaginatorInterface $paginator)
  30.     {
  31.     }
  32.     /**
  33.      *
  34.      * @param Request $request
  35.      *
  36.      * @return Response
  37.      */
  38.     public function overviewAction(Request $requestPaginatorInterface $paginator): Response
  39.     {
  40.         $siteConfig $this->configHelper->getSiteConfig();
  41.         $resort $this->document->getProperty('resort');
  42.         $today = new Carbon();
  43.         $jobs = new Job\Listing();
  44.         $jobs->addConditionParam("name != '' AND name IS NOT NULL");
  45.         $jobs->addConditionParam('config LIKE :config', ['config' => '%' $siteConfig->getId() . '%']);
  46.         $jobs->addConditionParam('showFrom <= "' $today->format('Y-m-d') . '" OR showFrom IS NULL');
  47.         $jobs->addConditionParam('showTo >= "' $today->format('Y-m-d') .'" OR showTo IS NULL');
  48.         $locations = [];
  49.         $departments = [];
  50.         foreach ($jobs->load() as $job) {
  51.             if ($job->getLocation() instanceof Location) {
  52.                 $locations[$job->getLocation()->getId()]['location'] = $job->getLocation();
  53.                 $locations[$job->getLocation()->getId()]['disabled'] = true;
  54.             }
  55.             if ($job->getDepartment() instanceof Departments) {
  56.                 $departments[$job->getDepartment()->getId()]['department'] = $job->getDepartment();
  57.                 $departments[$job->getDepartment()->getId()]['disabled'] = true;
  58.             }
  59.         }
  60.         if($resort != null && !$request->get('resort')){
  61.             $jobs->addConditionParam("resorts LIKE :resort", ['resort' => '%' $resort '%']);
  62.         }
  63.         if ($request->get('resort') && $request->get('resort') != 'all') {
  64.             $jobs->addConditionParam('resorts LIKE :keyword', ['keyword' => '%' $request->get('resort') . '%']);
  65.         }
  66.         foreach ($jobs->load() as $job) {
  67.             if ($job->getLocation() instanceof Location) {
  68.                 $locations[$job->getLocation()->getId()]['disabled'] = false;
  69.             }
  70.             if ($job->getDepartment() instanceof Departments) {
  71.                 $departments[$job->getDepartment()->getId()]['disabled'] = false;
  72.             }
  73.         }
  74.         if ($request->get('department')) {
  75.             $jobs->addConditionParam('department__id = :department', ['department' => $request->get('department')]);
  76.         }
  77.         if ($request->get('location')) {
  78.             $jobs->addConditionParam('location__id = :location', ['location' => $request->get('location')]);
  79.         }
  80.         if ($request->get('q')) {
  81.             $jobs->addConditionParam('name LIKE :search OR teaserText LIKE :search OR text LIKE :search', ['search' => '%'.$request->get('q').'%']);
  82.         }
  83.         $jobs->setOrderKey(['beginDate']);
  84.         $jobs->setOrder(['DESC']);
  85.         $jobs $paginator->paginate(
  86.             $jobs,
  87.             $request->get('page'1),
  88.             6
  89.         );
  90.         $returnArray = [
  91.             'paginator' => $jobs,
  92.             'locations' => $locations,
  93.             'departments' => $departments,
  94.         ];
  95.         if ($request->isXmlHttpRequest() && $request->get('ajax')) {
  96.             return $this->json([
  97.                 'success' => true,
  98.                 'html' => $this->render('jobs/includes/jobs-filter.html.twig'$returnArray)->getContent(),
  99.             ]);
  100.         }
  101.         return $this->renderTemplate('jobs/overview.html.twig'$returnArray);
  102.     }
  103.     /**
  104.      *
  105.      * @param Request $request
  106.      *
  107.      * @return Response
  108.      */
  109.     public function detailAction(Request $requestJob $idPaginatorInterface $paginatorConfigHelper $configHelper): Response
  110.     {
  111.         $job $id;
  112.         $siteConfig $configHelper->getSiteConfig();
  113.         $orCondition = [];
  114.         foreach ($job->getResorts() ?? [] as $resort) {
  115.             $orCondition[] = 'resorts LIKE "%' $resort '%"';
  116.         }
  117.         $similarJobs = [];
  118.         if(!empty($orCondition)) {
  119.             $similarJobs = new Job\Listing();
  120.             $similarJobs->addConditionParam('o_id != :id', ['id' => $job->getId()]);
  121.             $similarJobs->addConditionParam('name != "" AND name IS NOT NULL');
  122.             if ($siteConfig) {
  123.                 $similarJobs->addConditionParam('config LIKE :config', ['config' => '%,' $siteConfig->getId() . ',%']);
  124.             }
  125.             $similarJobs->addConditionParam(implode(' OR '$orCondition));
  126.         }
  127.         $siteConfig $this->configHelper->getSiteConfig();
  128.         \Pimcore\Cache::disable();
  129.         $errors = [];
  130.         $success null;
  131.         if ($request->isMethod('POST')) {
  132.             if ($siteConfig instanceof SiteConfig) {
  133.                 $successPage $siteConfig->getSuccessPage();
  134.                 if ($request->get('resort') == 'construction') {
  135.                     $adminMail $siteConfig->getConstructionAdminMail();
  136.                     $userMail $siteConfig->getConstructionUserMail();
  137.                     $mailAddress $siteConfig->getConstructionMailAddress();
  138.                 }
  139.                 if ($request->get('resort') == 'loader') {
  140.                     $adminMail $siteConfig->getLoaderAdminMail();
  141.                     $userMail $siteConfig->getLoaderUserMail();
  142.                     $mailAddress $siteConfig->getLoaderMailAddress();
  143.                 }
  144.             }
  145.             $required = ['firstname''lastname''email''street''nr''plz''city'];
  146.             $params $this->checkForm($request$required);
  147.             $file1 $request->files->get('cv');
  148.             $filenumber 1;
  149.             $filesize 0;
  150.             if ($file1 instanceof UploadedFile) {
  151.                 $filesize $this->simpleHelper->bytesToMiB($file1->getSize(), 3);
  152.                 $this->applicationLogger->info('CV for Job ID' .$job->getId().': Complete Filesize in MB: '.$filesize.' Time: '.Carbon::now(), ['component' => 'Job Admin Mail''relatedObject' => $job]);
  153.                 if ($file1->getSize() > $this->simpleHelper->mbToBytes($this->maxUploadLimitContest)) {
  154.                     $params['errors']['cv'] =  $this->translator->trans('error.form.Lebenslauf size too big');
  155.                 }
  156.             } else {
  157.                 $params['errors']['cv'] = $this->translator->trans('error.form.file required');
  158.             }
  159.             $file2 $request->files->get('motivation');
  160.             if ($file2 instanceof UploadedFile) {
  161.                 $filenumber 2;
  162.                 $filesize $filesize $this->simpleHelper->bytesToMiB($file2->getSize(), 3);
  163.                 $this->applicationLogger->info('Motivation for Job ID' .$job->getId().': Complete Filesize in MB: '.$filesize.' Time: '.Carbon::now(), ['component' => 'Job Admin Mail''relatedObject' => $job]);
  164.                 if ($file2->getSize() > $this->simpleHelper->mbToBytes($this->maxUploadLimitContest)) {
  165.                     $params['errors']['motivation'] =  $this->translator->trans('error.form.Motivation size too big');
  166.                 }
  167.             }
  168.             $file3 $request->files->get('others');
  169.             if ($file3 instanceof UploadedFile) {
  170.                 $filenumber 3;
  171.                 $filesize $filesize $this->simpleHelper->bytesToMiB($file3->getSize(), 3);
  172.                 $this->applicationLogger->info('Other Attachments for Job ID' .$job->getId().': Complete Filesize in MB: '.$filesize.' Time: '.Carbon::now(), ['component' => 'Job Admin Mail''relatedObject' => $job]);
  173.                 if ($file3->getSize() > $this->simpleHelper->mbToBytes($this->maxUploadLimitContest)) {
  174.                     $params['errors']['others'] =  $this->translator->trans('error.form.Others size too big');
  175.                 }
  176.             }
  177.             if($filesize $this->maxUploadLimitContest){
  178.                 $params['errors']['attachments'] = $this->translator->trans('error.form.size of Attachments too big');
  179.             }
  180.             if (!empty($params['errors'])) {
  181.                 $errors $params['errors'];
  182.                 $success false;
  183.             } else {
  184.                 $adminMailSuccess false;
  185.                 $userMailSuccess false;
  186.                 if (isset($adminMail) && $adminMail instanceof Email && isset($mailAddress)) {
  187.                     $mail = new Mail();
  188.                     try {
  189.                         $mail->setParams($params);
  190.                         $mail->setDocument($adminMail);
  191.                         $mail->addTo($mailAddress);
  192.                         if ($file1 !== null) {
  193.                             $mail->attach(file_get_contents($file1->getPathname()), $file1->getClientOriginalName(), $file1->getClientMimeType());
  194.                         }
  195.                         if ($file2 != null) {
  196.                             $mail->attach(file_get_contents($file2->getPathname()), $file2->getClientOriginalName(), $file2->getClientMimeType());
  197.                         }
  198.                         if ($file3 != null) {
  199.                             $mail->attach(file_get_contents($file3->getPathname()), $file3->getClientOriginalName(), $file3->getClientMimeType());
  200.                         }
  201.                         $mail->send();
  202.                         $this->applicationLogger->info('Send Admin Mail for Job ID' .$job->getId().': Number of Files: '.$filenumber.' Complete Filesize in MB: '.$filesize.'Time: '.Carbon::now(), ['component' => 'Job Admin Mail''relatedObject' => $job]);
  203.                         $adminMailSuccess true;
  204.                     } catch(\Exception $e) {
  205.                         $this->applicationLogger->error('Error on sending Admin Mail for Job ID' .$job->getId().': Number of Files: '.$filenumber.' Complete Filesize in MB: '.$filesize.'Time: '.Carbon::now(), ['component' => 'Job Admin Mail''relatedObject' => $job]);
  206.                         $errors[] = 'sending admin mail failed';
  207.                     }
  208.                 }
  209.                 if (isset($userMail) && $userMail instanceof Email) {
  210.                     $mail = new Mail();
  211.                     try {
  212.                         $mail->addTo($params['email']);
  213.                         $mail->setDocument($userMail);
  214.                         $mail->send();
  215.                         $this->applicationLogger->info('Send User Mail for Job ID' .$job->getId().': Number of Files: '.$filenumber.' Complete Filesize in MB: '.$filesize.'Time: '.Carbon::now(), ['component' => 'Job User Mail''relatedObject' => $job]);
  216.                         $userMailSuccess true;
  217.                     } catch(\Exception $e) {
  218.                         $errors[] = 'sending user mail failed';
  219.                     }
  220.                 }
  221.                 if ($adminMailSuccess && $userMailSuccess && isset($successPage) && $successPage instanceof Page) {
  222.                     return $this->redirect((string)$successPage);
  223.                 }
  224.             }
  225.         }
  226.         $similarJobs $paginator->paginate(
  227.             $similarJobs,
  228.             $request->get('page'1),
  229.             6
  230.         );
  231.         if ($request->isXmlHttpRequest() && $request->get('ajax')) {
  232.             return $this->json([
  233.                 'success' => true,
  234.                 'html' => $this->render('jobs/includes/jobs-content.html.twig', [
  235.                     'errors' => $errors,
  236.                     'success' => $success,
  237.                     'job' => $job,
  238.                     'similarJobs' => $similarJobs,
  239.                     'paginator' => $similarJobs,
  240.                 ])->getContent(),
  241.             ]);
  242.         }
  243.         return $this->renderTemplate('jobs/detail.html.twig', [
  244.             'errors' => $errors,
  245.             'success' => $success,
  246.             'job' => $job,
  247.             'similarJobs' => $similarJobs,
  248.         ]);
  249.     }
  250.     /**
  251.      *
  252.      * @param Request $request
  253.      *
  254.      * @return Response
  255.      */
  256.     public function teamDetailAction(Request $requestTeam $id): Response
  257.     {
  258.         $team $id;
  259.         return $this->renderTemplate('jobs/team-detail.html.twig', [
  260.             'team' => $team,
  261.         ]);
  262.     }
  263.     /**
  264.      * @param Request $request
  265.      * @param mixed $required
  266.      *
  267.      * @return mixed
  268.      */
  269.     private function checkForm(Request $requestmixed $required): mixed
  270.     {
  271.         $validHashCash $this->hashCashService->validateProcessFrom();
  272.         if ($validHashCash) {
  273.             $params $request->request->all();
  274.             unset($params['elhc_stamp'], $params['elhc_difficulty'], $params['elhc_nonce']);
  275.             $errors = [];
  276.             foreach ($required as $param) {
  277.                 if ($request->get($param) == '') {
  278.                     $errors[$param] =  $param ' is missing';
  279.                 }
  280.                 if ($param == 'email' && !MailHelper::isValidEmailAddress($request->get($param))) {
  281.                     $errors[$param] = 'email invalid';
  282.                 }
  283.             }
  284.             if (!empty($errors)) {
  285.                 $params['errors'] = $errors;
  286.             }
  287.             if (isset($params['salutation'])) {
  288.                 $params['salutation'] = $this->translator->trans('form.salutation.' $params['salutation'], [], null$request->getLocale());
  289.             }
  290.             $params['items'] = $params;
  291.         } else {
  292.             $params['errors'] = ['recaptcha' => 'invalid captcha'];
  293.         }
  294.         return $params;
  295.     }
  296. }