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

07 - Display the registrations for each event in the admin interface #12

Open
wants to merge 1 commit into
base: assignment/06
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
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

final public const EVENT_LIST_VIEW = 'app.events_list';

final public const EVENT_REGISTRATION_LIST_KEY = 'event_registrations';

final public const EVENT_ADD_FORM_VIEW = 'app.event_add_form';

final public const EVENT_EDIT_FORM_VIEW = 'app.event_edit_form';
Expand Down Expand Up @@ -107,5 +110,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);
}
}
29 changes: 29 additions & 0 deletions src/Controller/Admin/EventRegistrationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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
{
public function __construct(private readonly DoctrineListRepresentationFactory $doctrineListRepresentationFactory)
{
}

#[Route(path: '/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' => (string) $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 @@
#[ORM\Entity(repositoryClass: EventRegistrationRepository::class)]
class EventRegistration
{
final public const RESOURCE_KEY = 'event_registrations';

#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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;

private KernelBrowser $client;

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

public function testGetList(): 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->jsonRequest('GET', '/admin/api/events/' . $event1->getId() . '/registrations');

$response = $this->client->getResponse();
$this->assertInstanceOf(Response::class, $response);
/**
* @var array{
* _embedded: array{
* event_registrations: array<array{
* id: int,
* firstName: string,
* lastName: string,
* email: string,
* }>
* },
* total: int,
* } $result
*/
$result = \json_decode($response->getContent() ?: '', true, 512, \JSON_THROW_ON_ERROR);
$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