mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-30 03:23:48 +00:00
rewrite workflow and handle finalize differently
This commit is contained in:
@@ -21,6 +21,7 @@ use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Workflow\Event\Event;
|
||||
use Symfony\Component\Workflow\Event\GuardEvent;
|
||||
use Symfony\Component\Workflow\TransitionBlocker;
|
||||
use function array_key_exists;
|
||||
|
||||
class EntityWorkflowTransitionEventSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
@@ -44,6 +45,7 @@ class EntityWorkflowTransitionEventSubscriber implements EventSubscriberInterfac
|
||||
{
|
||||
return [
|
||||
'workflow.transition' => 'onTransition',
|
||||
'workflow.completed' => 'onCompleted',
|
||||
'workflow.guard' => [
|
||||
['guardEntityWorkflow', 0],
|
||||
],
|
||||
@@ -59,7 +61,7 @@ class EntityWorkflowTransitionEventSubscriber implements EventSubscriberInterfac
|
||||
/** @var EntityWorkflow $entityWorkflow */
|
||||
$entityWorkflow = $event->getSubject();
|
||||
|
||||
if ($entityWorkflow->isFinalize()) {
|
||||
if ($entityWorkflow->isFinal()) {
|
||||
$event->addTransitionBlocker(
|
||||
new TransitionBlocker(
|
||||
'workflow.The workflow is finalized',
|
||||
@@ -88,7 +90,25 @@ class EntityWorkflowTransitionEventSubscriber implements EventSubscriberInterfac
|
||||
}
|
||||
}
|
||||
|
||||
public function onTransition(Event $event)
|
||||
public function onCompleted(Event $event): void
|
||||
{
|
||||
if (!$event->getSubject() instanceof EntityWorkflow) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var EntityWorkflow $entityWorkflow */
|
||||
$entityWorkflow = $event->getSubject();
|
||||
$step = $entityWorkflow->getCurrentStep();
|
||||
|
||||
$placeMetadata = $event->getWorkflow()->getMetadataStore()
|
||||
->getPlaceMetadata($step->getCurrentStep());
|
||||
|
||||
if (array_key_exists('isFinal', $placeMetadata) && true === $placeMetadata['isFinal']) {
|
||||
$step->setIsFinal(true);
|
||||
}
|
||||
}
|
||||
|
||||
public function onTransition(Event $event): void
|
||||
{
|
||||
if (!$event->getSubject() instanceof EntityWorkflow) {
|
||||
return;
|
||||
|
@@ -61,7 +61,7 @@ class NotificationOnTransition implements EventSubscriberInterface
|
||||
|
||||
$dests = array_merge(
|
||||
$entityWorkflow->getSubscriberToStep()->toArray(),
|
||||
$entityWorkflow->isFinalize() ? $entityWorkflow->getSubscriberToFinal()->toArray() : [],
|
||||
$entityWorkflow->isFinal() ? $entityWorkflow->getSubscriberToFinal()->toArray() : [],
|
||||
$entityWorkflow->getCurrentStep()->getDestUser()->toArray()
|
||||
);
|
||||
|
||||
|
@@ -24,6 +24,10 @@ class WorkflowTwigExtension extends AbstractExtension
|
||||
[WorkflowTwigExtensionRuntime::class, 'listWorkflows'],
|
||||
['needs_environment' => true, 'is_safe' => ['html']]
|
||||
),
|
||||
new TwigFunction(
|
||||
'chill_workflow_transition_by_string',
|
||||
[WorkflowTwigExtensionRuntime::class, 'getTransitionByString']
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@@ -17,6 +17,7 @@ use Chill\MainBundle\Workflow\EntityWorkflowManager;
|
||||
use Chill\MainBundle\Workflow\Helper\MetadataExtractor;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Workflow\Registry;
|
||||
use Symfony\Component\Workflow\Transition;
|
||||
use Twig\Environment;
|
||||
use Twig\Extension\RuntimeExtensionInterface;
|
||||
|
||||
@@ -46,6 +47,20 @@ class WorkflowTwigExtensionRuntime implements RuntimeExtensionInterface
|
||||
$this->normalizer = $normalizer;
|
||||
}
|
||||
|
||||
public function getTransitionByString(EntityWorkflow $entityWorkflow, string $key): ?Transition
|
||||
{
|
||||
$workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
|
||||
$transitions = $workflow->getDefinition()->getTransitions();
|
||||
|
||||
foreach ($transitions as $transition) {
|
||||
if ($transition->getName() === $key) {
|
||||
return $transition;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function listWorkflows(Environment $environment, string $relatedEntityClass, int $relatedEntityId, array $options = []): string
|
||||
{
|
||||
$blankEntityWorkflow = new EntityWorkflow();
|
||||
|
Reference in New Issue
Block a user