|
@@ -0,0 +1,35 @@
|
|
|
|
+<?php
|
|
|
|
+
|
|
|
|
+namespace WorkflowBundle\EventListener;
|
|
|
|
+
|
|
|
|
+use WorkflowBundle\Entity\Interfaces\WorkflowInterface;
|
|
|
|
+use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
|
|
|
|
+use Doctrine\ORM\EntityManager;
|
|
|
|
+
|
|
|
|
+class WorkflowInterfaceListener
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @param LifecycleEventArgs $event
|
|
|
|
+ */
|
|
|
|
+ public function prePersist(LifecycleEventArgs $event)
|
|
|
|
+ {
|
|
|
|
+ $em = $event->getEntityManager();
|
|
|
|
+ $entity = $event->getEntity();
|
|
|
|
+ if ($entity instanceof WorkflowInterface) {
|
|
|
|
+ $workflow = $entity->getWorkflow();
|
|
|
|
+ // La entidad no tiene workflow, busco por la clase y le asigno el primero
|
|
|
|
+ if (is_null($workflow)) {
|
|
|
|
+ $workflows = $em->getRepository('WorkflowBundle:Workflow')->findAllByClass(get_class($entity));
|
|
|
|
+ if (count($workflows)) {
|
|
|
|
+ $workflow = $workflows[array_keys($workflows)[0]];
|
|
|
|
+ $entity->setWorkflow($workflow);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (!is_null($workflow)) {
|
|
|
|
+ $entity->setCurrentState($workflow->getInitialPlace($entity));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|