瀏覽代碼

Refactory workflow repository y workflowbaseadmin, para que tome workflows de db o por yml

Guillermo Espinoza 7 年之前
父節點
當前提交
27c3099a9f
共有 2 個文件被更改,包括 33 次插入7 次删除
  1. 30 6
      Admin/WorkflowBaseAdmin.php
  2. 3 1
      Repository/WorkflowRepository.php

+ 30 - 6
Admin/WorkflowBaseAdmin.php

@@ -14,13 +14,36 @@ class WorkflowBaseAdmin extends BaseAdmin
     {
         $actions = parent::getBatchActions();
 
-        $workflows = $this->getRepository('WorkflowBundle:Workflow')->findAllByClass($this->getClass());
+        $workflows = array();
+        $registry = $this->get('workflow.registry');
+        $class = $this->getClass();
+        $tmpEntity = new $class();
+        try {
+            $workflows = $this->getRepository('WorkflowBundle:Workflow')->findAllByClass($this->getClass());
+            // No hay workflows en db, pruebo traer desde el workflow registry
+            if (count($workflows) == 0) {
+                $workflows[] = $registry->get($tmpEntity);
+            }
+        } catch (\Exception $ex) {
+            // No hay workflows en db, pruebo traer desde el workflow registry 
+            // con los names definidos en el admin $batchWorkflows
+            if (isset($this->batchWorkflows)) {
+                foreach ($this->batchWorkflows as $name) {
+                    $workflows[] = $registry->get($tmpEntity, $name);
+                }
+            }
+        }
+        
         foreach ($workflows as $workflow) {
-            $definition = $workflow->getDefinition($workflow->getSubject());
+            $definition = method_exists($workflow, 'getSubject') 
+                    ? $workflow->getDefinition($workflow->getSubject())
+                    : $workflow->getDefinition();
             $transitions = $definition ? $definition->getTransitions() : array();
-            foreach ($transitions as $key => $transition) {
-                $actions[$key] = array(
-                    'label' => $this->trans('workflow.' . $workflow->getName() . '.transitions.' . $transition->getName(), array(), 'WorkflowLabel'),
+            foreach ($transitions as $transition) {
+                $label = $this->trans('workflow.' . $workflow->getName() . '.transitions.' . $transition->getName(), array(), 'WorkflowLabel');
+                $label .= " ( {$transition->getName()} )";
+                $actions[$transition->getName()] = array(
+                    'label' => $label,
                     'ask_confirmation' => true,
                     'workflow' => array(
                         'name' => $workflow->getName(),
@@ -29,7 +52,8 @@ class WorkflowBaseAdmin extends BaseAdmin
                 );
             }
         }
-
+        asort($actions);
+        
         return $actions;
     }
 

+ 3 - 1
Repository/WorkflowRepository.php

@@ -14,7 +14,9 @@ class WorkflowRepository extends EntityRepository
      */
     public function findAllByClass($class)
     {
-        $results = $this->createQueryBuilder('Workflow')->getQuery()->getResult();
+        $results = $this->createQueryBuilder('Workflow')
+                ->where('Workflow.enable = :enable')->setParameter('enable', true)
+                ->getQuery()->getResult();
         foreach ($results as $key => &$result) {
             if (!in_array($class, $result->getSupport())) {
                 unset($results[$key]);