Skip to content

Commit c42e76a

Browse files
committed
Add contact form
1 parent 958aa91 commit c42e76a

File tree

8 files changed

+260
-0
lines changed

8 files changed

+260
-0
lines changed

app/Resources/views/base.html.twig

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
<li>
5050
<a href="{{ path("faq") }}">FAQ</a>
5151
</li>
52+
<li>
53+
<a href="{{ path("contact") }}">Kontakt</a>
54+
</li>
5255
</ul>
5356
<p class="navbar-text navbar-right">
5457
{% if user is defined and user %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% extends 'base.html.twig' %}
2+
3+
{% block body %}
4+
{{ form_start(form) }}
5+
{{ form_row(form.email) }}
6+
{{ form_row(form.text) }}
7+
<br />
8+
<button class="btn btn-lg btn-primary btn-block" type="submit">Odeslat</button>
9+
{{ form_end(form) }}
10+
11+
{% endblock %}

app/config/services.yml

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ services:
2424
class: AppBundle\Controller\FaqController
2525
autowire: true
2626

27+
app.controller.contact_controller:
28+
class: AppBundle\Controller\ContactController
29+
autowire: true
30+
2731
app.facade.category_facade:
2832
class: AppBundle\Facade\CategoryFacade
2933
autowire: true
@@ -44,6 +48,10 @@ services:
4448
class: AppBundle\Facade\QuestionFacade
4549
autowire: true
4650

51+
app.facade.message_facade:
52+
class: AppBundle\Facade\MessageFacade
53+
autowire: true
54+
4755
app.repository.category_repository:
4856
class: AppBundle\Repository\CategoryRepository
4957
factory: ['@doctrine.orm.default_entity_manager', getRepository]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace AppBundle\Controller;
4+
5+
use AppBundle\Facade\MessageFacade;
6+
use AppBundle\FormType\ContactFormType;
7+
use AppBundle\FormType\VO\MessageVO;
8+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
10+
use Symfony\Component\Form\FormFactory;
11+
use Symfony\Component\HttpFoundation\Request;
12+
13+
/**
14+
* @Route(service="app.controller.contact_controller")
15+
*/
16+
class ContactController
17+
{
18+
/** @var FormFactory */
19+
private $formFactory;
20+
21+
/** @var MessageFacade */
22+
private $messageFacade;
23+
24+
/**
25+
* @param FormFactory $formFactory
26+
* @param MessageFacade $messageFacade
27+
*/
28+
public function __construct(FormFactory $formFactory, MessageFacade $messageFacade)
29+
{
30+
$this->formFactory = $formFactory;
31+
$this->messageFacade = $messageFacade;
32+
}
33+
34+
/**
35+
* @Route("/contact", name="contact")
36+
* @Template("contact/contact.html.twig")
37+
*/
38+
public function contactAction(Request $request)
39+
{
40+
$messageVO = new MessageVO();
41+
$form = $this->formFactory->create(ContactFormType::class, $messageVO);
42+
43+
$form->handleRequest($request);
44+
if ($form->isSubmitted() && $form->isValid()) {
45+
$this->messageFacade->save($messageVO);
46+
}
47+
48+
return [
49+
"form" => $form->createView()
50+
];
51+
}
52+
}

src/AppBundle/Entity/Message.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace AppBundle\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Symfony\Component\Validator\Constraints as Assert;
7+
8+
/**
9+
* @ORM\Entity
10+
*/
11+
class Message
12+
{
13+
/**
14+
* @ORM\Id
15+
* @ORM\GeneratedValue
16+
* @ORM\Column(type="integer")
17+
* @var int
18+
*/
19+
private $id;
20+
21+
/**
22+
* @ORM\Column(type="string", length=255, unique=true, name="email")
23+
* @Assert\NotBlank()
24+
* @Assert\Email()
25+
* @var string
26+
*/
27+
private $email;
28+
29+
/**
30+
* @ORM\Column(type="string")
31+
* @var string
32+
*/
33+
private $text;
34+
35+
/**
36+
* @return int
37+
*/
38+
public function getId(): int
39+
{
40+
return $this->id;
41+
}
42+
43+
/**
44+
* @return string
45+
*/
46+
public function getEmail() : string
47+
{
48+
return $this->email;
49+
}
50+
51+
/**
52+
* @param string $email
53+
*/
54+
public function setEmail(string $email)
55+
{
56+
$this->email = $email;
57+
}
58+
59+
/**
60+
* @return string
61+
*/
62+
public function getText(): string
63+
{
64+
return $this->text;
65+
}
66+
67+
/**
68+
* @param string $text
69+
*/
70+
public function setText(string $text)
71+
{
72+
$this->text = $text;
73+
}
74+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace AppBundle\Facade;
4+
5+
use AppBundle\Entity\Message;
6+
use AppBundle\FormType\VO\MessageVO;
7+
use Doctrine\ORM\EntityManager;
8+
9+
class MessageFacade
10+
{
11+
/** @var EntityManager */
12+
private $entityManager;
13+
14+
/**
15+
* @param EntityManager $entityManager
16+
*/
17+
public function __construct(EntityManager $entityManager)
18+
{
19+
$this->entityManager = $entityManager;
20+
}
21+
22+
/**
23+
* @param MessageVO $messageVO
24+
*/
25+
public function save(MessageVO $messageVO)
26+
{
27+
$message = new Message();
28+
29+
$message->setEmail($messageVO->getEmail());
30+
$message->setText($messageVO->getText());
31+
32+
$this->entityManager->persist($message);
33+
$this->entityManager->flush($message);
34+
}
35+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace AppBundle\FormType;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\Extension\Core\Type\EmailType;
7+
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
8+
use Symfony\Component\Form\FormBuilderInterface;
9+
use Symfony\Component\Validator\Constraints\NotBlank;
10+
11+
class ContactFormType extends AbstractType
12+
{
13+
public function buildForm(FormBuilderInterface $builder, array $options)
14+
{
15+
$builder->add("email", EmailType::class, [
16+
"label" => "E-mail",
17+
"attr" => [
18+
"class" => "form-control",
19+
],
20+
"constraints" => [
21+
new NotBlank(["message" => "Prosím vyplňte Váš e-mail"]),
22+
],
23+
])->add("text", TextareaType::class, [
24+
"label" => "Zpráva",
25+
"attr" => [
26+
"class" => "form-control",
27+
],
28+
"constraints" => [
29+
new NotBlank(["message" => "Prosím napište text zprávy"]),
30+
],
31+
]);
32+
}
33+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace AppBundle\FormType\VO;
4+
5+
class MessageVO
6+
{
7+
/** @var string */
8+
private $email;
9+
10+
/** @var string */
11+
private $text;
12+
13+
/**
14+
* @return string
15+
*/
16+
public function getEmail()
17+
{
18+
return $this->email;
19+
}
20+
21+
/**
22+
* @param string $email
23+
*/
24+
public function setEmail(string $email)
25+
{
26+
$this->email = $email;
27+
}
28+
29+
/**
30+
* @return string
31+
*/
32+
public function getText()
33+
{
34+
return $this->text;
35+
}
36+
37+
/**
38+
* @param string $text
39+
*/
40+
public function setText(string $text)
41+
{
42+
$this->text = $text;
43+
}
44+
}

0 commit comments

Comments
 (0)