Skip to content

Úkol 3 - Vojtěch Havlíček #30

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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_profile") }}" 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
18 changes: 18 additions & 0 deletions app/Resources/views/user/addaddress.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends 'base.html.twig' %}

{% block body %}

<div class="row">
<div class="col-sm-6 col-lg-6 col-md-6">
{{ form_start(form) }}
{{ form_row(form.street) }}
{{ form_row(form.city) }}
{{ form_row(form.postcode) }}
{{ form_row(form.country) }}
<br/>
<button class="btn btn-lg btn-primary" type="submit">Vytvořit novou adresu</button>
{{ form_end(form) }}
</div>
</div>

{% endblock %}
10 changes: 10 additions & 0 deletions app/Resources/views/user/changepassword.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends 'base.html.twig' %}

{% block body %}
{{ form_start(form) }}
{{ form_row(form.plainPassword.first) }}
{{ form_row(form.plainPassword.second) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Změnit heslo</button>
{{ form_end(form) }}
{% endblock %}
26 changes: 26 additions & 0 deletions app/Resources/views/user/profile.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends 'base.html.twig' %}

{% block body %}
<div class="row">
<div class="col-sm-6 col-lg-6 col-md-6">
<h3>Osobní údaje</h3>
<p><a href="{{ path("user_changepassword") }}">Změnit heslo</a></p>
{{ form_start(form) }}
{{ form_row(form.username) }}
{{ form_row(form.name) }}
{{ form_row(form.phone) }}
<br/>
<button class="btn btn-lg btn-primary btn-block" type="submit">Uložit</button>
{{ form_end(form) }}
</div>
<div class="col-sm-6 col-lg-6 col-md-6">
<h3>Adresy</h3>
{% for address in addresses %}
<div class="address">
<p>{{ address.street }}, {{ address.postcode }} {{ address.city }}, {{ address.country }}</p>
</div>
{% endfor %}
<p><a href="{{ path("add_address") }}">Správa adres</a></p>
</div>
</div>
{% endblock %}
6 changes: 5 additions & 1 deletion app/config/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ security:
# The route name the user can go to in order to logout
path: user_logout
# The name of the route to redirect to after logging out
target: homepage
target: homepage

access_control:
- { path: /prihlasit, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /uzivatel/*, roles: IS_AUTHENTICATED_FULLY }
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ahoj codecampe
162 changes: 127 additions & 35 deletions src/AppBundle/Controller/UserController.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\User;
use AppBundle\Entity\Address;
use AppBundle\Facade\UserFacade;
use AppBundle\FormType\RegistrationFormType;
use AppBundle\FormType\ProfileFormType;
use AppBundle\FormType\AddressFormType;
use AppBundle\FormType\ChangePasswordFormType;
use Doctrine\ORM\EntityManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
Expand Down Expand Up @@ -41,43 +45,74 @@ 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("/uzivatel/zmenit-heslo", name="user_changepassword")
* @Template("user/changepassword.html.twig")
*
* @param Request $request
* @return RedirectResponse|array
*/
public function changepasswordAction(Request $request)
{
$user = $this->userFacade->getUser();
$form = $this->formFactory->create(ChangePasswordFormType::class, $user);

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

$user->setPassword(
$this->passwordEncoder->encodePassword($user->getPlainPassword(), null)
);

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

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

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

/**
* @Route("/prihlasit", name="user_login")
* @Template("user/login.html.twig")
*
Expand Down Expand Up @@ -113,4 +148,61 @@ public function superSecretAction()
];
}

/**
Copy link
Collaborator

Choose a reason for hiding this comment

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

Taky udělal bych na to separátní Controller.

* @Route("/uzivatel/profil", name="user_profile")
* @Template("user/profile.html.twig")
*
* @param Request $request
* @return RedirectResponse|array
*/
public function profileAction(Request $request)
{
$user = $this->userFacade->getUser();
$form = $this->formFactory->create(ProfileFormType::class, $user);
$addresses = $user->getAddresses();

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

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

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

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

/**
* @Route("/uzivatel/pridat-adresu", name="add_address")
* @Template("user/addaddress.html.twig")
*
* @param Request $request
* @return RedirectResponse|array
*/
public function addAddressAction(Request $request)
{
$user = $this->userFacade->getUser();
$address = new Address();
$form = $this->formFactory->create(AddressFormType::class, $address);

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

$address->setUser($user);

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

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

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