123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace WorkflowBundle\Twig;
- use Symfony\Component\Workflow\Registry;
- use Symfony\Component\Translation\TranslatorInterface;
- use Symfony\Component\Workflow\Exception\InvalidArgumentException;
- class WorkflowExtension extends \Twig_Extension
- {
- private $workflowRegistry;
- private $translator;
- public function __construct(Registry $workflowRegistry, TranslatorInterface $translator)
- {
- $this->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;
- }
- }
|