فهرست منبع

workflow:default command, asigna un workflow por defecto y lo asigna a las entidades soportadas

Espinoza Guillermo 6 سال پیش
والد
کامیت
b6ea92f53b
2فایلهای تغییر یافته به همراه86 افزوده شده و 5 حذف شده
  1. 80 0
      Command/WorkflowDefaultCommand.php
  2. 6 5
      Repository/WorkflowRepository.php

+ 80 - 0
Command/WorkflowDefaultCommand.php

@@ -0,0 +1,80 @@
+<?php
+
+namespace WorkflowBundle\Command;
+
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+use WorkflowBundle\Entity\Workflow;
+use WorkflowBundle\Utils\WorkFlowEntityClasses;
+
+class WorkflowDefaultCommand extends ContainerAwareCommand
+{
+    protected function configure()
+    {
+        $this
+            ->setName('workflow:default')
+            ->setDescription('Set class default workflow and assign to all class entities')
+            ->setHelp('Set class default workflow and assign to all class entities')
+            ->addOption(
+                'class',
+                null,
+                InputOption::VALUE_REQUIRED,
+                'Entity Class name. e.g.: FTTHBundle\\Entity\\ONU'
+            )
+            ->addOption(
+                'all',
+                null,
+                InputOption::VALUE_NONE,
+                'Assign default workflow to all class entities passed by parameters'
+            )
+            ;
+    }
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $class = $input->getOption('class');
+        
+        if (is_null($class)) {
+            $output->writeln("<error>El parametro --class es requerido</error>");
+        }
+        
+        $em = $this->getContainer()->get("doctrine.orm.entity_manager");
+        $workworkflowRepository = $em->getRepository(Workflow::class);
+        
+        $workflow = null;
+        $workflows = $workworkflowRepository->findAllByClass($class);
+        if (count($workflows) == 1) {
+            // Hay un workflow habilitado y por defecto
+            $workflow = current($workflows);
+            $output->writeln("<info>Workflow asignado por defecto: </info> {$workflow}");
+        } elseif (count($workflows)) {
+            // No hay workflow por defecto, seteo por defecto el primero
+            $workflow = current($workflows);
+            $workflow->setUsedByDefault(true);
+            
+            $em->flush();
+            
+            $output->writeln("<info>Se actualiza workflow: </info> {$workflow}");
+        }
+        
+        if (!is_null($workflow) && $input->getOption('all')) {
+            $qb = $em->getRepository($class)->createQueryBuilder($class);
+            $result = $qb->update($class, 'o')
+            ->set('o.workflow', ':workflow')
+            ->setParameter('workflow', $workflow)
+            ->getQuery()
+            ->getResult()
+            ;
+            
+            $output->writeln("<info>Se ejecutó la actualización de entidates</info> {$class}");
+        } elseif (is_null($workflow)) {
+            $output->writeln("<error>No hay workflows para la clase</error>");
+        }
+    }
+}

+ 6 - 5
Repository/WorkflowRepository.php

@@ -18,16 +18,17 @@ class WorkflowRepository extends EntityRepository
      */
     public function findAllByClass($class)
     {
-        $qb = $this->createQueryBuilder('Workflow')
-                ->where('Workflow.enable = :enable')->setParameter('enable', true);
-                
-        $results = $qb->andWhere('Workflow.usedByDefault = :usedByDefault')
+        $results = $this->createQueryBuilder('Workflow')
+                ->where('Workflow.enable = :enable')->setParameter('enable', true)
+                ->andWhere('Workflow.usedByDefault = :usedByDefault')
                 ->setParameter('usedByDefault', true)
                 ->getQuery()->getResult();
         
         // No hay workflow enable y usedByDefault, busco los enable
         if (count($results) == 0) {
-            $results = $qb->getQuery()->getResult();
+            $results = $this->createQueryBuilder('Workflow')
+                    ->where('Workflow.enable = :enable')->setParameter('enable', true)
+                    ->getQuery()->getResult();
         }
         
         foreach ($results as $key => &$result) {