Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ukol 4 - Milan Staněk #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<li>
<a href="{{ path("homepage") }}">Domů</a>
</li>
<li>
<a href="{{ path("message") }}">Kontakt</a>
</li>
</ul>
<p class="navbar-text navbar-right">
{% if user is defined and user %}
Expand Down
6 changes: 6 additions & 0 deletions app/Resources/views/message/message_form.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends 'base.html.twig' %}

{% block body %}
{{ form(form) }}
{% endblock %}

9 changes: 9 additions & 0 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ services:
class: AppBundle\Controller\UserController
autowire: true

app.controller.message_controller:
class: AppBundle\Controller\MessageController
autowire: true

app.facade.category_facade:
class: AppBundle\Facade\CategoryFacade
autowire: true
Expand All @@ -42,6 +46,11 @@ services:
factory: ['@doctrine.orm.default_entity_manager', getRepository]
arguments: ['AppBundle\Entity\Product']

app.repository.message_repository:
class: AppBundle\Repository\MessageRepository
factory: ['@doctrine.orm.default_entity_manager', getRepository]
arguments: ['AppBundle\Entity\Message']

encoder:
class: Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder
arguments:
Expand Down
65 changes: 65 additions & 0 deletions src/AppBundle/Controller/MessageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
namespace AppBundle\Controller;

use AppBundle\Entity\Message;
use AppBundle\Facade\MessageFacade;
use AppBundle\FormType\MessageFormType;
use Doctrine\ORM\EntityManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;


/**
* @author Milan Staněk <mistacms@gmail.com>
* @Route(service="app.controller.message_controller")
*/
class MessageController
{
private $messageFacade;
private $formFactory;
private $entityManager;
private $router;

public function __construct(
MessageFacade $messageFacade,
EntityManager $entityManager,
FormFactory $formFactory,
RouterInterface $router
) {
$this->messageFacade = $messageFacade;
$this->formFactory = $formFactory;
$this->entityManager = $entityManager;
$this->router = $router;
}

/**
* @Route("/contact", name="message")
* @Template("message/message_form.html.twig")
* @param Request $request
* @return RedirectResponse|array
*/
public function formAction(Request $request)
{
$message = new Message();
$form = $this->formFactory->create(MessageFormType::class, $message);

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {

$this->entityManager->persist($message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pryč z toho controlleru ;)

$this->entityManager->flush([$message]);

return RedirectResponse::create($this->router->generate("message"));
}

return [
"form" => $form->createView(),
];
}


}
116 changes: 116 additions & 0 deletions src/AppBundle/Entity/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @author Milan Staněk <mistacms@gmail.com>
* @ORM\Entity(repositoryClass="AppBundle\Repository\MessageRepository")
*/
class Message
{

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

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

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

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


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

/**
* @param int $id
* @return self
*/
public function setId($id)
{
$this->id = $id;

return $this;
}

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

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

return $this;
}

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

/**
* @param string $subject
* @return self
*/
public function setSubject($subject)
{
$this->subject = $subject;

return $this;
}

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

/**
* @param string $message
* @return self
*/
public function setMessage($message)
{
$this->message = $message;

return $this;
}

}
32 changes: 32 additions & 0 deletions src/AppBundle/Facade/MessageFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace AppBundle\Facade;

use AppBundle\Entity\Message;
use AppBundle\Repository\MessageRepository;
use Doctrine\ORM\EntityManager;

/**
* @author Milan Stanek <mistacms@google.com>
*/
class MessageFacade
{

private $messageRepository;
private $entityManager;

public function __construct(
MessageRepository $messageRepository,
EntityManager $entityManager
) {
$this->messageRepository = $messageRepository;
$this->entityManager = $entityManager;
}


public function save(Message $mmessage)
{
$this->entityManager->persist($mmessage);
$this->entityManager->flush($mmessage);
}

}
54 changes: 54 additions & 0 deletions src/AppBundle/FormType/MessageFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
namespace AppBundle\FormType;

use AppBundle\Entity\Message;
use AppBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Milan Staněk <mistacms@gmail.com>
* @ORM\Entity
* @ORM\Table(name="message_form_type")
*/
class MessageFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add("email", EmailType::class, [
"label" => "E-mail",
"attr" => [
"class" => "form-control",
],
])
->add("subject", TextType::class, [
"label" => "Predmet",
"attr" => [
"class" => "form-control",
],
])
->add("message", TextareaType::class, [
"label" => "Obsah",
"attr" => [
"class" => "form-control",
],
])
->add('submit', SubmitType::class, [
'label' => 'Uložit změny',
]);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
"data_class" => Message::class,
));
}
}
15 changes: 15 additions & 0 deletions src/AppBundle/Repository/MessageRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace AppBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
* CategoryRepository
*
* This class was generated by the PhpStorm "Php Annotations" Plugin. Add your own custom
* repository methods below.
*/
class MessageRepository extends EntityRepository
{
}