From 66ba5d333c57bddf24a4c82f4f7cc7402d688fdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Such=C3=A1nek?= Date: Mon, 24 Oct 2016 01:42:46 +0200 Subject: [PATCH 1/7] Add insert address form --- .DS_Store | Bin 0 -> 6148 bytes app/Resources/views/base.html.twig | 2 +- app/Resources/views/user/user_edit.html.twig | 14 ++ src/AppBundle/Controller/UserController.php | 102 ++++++++---- src/AppBundle/Entity/Address.php | 151 ++++++++++++++++++ src/AppBundle/Entity/User.php | 13 ++ .../FormType/RegistrationFormType.php | 2 +- src/AppBundle/FormType/UserEditFormType.php | 59 +++++++ .../Repository/AddressRepository.php | 10 ++ 9 files changed, 317 insertions(+), 36 deletions(-) create mode 100644 .DS_Store create mode 100644 app/Resources/views/user/user_edit.html.twig create mode 100644 src/AppBundle/Entity/Address.php create mode 100644 src/AppBundle/FormType/UserEditFormType.php create mode 100644 src/AppBundle/Repository/AddressRepository.php diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1463066f885325975b35cd29895e1fcfde6475a8 GIT binary patch literal 6148 zcmeHKPfP4D5KngZX;&{56$CH23VU$XCwi-^9)*R9A}ae@THHWs%eLzx%RN%Kod8parJbb~r$He<{JMJ!dJ_&~57bwo!Cp|a! z@^04fI)|zzgU~m2)63gKdpAj{VC3CipOb*Xl0s0RV1>*{huPI=tqZnfGD4qjV0)Bx8pkuJm R2p$mn5l}QxK@5B=126VPVv7I( literal 0 HcmV?d00001 diff --git a/app/Resources/views/base.html.twig b/app/Resources/views/base.html.twig index 7e93622..3a492f2 100644 --- a/app/Resources/views/base.html.twig +++ b/app/Resources/views/base.html.twig @@ -49,7 +49,7 @@

Adresa uživatele

+ {{ form_start(form) }} + {{ form_row(form.firstName) }} + {{ form_row(form.surName) }} + {{ form_row(form.street) }} + {{ form_row(form.city) }} + {{ form_row(form.zip) }} +
+ + {{ form_end(form) }} +{% endblock %} \ No newline at end of file diff --git a/src/AppBundle/Controller/UserController.php b/src/AppBundle/Controller/UserController.php index d0958e3..eeb3b0a 100644 --- a/src/AppBundle/Controller/UserController.php +++ b/src/AppBundle/Controller/UserController.php @@ -1,8 +1,10 @@ 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) + { + 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(), + ]; + + } + } \ No newline at end of file diff --git a/src/AppBundle/Entity/Address.php b/src/AppBundle/Entity/Address.php new file mode 100644 index 0000000..eed7e39 --- /dev/null +++ b/src/AppBundle/Entity/Address.php @@ -0,0 +1,151 @@ + + * @author Jan Klat + * + * @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") + * @Assert\NotBlank() + */ + private $firstName; + + /** + * @ORM\Column(type="string", length=255, unique=false, name="surName") + * @Assert\NotBlank() + */ + private $surName; + + /** + * @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; + } +} diff --git a/src/AppBundle/Entity/User.php b/src/AppBundle/Entity/User.php index 5dd2213..1d563a8 100644 --- a/src/AppBundle/Entity/User.php +++ b/src/AppBundle/Entity/User.php @@ -23,6 +23,12 @@ class User implements UserInterface */ private $id; + /** + * @var Address + * @ORM\ManyToOne(targetEntity="Address") + */ + 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; + } } diff --git a/src/AppBundle/FormType/RegistrationFormType.php b/src/AppBundle/FormType/RegistrationFormType.php index 0e65ec6..7f46cbd 100644 --- a/src/AppBundle/FormType/RegistrationFormType.php +++ b/src/AppBundle/FormType/RegistrationFormType.php @@ -1,7 +1,7 @@ + * @author Jan Klat + */ +class UserEditFormType extends AbstractType +{ + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add("firstName", EmailType::class, [ + "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, + )); + } +} \ No newline at end of file diff --git a/src/AppBundle/Repository/AddressRepository.php b/src/AppBundle/Repository/AddressRepository.php new file mode 100644 index 0000000..3913c44 --- /dev/null +++ b/src/AppBundle/Repository/AddressRepository.php @@ -0,0 +1,10 @@ + Date: Tue, 25 Oct 2016 16:29:19 +0200 Subject: [PATCH 2/7] Repar tabs on UserController.php --- src/AppBundle/Controller/UserController.php | 132 ++++++++++---------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/src/AppBundle/Controller/UserController.php b/src/AppBundle/Controller/UserController.php index eeb3b0a..59d2da2 100644 --- a/src/AppBundle/Controller/UserController.php +++ b/src/AppBundle/Controller/UserController.php @@ -43,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")); - } - - return [ - "form" => $form->createView(), - ]; - } + /** + * @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")); + } + + return [ + "form" => $form->createView(), + ]; + } /** * @Route("/prihlasit", name="user_login") @@ -115,36 +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) - { - 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(), - ]; - - } + /** + * @Route("/uzivatel/edit", name="user_edit") + * @Template("user/user_edit.html.twig") + * + * @param Request $request + * @return RedirectResponse|array + */ + public function editAction(Request $request) + { + 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(), + ]; + + } } \ No newline at end of file From 29d2c29fa8e656c136fe264ce7eaf502862c94eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Such=C3=A1nek?= Date: Tue, 25 Oct 2016 16:33:40 +0200 Subject: [PATCH 3/7] Rename form and template for User Address --- .../user/{user_edit.html.twig => add_address.html.twig} | 4 ++-- src/AppBundle/Controller/UserController.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) rename app/Resources/views/user/{user_edit.html.twig => add_address.html.twig} (82%) diff --git a/app/Resources/views/user/user_edit.html.twig b/app/Resources/views/user/add_address.html.twig similarity index 82% rename from app/Resources/views/user/user_edit.html.twig rename to app/Resources/views/user/add_address.html.twig index 659988a..19df7ad 100644 --- a/app/Resources/views/user/user_edit.html.twig +++ b/app/Resources/views/user/add_address.html.twig @@ -1,7 +1,7 @@ {% extends 'base.html.twig' %} {% block body %} -

