-
Notifications
You must be signed in to change notification settings - Fork 13
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
66ba5d3
15389d9
29d2c29
d69d8fd
c4b4cc4
cb3680c
01f78ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 %} |
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; | ||
|
@@ -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) | ||
$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") | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tady spíš logická - vlastně pokaždé přidáváš novou adresu. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(), | ||
]; | ||
|
||
} | ||
|
||
} |
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tady jdeš proti zbytku projektu, sloupec v db se bude jmenovat |
||
* @Assert\NotBlank() | ||
*/ | ||
private $firstName; | ||
|
||
/** | ||
* @ORM\Column(type="string", length=255, unique=false, name="surName") | ||
* @Assert\NotBlank() | ||
*/ | ||
private $surName; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. když už tak There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,12 @@ class User implements UserInterface | |
*/ | ||
private $id; | ||
|
||
/** | ||
* @var Address | ||
* @ORM\ManyToOne(targetEntity="Address") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
@@ -129,4 +135,11 @@ public function eraseCredentials() | |
return; | ||
} | ||
|
||
/** | ||
* @param Address $address | ||
*/ | ||
public function setAddress($address) | ||
{ | ||
$this->address = $address; | ||
} | ||
} |
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, [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rozházená indentace proti zbytku kódu There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
)); | ||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oj ok, díky