WorkflowBaseAdmin.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace WorkflowBundle\Admin;
  3. use Base\AdminBundle\Admin\BaseAdmin;
  4. class WorkflowBaseAdmin extends BaseAdmin
  5. {
  6. /**
  7. * @return array
  8. */
  9. public function getBatchActions()
  10. {
  11. $actions = parent::getBatchActions();
  12. $tmp = $actions;
  13. $actions = [];
  14. $actions['Default'] = array(
  15. 'label' => $this->trans('Default', array(), 'WorkflowLabel'),
  16. 'group' => $tmp
  17. );
  18. $workflows = array();
  19. $registry = $this->get('workflow.registry');
  20. $class = $this->getClass();
  21. $tmpEntity = new $class();
  22. try {
  23. $workflows = $this->getRepository('WorkflowBundle:Workflow')->findAllByClass($this->getClass());
  24. // No hay workflows en db, pruebo traer desde el workflow registry
  25. if (count($workflows) == 0) {
  26. $workflows[] = $registry->get($tmpEntity);
  27. }
  28. } catch (\Exception $ex) {
  29. }
  30. if (isset($this->batchWorkflows)) {
  31. // No hay workflows en db, pruebo traer desde el workflow registry
  32. // con los names definidos en el admin $batchWorkflows
  33. foreach ($this->batchWorkflows as $name) {
  34. $workflows[] = $registry->get($tmpEntity, $name);
  35. }
  36. }
  37. foreach ($workflows as $workflow) {
  38. $definition = method_exists($workflow, 'getSubject')
  39. ? $workflow->getDefinition($workflow->getSubject())
  40. : $workflow->getDefinition();
  41. $transitions = $definition ? $definition->getTransitions() : array();
  42. $tmp = array();
  43. foreach ($transitions as $transition) {
  44. $id = 'workflow.' . $workflow->getName() . '.transitions.' . $transition->getName();
  45. $label = $this->trans($id, array(), 'WorkflowLabel');
  46. if ($label == $id) {
  47. $label = $transition->getName();
  48. }
  49. $label .= " ( {$transition->getName()} )";
  50. $tmp[$workflow->getName() . ':' . $transition->getName()] = array(
  51. 'label' => $label,
  52. 'ask_confirmation' => true,
  53. 'workflow' => array(
  54. 'name' => $workflow->getName(),
  55. 'transition' => $transition->getName(),
  56. ),
  57. );
  58. }
  59. $actions[$workflow->getName()] = array(
  60. 'label' => $this->trans($workflow->getName(), array(), 'WorkflowLabel'),
  61. 'group' => $tmp
  62. );
  63. }
  64. return $actions;
  65. }
  66. }