src/Controller/DefaultController.php line 96

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Doctrine\Persistence\ManagerRegistry;
  4. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  5. use Symfony\Component\Mailer\MailerInterface;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Form\FormError;
  12. use Symfony\Component\Validator\Validator\ValidatorInterface;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. use App\Entity\Master\User;
  15. use App\Form\PasswordCreationType;
  16. use App\Form\PasswordRecoveryType;
  17. use App\Form\Model\Contact;
  18. use App\Form\ContactType;
  19. use App\Service\ValidationService;
  20. use App\Service\OfficeService;
  21. class DefaultController extends AbstractController
  22.     protected $mr;
  23.     private $params;
  24.     public function __construct(ManagerRegistry $managerRegistryParameterBagInterface $params)
  25.     {
  26.         $this->mr $managerRegistry;
  27.         $this->params $params;
  28.     }
  29.     /**
  30.      * @Route("/", name="homepage")
  31.      */
  32.     public function index(Request $requestMailerInterface $mailer)
  33.     {
  34.         $em $this->mr->getManager('master');
  35.         $contact = new Contact();
  36.         $form $this->createForm(ContactType::class, $contact);
  37.         $form->handleRequest($request);
  38.         
  39.         if($form->isSubmitted()){
  40.             $valid true;            
  41.             $path 'https://www.google.com/recaptcha/api/siteverify?secret=6LcmTdgUAAAAAHtbLS0hf0fJtNZALbjDqU_6Xxhq&response='.$request->request->get("g-recaptcha-response");
  42.             $ch curl_init();
  43.             curl_setopt($chCURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/json'));
  44.             curl_setopt($chCURLOPT_URL,$path);
  45.             curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
  46.             curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  47.             curl_setopt($chCURLOPT_CUSTOMREQUEST"GET");
  48.             $result curl_exec($ch);
  49.             curl_close($ch);
  50.             
  51.             $res json_decode($resulttrue);
  52.             
  53.             if(!$res["success"]){
  54.                 $valid false;
  55.                 $form->get('recaptcha')->addError(new FormError("Convalidare prima di inviare la richiesta"));
  56.                 $this->addFlash('notice_warning''Prima di inviare la richiesta, provare di non essere un robot.');
  57.             }
  58.             if($valid && $form->isValid()){
  59.                 
  60.                 $message = (new TemplatedEmail())
  61.                     ->subject($this->params->get('subject_contact'))
  62.                     ->from($form->get('email')->getData())
  63.                     ->to($this->params->get('contact_email'))
  64.                     ->htmlTemplate('email/contact.html.twig')
  65.                     ->context(['name' => $form->get('name')->getData(), 'surname' => $form->get('surname')->getData(), 'message' => $form->get('message')->getData()]);
  66.                 $mailer->send($message);
  67.                 $this->addFlash('notice_success''Richiesta di informazioni inoltrata correttamente!');
  68.                 return $this->redirectToRoute('homepage');
  69.             }
  70.             else
  71.                 $this->addFlash('notice_warning''Controllare le informazioni inserite');
  72.         }
  73.         return $this->render('default/index.html.twig', array(
  74.             'form' => $form->createView()
  75.         ));
  76.     }
  77.     /**
  78.      * @Route("/home/{section}", name="homepage_with_section")
  79.      */
  80.     public function indexWithSection(Request $request$section)
  81.     {
  82.         $session $request->getSession();
  83.         $session->set('section'$section);
  84.         $session->set('dashboardTab''profile');
  85.         return $this->redirectToRoute('homepage');
  86.     }
  87.     
  88.     /**
  89.      * @Route("/aiuto", name="help")
  90.      */
  91.     public function help(Request $request)
  92.     {
  93.         $em $this->mr->getManager('master');
  94.         $slug $request->request->get('slug');
  95.         $help $em->getRepository("App\Entity\Master\Help")->findOneBySlug($slug);
  96.         $response = array("code" => 200"success" => true"title" => $help->getTitle(), "text" => $help->getText());
  97.         return new Response(json_encode($response)); 
  98.     }
  99.     // FILTRI
  100.     /**
  101.     * @Route("/aggiorna-comuni", name="update_cities")
  102.     */
  103.     public function updateCities(Request $request)
  104.     {
  105.         $em $this->mr->getManager('master');
  106.         $name $request->request->get('name');
  107.         $cities $em->getRepository("App\Entity\Master\City")->findByName($name);
  108.         $first true;
  109.         $json '[';
  110.         foreach($cities as $c){
  111.             if($first$first false; else $json.= ',';
  112.             $json.= '{
  113.                 "id":"'.$c->getId().'", 
  114.                 "name":'.json_encode($c->getName()).', 
  115.                 "province":'.json_encode($c->getProvince()->getSign()).', 
  116.                 "country":'.json_encode($c->getProvince()->getCountry()->getName()).',
  117.                 "defaultZip":"'.$c->getDefaultZip().'"
  118.             }';
  119.         }
  120.         $json .= ']';
  121.         $response = array("code" => 200"success" => true"cities" => $json);
  122.         return new Response(json_encode($response));
  123.     }
  124.     
  125.     /**
  126.     * @Route("/aggiorna-paesi-esteri", name="update_countries")
  127.     */
  128.     public function updateCountries(Request $request)
  129.     {
  130.         $em $this->mr->getManager('master');
  131.         $name $request->request->get('name');
  132.         $countries $em->getRepository("App\Entity\Master\Country")->findByName($name);
  133.         $first true;
  134.         $jsonCountries '[';
  135.         foreach($countries as $c){
  136.             if($first)
  137.                 $first false;
  138.             else
  139.                 $jsonCountries.= ',';
  140.             $jsonCountries.= '{"id":"'.$c->getId().'", "name":'.json_encode($c->getName()).'}';
  141.         }
  142.         $jsonCountries .= ']';
  143.         $response = array("code" => 200"success" => true"countries" => $jsonCountries);
  144.         return new Response(json_encode($response));
  145.     }
  146.     
  147.     /**
  148.     * @Route("/aggiorna-cittadinanze", name="update_nationalities")
  149.     */
  150.     public function updateNationalities(Request $request)
  151.     {
  152.         $em $this->mr->getManager('master');
  153.         $nationality $request->request->get('nationality');
  154.         $countries $em->getRepository("App\Entity\Master\Country")->findByNationality($nationality);
  155.         $first true;
  156.         $jsonCountries '[';
  157.         foreach($countries as $c){
  158.             if($first)
  159.                 $first false;
  160.             else
  161.                 $jsonCountries.= ',';
  162.             $jsonCountries.= '{"id":"'.$c->getId().'", "nationality":'.json_encode($c->getNationality()).'}';
  163.         }
  164.         $jsonCountries .= ']';
  165.         $response = array("code" => 200"success" => true"countries" => $jsonCountries);
  166.         return new Response(json_encode($response));
  167.     }
  168.     /**
  169.     * @Route("/aggiorna-cartelle", name="update_directories")
  170.     */
  171.     public function updateDirectories(Request $request)
  172.     {
  173.         $em OfficeService::getSlaveManager($this->mr$this->params$request->getSession());
  174.         $directoryName $request->request->get('directory');
  175.         $documents $em->getRepository("App\Entity\Slave\Document")->findByDirectoryName($directoryName);
  176.         
  177.         $first true;
  178.         $json '[';
  179.         foreach($documents as $document){
  180.             if($first)
  181.                 $first false;
  182.             else
  183.                 $json.= ',';
  184.         $json.= '{"id":'.$document->getId().',
  185.                 "directoryName":'.json_encode($document->getDirectory()).'}';
  186.         }
  187.         $json .= ']';
  188.         $response = array("code" => 100"success" => true"documents" => $json);
  189.         return new Response(json_encode($response));
  190.     }
  191.     /**
  192.     * @Route("/aggiorna-tipo-operazione", name="update_performance_name")
  193.     */
  194.     public function updatePerformanceName(Request $request)
  195.     {
  196.         $em $this->mr->getManager('master');
  197.         $name $request->request->get('name');
  198.         $performances $em->getRepository("App\Entity\Master\Configuration")->findByTypeSlugAndValue('performance_name'$name);
  199.         $first true;
  200.         $jsonPerformances '[';
  201.         foreach($performances as $p){
  202.             if($first)
  203.                 $first false;
  204.             else
  205.                 $jsonPerformances.= ',';
  206.             $jsonPerformances.= '{"id":"'.$p->getId().'", "value":'.json_encode($p->getValue()).', "typeId":'.json_encode($p->getType()->getId()).', "value2":'.$p->getValue2().'}';
  207.         }
  208.         $jsonPerformances .= ']';
  209.         $response = array("code" => 200"success" => true"pNames" => $jsonPerformances);
  210.         return new Response(json_encode($response));
  211.     }
  212.     
  213.     /**
  214.     * @Route("/aggiorna-tipo-operazione-com4", name="update_performance_name_com4")
  215.     */
  216.     public function updatePerformanceNameCom4(Request $request)
  217.     {
  218.         $em $this->mr->getManager('master');
  219.         $name $request->request->get('name');
  220.         $performances $em->getRepository("App\Entity\Master\Configuration")->findByTypeSlugAndValue('performance_name_com4'$name);
  221.         $first true;
  222.         $jsonPerformances '[';
  223.         foreach($performances as $p){
  224.             if($first)
  225.                 $first false;
  226.             else
  227.                 $jsonPerformances.= ',';
  228.             $jsonPerformances.= '{"id":"'.$p->getId().'", "value":'.json_encode($p->getValue()).', "value2":'.$p->getValue2().'}';
  229.         }
  230.         $jsonPerformances .= ']';
  231.         $response = array("code" => 200"success" => true"pNamesCom4" => $jsonPerformances);
  232.         return new Response(json_encode($response));
  233.     }
  234.     
  235.     /**
  236.     * @Route("/aggiorna-tipo-operazione-cnl4", name="update_performance_name_cnl4")
  237.     */
  238.     public function updatePerformanceNameCnl4(Request $request)
  239.     {
  240.         $em $this->mr->getManager('master');
  241.         $name $request->request->get('name');
  242.         $performances $em->getRepository("App\Entity\Master\Configuration")->findByTypeSlugAndValue('performance_name_cnl4'$name);
  243.         $first true;
  244.         $jsonPerformances '[';
  245.         foreach($performances as $p){
  246.             if($first)
  247.                 $first false;
  248.             else
  249.                 $jsonPerformances.= ',';
  250.             $jsonPerformances.= '{"id":"'.$p->getId().'", "value":'.json_encode($p->getValue()).', "value2":'.$p->getValue2().'}';
  251.         }
  252.         $jsonPerformances .= ']';
  253.         $response = array("code" => 200"success" => true"pNamesCnl4" => $jsonPerformances);
  254.         return new Response(json_encode($response));
  255.     }
  256.     /**
  257.     * @Route("/aggiorna-tipo-operazione-art1", name="update_performance_name_art1")
  258.     */
  259.     public function updatePerformanceNameArt1(Request $request)
  260.     {
  261.         $em $this->mr->getManager('master');
  262.         $name $request->request->get('name');
  263.         $performances $em->getRepository("App\Entity\Master\Configuration")->findByTypeSlugAndValue('performance_name_art1'$name);
  264.         $first true;
  265.         $jsonPerformances '[';
  266.         foreach($performances as $p){
  267.             if($first)
  268.                 $first false;
  269.             else
  270.                 $jsonPerformances.= ',';
  271.             $jsonPerformances.= '{"id":"'.$p->getId().'", "value":'.json_encode($p->getValue()).', "value2":'.$p->getValue2().'}';
  272.         }
  273.         $jsonPerformances .= ']';
  274.         $response = array("code" => 200"success" => true"pNamesArt1" => $jsonPerformances);
  275.         return new Response(json_encode($response));
  276.     }
  277.     
  278.     /**
  279.     * @Route("/trova-comune-da-codice", name="find_city_from_code")
  280.     */
  281.     public function findCityFromCode(Request $request)
  282.     {
  283.         $em $this->mr->getManager('master');
  284.         $code $request->request->get('cityCode');
  285.         $cityId '';
  286.         $cityName '';
  287.         $city $em->getRepository("App\Entity\Master\City")->findOneByCode($code);
  288.         if($city != null){
  289.             $cityId $city->getId();
  290.             $cityName $city->getName().' ('.$city->getProvince()->getSign().')';
  291.         }
  292.         $response = array("code" => 200"success" => true"cityId" => $cityId"cityName" => $cityName);
  293.         return new Response(json_encode($response));
  294.     }
  295. }