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 3 - Jan Suchánek #34

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</ul>
<p class="navbar-text navbar-right">
{% if user is defined and user %}
<a href="#" class="navbar-link">{{ user.username }}</a>
<a href="{{ path("user_edit") }}" class="navbar-link">{{ user.username }}</a>
| <a href="{{ path("user_logout") }}" class="navbar-link">Odhlásit</a>
{% else %}
<a href="{{ path("user_login") }}" class="navbar-link">Přihlásit</a>
Expand Down
14 changes: 14 additions & 0 deletions app/Resources/views/user/user_edit.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends 'base.html.twig' %}

{% block body %}
<h1>Adresa uživatele</h1>
{{ form_start(form) }}
{{ form_row(form.firstName) }}
{{ form_row(form.surName) }}
{{ form_row(form.street) }}
{{ form_row(form.city) }}
{{ form_row(form.zip) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Upravit</button>
{{ form_end(form) }}
{% endblock %}
102 changes: 68 additions & 34 deletions src/AppBundle/Controller/UserController.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\User;
use AppBundle\Entity\Address;
use AppBundle\Facade\UserFacade;
use AppBundle\FormType\RegistrationFormType;
use AppBundle\FormType\UserEditFormType;
use Doctrine\ORM\EntityManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
Expand Down Expand Up @@ -41,41 +43,41 @@ public function __construct(
$this->router = $router;
}

/**
* @Route("/registrovat", name="user_registration")
* @Template("user/registration.html.twig")
*
* @param Request $request
* @return RedirectResponse|array
*/
public function registrationAction(Request $request)
{
// 1) build the form
$user = new User();
$form = $this->formFactory->create(RegistrationFormType::class, $user);

// 2) handle the submit (will only happen on POST)
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {

// 3) Encode the password (you could also do this via Doctrine listener)
$user->setPassword(
$this->passwordEncoder->encodePassword($user->getPlainPassword(), null)
);

// 4) save the User!
$this->entityManager->persist($user);
$this->entityManager->flush();

// ... do any other work - like sending them an email, etc
// maybe set a "flash" success message for the user
return RedirectResponse::create($this->router->generate("homepage"));
}
/**
* @Route("/registrovat", name="user_registration")
* @Template("user/registration.html.twig")
*
* @param Request $request
* @return RedirectResponse|array
*/
public function registrationAction(Request $request)
{
// 1) build the form
$user = new User();
$form = $this->formFactory->create(RegistrationFormType::class, $user);

return [
"form" => $form->createView(),
];
}
// 2) handle the submit (will only happen on POST)
Copy link
Member

Choose a reason for hiding this comment

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

Bacha tady, změnil jsi indent na mezery

Copy link
Author

Choose a reason for hiding this comment

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

Oj ok, díky

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

// 3) Encode the password (you could also do this via Doctrine listener)
$user->setPassword(
$this->passwordEncoder->encodePassword($user->getPlainPassword(), null)
);

// 4) save the User!
$this->entityManager->persist($user);
$this->entityManager->flush();

// ... do any other work - like sending them an email, etc
// maybe set a "flash" success message for the user
return RedirectResponse::create($this->router->generate("homepage"));
}

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

/**
* @Route("/prihlasit", name="user_login")
Expand Down Expand Up @@ -113,4 +115,36 @@ public function superSecretAction()
];
}

/**
* @Route("/uzivatel/edit", name="user_edit")
* @Template("user/user_edit.html.twig")
*
* @param Request $request
* @return RedirectResponse|array
*/
public function editAction(Request $request)
Copy link
Member

Choose a reason for hiding this comment

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

Tady spíš logická - vlastně pokaždé přidáváš novou adresu. editAction tedy není zrovna vypovídající název

Copy link
Author

Choose a reason for hiding this comment

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

Jasné, já sem ze začátku válčil jak to pojmenovat, máš pravdu.

{
if (!$this->userFacade->getUser()) {
throw new UnauthorizedHttpException("Přihlašte se");
}

$address = new Address();
$form = $this->formFactory->create(UserEditFormType::class, $address);

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

$this->entityManager->persist($address);
$this->entityManager->flush();

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

return [
"form" => $form->createView(),
"user" => $this->userFacade->getUser(),
];

}

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

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

/**
* @author Vašek Boch <vasek.boch@live.com>
* @author Jan Klat <jenik@klatys.cz>
*
* @ORM\Entity(repositoryClass="AppBundle\Repository\AddressRepository")
* @ORM\Table(name="address")
*/
class Address
{

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

/**
* @ORM\Column(type="string", length=255, unique=false, name="firstName")
Copy link
Member

Choose a reason for hiding this comment

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

tady jdeš proti zbytku projektu, sloupec v db se bude jmenovat firstName, přestože jinde by to bylo first_name. Platí i pro ostatní sloupce

* @Assert\NotBlank()
*/
private $firstName;

/**
* @ORM\Column(type="string", length=255, unique=false, name="surName")
* @Assert\NotBlank()
*/
private $surName;
Copy link
Member

Choose a reason for hiding this comment

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

když už tak lastName. Surname je jedno slovo

Copy link
Author

Choose a reason for hiding this comment

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

Ok špatné názvosloví, díky.


/**
* @ORM\Column(type="string", length=255, unique=false, name="street")
* @Assert\NotBlank()
*/
private $street;

/**
* @ORM\Column(type="string", length=255, unique=false, name="city")
* @Assert\NotBlank()
*/
private $city;

/**
* @ORM\Column(type="string", length=5, unique=false, name="zip")
* @Assert\NotBlank()
*/
private $zip;

/**
* @return mixed
*/
public function getFirstName()
{
return $this->firstName;
}

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

/**
* @return mixed
*/
public function getCity()
{
return $this->city;
}

/**
* @return mixed
*/
public function getSurName()
{
return $this->surName;
}

/**
* @return mixed
*/
public function getStreet()
{
return $this->street;
}

/**
* @return mixed
*/
public function getZip()
{
return $this->zip;
}

/**
* @param mixed $city
*/
public function setCity($city)
{
$this->city = $city;
}

/**
* @param mixed $firstName
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
}

/**
* @param mixed $street
*/
public function setStreet($street)
{
$this->street = $street;
}

/**
* @param mixed $surName
*/
public function setSurName($surName)
{
$this->surName = $surName;
}

/**
* @param mixed $zip
*/
public function setZip($zip)
{
$this->zip = $zip;
}

/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
}
13 changes: 13 additions & 0 deletions src/AppBundle/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class User implements UserInterface
*/
private $id;

/**
* @var Address
* @ORM\ManyToOne(targetEntity="Address")
Copy link
Member

Choose a reason for hiding this comment

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

Tohle je bohužel špatně, ve formuláři přidáváš pokaždé novou adresu, tady připojuješ víc uživatelů k jedné adrese (což ani nedává smysl) správně by tato vazba měla být jako OneToMany

Copy link
Author

Choose a reason for hiding this comment

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

Ok

*/
private $address;

/**
* @ORM\Column(type="string", length=255, unique=true, name="email")
* @Assert\NotBlank()
Expand Down Expand Up @@ -129,4 +135,11 @@ public function eraseCredentials()
return;
}

/**
* @param Address $address
*/
public function setAddress($address)
{
$this->address = $address;
}
}
2 changes: 1 addition & 1 deletion src/AppBundle/FormType/RegistrationFormType.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace AppBundle\FormType;

use AppBundle\Entity\User;
use AppBundle\Entity\Address;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
Expand Down
59 changes: 59 additions & 0 deletions src/AppBundle/FormType/UserEditFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
namespace AppBundle\FormType;

use AppBundle\Entity\Address;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Vašek Boch <vasek.boch@live.com>
* @author Jan Klat <jenik@klatys.cz>
*/
class UserEditFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add("firstName", EmailType::class, [
Copy link
Member

Choose a reason for hiding this comment

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

Rozházená indentace proti zbytku kódu

Copy link
Author

Choose a reason for hiding this comment

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

Ach jo, přitom jsem používal PHPStorm. Zkusím se polepšit.

"label" => "Jméno",
"attr" => [
"class" => "form-control",
],
])
->add("surName", EmailType::class, [
"label" => "Příjmení",
"attr" => [
"class" => "form-control",
],
])
->add("street", EmailType::class, [
"label" => "Ulice",
"attr" => [
"class" => "form-control",
],
])
->add("city", EmailType::class, [
"label" => "Město",
"attr" => [
"class" => "form-control",
],
])
->add("zip", EmailType::class, [
"label" => "PSČ",
"attr" => [
"class" => "form-control",
],
]);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
"data_class" => Address::class,
));
}
}
Loading