Skip to content

Commit

Permalink
Add trash functionality for forms (sulu#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasnatter authored Jul 13, 2022
1 parent 1ab810f commit ba31c3e
Show file tree
Hide file tree
Showing 14 changed files with 744 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ npm-debug.log

# php-cs-fixer
.php_cs.cache
php-cs-fixer
.php-cs-fixer.cache
4 changes: 4 additions & 0 deletions DependencyInjection/SuluFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('article.xml');
}

if (\array_key_exists('SuluTrashBundle', $bundles)) {
$loader->load('services_trash.xml');
}

if (\class_exists(\EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType::class)) {
$loader->load('type_recaptcha.xml');
}
Expand Down
92 changes: 92 additions & 0 deletions Domain/Event/FormRestoredEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/*
* 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 Sulu\Bundle\FormBundle\Domain\Event;

use Sulu\Bundle\ActivityBundle\Domain\Event\DomainEvent;
use Sulu\Bundle\FormBundle\Admin\FormAdmin;
use Sulu\Bundle\FormBundle\Entity\Form;
use Sulu\Bundle\FormBundle\Entity\FormTranslation;

class FormRestoredEvent extends DomainEvent
{
/**
* @var Form
*/
private $form;

/**
* @var mixed[]
*/
private $payload;

/**
* @param mixed[] $payload
*/
public function __construct(
Form $form,
array $payload
) {
parent::__construct();

$this->form = $form;
$this->payload = $payload;
}

public function getForm(): Form
{
return $this->form;
}

public function getEventType(): string
{
return 'restored';
}

public function getEventPayload(): ?array
{
return $this->payload;
}

public function getResourceKey(): string
{
return Form::RESOURCE_KEY;
}

public function getResourceId(): string
{
return (string) $this->form->getId();
}

public function getResourceTitle(): ?string
{
$translation = $this->getFormTranslation();

return $translation ? $translation->getTitle() : null;
}

public function getResourceTitleLocale(): ?string
{
$translation = $this->getFormTranslation();

return $translation ? $translation->getLocale() : null;
}

private function getFormTranslation(): ?FormTranslation
{
return $this->form->getTranslation($this->form->getDefaultLocale());
}

public function getResourceSecurityContext(): ?string
{
return FormAdmin::SECURITY_CONTEXT;
}
}
1 change: 1 addition & 0 deletions Entity/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function getTranslation(string $locale, bool $create = false, bool $fallb
$translation = new FormTranslation();
$translation->setLocale($locale);
$this->addTranslation($translation);
$translation->setForm($this);

return $translation;
}
Expand Down
14 changes: 13 additions & 1 deletion Manager/FormManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Sulu\Bundle\FormBundle\Entity\FormTranslationReceiver;
use Sulu\Bundle\FormBundle\Exception\FormNotFoundException;
use Sulu\Bundle\FormBundle\Repository\FormRepository;
use Sulu\Bundle\TrashBundle\Application\TrashManager\TrashManagerInterface;

class FormManager
{
Expand All @@ -43,17 +44,24 @@ class FormManager
*/
private $domainEventCollector;

/**
* @var TrashManagerInterface|null
*/
private $trashManager;

/**
* EventManager constructor.
*/
public function __construct(
EntityManagerInterface $entityManager,
FormRepository $formRepository,
DomainEventCollectorInterface $domainEventCollector
DomainEventCollectorInterface $domainEventCollector,
?TrashManagerInterface $trashManager
) {
$this->entityManager = $entityManager;
$this->formRepository = $formRepository;
$this->domainEventCollector = $domainEventCollector;
$this->trashManager = $trashManager;
}

public function findById(int $id, ?string $locale = null): ?Form
Expand Down Expand Up @@ -242,6 +250,10 @@ public function delete(int $id, ?string $locale = null): ?Form
return null;
}

if ($this->trashManager) {
$this->trashManager->store(Form::RESOURCE_KEY, $object);
}

