Skip to content

Commit

Permalink
Implement custom event entity
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasnatter committed Feb 26, 2021
1 parent 0a255fb commit 69b1e17
Show file tree
Hide file tree
Showing 9 changed files with 452 additions and 2 deletions.
37 changes: 37 additions & 0 deletions config/forms/event_details.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" ?>
<form xmlns="http://schemas.sulu.io/template/template"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.sulu.io/template/template http://schemas.sulu.io/template/form-1.0.xsd"
>
<key>event_details</key>

<properties>
<property name="name" type="text_line" mandatory="true" colspan="12">
<meta>
<title>sulu_admin.name</title>
</meta>

<params>
<param name="headline" value="true"/>
</params>
</property>

<property name="image" type="single_media_selection" colspan="12">
<meta>
<title>app.image</title>
</meta>
</property>

<property name="startDate" type="date" colspan="6">
<meta>
<title>app.start_date</title>
</meta>
</property>

<property name="endDate" type="date" colspan="6">
<meta>
<title>app.end_date</title>
</meta>
</property>
</properties>
</form>
30 changes: 30 additions & 0 deletions config/lists/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" ?>
<list xmlns="http://schemas.sulu.io/list-builder/list">
<key>events</key>

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

<property name="name" visibility="always" searchability="yes" translation="sulu_admin.name">
<field-name>name</field-name>
<entity-name>App\Entity\Event</entity-name>
</property>

<property name="startDate" visibility="yes" translation="app.start_date" type="date">
<field-name>startDate</field-name>
<entity-name>App\Entity\Event</entity-name>

<filter type="datetime" />
</property>

<property name="endDate" visibility="yes" translation="app.end_date" type="date">
<field-name>endDate</field-name>
<entity-name>App\Entity\Event</entity-name>

<filter type="datetime" />
</property>
</properties>
</list>
6 changes: 6 additions & 0 deletions config/packages/app_event_admin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sulu_admin:
resources:
events:
routes:
list: 'app.get_events'
detail: 'app.get_event'
6 changes: 6 additions & 0 deletions config/routes_admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ app.album_api:
prefix: /admin/api
resource: App\Controller\Admin\AlbumController
name_prefix: app.

app.event_api:
type: rest
prefix: /admin/api
resource: App\Controller\Admin\EventController
name_prefix: app.
133 changes: 133 additions & 0 deletions src/Admin/EventAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

declare(strict_types=1);

namespace App\Admin;

use App\Entity\Event;
use Sulu\Bundle\AdminBundle\Admin\Admin;
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItem;
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItemCollection;
use Sulu\Bundle\AdminBundle\Admin\View\ToolbarAction;
use Sulu\Bundle\AdminBundle\Admin\View\ViewBuilderFactoryInterface;
use Sulu\Bundle\AdminBundle\Admin\View\ViewCollection;
use Sulu\Component\Security\Authorization\PermissionTypes;
use Sulu\Component\Security\Authorization\SecurityCheckerInterface;

