Skip to content

Commit 23c93cb

Browse files
wachterjohannesniklasnatter
authored andcommitted
07 - Display the registrations for each event in the admin interface
1 parent 0467910 commit 23c93cb

File tree

9 files changed

+159
-0
lines changed

9 files changed

+159
-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
const EVENT_LIST_VIEW = 'app.events_list';
2425

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

2730
const EVENT_EDIT_FORM_VIEW = 'app.event_edit_form';
@@ -119,5 +122,16 @@ public function configureViews(ViewCollection $viewCollection): void
119122
->addToolbarActions($formToolbarActions)
120123
->setParent(static::EVENT_EDIT_FORM_VIEW);
121124
$viewCollection->add($editDetailsFormView);
125+
126+
$editDetailsFormView = $this->viewBuilderFactory->createListViewBuilder(static::EVENT_EDIT_FORM_VIEW . '.registrations', '/registrations')
127+
->setResourceKey(EventRegistration::RESOURCE_KEY)
128+
->setListKey(self::EVENT_REGISTRATION_LIST_KEY)
129+
->setTabTitle('app.registrations')
130+
->addRouterAttributesToListRequest(['id' => 'eventId'])
131+
->addListAdapters(['table'])
132+
->addToolbarActions([])
133+
->setUserSettingsKey(EventRegistration::RESOURCE_KEY)
134+
->setParent(static::EVENT_EDIT_FORM_VIEW);
135+
$viewCollection->add($editDetailsFormView);
122136
}
123137
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
/**
16+
* @var DoctrineListRepresentationFactory
17+
*/
18+
private $doctrineListRepresentationFactory;
19+
20+
public function __construct(
21+
DoctrineListRepresentationFactory $doctrineListRepresentationFactory
22+
) {
23+
$this->doctrineListRepresentationFactory = $doctrineListRepresentationFactory;
24+
}
25+
26+
/**
27+
* @Route("/admin/api/events/{eventId}/registrations", methods={"GET"}, name="app.get_event_registration_list")
28+
*/
29+
public function getListAction(int $eventId): Response
30+
{
31+
$listRepresentation = $this->doctrineListRepresentationFactory->createDoctrineListRepresentation(
32+
EventRegistration::RESOURCE_KEY,
33+
['eventId' => $eventId]
34+
);
35+
36+
return $this->json($listRepresentation->toArray());
37+
}
38+
}

src/Entity/EventRegistration.php

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
class EventRegistration
1414
{
15+
const RESOURCE_KEY = 'event_registrations';
16+
1517
/**
1618
* @var int|null
1719
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
/**
19+
* @var KernelBrowser
20+
*/
21+
private $client;
22+
23+
protected function setUp(): void
24+
{
25+
$this->client = $this->createAuthenticatedClient();
26+
$this->purgeDatabase();
27+
}
28+
29+
public function testGetList(): void
30+
{
31+
$event1 = $this->createEvent('Sulu is awesome', 'de');
32+
$event2 = $this->createEvent('Symfony live is awesome', 'de');
33+
34+
$registration1 = $this->createEventRegistration($event1, 'Max', 'Mustermann');
35+
$registration2 = $this->createEventRegistration($event2, 'Mira', 'Musterfrau');
36+
37+
$this->client->jsonRequest('GET', '/admin/api/events/' . $event1->getId() . '/registrations');
38+
39+
$response = $this->client->getResponse();
40+
$this->assertInstanceOf(Response::class, $response);
41+
$result = json_decode($response->getContent() ?: '', true);
42+
$this->assertHttpStatusCode(200, $response);
43+
44+
$this->assertSame(1, $result['total']);
45+
$this->assertCount(1, $result['_embedded']['event_registrations']);
46+
$items = $result['_embedded']['event_registrations'];
47+
48+
$this->assertSame($registration1->getId(), $items[0]['id']);
49+
50+
$this->assertSame($registration1->getFirstName(), $items[0]['firstName']);
51+
$this->assertSame($registration1->getLastName(), $items[0]['lastName']);
52+
$this->assertArrayHasKey('email', $items[0]);
53+
}
54+
}

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)