workflowRegistry = $workflowRegistry; $this->translator = $translator; } public function getFunctions() { return array( new \Twig_SimpleFunction('workflow_can', array($this, 'canTransition')), new \Twig_SimpleFunction('workflow_transitions', array($this, 'getEnabledTransitions')), new \Twig_SimpleFunction('workflow_correct_state', array($this, 'isCorrectState')), new \Twig_SimpleFunction('get_class', array($this, 'getClass')), new \Twig_SimpleFunction('workflow_translate_label', array($this, 'getLabelTranslate')), new \Twig_SimpleFunction('workflow_go_json', array($this, 'getGoJson')), ); } public function isCorrectState($object, $name = null) { try { $places = $this->workflowRegistry->get($object, $name)->getDefinition()->getPlaces(); } catch (\Exception $e) { $places = array(); } $state = $object->getCurrentState(); if(is_null($state)) return false; if(isset($places[$state])) return true; return false; } public function canTransition($object, $transition, $name = null) { return $this->workflowRegistry->get($object, $name)->can($object, $transition); } public function getEnabledTransitions($object, $name = null) { try { $return = $this->workflowRegistry->get($object, $name)->getEnabledTransitions($object); } catch (\Exception $e) { $return = array(); } return $return; } public function getName() { return 'workflow'; } public function getClass($object) { return get_class($object); } public function getLabelTranslate($label, $alternative, $params = null, $resource = "WorkflowLabel") { if(is_null($params)) $params = array(); $trans = $this->translator->trans($label,$params,$resource); if($trans == $label) { return $alternative; } return $trans; } public function getGoJson($object, $name = null) { $data = array(); $nodes = array(); $links = array(); try{ $definition = $this->workflowRegistry->get($object, $name)->getDefinition(); }catch (InvalidArgumentException $e){ return json_encode(array()); } $places = $definition->getPlaces(); $initial = $definition->getInitialPlace(); $transitions = $definition->getTransitions(); foreach($places as $p => $place) { $label = $p; $color = "white"; if($p == $initial) $color = "lightblue"; $nodes[] = array('key' => $label, 'color' => $color); } foreach($transitions as $transition) { $label = $transition->getName(); $from = $transition->getFroms()[0]; $to = $transition->getTos()[0]; $links[] = array('from' => $from, 'to' => $to, 'text' => $label); } $data[] = $nodes; $data[] = $links; //return $data; $return = json_encode($nodes).",".json_encode($links); return $return; } }