class EventAdmin extends Admin
{
const LIST_VIEW = 'app.event.list';
const ADD_FORM_VIEW = 'app.event.add_form';
const ADD_FORM_DETAILS_VIEW = 'app.event.add_form.details';
const EDIT_FORM_VIEW = 'app.event.edit_form';
const EDIT_FORM_DETAILS_VIEW = 'app.event.edit_form.details';

private ViewBuilderFactoryInterface $viewBuilderFactory;
private SecurityCheckerInterface $securityChecker;

public function __construct(
ViewBuilderFactoryInterface $viewBuilderFactory,
SecurityCheckerInterface $securityChecker
) {
$this->viewBuilderFactory = $viewBuilderFactory;
$this->securityChecker = $securityChecker;
}

public function configureNavigationItems(NavigationItemCollection $navigationItemCollection): void
{
if ($this->securityChecker->hasPermission(Event::SECURITY_CONTEXT, PermissionTypes::EDIT)) {
$rootNavigationItem = new NavigationItem('app.events');
$rootNavigationItem->setIcon('su-calendar');
$rootNavigationItem->setPosition(30);
$rootNavigationItem->setView(static::LIST_VIEW);

$navigationItemCollection->add($rootNavigationItem);
}
}

public function configureViews(ViewCollection $viewCollection): void
{
$formToolbarActions = [];
$listToolbarActions = [];

if ($this->securityChecker->hasPermission(Event::SECURITY_CONTEXT, PermissionTypes::ADD)) {
$listToolbarActions[] = new ToolbarAction('sulu_admin.add');
}

if ($this->securityChecker->hasPermission(Event::SECURITY_CONTEXT, PermissionTypes::EDIT)) {
$formToolbarActions[] = new ToolbarAction('sulu_admin.save');
}

if ($this->securityChecker->hasPermission(Event::SECURITY_CONTEXT, PermissionTypes::DELETE)) {
$formToolbarActions[] = new ToolbarAction('sulu_admin.delete');
$listToolbarActions[] = new ToolbarAction('sulu_admin.delete');
}

if ($this->securityChecker->hasPermission(Event::SECURITY_CONTEXT, PermissionTypes::VIEW)) {
$listToolbarActions[] = new ToolbarAction('sulu_admin.export');
}

if ($this->securityChecker->hasPermission(Event::SECURITY_CONTEXT, PermissionTypes::EDIT)) {
$viewCollection->add(
$this->viewBuilderFactory->createListViewBuilder(static::LIST_VIEW, '/events')
->setResourceKey(Event::RESOURCE_KEY)
->setListKey(Event::LIST_KEY)
->setTitle('app.events')
->addListAdapters(['table'])
->setAddView(static::ADD_FORM_VIEW)
->setEditView(static::EDIT_FORM_VIEW)
->addToolbarActions($listToolbarActions)
);

$viewCollection->add(
$this->viewBuilderFactory->createResourceTabViewBuilder(static::ADD_FORM_VIEW, '/events/add')
->setResourceKey(Event::RESOURCE_KEY)
->setBackView(static::LIST_VIEW)
);

$viewCollection->add(
$this->viewBuilderFactory->createFormViewBuilder(static::ADD_FORM_DETAILS_VIEW, '/details')
->setResourceKey(Event::RESOURCE_KEY)
->setFormKey(Event::FORM_KEY)
->setTabTitle('sulu_admin.details')
->setEditView(static::EDIT_FORM_VIEW)
->addToolbarActions($formToolbarActions)
->setParent(static::ADD_FORM_VIEW)
);

$viewCollection->add(
$this->viewBuilderFactory->createResourceTabViewBuilder(static::EDIT_FORM_VIEW, '/events/:id')
->setResourceKey(Event::RESOURCE_KEY)
->setBackView(static::LIST_VIEW)
);

$viewCollection->add(
$this->viewBuilderFactory->createFormViewBuilder(static::EDIT_FORM_DETAILS_VIEW, '/details')
->setResourceKey(Event::RESOURCE_KEY)
->setFormKey(Event::FORM_KEY)
->setTabTitle('sulu_admin.details')
->addToolbarActions($formToolbarActions)
->setParent(static::EDIT_FORM_VIEW)
);
}
}

/**
* @return mixed[]
*/
public function getSecurityContexts(): array
{
return [
self::SULU_ADMIN_SECURITY_SYSTEM => [
'Events' => [
Event::SECURITY_CONTEXT => [
PermissionTypes::VIEW,
PermissionTypes::ADD,
PermissionTypes::EDIT,
PermissionTypes::DELETE,
],
],
],
];
}
}
114 changes: 114 additions & 0 deletions src/Controller/Admin/EventController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Common\DoctrineListRepresentationFactory;
use App\Entity\Event;
use Doctrine\ORM\EntityManagerInterface;
use FOS\RestBundle\View\ViewHandlerInterface;
use HandcraftedInTheAlps\RestRoutingBundle\Controller\Annotations\RouteResource;
use HandcraftedInTheAlps\RestRoutingBundle\Routing\ClassResourceInterface;
use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface;
use Sulu\Component\Rest\AbstractRestController;
use Sulu\Component\Security\SecuredControllerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

/**
* @RouteResource("event")
*/
class EventController extends AbstractRestController implements ClassResourceInterface, SecuredControllerInterface
{
private DoctrineListRepresentationFactory $doctrineListRepresentationFactory;
private EntityManagerInterface $entityManager;
private MediaManagerInterface $mediaManager;

public function __construct(
DoctrineListRepresentationFactory $doctrineListRepresentationFactory,
EntityManagerInterface $entityManager,
MediaManagerInterface $mediaManager,
ViewHandlerInterface $viewHandler,
?TokenStorageInterface $tokenStorage = null
) {
$this->doctrineListRepresentationFactory = $doctrineListRepresentationFactory;
$this->entityManager = $entityManager;
$this->mediaManager = $mediaManager;

parent::__construct($viewHandler, $tokenStorage);
}

public function cgetAction(): Response
{
$listRepresentation = $this->doctrineListRepresentationFactory->createDoctrineListRepresentation(
Event::RESOURCE_KEY
);

return $this->handleView($this->view($listRepresentation));
}

public function getAction(int $id): Response
{
$event = $this->entityManager->getRepository(Event::class)->find($id);
if (!$event) {
throw new NotFoundHttpException();
}

return $this->handleView($this->view($event));
}

public function putAction(Request $request, int $id): Response
{
$event = $this->entityManager->getRepository(Event::class)->find($id);
if (!$event) {
throw new NotFoundHttpException();
}

$this->mapDataToEntity($request->request->all(), $event);
$this->entityManager->flush();

return $this->handleView($this->view($event));
}

public function postAction(Request $request): Response
{
$event = new Event();

$this->mapDataToEntity($request->request->all(), $event);
$this->entityManager->persist($event);
$this->entityManager->flush();

return $this->handleView($this->view($event, 201));
}

public function deleteAction(int $id): Response
{
/** @var Event $event */
$event = $this->entityManager->getReference(Event::class, $id);
$this->entityManager->remove($event);
$this->entityManager->flush();

return $this->handleView($this->view(null, 204));
}

/**
* @param array<string, mixed> $data
*/
protected function mapDataToEntity(array $data, Event $entity): void
{
$imageId = $data['image']['id'] ?? null;

$entity->setName($data['name']);
$entity->setImage($imageId ? $this->mediaManager->getEntityById($imageId) : null);
$entity->setStartDate($data['startDate'] ? new \DateTimeImmutable($data['startDate']) : null);
$entity->setEndDate($data['endDate'] ? new \DateTimeImmutable($data['endDate']) : null);
}

public function getSecurityContext(): string
{
return Event::SECURITY_CONTEXT;
}
}
Loading

0 comments on commit 69b1e17

Please sign in to comment.