/** @var FormTranslation $translation */
$translation = $object->getTranslation($locale, false, true);
$this->domainEventCollector->collect(
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="sulu_form.repository.form" />
<argument type="service" id="sulu_activity.domain_event_collector"/>
<argument type="service" id="sulu_trash.trash_manager" on-invalid="null"/>
</service>

<!-- Content Types -->
Expand Down
17 changes: 17 additions & 0 deletions Resources/config/services_trash.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="sulu_form.form_trash_item_handler"
class="Sulu\Bundle\FormBundle\Trash\FormTrashItemHandler">
<argument type="service" id="sulu_trash.trash_item_repository"/>
<argument type="service" id="doctrine.orm.entity_manager"/>
<argument type="service" id="sulu_trash.doctrine_restore_helper"/>
<argument type="service" id="sulu_activity.domain_event_collector"/>

<tag name="sulu_trash.store_trash_item_handler"/>
<tag name="sulu_trash.restore_trash_item_handler"/>
<tag name="sulu_trash.restore_configuration_provider"/>
</service>
</services>
</container>
4 changes: 3 additions & 1 deletion Resources/translations/admin.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@
"sulu_form.salutation_ms": "Frau",
"sulu_form.single_form_selection.no_form_selected": "Kein Formular ausgewählt",
"sulu_form.single_form_selection.overlay_title": "Wählen Sie ein Formular aus",
"sulu_activity.resource.forms": "Formular",
"sulu_activity.description.forms.created": "{userFullName} hat das Formular \"{resourceTitle}\" erstellt",
"sulu_activity.description.forms.modified": "{userFullName} hat das Formular \"{resourceTitle}\" geändert",
"sulu_activity.description.forms.translation_added": "{userFullName} hat eine Übersetzung für \"{resourceLocale}\" zum Forumlar \"{resourceTitle}\" hinzugefügt",
"sulu_activity.description.forms.removed": "{userFullName} hat das Formular \"{resourceTitle}\" gelöscht",
"sulu_activity.description.forms.copied": "{userFullName} hat das Formular \"{resourceTitle}\" kopiert"
"sulu_activity.description.forms.copied": "{userFullName} hat das Formular \"{resourceTitle}\" kopiert",
"sulu_activity.description.forms.restored": "{userFullName} hat das Formular \"{resourceTitle}\" wiederhergestellt"
}
4 changes: 3 additions & 1 deletion Resources/translations/admin.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@
"sulu_form.salutation_ms": "Ms.",
"sulu_form.single_form_selection.no_form_selected": "No form selected",
"sulu_form.single_form_selection.overlay_title": "Select a form",
"sulu_activity.resource.forms": "Form",
"sulu_activity.description.forms.created": "{userFullName} has created the form \"{resourceTitle}\"",
"sulu_activity.description.forms.modified": "{userFullName} has changed the form \"{resourceTitle}\"",
"sulu_activity.description.forms.translation_added": "{userFullName} has added a translation \"{resourceLocale}\" to the form \"{resourceTitle}\"",
"sulu_activity.description.forms.removed": "{userFullName} has removed the form \"{resourceTitle}\"",
"sulu_activity.description.forms.copied": "{userFullName} has copied the form \"{resourceTitle}\""
"sulu_activity.description.forms.copied": "{userFullName} has copied the form \"{resourceTitle}\"",
"sulu_activity.description.forms.restored": "{userFullName} has restored the form \"{resourceTitle}\""
}
4 changes: 2 additions & 2 deletions Tests/Application/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct($environment, $debug, $suluContext = self::CONTEXT_A
parent::__construct($environment, $debug, $suluContext);
}

public function registerBundles()
public function registerBundles(): iterable
{
return \array_merge(
parent::registerBundles(),
Expand All @@ -39,7 +39,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
$loader->load(__DIR__ . '/config/config_' . $this->getContext() . '.yml');
}

protected function getKernelParameters()
protected function getKernelParameters(): array
{
$parameters = parent::getKernelParameters();

Expand Down
5 changes: 5 additions & 0 deletions Tests/Functional/Controller/FormControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Sulu\Bundle\FormBundle\Entity\FormTranslation;
use Sulu\Bundle\FormBundle\Entity\FormTranslationReceiver;
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
use Sulu\Bundle\TrashBundle\Domain\Model\TrashItemInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;

class FormControllerTest extends SuluTestCase
Expand Down Expand Up @@ -239,6 +240,10 @@ public function testDelete(): void
$activity = $this->em->getRepository(ActivityInterface::class)->findOneBy(['type' => 'removed']);
$this->assertNotNull($activity);
$this->assertSame((string) $formId, $activity->getResourceId());

$trashItemRepository = $this->em->getRepository(TrashItemInterface::class);
$trashItem = $trashItemRepository->findOneBy(['resourceKey' => Form::RESOURCE_KEY, 'resourceId' => $formId]);
$this->assertNotNull($trashItem);
}

public function testDeleteNotFound(): void
Expand Down
Loading

0 comments on commit ba31c3e

Please sign in to comment.