Browse Source

doctrine2workflowaction

Guillermo Espinoza 8 years ago
parent
commit
67295011ca

+ 83 - 0
src/WorkflowBundle/Admin/Doctrine2WorkFlowActionAdmin.php

@@ -0,0 +1,83 @@
+<?php
+
+namespace WorkflowBundle\Admin;
+
+use Base\AdminBundle\Admin\BaseAdmin;
+use Sonata\AdminBundle\Datagrid\DatagridMapper;
+use Sonata\AdminBundle\Datagrid\ListMapper;
+use Sonata\AdminBundle\Form\FormMapper;
+use Sonata\AdminBundle\Show\ShowMapper;
+use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
+use \WorkflowBundle\Utils\DoctrineEvents;
+use \WorkflowBundle\Utils\WorkFlowEntities;
+
+class Doctrine2WorkFlowActionAdmin extends BaseAdmin
+{
+
+    /**
+     * @param DatagridMapper $datagridMapper
+     */
+    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
+    {
+        $datagridMapper
+            ->add('name')
+            ->add('event')
+            ->add('entity')
+        ;
+    }
+
+    /**
+     * @param ListMapper $listMapper
+     */
+    protected function configureListFields(ListMapper $listMapper)
+    {
+        $listMapper
+            ->add('name')
+            ->add('event')
+            ->add('entity')
+            ->add('actions')
+            ->add('_action', null, array(
+                'actions' => array(
+                    'show' => array(),
+                    'edit' => array(),
+                    'delete' => array(),
+                )
+            ))
+        ;
+    }
+
+    /**
+     * @param FormMapper $formMapper
+     */
+    protected function configureFormFields(FormMapper $formMapper)
+    {
+        $formMapper
+            ->add('name')
+            ->add('event', ChoiceType::class, array(
+                'choices' => DoctrineEvents::getChoices(),
+            ))
+            ->add('entity', ChoiceType::class, array(
+                'choices' => WorkFlowEntities::getChoices(),
+            ))
+            ->add('actions', null, array(
+                'multiple' => true,
+                'expanded' => true,
+                'by_reference' => false
+            ))
+        ;
+    }
+
+    /**
+     * @param ShowMapper $showMapper
+     */
+    protected function configureShowFields(ShowMapper $showMapper)
+    {
+        $showMapper
+            ->add('name')
+            ->add('event')
+            ->add('entity')
+            ->add('actions')
+        ;
+    }
+
+}

+ 41 - 2
src/WorkflowBundle/Entity/Action.php

@@ -40,7 +40,7 @@ class Action
      * @ORM\Column(type="string", length=255, nullable=false)
      */
     protected $objectClass;
-    
+
     /**
      * @var string
      *
@@ -62,6 +62,13 @@ class Action
      */
     protected $template;
 
