Skip to content

Commit

Permalink
Add contact form
Browse files Browse the repository at this point in the history
  • Loading branch information
Barvoj committed Oct 29, 2016
1 parent 958aa91 commit c42e76a
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
<li>
<a href="{{ path("faq") }}">FAQ</a>
</li>
<li>
<a href="{{ path("contact") }}">Kontakt</a>
</li>
</ul>
<p class="navbar-text navbar-right">
{% if user is defined and user %}
Expand Down
11 changes: 11 additions & 0 deletions app/Resources/views/contact/contact.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}

{% block body %}
{{ form_start(form) }}
{{ form_row(form.email) }}
{{ form_row(form.text) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Odeslat</button>
{{ form_end(form) }}

{% endblock %}
8 changes: 8 additions & 0 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ services:
class: AppBundle\Controller\FaqController
autowire: true

app.controller.contact_controller:
class: AppBundle\Controller\ContactController
autowire: true

app.facade.category_facade:
class: AppBundle\Facade\CategoryFacade
autowire: true
Expand All @@ -44,6 +48,10 @@ services:
class: AppBundle\Facade\QuestionFacade
autowire: true

app.facade.message_facade:
class: AppBundle\Facade\MessageFacade
autowire: true

app.repository.category_repository:
class: AppBundle\Repository\CategoryRepository
factory: ['@doctrine.orm.default_entity_manager', getRepository]
Expand Down
52 changes: 52 additions & 0 deletions src/AppBundle/Controller/ContactController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace AppBundle\Controller;

use AppBundle\Facade\MessageFacade;
use AppBundle\FormType\ContactFormType;
use AppBundle\FormType\VO\MessageVO;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\HttpFoundation\Request;

/**
* @Route(service="app.controller.contact_controller")
*/
class ContactController
{
/** @var FormFactory */
private $formFactory;

/** @var MessageFacade */
private $messageFacade;

/**
* @param FormFactory $formFactory
* @param MessageFacade $messageFacade
*/
public function __construct(FormFactory $formFactory, MessageFacade $messageFacade)
{
$this->formFactory = $formFactory;
$this->messageFacade = $messageFacade;
}

/**
* @Route("/contact", name="contact")
* @Template("contact/contact.html.twig")
*/
public function contactAction(Request $request)
{
$messageVO = new MessageVO();
$form = $this->formFactory->create(ContactFormType::class, $messageVO);

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->messageFacade->save($messageVO);
}

return [
"form" => $form->createView()
];
}
}
74 changes: 74 additions & 0 deletions src/AppBundle/Entity/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ORM\Entity
*/
class Message
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @var int
*/
private $id;

/**
* @ORM\Column(type="string", length=255, unique=true, name="email")
* @Assert\NotBlank()
* @Assert\Email()
* @var string
*/
private $email;

/**
* @ORM\Column(type="string")
* @var string
*/
private $text;

/**
* @return int
*/
public function getId(): int
{
return $this->id;
}

/**
* @return string
*/
public function getEmail() : string
{
return $this->email;
}

/**
* @param string $email
*/
public function setEmail(string $email)
{
$this->email = $email;
}

/**
* @return string
*/
public function getText(): string
{
return $this->text;
}

/**
* @param string $text
*/
public function setText(string $text)
{
$this->text = $text;
}
}
35 changes: 35 additions & 0 deletions src/AppBundle/Facade/MessageFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace AppBundle\Facade;

use AppBundle\Entity\Message;
use AppBundle\FormType\VO\MessageVO;
use Doctrine\ORM\EntityManager;

class MessageFacade
{
/** @var EntityManager */
private $entityManager;

/**
* @param EntityManager $entityManager
*/
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}

/**
* @param MessageVO $messageVO
*/
public function save(MessageVO $messageVO)
{
$message = new Message();

$message->setEmail($messageVO->getEmail());
$message->setText($messageVO->getText());

$this->entityManager->persist($message);
$this->entityManager->flush($message);
}
}
33 changes: 33 additions & 0 deletions src/AppBundle/FormType/ContactFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace AppBundle\FormType;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotBlank;

class ContactFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add("email", EmailType::class, [
"label" => "E-mail",
"attr" => [
"class" => "form-control",
],
"constraints" => [
new NotBlank(["message" => "Prosím vyplňte Váš e-mail"]),
],
])->add("text", TextareaType::class, [
"label" => "Zpráva",
"attr" => [
"class" => "form-control",
],
"constraints" => [
new NotBlank(["message" => "Prosím napište text zprávy"]),
],
]);
}
}
44 changes: 44 additions & 0 deletions src/AppBundle/FormType/VO/MessageVO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace AppBundle\FormType\VO;

class MessageVO
{
/** @var string */
private $email;

/** @var string */
private $text;

/**
* @return string
*/
public function getEmail()
{
return $this->email;
}

/**
* @param string $email
*/
public function setEmail(string $email)
{
$this->email = $email;
}

/**
* @return string
*/
public function getText()
{
return $this->text;
}

/**
* @param string $text
*/
public function setText(string $text)
{
$this->text = $text;
}
}

0 comments on commit c42e76a

Please sign in to comment.