Skip to content

Commit

Permalink
Add route to event entity
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasnatter committed Nov 27, 2020
1 parent 9f4a7f0 commit 15ca3f0
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 1 deletion.
8 changes: 8 additions & 0 deletions config/forms/event_details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
<params>
<param name="headline" value="true"/>
</params>

<tag name="sulu.rlp.part"/>
</property>

<property name="route" type="route">
<meta>
<title>sulu_admin.url</title>
</meta>
</property>

<property name="startDate" type="date" colspan="6">
Expand Down
8 changes: 8 additions & 0 deletions config/packages/app_event_admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ sulu_admin:
routes:
list: 'app.get_events'
detail: 'app.get_event'

sulu_route:
mappings:
App\Entity\Event:
generator: schema
options:
route_schema: /events/{object["name"]}
resource_key: events
4 changes: 4 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
Sulu\Bundle\SnippetBundle\Snippet\DefaultSnippetManagerInterface: '@sulu_snippet.default_snippet.manager'
Sulu\Component\PHPCR\PathCleanupInterface: '@sulu.content.path_cleaner'
Doctrine\ORM\EntityManagerInterface: '@doctrine.orm.entity_manager'
Sulu\Bundle\RouteBundle\Entity\RouteRepositoryInterface: '@sulu.repository.route'

# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
Expand Down Expand Up @@ -47,3 +48,6 @@ services:

App\Content\Type\AlbumSelection:
tags: [{name: 'sulu.content.type', alias: 'album_selection'}]

App\Routing\EventRouteDefaultsProvider:
tags: [{ name: 'sulu_route.defaults_provider' }]
49 changes: 48 additions & 1 deletion src/Controller/Admin/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
use FOS\RestBundle\View\ViewHandlerInterface;
use HandcraftedInTheAlps\RestRoutingBundle\Controller\Annotations\RouteResource;
use HandcraftedInTheAlps\RestRoutingBundle\Routing\ClassResourceInterface;
use Sulu\Bundle\RouteBundle\Entity\RouteRepositoryInterface;
use Sulu\Bundle\RouteBundle\Manager\RouteManagerInterface;
use Sulu\Component\Rest\AbstractRestController;
use Sulu\Component\Security\SecuredControllerInterface;
use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand All @@ -24,15 +27,24 @@ class EventController extends AbstractRestController implements ClassResourceInt
{
private DoctrineListRepresentationFactory $doctrineListRepresentationFactory;
private EntityManagerInterface $entityManager;
private WebspaceManagerInterface $webspaceManager;
private RouteManagerInterface $routeManager;
private RouteRepositoryInterface $routeRepository;

public function __construct(
DoctrineListRepresentationFactory $doctrineListRepresentationFactory,
EntityManagerInterface $entityManager,
WebspaceManagerInterface $webspaceManager,
RouteManagerInterface $routeManager,
RouteRepositoryInterface $routeRepository,
ViewHandlerInterface $viewHandler,
?TokenStorageInterface $tokenStorage = null
) {
$this->doctrineListRepresentationFactory = $doctrineListRepresentationFactory;
$this->entityManager = $entityManager;
$this->webspaceManager = $webspaceManager;
$this->routeManager = $routeManager;
$this->routeRepository = $routeRepository;

parent::__construct($viewHandler, $tokenStorage);
}
Expand Down Expand Up @@ -64,6 +76,7 @@ public function putAction(Request $request, int $id): Response
}

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

return $this->handleView($this->view($event));
Expand All @@ -77,13 +90,17 @@ public function postAction(Request $request): Response
$this->entityManager->persist($event);
$this->entityManager->flush();

$this->updateRoutesForEntity($event);
$this->entityManager->flush();

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

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

Expand All @@ -96,10 +113,40 @@ public function deleteAction(int $id): Response
protected function mapDataToEntity(array $data, Event $entity): void
{
$entity->setName($data['name']);
$entity->setRoute($data['route']);
$entity->setStartDate($data['startDate'] ? new \DateTimeImmutable($data['startDate']) : null);
$entity->setEndDate($data['endDate'] ? new \DateTimeImmutable($data['endDate']) : null);
}

protected function updateRoutesForEntity(Event $entity): void
{
// create route for all locales of the application because event entity is not localized
foreach ($this->webspaceManager->getAllLocales() as $locale) {
$this->routeManager->createOrUpdateByAttributes(
Event::class,
(string) $entity->getId(),
$locale,
$entity->getRoute(),
);
}
}

protected function removeRoutesForEntity(Event $entity): void
{
// remove route for all locales of the application because event entity is not localized
foreach ($this->webspaceManager->getAllLocales() as $locale) {
$routes = $this->routeRepository->findAllByEntity(
Event::class,
(string) $entity->getId(),
$locale
);

foreach ($routes as $route) {
$this->routeRepository->remove($route);
}
}
}

public function getSecurityContext(): string
{
return Event::SECURITY_CONTEXT;
Expand Down
17 changes: 17 additions & 0 deletions src/Controller/Website/EventController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\Controller\Website;

use App\Entity\Event;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class EventController extends AbstractController
{
public function indexAction(Event $event): Response
{
return $this->render('events/event.html.twig', ['event' => $event]);
}
}
17 changes: 17 additions & 0 deletions src/Entity/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class Event
*/
private string $name;

/**
* @ORM\Column(type="string", length=255)
*
* @Serializer\Expose()
*/
private string $route;

/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*
Expand Down Expand Up @@ -62,6 +69,16 @@ public function setName(string $name): void
$this->name = $name;
}

public function getRoute(): string
{
return $this->route ?? '';
}

public function setRoute(string $route): void
{
$this->route = $route;
}

public function getStartDate(): ?\DateTimeImmutable
{
return $this->startDate;
Expand Down
40 changes: 40 additions & 0 deletions src/Routing/EventRouteDefaultsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Routing;

use App\Controller\Website\EventController;
use App\Entity\Event;
use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\RouteBundle\Routing\Defaults\RouteDefaultsProviderInterface;

class EventRouteDefaultsProvider implements RouteDefaultsProviderInterface
{
private EntityManagerInterface $entityManager;

public function __construct(
EntityManagerInterface $entityManager
) {
$this->entityManager = $entityManager;
}

/**
* @return mixed[]
*/
public function getByEntity($entityClass, $id, $locale, $object = null)
{
return [
'_controller' => EventController::class . '::indexAction',
'event' => $object ?: $this->entityManager->getRepository(Event::class)->find($id),
];
}

public function isPublished($entityClass, $id, $locale)
{
return true;
}

public function supports($entityClass)
{
return Event::class === $entityClass;
}
}
1 change: 1 addition & 0 deletions templates/events/event.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>{{ event.name }}</h1>

0 comments on commit 15ca3f0

Please sign in to comment.