+    /**
+     * @var Doctrine2WorkFlowAction
+     * 
+     * @ORM\ManyToMany(targetEntity="Doctrine2WorkFlowAction", inversedBy="actions", fetch="EXTRA_LAZY")
+     */
+    private $doctrine2WorkFlowActions;
+
     /**
      * @var int
      *
@@ -146,7 +153,6 @@ class Action
         return $this->template;
     }
 
-
     /**
      * Set workflowName
      *
@@ -219,6 +225,38 @@ class Action
         return $this->eventName;
     }
 
+    /**
+     * @return Doctrine2WorkFlowAction
+     */
+    public function getDoctrine2WorkFlowActions()
+    {
+        return $this->doctrine2WorkFlowActions;
+    }
+
+    /**
+     * @param Doctrine2WorkFlowAction $doctrine2WorkFlowAction
+     * 
+     * @return Workflow
+     */
+    public function addDoctrine2WorkFlowAction($doctrine2WorkFlowAction)
+    {
+        $this->doctrine2WorkFlowActions[] = $doctrine2WorkFlowAction;
+
+        return $this;
+    }
+
+    /**
+     * @param Doctrine2WorkFlowAction $doctrine2WorkFlowAction
+     * 
+     * @return Workflow
+     */
+    public function removeDoctrine2WorkFlowAction($doctrine2WorkFlowAction)
+    {
+        $this->doctrine2WorkFlowActions->removeElement($doctrine2WorkFlowAction);
+
+        return $this;
+    }
+
     /**
      * Set tenancyId
      *
@@ -242,4 +280,5 @@ class Action
     {
         return $this->tenancyId;
     }
+
 }

+ 170 - 0
src/WorkflowBundle/Entity/Doctrine2WorkFlowAction.php

@@ -0,0 +1,170 @@
+<?php
+
+namespace WorkflowBundle\Entity;
+
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\Table
+ * @ORM\Entity(repositoryClass="WorkflowBundle\Repository\Doctrine2WorkFlowActionRepository")
+ */
+class Doctrine2WorkFlowAction
+{
+   
+    /**
+     * @var int
+     *
+     * @ORM\Column(name="id", type="integer")
+     * @ORM\Id
+     * @ORM\GeneratedValue(strategy="AUTO")
+     */
+    private $id;
+
+    /**
+     * @var string
+     * 
+     * @ORM\Column(type="string", length=255) 
+     */
+    private $name;
+    
+    /**
+     * @var string
+     * 
+     * @ORM\Column(type="string", length=255) 
+     */
+    protected $event;
+
+    /**
+     * @var string
+     * 
+     * @ORM\Column(type="string", length=255) 
+     */
+    protected $entity;
+    
+    /**
+     * @var ArrayCollection
+     * 
+     * @ORM\ManyToMany(targetEntity="Action", mappedBy="doctrine2WorkFlowActions", cascade={"persist"})
+     */
+    protected $actions;
+
+    
+    /**
+     * @return string
+     */
+    public function __toString()
+    {
+        return $this->name;
+    }
+
+    public function __construct()
+    {
+        $this->actions = new ArrayCollection();
+    }
+
+    /**
+     * Get id
+     *
+     * @return int
+     */
+    public function getId()
+    {
+        return $this->id;
+    }
+    
+    /**
+     * @return string
+     */
+    public function getName()
+    {
+        return $this->name;
+    }
+
+    /**
+     * @param string $name
+     * 
+     * @return $this
+     */
+    public function setName($name)
+    {
+        $this->name = $name;
+        
+        return $this;
+    }
+    
+    /**
+     * @return string
+     */
+    public function getEvent()
+    {
+        return $this->event;
+    }
+
+    /**
+     * @param string $event
+     * 
+     * @return $this
+     */
+    public function setEvent($event)
+    {
+        $this->event = $event;
+
+        return $this;
+    }
+    
+    /**
+     * @return string
+     */
+    public function getEntity()
+    {
+        return $this->entity;
+    }
+
+    /**
+     * @param string $entity
+     * 
+     * @return $this
+     */
+    public function setEntity($entity)
+    {
+        $this->entity = $entity;
+        
+        return $this;
+    }
+    
+    /**
+     * @param Action $action
+     * 
+     * @return Workflow
+     */
+    public function addAction($action)
+    {
+        $action->addDoctrine2WorkFlowAction($this);
+        $this->actions[] = $action;
+
+        return $this;
+    }
+
+    /**
+     * @param Action $action
+     * 
+     * @return Workflow
+     */
+    public function removeAction($action)
+    {
+        $action->removeDoctrine2WorkFlowAction($this);
+        $this->actions->removeElement($action);
+
+        return $this;
+    }
+
+    /**
+     * @return ArrayCollection 
+     */
+    public function getActions()
+    {
+        return $this->actions;
+    }
+
+}

+ 78 - 0
src/WorkflowBundle/EventListener/DoctrineEventSubscriber.php

