WorkflowBaseAdmin.php 3.1 KB

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