WorkflowExtension.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace WorkflowBundle\Twig;
  3. use Symfony\Component\Workflow\Registry;
  4. use Symfony\Component\Translation\DataCollectorTranslator as Translator;
  5. class WorkflowExtension extends \Twig_Extension
  6. {
  7. private $workflowRegistry;
  8. private $translator;
  9. public function __construct(Registry $workflowRegistry, Translator $translator)
  10. {
  11. $this->workflowRegistry = $workflowRegistry;
  12. $this->translator = $translator;
  13. }
  14. public function getFunctions()
  15. {
  16. return array(
  17. new \Twig_SimpleFunction('workflow_can', array($this, 'canTransition')),
  18. new \Twig_SimpleFunction('workflow_transitions', array($this, 'getEnabledTransitions')),
  19. new \Twig_SimpleFunction('workflow_correct_state', array($this, 'isCorrectState')),
  20. new \Twig_SimpleFunction('get_class', array($this, 'getClass')),
  21. new \Twig_SimpleFunction('workflow_translate_label', array($this, 'getLabelTranslate')),
  22. new \Twig_SimpleFunction('workflow_go_json', array($this, 'getGoJson')),
  23. );
  24. }
  25. public function isCorrectState($object, $name = null)
  26. {
  27. try {
  28. $places = $this->workflowRegistry->get($object, $name)->getDefinition()->getPlaces();
  29. } catch (\Exception $e) {
  30. $places = array();
  31. }
  32. $state = $object->getCurrentState();
  33. if(is_null($state)) return false;
  34. if(isset($places[$state])) return true;
  35. return false;
  36. }
  37. public function canTransition($object, $transition, $name = null)
  38. {
  39. return $this->workflowRegistry->get($object, $name)->can($object, $transition);
  40. }
  41. public function getEnabledTransitions($object, $name = null)
  42. {
  43. try {
  44. $return = $this->workflowRegistry->get($object, $name)->getEnabledTransitions($object);
  45. } catch (\Exception $e) {
  46. $return = array();
  47. }
  48. return $return;
  49. }
  50. public function getName()
  51. {
  52. return 'workflow';
  53. }
  54. public function getClass($object)
  55. {
  56. return get_class($object);
  57. }
  58. public function getLabelTranslate($label, $alternative, $params = null, $resource = "WorkflowLabel")
  59. {
  60. if(is_null($params)) $params = array();
  61. $trans = $this->translator->trans($label,$params,$resource);
  62. if($trans == $label) {
  63. return $alternative;
  64. }
  65. return $trans;
  66. }
  67. public function getGoJson($object, $name = null)
  68. {
  69. $data = array();
  70. $nodes = array();
  71. $links = array();
  72. $definition = $this->workflowRegistry->get($object, $name)->getDefinition();
  73. $places = $definition->getPlaces();
  74. $initial = $definition->getInitialPlace();
  75. $transitions = $definition->getTransitions();
  76. foreach($places as $p => $place) {
  77. $label = $p;
  78. $color = "white";
  79. if($p == $initial) $color = "lightblue";
  80. $nodes[] = array('key' => $label, 'color' => $color);
  81. }
  82. foreach($transitions as $transition) {
  83. $label = $transition->getName();
  84. $from = $transition->getFroms()[0];
  85. $to = $transition->getTos()[0];
  86. $links[] = array('from' => $from, 'to' => $to, 'text' => $label);
  87. }
  88. $data[] = $nodes;
  89. $data[] = $links;
  90. //return $data;
  91. $return = json_encode($nodes).",".json_encode($links);
  92. return $return;
  93. }
  94. }