WorkflowExtension.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. $c = "FTTHBundle\Entity\ONU";
  73. $class = "\\$c";
  74. $object = new $class();
  75. //print_r(get_class($object));
  76. //die;
  77. $definition = $this->workflowRegistry->get($object, $name)->getDefinition();
  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. }