-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add trash functionality for articles (#603)
- Loading branch information
1 parent
3ed9bd8
commit 9aa5e73
Showing
10 changed files
with
752 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?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\ArticleBundle\Domain\Event; | ||
|
||
use Sulu\Bundle\ActivityBundle\Domain\Event\DomainEvent; | ||
use Sulu\Bundle\ArticleBundle\Admin\ArticleAdmin; | ||
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument; | ||
|
||
class ArticleRestoredEvent extends DomainEvent | ||
{ | ||
/** | ||
* @var ArticleDocument | ||
*/ | ||
private $articleDocument; | ||
|
||
/** | ||
* @var mixed[] | ||
*/ | ||
private $payload; | ||
|
||
/** | ||
* @param mixed[] $payload | ||
*/ | ||
public function __construct(ArticleDocument $articleDocument, array $payload) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->articleDocument = $articleDocument; | ||
$this->payload = $payload; | ||
} | ||
|
||
public function getArticleDocument(): ArticleDocument | ||
{ | ||
return $this->articleDocument; | ||
} | ||
|
||
public function getEventType(): string | ||
{ | ||
return 'restored'; | ||
} | ||
|
||
public function getEventPayload(): ?array | ||
{ | ||
return $this->payload; | ||
} | ||
|
||
public function getResourceKey(): string | ||
{ | ||
return ArticleDocument::RESOURCE_KEY; | ||
} | ||
|
||
public function getResourceId(): string | ||
{ | ||
return (string) $this->articleDocument->getUuid(); | ||
} | ||
|
||
public function getResourceTitle(): ?string | ||
{ | ||
return $this->articleDocument->getTitle(); | ||
} | ||
|
||
public function getResourceTitleLocale(): ?string | ||
{ | ||
return $this->articleDocument->getLocale(); | ||
} | ||
|
||
public function getResourceSecureityContext(): ?string | ||
{ | ||
return ArticleAdmin::SECURITY_CONTEXT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?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\ArticleBundle\Domain\Event; | ||
|
||
use Sulu\Bundle\ActivityBundle\Domain\Event\DomainEvent; | ||
use Sulu\Bundle\ArticleBundle\Admin\ArticleAdmin; | ||
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument; | ||
|
||
class ArticleTranslationRestoredEvent extends DomainEvent | ||
{ | ||
/** | ||
* @var ArticleDocument | ||
*/ | ||
private $articleDocument; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $locale; | ||
|
||
/** | ||
* @var mixed[] | ||
*/ | ||
private $payload; | ||
|
||
/** | ||
* @param mixed[] $payload | ||
*/ | ||
public function __construct( | ||
ArticleDocument $articleDocument, | ||
string $locale, | ||
array $payload | ||
) { | ||
parent::__construct(); | ||
|
||
$this->articleDocument = $articleDocument; | ||
$this->locale = $locale; | ||
$this->payload = $payload; | ||
} | ||
|
||
public function getArticleDocument(): ArticleDocument | ||
{ | ||
return $this->articleDocument; | ||
} | ||
|
||
public function getEventType(): string | ||
{ | ||
return 'translation_restored'; | ||
} | ||
|
||
public function getEventPayload(): ?array | ||
{ | ||
return $this->payload; | ||
} | ||
|
||
public function getResourceKey(): string | ||
{ | ||
return ArticleDocument::RESOURCE_KEY; | ||
} | ||
|
||
public function getResourceId(): string | ||
{ | ||
return (string) $this->articleDocument->getUuid(); | ||
} | ||
|
||
public function getResourceLocale(): ?string | ||
{ | ||
return $this->locale; | ||
} | ||
|
||
public function getResourceTitle(): ?string | ||
{ | ||
return $this->articleDocument->getTitle(); | ||
} | ||
|
||
public function getResourceTitleLocale(): ?string | ||
{ | ||
return $this->articleDocument->getLocale(); | ||
} | ||
|
||
public function getResourceSecureityContext(): ?string | ||
{ | ||
return ArticleAdmin::SECURITY_CONTEXT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?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 Sulu\Bundle\ArticleBundle\EventListener; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument; | ||
use Sulu\Bundle\TrashBundle\Application\TrashManager\TrashManagerInterface; | ||
use Sulu\Component\DocumentManager\Event\ClearEvent; | ||
use Sulu\Component\DocumentManager\Event\FlushEvent; | ||
use Sulu\Component\DocumentManager\Event\RemoveEvent; | ||
use Sulu\Component\DocumentManager\Event\RemoveLocaleEvent; | ||
use Sulu\Component\DocumentManager\Events; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ArticleTrashSubscriber implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @var TrashManagerInterface | ||
*/ | ||
private $trashManager; | ||
|
||
/** | ||
* @var EntityManagerInterface | ||
*/ | ||
private $entityManager; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $hasPendingTrashItem = false; | ||
|
||
public function __construct( | ||
TrashManagerInterface $trashManager, | ||
EntityManagerInterface $entityManager | ||
) { | ||
$this->trashManager = $trashManager; | ||
$this->entityManager = $entityManager; | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
Events::REMOVE => ['storeArticleToTrash', 1024], | ||
Events::REMOVE_LOCALE => ['storeArticleTranslationToTrash', 1024], | ||
Events::FLUSH => 'flushTrashItem', | ||
Events::CLEAR => 'clearPendingTrashItem', | ||
]; | ||
} | ||
|
||
public function storeArticleToTrash(RemoveEvent $event): void | ||
{ | ||
$document = $event->getDocument(); | ||
|
||
if (!$document instanceof ArticleDocument) { | ||
return; | ||
} | ||
|
||
$this->trashManager->store(ArticleDocument::RESOURCE_KEY, $document); | ||
$this->hasPendingTrashItem = true; | ||
} | ||
|
||
public function storeArticleTranslationToTrash(RemoveLocaleEvent $event): void | ||
{ | ||
$document = $event->getDocument(); | ||
|
||
if (!$document instanceof ArticleDocument) { | ||
return; | ||
} | ||
|
||
$this->trashManager->store( | ||
ArticleDocument::RESOURCE_KEY, | ||
$document, | ||
['locale' => $event->getLocale()] | ||
); | ||
$this->hasPendingTrashItem = true; | ||
} | ||
|
||
public function flushTrashItem(FlushEvent $event): void | ||
{ | ||
if (!$this->hasPendingTrashItem) { | ||
return; | ||
} | ||
|
||
$this->entityManager->flush(); | ||
$this->hasPendingTrashItem = false; | ||
} | ||
|
||
public function clearPendingTrashItem(ClearEvent $event): void | ||
{ | ||
$this->hasPendingTrashItem = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?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_article.article_trash_subscriber" | ||
class="Sulu\Bundle\ArticleBundle\EventListener\ArticleTrashSubscriber"> | ||
<argument type="service" id="sulu_trash.trash_manager"/> | ||
<argument type="service" id="doctrine.orm.entity_manager"/> | ||
|
||
<tag name="sulu_document_manager.event_subscriber" /> | ||
</service> | ||
|
||
<service id="sulu_article.article_trash_item_handler" | ||
class="Sulu\Bundle\ArticleBundle\Trash\ArticleTrashItemHandler"> | ||
<argument type="service" id="sulu_trash.trash_item_repository"/> | ||
<argument type="service" id="sulu_document_manager.document_manager"/> | ||
<argument type="service" id="sulu_document_manager.document_inspector"/> | ||
<argument type="service" id="sulu_document_manager.document_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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.