WorkflowDefaultCommand.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace WorkflowBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Input\InputOption;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use WorkflowBundle\Entity\Workflow;
  8. use WorkflowBundle\Utils\WorkFlowEntityClasses;
  9. class WorkflowDefaultCommand extends ContainerAwareCommand
  10. {
  11. protected function configure()
  12. {
  13. $this
  14. ->setName('workflow:default')
  15. ->setDescription('Set class default workflow and assign to all class entities')
  16. ->setHelp('Set class default workflow and assign to all class entities')
  17. ->addOption(
  18. 'class',
  19. null,
  20. InputOption::VALUE_REQUIRED,
  21. 'Entity Class name. e.g.: FTTHBundle\\Entity\\ONU'
  22. )
  23. ->addOption(
  24. 'all',
  25. null,
  26. InputOption::VALUE_NONE,
  27. 'Assign default workflow to all class entities passed by parameters'
  28. )
  29. ;
  30. }
  31. /**
  32. * @param InputInterface $input
  33. * @param OutputInterface $output
  34. */
  35. protected function execute(InputInterface $input, OutputInterface $output)
  36. {
  37. $class = $input->getOption('class');
  38. if (is_null($class)) {
  39. $output->writeln("<error>El parametro --class es requerido</error>");
  40. }
  41. $em = $this->getContainer()->get("doctrine.orm.entity_manager");
  42. $workworkflowRepository = $em->getRepository(Workflow::class);
  43. $workflow = null;
  44. $workflows = $workworkflowRepository->findAllByClass($class);
  45. if (count($workflows)) {
  46. // Hay workflows habilitados
  47. $workflow = current($workflows);
  48. // en el caso que haya varios workflows, seteo por defecto el primero
  49. if ($workflow->getUsedByDefault() == false) {
  50. $workflow->setUsedByDefault(true);
  51. $em->flush();
  52. }
  53. $output->writeln("<info>Workflow asignado por defecto: </info> {$workflow}");
  54. }
  55. if (!is_null($workflow) && $input->getOption('all')) {
  56. // actualizo las onus que no tengan currentState.
  57. // por ej si fueron importadas
  58. $qb = $em->getRepository($class)->createQueryBuilder($class);
  59. $result = $qb->update($class, 'o')
  60. ->set('o.currentState', ':currentState')
  61. ->setParameter('currentState', 'active')
  62. ->where('o.currentState IS NULL')
  63. ->getQuery()->getResult();
  64. $qb = $em->getRepository($class)->createQueryBuilder($class);
  65. $result = $qb->update($class, 'o')
  66. ->set('o.workflow', ':workflow')
  67. ->setParameter('workflow', $workflow)
  68. ->getQuery()->getResult();
  69. $output->writeln("<info>Se ejecutó la actualización de entidates</info> {$class}");
  70. $output->writeln("<info>Result:</info> {$result}");
  71. } elseif (is_null($workflow)) {
  72. $output->writeln("<error>No hay workflows para la clase</error>");
  73. }
  74. }
  75. }