Skip to content

Commit 58315b4

Browse files
wachterjohannesniklasnatter
authored andcommitted
06 - Add an event registration to the website
1 parent ed28bc2 commit 58315b4

File tree

8 files changed

+360
-0
lines changed

8 files changed

+360
-0
lines changed

src/Controller/Website/EventWebsiteController.php

+31
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
namespace App\Controller\Website;
66

7+
use App\Form\EventRegistrationType;
8+
use App\Repository\EventRegistrationRepository;
79
use App\Repository\EventRepository;
810
use Sulu\Bundle\WebsiteBundle\Resolver\TemplateAttributeResolverInterface;
911
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12+
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
1013
use Symfony\Component\HttpFoundation\Request;
1114
use Symfony\Component\HttpFoundation\Response;
1215
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -20,11 +23,38 @@ public function indexAction(int $id, Request $request): Response
2023
throw new NotFoundHttpException();
2124
}
2225

26+
$eventRegistrationRepository = $this->get(EventRegistrationRepository::class);
27+
$registration = $eventRegistrationRepository->create($event);
28+
$form = $this->createForm(EventRegistrationType::class, $registration);
29+
$form->add(
30+
'submit',
31+
SubmitType::class,
32+
[
33+
'label' => 'Create',
34+
]
35+
);
36+
37+
$form->handleRequest($request);
38+
39+
if ($form->isSubmitted() && $form->isValid()) {
40+
$eventRegistrationRepository->save($registration);
41+
42+
return $this->redirectToRoute(
43+
'app.event',
44+
[
45+
'id' => $event->getId(),
46+
'success' => true,
47+
]
48+
);
49+
}
50+
2351
return $this->render(
2452
'events/index.html.twig',
2553
$this->get(TemplateAttributeResolverInterface::class)->resolve(
2654
[
2755
'event' => $event,
56+
'success' => $request->query->get('success'),
57+
'form' => $form->createView(),
2858
'content' => ['title' => $event->getTitle()],
2959
]
3060
)
@@ -40,6 +70,7 @@ public static function getSubscribedServices(): array
4070
parent::getSubscribedServices(),
4171
[
4272
EventRepository::class,
73+
EventRegistrationRepository::class,
4374
TemplateAttributeResolverInterface::class,
4475
]
4576
);

src/Entity/EventRegistration.php

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Entity;
6+
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Symfony\Component\Validator\Constraints as Assert;
9+
10+
/**
11+
* @ORM\Entity(repositoryClass="App\Repository\EventRegistrationRepository")
12+
*/
13+
class EventRegistration
14+
{
15+
/**
16+
* @var int|null
17+
*
18+
* @ORM\Id()
19+
* @ORM\GeneratedValue()
20+
* @ORM\Column(type="integer")
21+
*/
22+
private $id;
23+
24+
/**
25+
* @var Event
26+
*
27+
* @ORM\ManyToOne(targetEntity="App\Entity\Event")
28+
* @ORM\JoinColumn(onDelete="CASCADE", nullable=false)
29+
*/
30+
private $event;
31+
32+
/**
33+
* @var string
34+
*
35+
* @ORM\Column(type="string", length=255)
36+
*
37+
* @Assert\NotBlank
38+
*/
39+
private $firstName;
40+
41+
/**
42+
* @var string
43+
*
44+
* @ORM\Column(type="string", length=255)
45+
*
46+
* @Assert\NotBlank
47+
*/
48+
private $lastName;
49+
50+
/**
51+
* @var string
52+
*
53+
* @ORM\Column(type="string", length=255)
54+
*
55+
* @Assert\NotBlank
56+
* @Assert\Email()
57+
*/
58+
private $email;
59+
60+
/**
61+
* @var string
62+
*
63+
* @ORM\Column(type="text", length=255)
64+
*/
65+
private $message;
66+
67+
public function __construct(Event $event)
68+
{
69+
$this->event = $event;
70+
}
71+
72+
public function getId(): ?int
73+
{
74+
return $this->id;
75+
}
76+
77+
public function getEvent(): Event
78+
{
79+
return $this->event;
80+
}
81+
82+
public function getFirstName(): ?string
83+
{
84+
return $this->firstName;
85+
}
86+
87+
public function setFirstName(string $firstName): self
88+
{
89+
$this->firstName = $firstName;
90+
91+
return $this;
92+
}
93+
94+
public function getLastName(): ?string
95+
{
96+
return $this->lastName;
97+
}
98+
99+
public function setLastName(string $lastName): self
100+
{
101+
$this->lastName = $lastName;
102+
103+
return $this;
104+
}
105+
106+
public function getEmail(): ?string
107+
{
108+
return $this->email;
109+
}
110+
111+
public function setEmail(string $email): self
112+
{
113+
$this->email = $email;
114+
115+
return $this;
116+
}
117+
118+
public function getMessage(): ?string
119+
{
120+
return $this->message;
121+
}
122+
123+
public function setMessage(string $message): self
124+
{
125+
$this->message = $message;
126+
127+
return $this;
128+
}
129+
}

