src/EventSubscriber/FormFieldRuleSubscriber.php line 17

  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Doctrine\ORM\Events;
  5. use Doctrine\ORM\Event\OnFlushEventArgs;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Doctrine\ORM\UnitOfWork;
  8. use App\Entity\FormField;
  9. use App\Document\Action;
  10. use App\Document\Condition;
  11. class FormFieldRuleSubscriber implements EventSubscriberInterface
  12. {
  13.     public function onFlush(OnFlushEventArgs $args): void
  14.     {
  15.         $em  $args->getEntityManager();
  16.         $uow $em->getUnitOfWork();
  17.         foreach ($uow->getScheduledEntityUpdates() as $entity) {
  18.             if (!$entity instanceof FormField) {
  19.                 continue;
  20.             }
  21.             $changeSet $uow->getEntityChangeSet($entity);
  22.             $this->handleFormFieldChange($entity$changeSet$em$uow);
  23.         }
  24.     }
  25.     private function handleFormFieldChange(
  26.         FormField $field,
  27.         array $changeSet,
  28.         EntityManagerInterface $em,
  29.         UnitOfWork $uow
  30.     ): void {
  31.         if (!isset($changeSet['multiple'])) {
  32.             return;
  33.         }
  34.     
  35.         [$old$new] = $changeSet['multiple'];
  36.     
  37.         if ($old === true && $new === false) {
  38.             $this->fixRulesForMultipleToSingle($field$em$uow);
  39.         }
  40.     }
  41.     
  42.     private function fixRulesForMultipleToSingle(
  43.         FormField $field,
  44.         EntityManagerInterface $em,
  45.         UnitOfWork $uow
  46.     ): void {
  47.         $fieldId $field->getId();
  48.     
  49.         $actions $this->dm
  50.             ->getRepository(Action::class)
  51.             ->findBy(['formField' => $fieldId]);
  52.     
  53.         foreach ($actions as $action) {
  54.             $value $action->getValue();
  55.     
  56.             if (is_array($value)) {
  57.                 // Keep first value
  58.                 $action->setValue(reset($value));
  59.                 $this->dm->persist($action);
  60.             }
  61.         }
  62.     
  63.         $conditions $this->dm
  64.             ->getRepository(Condition::class)
  65.             ->findBy(['formField' => $fieldId]);
  66.     
  67.         foreach ($conditions as $condition) {
  68.             $value $condition->getValue();
  69.     
  70.             if (is_array($value)) {
  71.                 // Keep first value
  72.                 $condition->setValue(reset($value));
  73.     
  74.                 // multiple → false => AND/OR no longer meaningful
  75.                 $condition->setAndOrValue(null);
  76.     
  77.                 $this->dm->persist($condition);
  78.             }
  79.         }
  80.     }
  81.     public static function getSubscribedEvents(): array
  82.     {
  83.         return [Events::onFlush];
  84.     }
  85. }