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 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
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/add_address.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends 'base.html.twig' %}

{% block body %}
<h1>Přidat adresu uživatele</h1>
{{ form_start(form) }}
{{ form_row(form.firstName) }}
{{ form_row(form.lastName) }}
{{ form_row(form.street) }}
{{ form_row(form.city) }}
{{ form_row(form.zip) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Přidat</button>
{{ form_end(form) }}
{% endblock %}
34 changes: 34 additions & 0 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 @@ -113,4 +115,36 @@ public function superSecretAction()
];
}

/**
* @Route("/uzivatel/pridat-adresu", name="add_address")
* @Template("user/add_address.html.twig")
*
* @param Request $request
* @return RedirectResponse|array
*/
public function addAddressAction(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(),
];

}

}
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="lastName")
* @Assert\NotBlank()
*/
private $lastName;

/**
* @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 getLastName()
{
return $this->lastName;
}

/**
* @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 $lastName
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
}

/**
* @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\OneToMany(targetEntity="Address")
*/
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;
}
}
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,
));
}
}
10 changes: 10 additions & 0 deletions src/AppBundle/Repository/AddressRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace AppBundle\Repository;

use Doctrine\ORM\EntityRepository;

class AddressRepository extends EntityRepository
{

}