src/Controller/Frontend/AjaxController.php line 336

  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Lib\HolidayWrapper;
  4. use Webbamboo\SpinnerBundle\Entity\Zip;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Webbamboo\SpinnersCommon\Entity\Company;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Webbamboo\SpinnersCommon\Entity\Presubmission;
  12. use Webbamboo\SpinnersCommon\Repository\ZipRepository;
  13. use Webbamboo\SpinnersCommon\Repository\CityRepository;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. class AjaxController extends AbstractController
  16. {
  17.     protected EntityManagerInterface $em;
  18.     protected CityRepository $cityRepository;
  19.     public function __construct(
  20.         EntityManagerInterface $em,
  21.         CityRepository $cityRepository
  22.     )
  23.     {
  24.         $this->em $em;        
  25.         $this->cityRepository $cityRepository;
  26.     }
  27.     #[Route('/endpoints/presubmissions'name:"endpoints_presubmissions"methods:['GET'])]
  28.     public function presubmissionsAction(Request $request
  29.     {
  30.         $remoteIP '178.79.154.17';
  31.         $remoteIP2 '139.162.163.203';
  32.         if(
  33.             $request->getClientIp() === $remoteIP || 
  34.             $request->getClientIp() === '78.83.25.137' ||
  35.             $request->getClientIp() === $remoteIP2
  36.         )
  37.         {
  38.             $first $request->query->get('first');
  39.             $presubmissions $this->em->getRepository(Presubmission::class)
  40.                 ->createQueryBuilder('p')
  41.                 ->where('p.id > :id')->setParameter('id'$first)
  42.                 ->orderBy('p.id''ASC')->getQuery()->getResult();
  43.             $responseArray = [];
  44.             foreach($presubmissions as $presubmission)
  45.             {
  46.                 $responseArray[] = [
  47.                     'remote_id' => $presubmission->getId(),
  48.                     'content' => $presubmission->getContent(),
  49.                     'date' => $presubmission->getCreated()->format("Y-m-d H:i:s"),
  50.                     'ip' => $presubmission->getIp(),
  51.                     'siteVersion' => $presubmission->getSiteVersion(),
  52.                     'site_id' => 'FSD',
  53.                     'url' => $presubmission->getUrl()
  54.                 ];
  55.             }
  56.             return new JsonResponse($responseArray);
  57.         }
  58.         else
  59.         {
  60.             return new JsonResponse(['error' => 'FORBIDDEN']);
  61.         }
  62.     }
  63.     
  64.     #[Route('/ajax/zip'name:"ajax_zip"methods:['GET'])]
  65.     public function zipAction(Request $requestZipRepository $zipRepository)
  66.     {
  67.         $results $zipRepository->findByQuery($request->query->get('q'));
  68.         $formattedResults = function() use($results){
  69.             $response = [];
  70.             foreach($results as &$result)
  71.             {
  72.                 $response[] = [
  73.                     'zip' => $result->getZip(),
  74.                     'city' => $result->getCity(),
  75.                     'display' => $result->getZip().' '.$result->getCity()
  76.                 ];
  77.             }
  78.             return $response;
  79.         };
  80.         return new JsonResponse($formattedResults());
  81.     }
  82.     #[Route('/ajax/presubmission'name:"spinner_ajax_presubmission"methods:['GET'])]
  83.     public function createPresubmissionAction(Request $request)
  84.     {
  85.         $postParameters $request->query->all();
  86.         $presubmission = new Presubmission();
  87.         $presubmission->setContent(print_r($postParameterstrue));
  88.         $presubmission->setCreated(new \DateTime('now', new \DateTimeZone('Europe/Stockholm')));
  89.         $presubmission->setIp($request->getClientIp());
  90.         $presubmission->setUrl($request->query->get("url"));
  91.         $presubmission->setSiteVersion(isset($postParameters['form']['siteVersion']) ? $postParameters['form']['siteVersion'] : '');
  92.         
  93.         $this->em->persist($presubmission);
  94.         $this->em->flush();
  95.         return new JsonResponse(['success' => true]);
  96.     }
  97.     /*
  98.     Error codes:
  99.     400 - Bad request
  100.     */
  101.     public function sanitizepermalinkAction(Request $request)
  102.     {
  103.         if ($request->getMethod() == 'POST') {
  104.             $title $request->request->get('title''');
  105.             $response = new Response(json_encode(array('sanitized' => $this->generatePermalink($title))));
  106.         } else {
  107.             $response = new Response(json_encode(array('error' => '400')));
  108.         }
  109.         $response->headers->set('Content-Type''application/json');
  110.         return $response;
  111.     }
  112.     private function generatePermalink($text$delimiter='-')
  113.     {
  114.         $t strtolower($text);
  115.         $url=array(
  116.             "%C3%A5","%C3%A5",
  117.             "%C3%A4","%C3%A4",
  118.             "%C3%B6","%C3%B6",
  119.             "%26","-",
  120.             """",
  121.             "%E9""",
  122.             """"
  123.         );
  124.         $char=array(
  125.             "Å","å",
  126.             "Ä","ä",
  127.             "Ö","ö",
  128.             "&amp;",":",
  129.             ".""'",
  130.             "é"",",
  131.             "("")"
  132.         );
  133.         $str str_replace($char$url$t);
  134.         //$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $str);
  135.         $clean trim($str'-');
  136.         $clean preg_replace("/[\/_|+ -]+/"$delimiter$clean);
  137.         return $clean;
  138.     }
  139.     #[Route('/ajax/querycity'name:"spinner_ajax_autocomplete"methods: ['GET'])]
  140.     public function querycityAction(Request $request)
  141.     {
  142.         $cityName $this->mb_ucfirst($request->query->get('name'));
  143.         if ($cityName && !empty($cityName)) {
  144.             $response = array();
  145.             $query $this->cityRepository->createQueryBuilder('c')
  146.                    ->where('c.name LIKE :name')
  147.                    ->setParameter('name'$cityName.'%')
  148.                    ->getQuery();
  149.             $result $query->getResult();
  150.             $duplicates $this->getDuplicates($result);
  151.             foreach ($result as $city) {
  152.                 $data = array();
  153.                 $name = isset($duplicates[$city->getName()]) ? $this->mb_ucfirst($city->getName()).'( '.$city->getCounty()->getName().' län )' $this->mb_ucfirst($city->getName());
  154.                 $data['name'] = $name;
  155.                 $data['id'] = $this->generateUrl('spinner_front_city', array('countypermalink' => $this->mb_ucfirst($city->getCounty()->getPermalinkP()), 'citypermalink' => $this->mb_ucfirst($city->getPermalinkP()) ), true);
  156.                 
  157.                 if ($city->getName() == $cityName) {
  158.                     array_unshift($response$data);
  159.                 } else {
  160.                     $response[] = $data;
  161.                 }
  162.             }
  163.             return new JsonResponse($response);
  164.         } else {
  165.             return new JsonResponse(array('error' => 'Empty query...No funny business!'));
  166.         }
  167.     }
  168.     
  169.     #[Route('/ajax/querycitycreate'name:"spinner_ajax_autocomplete_create"methods:['GET'])]
  170.     public function querycitycreateAction(Request $request)
  171.     {
  172.         $cityName $this->mb_ucfirst($request->query->get('name'));
  173.         if ($cityName && !empty($cityName)) {
  174.             $response = array();
  175.             $query $this->cityRepository->createQueryBuilder('c')
  176.                    ->where('c.name LIKE :name')
  177.                    ->setParameter('name'$cityName.'%')
  178.                    ->getQuery();
  179.             $result $query->getResult();
  180.             $duplicates $this->getDuplicates($result);
  181.             foreach ($result as $city) {
  182.                 $data = array();
  183.                 $name = isset($duplicates[$city->getName()]) ? $this->mb_ucfirst($city->getName()).'( '.$city->getCounty()->getName().' län )' $this->mb_ucfirst($city->getName());
  184.                 $data['name'] = $name;
  185.                 $data['id'] = $city->getID();
  186.                 $data['longitude'] = $city->getLongitude();
  187.                 $data['latitude'] = $city->getLatitude();
  188.                 
  189.                 if ($city->getName() == $cityName) {
  190.                     array_unshift($response$data);
  191.                 } else {
  192.                     $response[] = $data;
  193.                 }
  194.             }
  195.             return new JsonResponse($response);
  196.         } else {
  197.             return new JsonResponse(array('error' => 'Empty query...No funny business!'));
  198.         }
  199.     }
  200.     
  201.     private function getDuplicates($input)
  202.     {
  203.         $registry = array();
  204.         $duplicates = array();
  205.         foreach ($input as $city) {
  206.             if (isset($registry[$city->getName()])) {
  207.                 $duplicates[$city->getName()] = true;
  208.             } else {
  209.                 $registry[$city->getName()] = true;
  210.             }
  211.         }
  212.         return $duplicates;
  213.     }
  214.     #[Route('/ajax/querycitymanual'name:"spinner_ajax_autocomplete_backend"methods:['GET'])]
  215.     public function querycitymanualAction(Request $request)
  216.     {
  217.         $cityName $this->mb_ucfirst($request->query->get('name'));
  218.         if ($cityName && !empty($cityName)) {
  219.             $response = array();
  220.             $query $this->cityRepository->createQueryBuilder('c')
  221.                    ->where('c.name LIKE :name')
  222.                    ->setParameter('name'$cityName.'%')
  223.                    ->getQuery();
  224.             $result $query->getResult();
  225.             $duplicates $this->getDuplicates($result);
  226.             foreach ($result as $city) {
  227.                 $data = array();
  228.                 $name = isset($duplicates[$city->getName()]) ? $this->mb_ucfirst($city->getName()).'( '.$city->getCounty()->getName().' län )' $this->mb_ucfirst($city->getName());
  229.                 $data['name'] = $name;
  230.                 $data['id'] = $this->generateUrl('spinner_backend_citymanual', array( 'cityid' => $city->getId() ), true);
  231.                 
  232.                 if ($city->getName() == $cityName) {
  233.                     array_unshift($response$data);
  234.                 } else {
  235.                     $response[] = $data;
  236.                 }
  237.             }
  238.             return new JsonResponse($response);
  239.         }
  240.     }
  241.     #[Route('/ajax/querycompanymanual'name:"spinner_ajax_autocomplete_backend_company"methods:['GET'])]
  242.     public function querycompanymanualAction(Request $request)
  243.     {
  244.         $companyName $this->mb_ucfirst($request->query->get('name'));
  245.         if ($companyName && !empty($companyName)) {
  246.             $repository $this->em->getRepository(Company::class);
  247.             $response = array();
  248.             $query $repository->createQueryBuilder('c')
  249.                    ->where('c.name LIKE :name')
  250.                    ->setParameter('name'$companyName.'%')
  251.                    ->getQuery();
  252.             $result $query->getResult();
  253.             //Preformat
  254.             
  255.             foreach ($result as $company) {
  256.                 $single = array();
  257.                 $single['id'] = $this->generateUrl('spinner_backend_companymanual', array( 'companyid' => $company->getId() ), true);
  258.                 $single['name'] = $company->getName();
  259.                 $response[] = $single;
  260.             }
  261.             //die();
  262.             return new JsonResponse($response);
  263.         } else {
  264.             return new JsonResponse(array('error' => 'Empty query...No funny business!'));
  265.         }
  266.     }
  267.     #[Route('/ajax/querycountymanual'name:"spinner_ajax_autocomplete_county_backend"methods:['GET'])]
  268.     public function querycountymanualAction(Request $request)
  269.     {
  270.         $countyName $this->mb_ucfirst($request->query->get('name'));
  271.         if ($countyName && !empty($countyName)) {
  272.             $repository $this->em->getRepository(Counties::class);
  273.             $response = array();
  274.             $query $repository->createQueryBuilder('c')
  275.                    ->where('c.name LIKE :name')
  276.                    ->setParameter('name'$countyName.'%')
  277.                    ->getQuery();
  278.             $result $query->getResult();
  279.             //Preformat
  280.             
  281.             foreach ($result as $county) {
  282.                 $single = array();
  283.                 $single['id'] = $this->generateUrl('spinner_backend_countymanual', array( 'countyid' => $county->getId() ), true);
  284.                 $single['name'] = $county->getName();
  285.                 $response[] = $single;
  286.             }
  287.             //die();
  288.             return new JsonResponse($response);
  289.         } else {
  290.             return new JsonResponse(array('error' => 'Empty query...No funny business!'));
  291.         }
  292.     }
  293.     #[Route('/ajax/querycounty'name:"spinner_ajax_autocomplete_county"methods:['GET'])]
  294.     public function querycountyAction()
  295.     {
  296.         $repository $this->em->getRepository(Counties::class);
  297.         //source: [{% for city in all_cities.get %}{"id":"{{ path('spinner_front_city', {'countypermalink': city.getCounty.getPermalinkP|capitalize, 'citypermalink': city.getPermalinkP|capitalize})|base64_encode}}","name":"{{city.getName|capitalize}}" }, {% endfor %}]
  298.         //$this->generateUrl('spinner_front_city', array('countypermalink' => $city->getCounty()->getPermalinkP(), 'citypermalink' => $city->city.getPermalinkP()), true);
  299.         //path('spinner_front_county', {'countypermalink': activecounty.getPermalinkP|capitalize})
  300.         $result $repository->findAll();
  301.         foreach ($result as $county) {
  302.             $single = array();
  303.             $single['id'] = $this->generateUrl('spinner_front_county', array('countypermalink' => $this->mb_ucfirst($county->getPermalinkP()) ), true);
  304.             $single['name'] = $this->mb_ucfirst($county->getName());
  305.             $response[] = $single;
  306.         }
  307.         return new JsonResponse($response);
  308.     }
  309.     private function mb_ucfirst($str)
  310.     {
  311.         $fc mb_strtoupper(mb_substr($str01'UTF-8'), 'UTF-8');
  312.         return $fc.mb_substr($str1mb_strlen($str), 'UTF-8');
  313.     }
  314.     #[Route("/ajax/holidays"name'spinner_ajax_holidays'methods: ['GET'])]
  315.     public function holidaysajaxAction()
  316.     {
  317.         $wrapper = new HolidayWrapper();
  318.         $response = new Response();
  319.         $response->setContent($wrapper->getHolidaysJSON());
  320.         $response->headers->set('Content-Type''application/json');
  321.         
  322.         return $response;
  323.     }
  324. }