src/Controller/DefaultController.php line 34

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