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

[Example] Add trash functionality to custom entity #98

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
16 changes: 12 additions & 4 deletions src/Controller/Admin/AlbumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use HandcraftedInTheAlps\RestRoutingBundle\Controller\Annotations\RouteResource;
use HandcraftedInTheAlps\RestRoutingBundle\Routing\ClassResourceInterface;
use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface;
use Sulu\Bundle\TrashBundle\Application\TrashManager\TrashManagerInterface;
use Sulu\Component\Rest\AbstractRestController;
use Sulu\Component\Security\SecuredControllerInterface;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -26,17 +27,20 @@ class AlbumController extends AbstractRestController implements ClassResourceInt
private DoctrineListRepresentationFactory $doctrineListRepresentationFactory;
private EntityManagerInterface $entityManager;
private MediaManagerInterface $mediaManager;
private TrashManagerInterface $trashManager;

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

parent::__construct($viewHandler, $tokenStorage);
}
Expand Down Expand Up @@ -86,10 +90,14 @@ public function postAction(Request $request): Response

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

if ($album) {
$this->trashManager->store(Album::RESOURCE_KEY, $album);

$this->entityManager->remove($album);
$this->entityManager->flush();
}

return $this->handleView($this->view(null, 204));
}
Expand Down
121 changes: 121 additions & 0 deletions src/Trash/AlbumTrashItemHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace App\Trash;

use App\Admin\AlbumAdmin;
use App\Entity\Album;
use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\MediaBundle\Entity\MediaInterface;
use Sulu\Bundle\TrashBundle\Application\DoctrineRestoreHelper\DoctrineRestoreHelperInterface;
use Sulu\Bundle\TrashBundle\Application\RestoreConfigurationProvider\RestoreConfiguration;
use Sulu\Bundle\TrashBundle\Application\RestoreConfigurationProvider\RestoreConfigurationProviderInterface;
use Sulu\Bundle\TrashBundle\Application\TrashItemHandler\RestoreTrashItemHandlerInterface;
use Sulu\Bundle\TrashBundle\Application\TrashItemHandler\StoreTrashItemHandlerInterface;
use Sulu\Bundle\TrashBundle\Domain\Model\TrashItemInterface;
use Sulu\Bundle\TrashBundle\Domain\Repository\TrashItemRepositoryInterface;
use Sulu\Component\Security\Authentication\UserInterface;

final class AlbumTrashItemHandler implements
StoreTrashItemHandlerInterface,
RestoreTrashItemHandlerInterface,
RestoreConfigurationProviderInterface
{
private TrashItemRepositoryInterface $trashItemRepository;
private EntityManagerInterface $entityManager;
private DoctrineRestoreHelperInterface $doctrineRestoreHelper;

public function __construct(
TrashItemRepositoryInterface $trashItemRepository,
EntityManagerInterface $entityManager,
DoctrineRestoreHelperInterface $doctrineRestoreHelper
) {
$this->trashItemRepository = $trashItemRepository;
$this->doctrineRestoreHelper = $doctrineRestoreHelper;
$this->entityManager = $entityManager;
}

/**
* @param Album $album
*/
public function store(object $album, array $options = []): TrashItemInterface
{
$image = $album->getImage();
$creator = $album->getCreator();

$data = [
'title' => $album->getTitle(),
'tracklist' => $album->getTracklist(),
'imageId' => $image ? $image->getId() : null,
'created' => $album->getCreated()->format('c'),
'creatorId' => $creator ? $creator->getId() : null,
];

return $this->trashItemRepository->create(
Album::RESOURCE_KEY,
(string) $album->getId(),
$album->getTitle(),
$data,
null,
$options,
Album::SECURITY_CONTEXT,
null,
null
);
}

public function restore(TrashItemInterface $trashItem, array $restoreFormData = []): object
{
/**
* @var array{
* title: string,
* imageId: int|null,
* created: string,
* creatorId: int|null,
* tracklist: array<array{
* type: string,
* title: string|null,
* interpreter: string|null,
* }>,
* } $data
*/
$data = $trashItem->getRestoreData();

$album = new Album();
$album->setTitle($data['title']);
$album->setTracklist($data['tracklist']);
$album->setCreated(new \DateTime($data['created']));

if ($data['imageId']) {
$album->setImage($this->entityManager->find(MediaInterface::class, $data['imageId']));
}

if ($data['creatorId']) {
$album->setCreator($this->entityManager->find(UserInterface::class, $data['creatorId']));
}

$this->doctrineRestoreHelper->persistAndFlushWithId($album, (int) $trashItem->getResourceId());

return $album;
}

public function getConfiguration(): RestoreConfiguration
{
return new RestoreConfiguration(null, AlbumAdmin::EDIT_FORM_VIEW, ['id' => 'id']);
}

public static function getResourceKey(): string
{
return Album::RESOURCE_KEY;
}
}
4 changes: 4 additions & 0 deletions tests/Functional/Controller/Admin/AlbumControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Entity\Album;
use App\Tests\Functional\Traits\CreateAlbumTrait;
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
use Sulu\Bundle\TrashBundle\Domain\Model\TrashItemInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -266,6 +267,8 @@ public function testDelete(): void

$albumId = $album->getId();
$this->assertNotNull($albumId);
$this->assertNotNull($this->getEntityManager()->getRepository(Album::class)->findOneBy(['id' => $albumId]));
$this->assertCount(0, $this->getEntityManager()->getRepository(TrashItemInterface::class)->findAll());

$this->client->request('DELETE', '/admin/api/albums/' . $albumId);

Expand All @@ -274,5 +277,6 @@ public function testDelete(): void
$this->assertHttpStatusCode(204, $response);

$this->assertNull($this->getEntityManager()->getRepository(Album::class)->findOneBy(['id' => $albumId]));
$this->assertCount(1, $this->getEntityManager()->getRepository(TrashItemInterface::class)->findAll());
}
}
3 changes: 2 additions & 1 deletion translations/admin.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"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.resource.albums": "Album"
}
3 changes: 2 additions & 1 deletion translations/admin.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"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.resource.albums": "Album"
}