src/Controller/DefaultController.php line 35

Open in your IDE?
  1. <?php namespace App\Controller;
  2. use App\Flash;
  3. use App\Security\SpamGuard;
  4. use Knp\Snappy\Pdf;
  5. use Symfony\Component\Form\FormError;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use App\Form\ContactType;
  8. use App\Form\UserChangePasswordType;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Mailer\MailerInterface;
  13. use Symfony\Component\Mime\Address;
  14. use Symfony\Component\Mime\Email;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class DefaultController extends Controller
  17. {
  18.     private MailerInterface $mailer;
  19.     private SpamGuard $spamGuard;
  20.     public function __construct(
  21.         MailerInterface $mailer,
  22.         SpamGuard $spamGuard
  23.     ) {
  24.         $this->mailer $mailer;
  25.         $this->spamGuard $spamGuard;
  26.     }
  27.     /**
  28.      * @Route("/", name="homepage")
  29.      */
  30.     public function indexAction(): Response
  31.     {
  32.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  33.             return $this->forward('App\Controller\PointController::listAction', [
  34.                 'type' => 'muff',
  35.             ]);
  36.         }
  37.         
  38.         return $this->render('default/index.html.twig');
  39.     }
  40.     /**
  41.      * @Route("/lang/{_locale}", name="change_locale")
  42.      */
  43.     public function changeLocaleAction(): RedirectResponse
  44.     {
  45.         return $this->redirectToRoute('homepage');
  46.     }
  47.     /**
  48.      *
  49.      * @Route("/company", name="company", options={"expose"=true})
  50.      */
  51.     public function companyAction(): JsonResponse
  52.     {
  53.         $this->denyAccessUnlessGranted('ROLE_USER');
  54.         $user $this->getUser();
  55.         $company $user->getCompany();
  56.         $response = new JsonResponse();
  57.         
  58.         if ($user->getReloadMap()) {
  59.             
  60.             $response->headers->clearCookie(sprintf("map-%d"$company->getId()));
  61.             
  62.             $user->setReloadMap(false);
  63.             $this->getEntityManager()->flush();
  64.         }
  65.         $response->setData([
  66.             'id'    => $company->getId(),
  67.             'lat'   => $company->getLatitude(),
  68.             'lng'   => $company->getLongitude(),
  69.             'zoom'  => $company->getZoom(),
  70.             'unit'  => $company->getUnitOfMeasure() == 'km' 'kilometres' 'landmiles',
  71.         ]);
  72.         
  73.         return $response;
  74.     }
  75.     
  76.     /**
  77.      *
  78.      * @Route("/change-password", name="change_password")
  79.      */
  80.     public function changePasswordAction(Request $request): Response
  81.     {
  82.         $this->denyAccessUnlessGranted('ROLE_USER');
  83.         $user $this->getUser();
  84.         
  85.         $form $this->createForm(UserChangePasswordType::class, $user);
  86.         $form->handleRequest($request);
  87.         if ($form->isSubmitted() && $form->isValid()) {
  88.             $em $this->getEntityManager();
  89.             $em->flush();
  90.             $this->addFlash(Flash::SUCCESS'Hasło zostało pomyślnie zmienione.');
  91.             return $this->redirectToRoute('change_password');
  92.         }
  93.         return $this->render('default/changePassword.html.twig', [
  94.             'user'  => $user,
  95.             'form'  => $form->createView(),
  96.         ]);
  97.     }
  98.     
  99.     /**
  100.      * @Route("/help", name="help")
  101.      */
  102.     public function helpAction(): Response
  103.     {
  104.         return $this->render('default/help.html.twig');
  105.     }
  106.     /**
  107.      * @Route("/order", name="order")
  108.      */
  109.     public function orderAction(Request $request): Response
  110.     {
  111.         $form $this->createForm(ContactType::class);
  112.         $form->handleRequest($request);
  113.         if ($form->isSubmitted()) {
  114.             if ($this->spamGuard->isRateLimited($request)) {
  115.                 $form->addError(new FormError(
  116.                     $this->translate('Zbyt wiele prób. Spróbuj ponownie za kilka minut.')
  117.                 ));
  118.             } elseif ($form->isValid()) {
  119.                 if (!$this->spamGuard->isSpam($request$form'order_form_rendered_at')) {
  120.                     $data $form->getData();
  121.                     $email = (new Email())
  122.                         ->subject("FIBKIT.com: formularz kontaktowy")
  123.                         ->from($data['email'])
  124.                         ->replyTo(new Address($data['email'], $data['name']))
  125.                         ->to('info@fibkit.com')
  126.                         ->html($this->renderView(
  127.                             'default/emailOrder.html.twig', [
  128.                                 'data' => $data,
  129.                             ]
  130.                         ));
  131.                     $this->mailer->send($email);
  132.                 }
  133.                 if ($request->isXmlHttpRequest()) {
  134.                     return new Response('1');
  135.                 }
  136.                 $this->addFlash(Flash::SUCCESS'Wiadomość została pomyślnie wysłana.');
  137.                 return $this->redirectToRoute('order');
  138.             }
  139.         } else {
  140.             $this->spamGuard->markRendered($request'order_form_rendered_at');
  141.         }
  142.         return $this->render('default/order.html.twig', [
  143.             'form' => $form->createView(),
  144.         ]);
  145.     }
  146.     /**
  147.      * @Route("/services", name="services")
  148.      */
  149.     public function servicesAction(): Response
  150.     {
  151.         return $this->render('default/services.html.twig');
  152.     }
  153.     /**
  154.      * @Route("/pricing", name="pricing")
  155.      */
  156.     public function pricingAction(): Response
  157.     {
  158.         return $this->render('default/pricing.html.twig');
  159.     }
  160.     /**
  161.      * @Route("/team", name="team")
  162.      */
  163.     public function teamAction(): Response
  164.     {
  165.         return $this->render('default/team.html.twig');
  166.     }
  167.     
  168.     /**
  169.      * @Route("/product", name="product")
  170.      */
  171.     public function productAction(): Response
  172.     {
  173.         return $this->render('default/product.html.twig');
  174.     }
  175.     
  176.     /**
  177.      * @Route("/resources", name="resources")
  178.      */
  179.     public function resourcesAction(): Response
  180.     {
  181.         return $this->render('default/resources.html.twig');
  182.     }
  183.     
  184.     /**
  185.      * @Route("/contact", name="contact")
  186.      */
  187.     public function contactAction(Request $request): Response
  188.     {
  189.         $form $this->createForm(ContactType::class);
  190.         $form->handleRequest($request);
  191.         if ($form->isSubmitted()) {
  192.             if ($this->spamGuard->isRateLimited($request)) {
  193.                 $form->addError(new FormError(
  194.                     $this->translate('Zbyt wiele prób. Spróbuj ponownie za kilka minut.')
  195.                 ));
  196.             } elseif ($this->spamGuard->failsRecaptcha($request)) {
  197.                 $form->addError(new FormError(
  198.                     $this->translate('Weryfikacja reCAPTCHA nie powiodła się. Spróbuj ponownie.')
  199.                 ));
  200.             } elseif ($form->isValid()) {
  201.                 if (!$this->spamGuard->isSpam($request$form'contact_form_rendered_at')) {
  202.                     $data $form->getData();
  203.                     $email = (new Email())
  204.                         ->subject("FIBKIT.com: formularz kontaktowy")
  205.                         ->from($data['email'])
  206.                         ->replyTo(new Address($data['email'], $data['name']))
  207.                         ->to('info@fibkit.com')
  208.                         ->html($this->renderView(
  209.                             'default/emailContact.html.twig', [
  210.                                 'data' => $data,
  211.                             ]
  212.                         ));
  213.                     $this->mailer->send($email);
  214.                 }
  215.                 if ($request->isXmlHttpRequest()) {
  216.                     return new Response('1');
  217.                 }
  218.                 $this->addFlash(Flash::SUCCESS$this->translate('Wiadomość została pomyślnie wysłana.'));
  219.                 return $this->redirectToRoute('contact');
  220.             }
  221.         } else {
  222.             $this->spamGuard->markRendered($request'contact_form_rendered_at');
  223.         }
  224.         return $this->render('default/contact.html.twig', [
  225.             'form' => $form->createView(),
  226.         ]);
  227.     }
  228.     
  229.     /**
  230.      * @Route("/clients", name="clients")
  231.      */
  232.     public function clientsAction(): Response
  233.     {
  234.         return $this->render('default/clients.html.twig');
  235.     }
  236.     /**
  237.      * @Route("/regulamin", name="terms")
  238.      */
  239.     public function termsAction(): Response
  240.     {
  241.         return $this->render('default/terms.html.twig');
  242.     }
  243.     /**
  244.      * @Route("/test-pdf", name="test_pdf")
  245.      */
  246.     public function pdfAction(Pdf $snappy): Response
  247.     {
  248.         $html $this->renderView('default/pdf.html.twig');
  249.         // $snappy->setOption('margin-top', '0');
  250.         $snappy->setOption('header-center'"POLSKIE ZNAKI: ĄŻĆŹŚĘŁÓŃ  ąśćźżęłóń");
  251.         $snappy->setOption('footer-center''POLSKIE ZNAKI: ĄŻĆŹŚĘŁÓŃ  ąśćźżęłóń');
  252.         return new Response(
  253.             $snappy->getOutputFromHtml($html), Response::HTTP_OK, [
  254.                 'Content-Type' => 'application/pdf',
  255.                 'Content-Disposition' => 'attachment; filename="test-pdf.pdf"',
  256.             ]
  257.         );
  258.     }
  259. }