@@ -0,0 +1,78 @@
+<?php
+
+namespace WorkflowBundle\EventListener;
+
+use Doctrine\Common\EventSubscriber;
+use Doctrine\ORM\Event\LifecycleEventArgs;
+use WorkflowBundle\Utils\DoctrineEvents;
+
+class DoctrineEventSubscriber implements EventSubscriber
+{
+
+    /**
+     * @return array
+     */
+    public function getSubscribedEvents()
+    {
+        return DoctrineEvents::getConstants();
+    }
+
+    /**
+     * @param LifecycleEventArgs $args
+     */
+    public function prePersist(LifecycleEventArgs $args)
+    {
+        $this->execute($args);
+    }
+
+    /**
+     * @param LifecycleEventArgs $args
+     */
+    public function postPersist(LifecycleEventArgs $args)
+    {
+        $this->execute($args);
+    }
+
+    /**
+     * @param LifecycleEventArgs $args
+     */
+    public function preUpdate(LifecycleEventArgs $args)
+    {
+        $this->execute($args);
+    }
+
+    /**
+     * @param LifecycleEventArgs $args
+     */
+    public function postUpdate(LifecycleEventArgs $args)
+    {
+        $this->execute($args);
+    }
+
+    /**
+     * @param LifecycleEventArgs $args
+     */
+    public function preRemove(LifecycleEventArgs $args)
+    {
+        $this->execute($args);
+    }
+
+    /**
+     * @param LifecycleEventArgs $args
+     */
+    public function postRemove(LifecycleEventArgs $args)
+    {
+        $this->execute($args);
+    }
+
+    /**
+     * @param LifecycleEventArgs $args
+     */
+    public function execute(LifecycleEventArgs $args)
+    {
+        $entity = $args->getEntity();
+        $entityManager = $args->getEntityManager();
+        $class = get_class($entity);
+    }
+
+}

+ 37 - 0
src/WorkflowBundle/Repository/Doctrine2WorkFlowActionRepository.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace WorkflowBundle\Repository;
+
+use \Doctrine\ORM\EntityRepository;
+
+class Doctrine2WorkFlowActionRepository extends EntityRepository
+{
+
+    /**
+     * @param array $criteria
+     * 
+     * @return array
+     */
+    public function findAllBy($criteria = array())
+    {
+        $qb = $this->createQueryBuilder('Doctrine2WorkFlowAction');
+
+        foreach ($criteria as $field => $value) {
+            $qb->andWhere("{$field} = :{$field}")->setParameter($field, $value);
+        }
+
+        return $qb->getQuery()->getResult();
+    }
+
+    /**
+     * @param string $event
+     * @param string $entity
+     * 
+     * @return array
+     */
+    public function findAllByEventAndEntity($event, $entity)
+    {
+        return $this->findAllBy(compact('event', 'entity'));
+    }
+
+}

+ 12 - 1
src/WorkflowBundle/Resources/config/services.yml

@@ -13,8 +13,19 @@ services:
             - { name: sonata.admin, manager_type: orm, group: Workflow, label: Action, label_catalogue: WorkflowBundle, label_translator_strategy: sonata.admin.label.strategy.underscore }
         calls:    
             - [setTranslationDomain, [WorkflowBundle]]
+    workflow.admin.doctrine2_work_flow_action:
+        class: WorkflowBundle\Admin\Doctrine2WorkFlowActionAdmin
+        arguments: [~, WorkflowBundle\Entity\Doctrine2WorkFlowAction, SonataAdminBundle:CRUD]
+        tags:
+            - { name: sonata.admin, manager_type: orm, group: Workflow, label: Doctrine2WorkFlowAction, label_catalogue: WorkflowBundle, label_translator_strategy: sonata.admin.label.strategy.underscore }
+        calls:
+            - [setTranslationDomain, [WorkflowBundle]]
+    workflow.doctrine_event.subscriber:
+        class: WorkflowBundle\EventListener\DoctrineEventSubscriber
+        tags:
+            - { name: doctrine.event_subscriber, connection: default }
     workflow.event.subscriber:
         class: WorkflowBundle\Event\EventSubscriber
         tags:
             - { name: kernel.event_subscriber }
-        arguments: ['@service_container']
+        arguments: ['@service_container']

+ 16 - 2
src/WorkflowBundle/Resources/translations/WorkflowBundle.es.yml

@@ -7,10 +7,16 @@ filter:
     label_created: Creado
     label_updated: Actualizado
     label_enable : Habilitado
