WorkflowBaseAdmin.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. asort($actions);
  13. $actionsBase = [];
  14. $actionsBase['Default'] = array(
  15. 'label' => $this->trans('Default', array(), 'WorkflowLabel'),
  16. 'group' => $actions
  17. );
  18. $actions = [];
  19. $workflows = array();
  20. $registry = $this->get('workflow.registry');
  21. $class = $this->getClass();
  22. $tmpEntity = new $class();
  23. try {
  24. $workflows = $this->getRepository('WorkflowBundle:Workflow')->findAllByClass($this->getClass());
  25. // No hay workflows en db, pruebo traer desde el workflow registry
  26. if (count($workflows) == 0) {
  27. $workflows[] = $registry->get($tmpEntity);
  28. }
  29. $actions += $this->addActions($workflows, 'Entity');
  30. } catch (\Exception $ex) {
  31. }
  32. if (isset($this->batchWorkflows)) {
  33. // No hay workflows en db, pruebo traer desde el workflow registry
  34. // con los names definidos en el admin $batchWorkflows
  35. foreach ($this->batchWorkflows as $name) {
  36. $workflows[] = $registry->get($tmpEntity, $name);
  37. }
  38. $actions += $this->addActions($workflows, 'File');
  39. }
  40. asort($actions);
  41. return $actionsBase + $actions;
  42. }
  43. private function addActions($workflows, $place)
  44. {
  45. foreach ($workflows as $workflow) {
  46. $definition = method_exists($workflow, 'getSubject')
  47. ? $workflow->getDefinition($workflow->getSubject())
  48. : $workflow->getDefinition();
  49. $transitions = $definition ? $definition->getTransitions() : array();
  50. $tmp = array();
  51. foreach ($transitions as $transition) {
  52. $id = 'workflow.' . $workflow->getName() . '.transitions.' . $transition->getName();
  53. $label = $this->trans($id, array(), 'WorkflowLabel');
  54. if ($label == $id) {
  55. $label = $transition->getName();
  56. }
  57. $label .= " ( {$transition->getName()} )";
  58. $tmp["workflow:" . $workflow->getName() . ':' . $transition->getName()] = array(
  59. 'label' => $label,
  60. 'ask_confirmation' => true,
  61. 'workflow' => array(
  62. 'name' => $workflow->getName(),
  63. 'transition' => $transition->getName(),
  64. ),
  65. );
  66. }
  67. asort($tmp);
  68. $actions[$workflow->getName()] = array(
  69. 'label' => $this->trans($workflow->getName(), array(), 'WorkflowLabel'),
  70. 'group' => $tmp
  71. );
  72. }
  73. return $actions;
  74. }
  75. }