Skip to content

Commit

Permalink
07 - Display the registrations for each event in the admin interface
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes authored and niklasnatter committed Jun 20, 2022
1 parent 0f4da74 commit 906becf
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 0 deletions.
31 changes: 31 additions & 0 deletions config/lists/event_registrations.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" ?>
<list xmlns="http://schemas.sulu.io/list-builder/list">
<key>event_registrations</key>

<properties>
<property name="id" visibility="no" translation="sulu_admin.id">
<field-name>id</field-name>
<entity-name>App\Entity\EventRegistration</entity-name>
</property>

<property name="firstName" visibility="always" searchability="yes" translation="sulu_contact.first_name">
<field-name>firstName</field-name>
<entity-name>App\Entity\EventRegistration</entity-name>
</property>

<property name="lastName" visibility="always" searchability="yes" translation="sulu_contact.last_name">
<field-name>lastName</field-name>
<entity-name>App\Entity\EventRegistration</entity-name>
</property>

<property name="email" visibility="always" searchability="yes" translation="sulu_contact.email">
<field-name>email</field-name>
<entity-name>App\Entity\EventRegistration</entity-name>
</property>

<identity-property name="eventId" visibility="never">
<field-name>event</field-name>
<entity-name>App\Entity\EventRegistration</entity-name>
</identity-property>
</properties>
</list>
4 changes: 4 additions & 0 deletions config/packages/sulu_admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ sulu_admin:
list: app.get_event_list
detail: app.get_event

event_registrations:
routes:
list: app.get_event_registration_list

# Registering Selection Field Types in this section
field_type_options:
selection:
Expand Down
14 changes: 14 additions & 0 deletions src/Admin/EventAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Admin;

use App\Entity\Event;
use App\Entity\EventRegistration;
use Sulu\Bundle\AdminBundle\Admin\Admin;
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItem;
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItemCollection;
Expand All @@ -22,6 +23,8 @@ class EventAdmin extends Admin

const EVENT_LIST_VIEW = 'app.events_list';

const EVENT_REGISTRATION_LIST_KEY = 'event_registrations';

const EVENT_ADD_FORM_VIEW = 'app.event_add_form';

const EVENT_EDIT_FORM_VIEW = 'app.event_edit_form';
Expand Down Expand Up @@ -119,5 +122,16 @@ public function configureViews(ViewCollection $viewCollection): void
->addToolbarActions($formToolbarActions)
->setParent(static::EVENT_EDIT_FORM_VIEW);
$viewCollection->add($editDetailsFormView);

$editDetailsFormView = $this->viewBuilderFactory->createListViewBuilder(static::EVENT_EDIT_FORM_VIEW . '.registrations', '/registrations')
->setResourceKey(EventRegistration::RESOURCE_KEY)
->setListKey(self::EVENT_REGISTRATION_LIST_KEY)
->setTabTitle('app.registrations')
->addRouterAttributesToListRequest(['id' => 'eventId'])
->addListAdapters(['table'])
->addToolbarActions([])
->setUserSettingsKey(EventRegistration::RESOURCE_KEY)
->setParent(static::EVENT_EDIT_FORM_VIEW);
$viewCollection->add($editDetailsFormView);
}
}
38 changes: 38 additions & 0 deletions src/Controller/Admin/EventRegistrationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Common\DoctrineListRepresentationFactory;
use App\Entity\EventRegistration;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class EventRegistrationController extends AbstractController
{
/**
* @var DoctrineListRepresentationFactory
*/
private $doctrineListRepresentationFactory;

public function __construct(
DoctrineListRepresentationFactory $doctrineListRepresentationFactory
) {
$this->doctrineListRepresentationFactory = $doctrineListRepresentationFactory;
}

/**
* @Route("/admin/api/events/{eventId}/registrations", methods={"GET"}, name="app.get_event_registration_list")
*/
public function getListAction(int $eventId): Response
{
$listRepresentation = $this->doctrineListRepresentationFactory->createDoctrineListRepresentation(
EventRegistration::RESOURCE_KEY,
['eventId' => $eventId]
);

return $this->json($listRepresentation->toArray());
}
}
2 changes: 2 additions & 0 deletions src/Entity/EventRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
class EventRegistration
{
const RESOURCE_KEY = 'event_registrations';

/**
* @var int|null
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace App\Tests\Functional\Controller\Admin;

use App\Tests\Functional\Traits\EventRegistrationTrait;
use App\Tests\Functional\Traits\EventTrait;
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\HttpFoundation\Response;

class EventRegistrationControllerTest extends SuluTestCase
{
use EventRegistrationTrait;
use EventTrait;

/**
* @var KernelBrowser
*/
private $client;

protected function setUp(): void
{
$this->client = $this->createAuthenticatedClient();
$this->purgeDatabase();
}

public function testCGet(): void
{
$event1 = $this->createEvent('Sulu is awesome', 'de');
$event2 = $this->createEvent('Symfony live is awesome', 'de');

$registration1 = $this->createEventRegistration($event1, 'Max', 'Mustermann');
$registration2 = $this->createEventRegistration($event2, 'Mira', 'Musterfrau');

$this->client->request('GET', '/admin/api/events/' . $event1->getId() . '/registrations');

$response = $this->client->getResponse();
$this->assertInstanceOf(Response::class, $response);
$result = json_decode($response->getContent() ?: '', true);
$this->assertHttpStatusCode(200, $response);

$this->assertSame(1, $result['total']);
$this->assertCount(1, $result['_embedded']['event_registrations']);
$items = $result['_embedded']['event_registrations'];

$this->assertSame($registration1->getId(), $items[0]['id']);

$this->assertSame($registration1->getFirstName(), $items[0]['firstName']);
$this->assertSame($registration1->getLastName(), $items[0]['lastName']);
$this->assertArrayHasKey('email', $items[0]);
}
}
14 changes: 14 additions & 0 deletions tests/Functional/Traits/EventRegistrationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@

trait EventRegistrationTrait
{
public function createEventRegistration(Event $event, string $firstName, string $lastName): EventRegistration
{
$event = $this->getEventRegistrationRepository()->create($event);
$event->setFirstName($firstName);
$event->setLastName($lastName);
$event->setEmail($firstName . '@' . $lastName . '.at');
$event->setMessage('');

static::getEntityManager()->persist($event);
static::getEntityManager()->flush();

return $event;
}

/**
* @return EventRegistration[]
*/
Expand Down
1 change: 1 addition & 0 deletions translations/admin.de.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"app.events": "Veranstaltungen",
"app.image": "Bild",
"app.registrations": "Registrierungen",
"app.teaser": "Kurzbeschreibung",
"app.start_date": "Start",
"app.end_date": "Ende",
Expand Down
1 change: 1 addition & 0 deletions translations/admin.en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"app.events": "Events",
"app.image": "Image",
"app.registrations": "Registrations",
"app.teaser": "Teaser",
"app.start_date": "Start",
"app.end_date": "Ende",
Expand Down

0 comments on commit 906becf

Please sign in to comment.