12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace WorkflowBundle\Twig;
- use Symfony\Component\Workflow\Registry;
- use Symfony\Component\Translation\DataCollectorTranslator as Translator;
- class WorkflowExtension extends \Twig_Extension
- {
- private $workflowRegistry;
- private $translator;
- public function __construct(Registry $workflowRegistry, Translator $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'))
- );
- }
- 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;
- }
- }
|