<?php
namespace App\Controller;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\FormError;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use App\Entity\Master\User;
use App\Form\PasswordCreationType;
use App\Form\PasswordRecoveryType;
use App\Form\Model\Contact;
use App\Form\ContactType;
use App\Service\ValidationService;
use App\Service\OfficeService;
class DefaultController extends AbstractController
{
protected $mr;
private $params;
public function __construct(ManagerRegistry $managerRegistry, ParameterBagInterface $params)
{
$this->mr = $managerRegistry;
$this->params = $params;
}
/**
* @Route("/", name="homepage")
*/
public function index(Request $request, MailerInterface $mailer)
{
$em = $this->mr->getManager('master');
$contact = new Contact();
$form = $this->createForm(ContactType::class, $contact);
$form->handleRequest($request);
if($form->isSubmitted()){
$valid = true;
$path = 'https://www.google.com/recaptcha/api/siteverify?secret=6LcmTdgUAAAAAHtbLS0hf0fJtNZALbjDqU_6Xxhq&response='.$request->request->get("g-recaptcha-response");
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/json'));
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$result = curl_exec($ch);
curl_close($ch);
$res = json_decode($result, true);
if(!$res["success"]){
$valid = false;
$form->get('recaptcha')->addError(new FormError("Convalidare prima di inviare la richiesta"));
$this->addFlash('notice_warning', 'Prima di inviare la richiesta, provare di non essere un robot.');
}
if($valid && $form->isValid()){
$message = (new TemplatedEmail())
->subject($this->params->get('subject_contact'))
->from($form->get('email')->getData())
->to($this->params->get('contact_email'))
->htmlTemplate('email/contact.html.twig')
->context(['name' => $form->get('name')->getData(), 'surname' => $form->get('surname')->getData(), 'message' => $form->get('message')->getData()]);
$mailer->send($message);
$this->addFlash('notice_success', 'Richiesta di informazioni inoltrata correttamente!');
return $this->redirectToRoute('homepage');
}
else
$this->addFlash('notice_warning', 'Controllare le informazioni inserite');
}
return $this->render('default/index.html.twig', array(
'form' => $form->createView()
));
}
/**
* @Route("/home/{section}", name="homepage_with_section")
*/
public function indexWithSection(Request $request, $section)
{
$session = $request->getSession();
$session->set('section', $section);
$session->set('dashboardTab', 'profile');
return $this->redirectToRoute('homepage');
}
/**
* @Route("/aiuto", name="help")
*/
public function help(Request $request)
{
$em = $this->mr->getManager('master');
$slug = $request->request->get('slug');
$help = $em->getRepository("App\Entity\Master\Help")->findOneBySlug($slug);
$response = array("code" => 200, "success" => true, "title" => $help->getTitle(), "text" => $help->getText());
return new Response(json_encode($response));
}
// FILTRI
/**
* @Route("/aggiorna-comuni", name="update_cities")
*/
public function updateCities(Request $request)
{
$em = $this->mr->getManager('master');
$name = $request->request->get('name');
$cities = $em->getRepository("App\Entity\Master\City")->findByName($name);
$first = true;
$json = '[';
foreach($cities as $c){
if($first) $first = false; else $json.= ',';
$json.= '{
"id":"'.$c->getId().'",
"name":'.json_encode($c->getName()).',
"province":'.json_encode($c->getProvince()->getSign()).',
"country":'.json_encode($c->getProvince()->getCountry()->getName()).',
"defaultZip":"'.$c->getDefaultZip().'"
}';
}
$json .= ']';
$response = array("code" => 200, "success" => true, "cities" => $json);
return new Response(json_encode($response));
}
/**
* @Route("/aggiorna-paesi-esteri", name="update_countries")
*/
public function updateCountries(Request $request)
{
$em = $this->mr->getManager('master');
$name = $request->request->get('name');
$countries = $em->getRepository("App\Entity\Master\Country")->findByName($name);
$first = true;
$jsonCountries = '[';
foreach($countries as $c){
if($first)
$first = false;
else
$jsonCountries.= ',';
$jsonCountries.= '{"id":"'.$c->getId().'", "name":'.json_encode($c->getName()).'}';
}
$jsonCountries .= ']';
$response = array("code" => 200, "success" => true, "countries" => $jsonCountries);
return new Response(json_encode($response));
}
/**
* @Route("/aggiorna-cittadinanze", name="update_nationalities")
*/
public function updateNationalities(Request $request)
{
$em = $this->mr->getManager('master');
$nationality = $request->request->get('nationality');
$countries = $em->getRepository("App\Entity\Master\Country")->findByNationality($nationality);
$first = true;
$jsonCountries = '[';
foreach($countries as $c){
if($first)
$first = false;
else
$jsonCountries.= ',';
$jsonCountries.= '{"id":"'.$c->getId().'", "nationality":'.json_encode($c->getNationality()).'}';
}
$jsonCountries .= ']';
$response = array("code" => 200, "success" => true, "countries" => $jsonCountries);
return new Response(json_encode($response));
}
/**
* @Route("/aggiorna-cartelle", name="update_directories")
*/
public function updateDirectories(Request $request)
{
$em = OfficeService::getSlaveManager($this->mr, $this->params, $request->getSession());
$directoryName = $request->request->get('directory');
$documents = $em->getRepository("App\Entity\Slave\Document")->findByDirectoryName($directoryName);
$first = true;
$json = '[';
foreach($documents as $document){
if($first)
$first = false;
else
$json.= ',';
$json.= '{"id":'.$document->getId().',
"directoryName":'.json_encode($document->getDirectory()).'}';
}
$json .= ']';
$response = array("code" => 100, "success" => true, "documents" => $json);
return new Response(json_encode($response));
}
/**
* @Route("/aggiorna-tipo-operazione", name="update_performance_name")
*/
public function updatePerformanceName(Request $request)
{
$em = $this->mr->getManager('master');
$name = $request->request->get('name');
$performances = $em->getRepository("App\Entity\Master\Configuration")->findByTypeSlugAndValue('performance_name', $name);
$first = true;
$jsonPerformances = '[';
foreach($performances as $p){
if($first)
$first = false;
else
$jsonPerformances.= ',';
$jsonPerformances.= '{"id":"'.$p->getId().'", "value":'.json_encode($p->getValue()).', "typeId":'.json_encode($p->getType()->getId()).', "value2":'.$p->getValue2().'}';
}
$jsonPerformances .= ']';
$response = array("code" => 200, "success" => true, "pNames" => $jsonPerformances);
return new Response(json_encode($response));
}
/**
* @Route("/aggiorna-tipo-operazione-com4", name="update_performance_name_com4")
*/
public function updatePerformanceNameCom4(Request $request)
{
$em = $this->mr->getManager('master');
$name = $request->request->get('name');
$performances = $em->getRepository("App\Entity\Master\Configuration")->findByTypeSlugAndValue('performance_name_com4', $name);
$first = true;
$jsonPerformances = '[';
foreach($performances as $p){
if($first)
$first = false;
else
$jsonPerformances.= ',';
$jsonPerformances.= '{"id":"'.$p->getId().'", "value":'.json_encode($p->getValue()).', "value2":'.$p->getValue2().'}';
}
$jsonPerformances .= ']';
$response = array("code" => 200, "success" => true, "pNamesCom4" => $jsonPerformances);
return new Response(json_encode($response));
}
/**
* @Route("/aggiorna-tipo-operazione-cnl4", name="update_performance_name_cnl4")
*/
public function updatePerformanceNameCnl4(Request $request)
{
$em = $this->mr->getManager('master');
$name = $request->request->get('name');
$performances = $em->getRepository("App\Entity\Master\Configuration")->findByTypeSlugAndValue('performance_name_cnl4', $name);
$first = true;
$jsonPerformances = '[';
foreach($performances as $p){
if($first)
$first = false;
else
$jsonPerformances.= ',';
$jsonPerformances.= '{"id":"'.$p->getId().'", "value":'.json_encode($p->getValue()).', "value2":'.$p->getValue2().'}';
}
$jsonPerformances .= ']';
$response = array("code" => 200, "success" => true, "pNamesCnl4" => $jsonPerformances);
return new Response(json_encode($response));
}
/**
* @Route("/aggiorna-tipo-operazione-art1", name="update_performance_name_art1")
*/
public function updatePerformanceNameArt1(Request $request)
{
$em = $this->mr->getManager('master');
$name = $request->request->get('name');
$performances = $em->getRepository("App\Entity\Master\Configuration")->findByTypeSlugAndValue('performance_name_art1', $name);
$first = true;
$jsonPerformances = '[';
foreach($performances as $p){
if($first)
$first = false;
else
$jsonPerformances.= ',';
$jsonPerformances.= '{"id":"'.$p->getId().'", "value":'.json_encode($p->getValue()).', "value2":'.$p->getValue2().'}';
}
$jsonPerformances .= ']';
$response = array("code" => 200, "success" => true, "pNamesArt1" => $jsonPerformances);
return new Response(json_encode($response));
}
/**
* @Route("/trova-comune-da-codice", name="find_city_from_code")
*/
public function findCityFromCode(Request $request)
{
$em = $this->mr->getManager('master');
$code = $request->request->get('cityCode');
$cityId = '';
$cityName = '';
$city = $em->getRepository("App\Entity\Master\City")->findOneByCode($code);
if($city != null){
$cityId = $city->getId();
$cityName = $city->getName().' ('.$city->getProvince()->getSign().')';
}
$response = array("code" => 200, "success" => true, "cityId" => $cityId, "cityName" => $cityName);
return new Response(json_encode($response));
}
}