+    label_event: Evento
+    label_actions: Actions
+    label_entity: Entidad
 breadcrumb:
     link_workflow_list: Listado Workflows
     link_workflow_create: Crear Workflow
     link_workflow_delete: Eliminar Workflow
+    link_doctrine2_work_flow_action_list: Listado Doctrine2WorkFlowAction
+    link_doctrine2_work_flow_action_create: Crear Doctrine2WorkFlowAction
+    link_doctrine2_work_flow_action_delete: Eliminar Doctrine2WorkFlowAction
 form:
     label_id: Id
     label_name: Nombre
@@ -23,6 +29,9 @@ form:
     label_type : Tipo de Workflow
     label_marking_type : Tipo de marcador
     label_marking_name : Nombre de marcador
+    label_event: Evento
+    label_actions: Actions
+    label_entity: Entidad
 list:
     label_id: Id
     label_name: Nombre
@@ -35,6 +44,9 @@ list:
     label_type : Tipo de Workflow
     label_marking_type : Tipo de marcador
     label_marking_name : Nombre de marcador
+    label_event: Evento
+    label_actions: Actions
+    label_entity: Entidad
 show:
     label_id: Id
     label_name: Nombre
@@ -47,9 +59,11 @@ show:
     label_type : Tipo de Workflow
     label_marking_type : Tipo de marcador
     label_marking_name : Nombre de marcador
+    label_event: Evento
+    label_actions: Actions
+    label_entity: Entidad
 helps:
     workflow_label_name: Nombre con el que se identificará el workflow. Se convertirá a minúscula y los espacios se reemplazarán por "_"
     workflow_label_type: "Tipo de Workflow: state_machine o workflow"
     workflow_label_marking_type: "Tipo de Marcador: single_state o multiple_state"
-    workflow_label_marking_name: "Nombre del Marcador, atributo de la entidad que tendrá el estado, por defecto: currentState"
-   
+    workflow_label_marking_name: "Nombre del Marcador, atributo de la entidad que tendrá el estado, por defecto: currentState"

+ 34 - 0
src/WorkflowBundle/Utils/ChoiceTrait.php

@@ -0,0 +1,34 @@
+<?php
+
+namespace WorkflowBundle\Utils;
+
+trait ChoiceTrait
+{
+
+    /**
+     * @return array
+     */
+    static function getConstants()
+    {
+        $oClass = new \ReflectionClass(__CLASS__);
+
+        return $oClass->getConstants();
+    }
+
+    /**
+     * Retorna un array con todas las constantes de la clase 
+     * para utilizar en campos de tipo choice en forms
+     * 
+     * @return array
+     */
+    public static function getChoices()
+    {
+        $choices = array();
+        foreach (self::getConstants() as $constant) {
+            $choices[$constant] = $constant;
+        }
+
+        return $choices;
+    }
+
+}

+ 22 - 0
src/WorkflowBundle/Utils/DoctrineEvents.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace WorkflowBundle\Utils;
+
+class DoctrineEvents
+{
+
+    use ChoiceTrait;
+
+    const PRE_PERSIST = 'prePersist';
+
+    const POST_PERSIST = 'postPersist';
+
+    const PRE_UPDATE = 'preUpdate';
+
+    const POST_UPDATE = 'postUpdate';
+
+    const PRE_REMOVE = 'preRemove';
+
+    const POST_REMOVE = 'postRemove';
+
+}

+ 20 - 0
src/WorkflowBundle/Utils/WorkFlowEntities.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace WorkflowBundle\Utils;
+
+/*
+ * Se definen las entidades que poseen Workflow
+ * Definir como:
+ * const <NOMBRE_CONSTANTE> = '<Namespace\Entidad>';
+ */
+
+class WorkFlowEntities
+{
+
+    use ChoiceTrait;
+
+    const FTTH_ONU = 'FTTHBundle\Entity\ONU';
+
+    const FTTH_OLT = 'FTTHBundle\Entity\OLT';
+
+}

+ 0 - 0
var/cache/.gitkeep


+ 0 - 0
var/logs/.gitkeep


+ 0 - 0
var/sessions/.gitkeep