Adresa uživatele

+

Přidat adresu uživatele

{{ form_start(form) }} {{ form_row(form.firstName) }} {{ form_row(form.surName) }} @@ -9,6 +9,6 @@ {{ form_row(form.city) }} {{ form_row(form.zip) }}
- + {{ form_end(form) }} {% endblock %} \ No newline at end of file diff --git a/src/AppBundle/Controller/UserController.php b/src/AppBundle/Controller/UserController.php index 59d2da2..2124e03 100644 --- a/src/AppBundle/Controller/UserController.php +++ b/src/AppBundle/Controller/UserController.php @@ -116,13 +116,13 @@ public function superSecretAction() } /** - * @Route("/uzivatel/edit", name="user_edit") - * @Template("user/user_edit.html.twig") + * @Route("/uzivatel/pridat-adresu", name="add_address") + * @Template("user/add_address.html.twig") * * @param Request $request * @return RedirectResponse|array */ - public function editAction(Request $request) + public function addAddressAction(Request $request) { if (!$this->userFacade->getUser()) { throw new UnauthorizedHttpException("Přihlašte se"); From d69d8fd9f9c5348b0f1b692b313426e276204356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Such=C3=A1nek?= Date: Tue, 25 Oct 2016 17:25:04 +0200 Subject: [PATCH 4/7] Rename params for Address first & lastName --- app/Resources/views/user/add_address.html.twig | 2 +- src/AppBundle/Entity/Address.php | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/Resources/views/user/add_address.html.twig b/app/Resources/views/user/add_address.html.twig index 19df7ad..0ebefa7 100644 --- a/app/Resources/views/user/add_address.html.twig +++ b/app/Resources/views/user/add_address.html.twig @@ -4,7 +4,7 @@

Přidat adresu uživatele

{{ form_start(form) }} {{ form_row(form.firstName) }} - {{ form_row(form.surName) }} + {{ form_row(form.lastName) }} {{ form_row(form.street) }} {{ form_row(form.city) }} {{ form_row(form.zip) }} diff --git a/src/AppBundle/Entity/Address.php b/src/AppBundle/Entity/Address.php index eed7e39..fc62bae 100644 --- a/src/AppBundle/Entity/Address.php +++ b/src/AppBundle/Entity/Address.php @@ -30,10 +30,10 @@ class Address private $firstName; /** - * @ORM\Column(type="string", length=255, unique=false, name="surName") + * @ORM\Column(type="string", length=255, unique=false, name="lastName") * @Assert\NotBlank() */ - private $surName; + private $lastName; /** * @ORM\Column(type="string", length=255, unique=false, name="street") @@ -80,9 +80,9 @@ public function getCity() /** * @return mixed */ - public function getSurName() + public function getLastName() { - return $this->surName; + return $this->lastName; } /** @@ -126,11 +126,11 @@ public function setStreet($street) } /** - * @param mixed $surName + * @param mixed $lastName */ - public function setSurName($surName) + public function setLastName($lastName) { - $this->surName = $surName; + $this->lastName = $lastName; } /** From c4b4cc46af3093a9bc1a90b88771b7008cc80ecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Such=C3=A1nek?= Date: Tue, 25 Oct 2016 17:29:02 +0200 Subject: [PATCH 5/7] Change space to tabs & replace ManyToOne to OneToMany on User Entity --- src/AppBundle/Entity/User.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/AppBundle/Entity/User.php b/src/AppBundle/Entity/User.php index 1d563a8..c3168ab 100644 --- a/src/AppBundle/Entity/User.php +++ b/src/AppBundle/Entity/User.php @@ -23,11 +23,11 @@ class User implements UserInterface */ private $id; - /** - * @var Address - * @ORM\ManyToOne(targetEntity="Address") - */ - private $address; + /** + * @var Address + * @ORM\OneToMany(targetEntity="Address") + */ + private $address; /** * @ORM\Column(type="string", length=255, unique=true, name="email") @@ -135,11 +135,11 @@ public function eraseCredentials() return; } - /** - * @param Address $address - */ - public function setAddress($address) - { - $this->address = $address; - } + /** + * @param Address $address + */ + public function setAddress($address) + { + $this->address = $address; + } } From cb3680cdd91dbdccc9179310e4013a7680aed073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Such=C3=A1nek?= Date: Tue, 25 Oct 2016 17:31:44 +0200 Subject: [PATCH 6/7] Change space to tabs on UserEditFormType --- src/AppBundle/FormType/UserEditFormType.php | 48 ++++++++++----------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/AppBundle/FormType/UserEditFormType.php b/src/AppBundle/FormType/UserEditFormType.php index 19996c2..61c64c7 100644 --- a/src/AppBundle/FormType/UserEditFormType.php +++ b/src/AppBundle/FormType/UserEditFormType.php @@ -24,30 +24,30 @@ public function buildForm(FormBuilderInterface $builder, array $options) "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", - ], - ]); + ->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) From 01f78cec5168d4eea8f45018cd1a07010dd1350f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Such=C3=A1nek?= Date: Tue, 25 Oct 2016 17:36:30 +0200 Subject: [PATCH 7/7] Change from Address Entity to User Entity on RegistrationFormType --- src/AppBundle/FormType/RegistrationFormType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AppBundle/FormType/RegistrationFormType.php b/src/AppBundle/FormType/RegistrationFormType.php index 7f46cbd..0e65ec6 100644 --- a/src/AppBundle/FormType/RegistrationFormType.php +++ b/src/AppBundle/FormType/RegistrationFormType.php @@ -1,7 +1,7 @@