diff --git a/src/Admin/AlbumAdmin.php b/src/Admin/AlbumAdmin.php index 89f2284f..ce4b9062 100644 --- a/src/Admin/AlbumAdmin.php +++ b/src/Admin/AlbumAdmin.php @@ -5,6 +5,7 @@ namespace App\Admin; use App\Entity\Album; +use Sulu\Bundle\ActivityBundle\Infrastructure\Sulu\Admin\View\ActivityViewBuilderFactoryInterface; use Sulu\Bundle\AdminBundle\Admin\Admin; use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItem; use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItemCollection; @@ -23,13 +24,16 @@ class AlbumAdmin extends Admin const EDIT_FORM_DETAILS_VIEW = 'app.album.edit_form.details'; private ViewBuilderFactoryInterface $viewBuilderFactory; + private ActivityViewBuilderFactoryInterface $activityViewBuilderFactory; private SecurityCheckerInterface $securityChecker; public function __construct( ViewBuilderFactoryInterface $viewBuilderFactory, + ActivityViewBuilderFactoryInterface $activityViewBuilderFactory, SecurityCheckerInterface $securityChecker ) { $this->viewBuilderFactory = $viewBuilderFactory; + $this->activityViewBuilderFactory = $activityViewBuilderFactory; $this->securityChecker = $securityChecker; } @@ -102,6 +106,7 @@ public function configureViews(ViewCollection $viewCollection): void $this->viewBuilderFactory->createResourceTabViewBuilder(static::EDIT_FORM_VIEW, '/albums/:id') ->setResourceKey(Album::RESOURCE_KEY) ->setBackView(static::LIST_VIEW) + ->setTitleProperty('title') ); $viewCollection->add( @@ -112,6 +117,18 @@ public function configureViews(ViewCollection $viewCollection): void ->addToolbarActions($formToolbarActions) ->setParent(static::EDIT_FORM_VIEW) ); + + if ($this->activityViewBuilderFactory->hasActivityListPermission()) { + $viewCollection->add( + $this->activityViewBuilderFactory + ->createActivityListViewBuilder( + static::EDIT_FORM_VIEW . '.activity', + '/activity', + Album::RESOURCE_KEY + ) + ->setParent(static::EDIT_FORM_VIEW) + ); + } } } diff --git a/src/Controller/Admin/AlbumController.php b/src/Controller/Admin/AlbumController.php index 0910d794..4c609de4 100644 --- a/src/Controller/Admin/AlbumController.php +++ b/src/Controller/Admin/AlbumController.php @@ -5,11 +5,15 @@ namespace App\Controller\Admin; use App\Common\DoctrineListRepresentationFactory; +use App\Domain\Event\AlbumCreatedEvent; +use App\Domain\Event\AlbumModifiedEvent; +use App\Domain\Event\AlbumRemovedEvent; use App\Entity\Album; use Doctrine\ORM\EntityManagerInterface; use FOS\RestBundle\View\ViewHandlerInterface; use HandcraftedInTheAlps\RestRoutingBundle\Controller\Annotations\RouteResource; use HandcraftedInTheAlps\RestRoutingBundle\Routing\ClassResourceInterface; +use Sulu\Bundle\ActivityBundle\Application\Collector\DomainEventCollectorInterface; use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface; use Sulu\Component\Rest\AbstractRestController; use Sulu\Component\Security\SecuredControllerInterface; @@ -26,17 +30,20 @@ class AlbumController extends AbstractRestController implements ClassResourceInt private DoctrineListRepresentationFactory $doctrineListRepresentationFactory; private EntityManagerInterface $entityManager; private MediaManagerInterface $mediaManager; + private DomainEventCollectorInterface $domainEventCollector; public function __construct( DoctrineListRepresentationFactory $doctrineListRepresentationFactory, EntityManagerInterface $entityManager, MediaManagerInterface $mediaManager, + DomainEventCollectorInterface $domainEventCollector, ViewHandlerInterface $viewHandler, ?TokenStorageInterface $tokenStorage = null ) { $this->doctrineListRepresentationFactory = $doctrineListRepresentationFactory; $this->entityManager = $entityManager; $this->mediaManager = $mediaManager; + $this->domainEventCollector = $domainEventCollector; parent::__construct($viewHandler, $tokenStorage); } @@ -62,12 +69,19 @@ public function getAction(int $id): Response public function putAction(Request $request, int $id): Response { + /** @var Album|null $album */ $album = $this->entityManager->getRepository(Album::class)->find($id); if (!$album) { throw new NotFoundHttpException(); } - $this->mapDataToEntity($request->request->all(), $album); + $data = $request->request->all(); + $this->mapDataToEntity($data, $album); + + $this->domainEventCollector->collect( + new AlbumModifiedEvent($album, $data) + ); + $this->entityManager->flush(); return $this->handleView($this->view($album)); @@ -77,8 +91,14 @@ public function postAction(Request $request): Response { $album = new Album(); - $this->mapDataToEntity($request->request->all(), $album); + $data = $request->request->all(); + $this->mapDataToEntity($data, $album); $this->entityManager->persist($album); + + $this->domainEventCollector->collect( + new AlbumCreatedEvent($album, $data) + ); + $this->entityManager->flush(); return $this->handleView($this->view($album, 201)); @@ -88,7 +108,14 @@ public function deleteAction(int $id): Response { /** @var Album $album */ $album = $this->entityManager->getReference(Album::class, $id); + $albumTitle = $album->getTitle(); + $this->entityManager->remove($album); + + $this->domainEventCollector->collect( + new AlbumRemovedEvent($id, $albumTitle) + ); + $this->entityManager->flush(); return $this->handleView($this->view(null, 204)); diff --git a/src/Domain/Event/AlbumCreatedEvent.php b/src/Domain/Event/AlbumCreatedEvent.php new file mode 100644 index 00000000..43ab78bc --- /dev/null +++ b/src/Domain/Event/AlbumCreatedEvent.php @@ -0,0 +1,64 @@ +album = $album; + $this->payload = $payload; + } + + public function getAlbum(): Album + { + return $this->album; + } + + public function getEventPayload(): ?array + { + return $this->payload; + } + + public function getEventType(): string + { + return 'created'; + } + + public function getResourceKey(): string + { + return Album::RESOURCE_KEY; + } + + public function getResourceId(): string + { + return (string) $this->album->getId(); + } + + public function getResourceTitle(): ?string + { + return $this->album->getTitle(); + } + + public function getResourceSecurityContext(): ?string + { + return Album::SECURITY_CONTEXT; + } +} diff --git a/src/Domain/Event/AlbumModifiedEvent.php b/src/Domain/Event/AlbumModifiedEvent.php new file mode 100644 index 00000000..897afdd6 --- /dev/null +++ b/src/Domain/Event/AlbumModifiedEvent.php @@ -0,0 +1,64 @@ +album = $album; + $this->payload = $payload; + } + + public function getAlbum(): Album + { + return $this->album; + } + + public function getEventPayload(): ?array + { + return $this->payload; + } + + public function getEventType(): string + { + return 'modified'; + } + + public function getResourceKey(): string + { + return Album::RESOURCE_KEY; + } + + public function getResourceId(): string + { + return (string) $this->album->getId(); + } + + public function getResourceTitle(): ?string + { + return $this->album->getTitle(); + } + + public function getResourceSecurityContext(): ?string + { + return Album::SECURITY_CONTEXT; + } +} diff --git a/src/Domain/Event/AlbumRemovedEvent.php b/src/Domain/Event/AlbumRemovedEvent.php new file mode 100644 index 00000000..3ef937c9 --- /dev/null +++ b/src/Domain/Event/AlbumRemovedEvent.php @@ -0,0 +1,47 @@ +id = $id; + $this->title = $title; + } + + public function getEventType(): string + { + return 'removed'; + } + + public function getResourceKey(): string + { + return Album::RESOURCE_KEY; + } + + public function getResourceId(): string + { + return (string) $this->id; + } + + public function getResourceTitle(): ?string + { + return $this->title; + } + + public function getResourceSecurityContext(): ?string + { + return Album::SECURITY_CONTEXT; + } +} diff --git a/translations/admin.de.json b/translations/admin.de.json index 4565f68a..a9ada90a 100644 --- a/translations/admin.de.json +++ b/translations/admin.de.json @@ -10,5 +10,8 @@ "app.album_selection_label": "{count} {count, plural, =1 {Album} other {Alben}} ausgewählt", "app.select_albums": "Alben auswählen", "app.no_album_selected": "Kein Album ausgewählt", - "app.select_album": "Album auswählen" + "app.select_album": "Album auswählen", + "sulu_activity.description.albums.created": "{userFullName} hat das Album \"{resourceTitle}\" erstellt", + "sulu_activity.description.albums.modified": "{userFullName} hat das Album \"{resourceTitle}\" geändert", + "sulu_activity.description.albums.removed": "{userFullName} hat das Album \"{resourceTitle}\" gelöscht" } diff --git a/translations/admin.en.json b/translations/admin.en.json index 2160e113..4a29a08d 100644 --- a/translations/admin.en.json +++ b/translations/admin.en.json @@ -10,5 +10,8 @@ "app.album_selection_label": "{count} {count, plural, =1 {album} other {albums}} selected", "app.select_albums": "Select albums", "app.no_album_selected": "No album selected", - "app.select_album": "Select album" + "app.select_album": "Select album", + "sulu_activity.description.albums.created": "{userFullName} has created the album \"{resourceTitle}\"", + "sulu_activity.description.albums.modified": "{userFullName} has changed the album \"{resourceTitle}\"", + "sulu_activity.description.albums.removed": "{userFullName} has removed the album \"{resourceTitle}\"" }