src/Form/EventRegistrationType.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Form;
6+
7+
use App\Entity\EventRegistration;
8+
use Symfony\Component\Form\AbstractType;
9+
use Symfony\Component\Form\FormBuilderInterface;
10+
use Symfony\Component\OptionsResolver\OptionsResolver;
11+
12+
class EventRegistrationType extends AbstractType
13+
{
14+
public function buildForm(FormBuilderInterface $builder, array $options): void
15+
{
16+
$builder
17+
->add('firstName')
18+
->add('lastName')
19+
->add('email')
20+
->add('message');
21+
}
22+
23+
public function configureOptions(OptionsResolver $resolver): void
24+
{
25+
$resolver->setDefaults(
26+
[
27+
'data_class' => EventRegistration::class,
28+
]
29+
);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Repository;
6+
7+
use App\Entity\Event;
8+
use App\Entity\EventRegistration;
9+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
10+
use Doctrine\Common\Persistence\ManagerRegistry;
11+
12+
/**
13+
* @method EventRegistration|null find($id, $lockMode = null, $lockVersion = null)
14+
* @method EventRegistration|null findOneBy(array $criteria, array $orderBy = null)
15+
* @method EventRegistration[] findAll()
16+
* @method EventRegistration[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
17+
*
18+
* @extends ServiceEntityRepository<EventRegistration>
19+
*/
20+
class EventRegistrationRepository extends ServiceEntityRepository
21+
{
22+
public function __construct(ManagerRegistry $registry)
23+
{
24+
parent::__construct($registry, EventRegistration::class);
25+
}
26+
27+
public function create(Event $event): EventRegistration
28+
{
29+
$registration = new EventRegistration($event);
30+
31+
return $registration;
32+
}
33+
34+
public function save(EventRegistration $registration): void
35+
{
36+
$this->getEntityManager()->persist($registration);
37+
$this->getEntityManager()->flush();
38+
}
39+
}

templates/events/index.html.twig

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{% extends "base.html.twig" %}
22

3+
{% form_theme form 'bootstrap_4_layout.html.twig' %}
4+
35
{% block content %}
46
<section class="jumbotron text-center">
57
<div class="container">
@@ -11,4 +13,16 @@
1113
<div class="container">
1214
{{ event.description|raw }}
1315
</div>
16+
17+
<div class="container">
18+
{% if success %}
19+
<div class="success">
20+
<b>Thanks for your registration.</b>
21+
</div>
22+
{% else %}
23+
<h2>Register for this event:</h2>
24+
25+
{{ form(form) }}
26+
{% endif %}
27+
</div>
1428
{% endblock %}

tests/Functional/Controller/Website/EventWebsiteControllerTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Tests\Functional\Controller\Website;
66

7+
use App\Tests\Functional\Traits\EventRegistrationTrait;
78
use App\Tests\Functional\Traits\EventTrait;
89
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
910
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
@@ -12,6 +13,7 @@
1213
class EventWebsiteControllerTest extends SuluTestCase
1314
{
1415
use EventTrait;
16+
use EventRegistrationTrait;
1517

1618
/**
1719
* @var KernelBrowser
@@ -37,4 +39,37 @@ public function testIndexAction(): void
3739

3840
$this->assertStringContainsString('Sulu is awesome', $crawler->filter('h1')->html());
3941
}
42+
43+
public function testRegister(): void
44+
{
45+
$event = $this->createEvent('Sulu is awesome', 'en');
46+
47+
$crawler = $this->client->request('GET', '/en/event/' . $event->getId());
48+
49+
$response = $this->client->getResponse();
50+
$this->assertInstanceOf(Response::class, $response);
51+
$this->assertHttpStatusCode(200, $response);
52+
53+
$form = $crawler->filter('#event_registration_submit')->form(
54+
[
55+
'event_registration[firstName]' => 'Max',
56+
'event_registration[lastName]' => 'Mustermann',
57+
'event_registration[email]' => '[email protected]',
58+
'event_registration[message]' => 'I would love to see this.',
59+
]
60+
);
61+
62+
$this->client->submit($form);
63+
$crawler = $this->client->followRedirect();
64+
65+
$this->assertStringContainsString('Thanks for your registration', $crawler->filter('.success')->html());
66+
67+
$registrations = $this->findEventRegistrations($event);
68+
69+
$this->assertCount(1, $registrations);
70+
$this->assertSame('Max', $registrations[0]->getFirstName());
71+
$this->assertSame('Mustermann', $registrations[0]->getLastName());
72+
$this->assertSame('[email protected]', $registrations[0]->getEmail());
73+
$this->assertSame('I would love to see this.', $registrations[0]->getMessage());
74+
}
4075
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Functional\Traits;
6+
7+
use App\Entity\Event;
8+
use App\Entity\EventRegistration;
9+
use App\Repository\EventRegistrationRepository;
10+
use Doctrine\ORM\EntityManagerInterface;
11+
12+
trait EventRegistrationTrait
13+
{
14+
/**
15+
* @return EventRegistration[]
16+
*/
17+
public function findEventRegistrations(Event $event): array
18+
{
19+
return $this->getEventRegistrationRepository()->findBy(['event' => $event]);
20+
}
21+
22+
protected function getEventRegistrationRepository(): EventRegistrationRepository
23+
{
24+
return $this->getEntityManager()->getRepository(EventRegistration::class);
25+
}
26+
27+
abstract protected function getEntityManager(): EntityManagerInterface;
28+
}

0 commit comments

Comments
 (0)