Пример #1
0
 /**
  * @Route("/contact", name="contact_route")
  * @param Request $request
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function contactAction(Request $request)
 {
     /** @var AnonymousToken $token */
     $token = $this->tokenStorage->getToken();
     /** @var User $user */
     $user = $token->getUser();
     $contact = new Contact();
     if ($user !== 'anon.') {
         /** @var User $user */
         $contact->setEmail($user->getEmail());
         $contact->setUser($user);
     }
     $emailGetParam = $request->query->get('email');
     if (!empty($emailGetParam)) {
         $contact->setEmail($emailGetParam);
     }
     $form = $this->formFactory->createBuilder(FormType::class, $contact)->add('email', TextType::class, ['attr' => ['placeholder' => '*****@*****.**']])->add('message', TextareaType::class, ['attr' => ['maxlength' => 1000, 'rows' => 10]])->add('submit', SubmitType::class, ['label' => 'Envoyer'])->getForm();
     $form->handleRequest($request);
     if ($form->isValid()) {
         $this->contactService->record($contact);
         $this->session->getFlashBag()->add(static::FLASH_BAG_EMAIL, $contact->getEmail());
         return new RedirectResponse($this->router->generate('confirmation_contact_route'));
     } else {
         return new Response($this->templating->render('base.html.twig', [TwigNodeTemplateTreeSection::TEMPLATE_TREE_BRANCH => 'contact', 'form' => $form->createView()]), $form->isSubmitted() ? Response::HTTP_BAD_REQUEST : Response::HTTP_OK);
     }
 }