Skip to content

Commit fc28698

Browse files
wachterjohannesniklasnatter
authored andcommitted
07 - Display the registrations for each event in the admin interface
1 parent 7784d3e commit fc28698

File tree

9 files changed

+160
-0
lines changed

9 files changed

+160
-0
lines changed

config/lists/event_registrations.xml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" ?>
2+
<list xmlns="http://schemas.sulu.io/list-builder/list">
3+
<key>event_registrations</key>
4+
5+
<properties>
6+
<property name="id" visibility="no" translation="sulu_admin.id">
7+
<field-name>id</field-name>
8+
<entity-name>App\Entity\EventRegistration</entity-name>
9+
</property>
10+
11+
<property name="firstName" visibility="always" searchability="yes" translation="sulu_contact.first_name">
12+
<field-name>firstName</field-name>
13+
<entity-name>App\Entity\EventRegistration</entity-name>
14+
</property>
15+
16+
<property name="lastName" visibility="always" searchability="yes" translation="sulu_contact.last_name">
17+
<field-name>lastName</field-name>
18+
<entity-name>App\Entity\EventRegistration</entity-name>
19+
</property>
20+
21+
<property name="email" visibility="always" searchability="yes" translation="sulu_contact.email">
22+
<field-name>email</field-name>
23+
<entity-name>App\Entity\EventRegistration</entity-name>
24+
</property>
25+
26+
<identity-property name="eventId" visibility="never">
27+
<field-name>event</field-name>
28+
<entity-name>App\Entity\EventRegistration</entity-name>
29+
</identity-property>
30+
</properties>
31+
</list>

config/packages/sulu_admin.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ sulu_admin:
1414
list: app.get_event_list
1515
detail: app.get_event
1616

17+
event_registrations:
18+
routes:
19+
list: app.get_event_registration_list
20+
1721
# Registering Selection Field Types in this section
1822
field_type_options:
1923
selection:

src/Admin/EventAdmin.php

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Admin;
66

77
use App\Entity\Event;
8+
use App\Entity\EventRegistration;
89
use Sulu\Bundle\AdminBundle\Admin\Admin;
910
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItem;
1011
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItemCollection;
@@ -22,6 +23,8 @@ class EventAdmin extends Admin
2223

2324
final public const EVENT_LIST_VIEW = 'app.events_list';
2425

26+
final public const EVENT_REGISTRATION_LIST_KEY = 'event_registrations';
27+
2528
final public const EVENT_ADD_FORM_VIEW = 'app.event_add_form';
2629

2730
final public const EVENT_EDIT_FORM_VIEW = 'app.event_edit_form';
@@ -107,5 +110,16 @@ public function configureViews(ViewCollection $viewCollection): void
107110
->addToolbarActions($formToolbarActions)
108111
->setParent(static::EVENT_EDIT_FORM_VIEW);
109112
$viewCollection->add($editDetailsFormView);
113+
114+
$editDetailsFormView = $this->viewBuilderFactory->createListViewBuilder(static::EVENT_EDIT_FORM_VIEW . '.registrations', '/registrations')
115+
->setResourceKey(EventRegistration::RESOURCE_KEY)
116+
->setListKey(self::EVENT_REGISTRATION_LIST_KEY)
117+
->setTabTitle('app.registrations')
118+
->addRouterAttributesToListRequest(['id' => 'eventId'])
119+
->addListAdapters(['table'])
120+
->addToolbarActions([])
121+
->setUserSettingsKey(EventRegistration::RESOURCE_KEY)
122+
->setParent(static::EVENT_EDIT_FORM_VIEW);
123+
$viewCollection->add($editDetailsFormView);
110124
}
111125
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Controller\Admin;
6+
7+
use App\Common\DoctrineListRepresentationFactory;
8+
use App\Entity\EventRegistration;
9+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10+
use Symfony\Component\HttpFoundation\Response;
11+
use Symfony\Component\Routing\Annotation\Route;
12+
13+
class EventRegistrationController extends AbstractController
14+
{
15+
public function __construct(private readonly DoctrineListRepresentationFactory $doctrineListRepresentationFactory)
16+
{
17+
}
18+
19+
#[Route(path: '/admin/api/events/{eventId}/registrations', methods: ['GET'], name: 'app.get_event_registration_list')]
20+
public function getListAction(int $eventId): Response
21+
{
22+
$listRepresentation = $this->doctrineListRepresentationFactory->createDoctrineListRepresentation(
23+
EventRegistration::RESOURCE_KEY,
24+
['eventId' => (string) $eventId],
25+
);
26+
27+
return $this->json($listRepresentation->toArray());
28+
}
29+
}

