WorkflowExtension.php 3.7 KB

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