src/Entity/EventRegistration.php

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#[ORM\Entity(repositoryClass: EventRegistrationRepository::class)]
1313
class EventRegistration
1414
{
15+
final public const RESOURCE_KEY = 'event_registrations';
16+
1517
#[ORM\Id]
1618
#[ORM\GeneratedValue]
1719
#[ORM\Column(type: Types::INTEGER)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Functional\Controller\Admin;
6+
7+
use App\Tests\Functional\Traits\EventRegistrationTrait;
8+
use App\Tests\Functional\Traits\EventTrait;
9+
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
10+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
11+
use Symfony\Component\HttpFoundation\Response;
12+
13+
class EventRegistrationControllerTest extends SuluTestCase
14+
{
15+
use EventRegistrationTrait;
16+
use EventTrait;
17+
18+
private KernelBrowser $client;
19+
20+
protected function setUp(): void
21+
{
22+
$this->client = $this->createAuthenticatedClient();
23+
$this->purgeDatabase();
24+
}
25+
26+
public function testGetList(): void
27+
{
28+
$event1 = $this->createEvent('Sulu is awesome', 'de');
29+
$event2 = $this->createEvent('Symfony live is awesome', 'de');
30+
31+
$registration1 = $this->createEventRegistration($event1, 'Max', 'Mustermann');
32+
$registration2 = $this->createEventRegistration($event2, 'Mira', 'Musterfrau');
33+
34+
$this->client->jsonRequest('GET', '/admin/api/events/' . $event1->getId() . '/registrations');
35+
36+
$response = $this->client->getResponse();
37+
$this->assertInstanceOf(Response::class, $response);
38+
/**
39+
* @var array{
40+
* _embedded: array{
41+
* event_registrations: array<array{
42+
* id: int,
43+
* firstName: string,
44+
* lastName: string,
45+
* email: string,
46+
* }>
47+
* },
48+
* total: int,
49+
* } $result
50+
*/
51+
$result = \json_decode($response->getContent() ?: '', true, 512, \JSON_THROW_ON_ERROR);
52+
$this->assertHttpStatusCode(200, $response);
53+
54+
$this->assertSame(1, $result['total']);
55+
$this->assertCount(1, $result['_embedded']['event_registrations']);
56+
$items = $result['_embedded']['event_registrations'];
57+
58+
$this->assertSame($registration1->getId(), $items[0]['id']);
59+
60+
$this->assertSame($registration1->getFirstName(), $items[0]['firstName']);
61+
$this->assertSame($registration1->getLastName(), $items[0]['lastName']);
62+
$this->assertArrayHasKey('email', $items[0]);
63+
}
64+
}

tests/Functional/Traits/EventRegistrationTrait.php

+14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@
1111

1212
trait EventRegistrationTrait
1313
{
14+
public function createEventRegistration(Event $event, string $firstName, string $lastName): EventRegistration
15+
{
16+
$event = $this->getEventRegistrationRepository()->create($event);
17+
$event->setFirstName($firstName);
18+
$event->setLastName($lastName);
19+
$event->setEmail($firstName . '@' . $lastName . '.at');
20+
$event->setMessage('');
21+
22+
static::getEntityManager()->persist($event);
23+
static::getEntityManager()->flush();
24+
25+
return $event;
26+
}
27+
1428
/**
1529
* @return EventRegistration[]
1630
*/

translations/admin.de.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"app.events": "Veranstaltungen",
33
"app.image": "Bild",
4+
"app.registrations": "Registrierungen",
45
"app.teaser": "Kurzbeschreibung",
56
"app.start_date": "Start",
67
"app.end_date": "Ende",

translations/admin.en.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"app.events": "Events",
33
"app.image": "Image",
4+
"app.registrations": "Registrations",
45
"app.teaser": "Teaser",
56
"app.start_date": "Start",
67
"app.end_date": "Ende",

0 commit comments

Comments
 (0)