1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- namespace WorkflowBundle\Twig;
- use Symfony\Component\Workflow\Registry;
- class WorkflowExtension extends \Twig_Extension
- {
- private $workflowRegistry;
- public function __construct(Registry $workflowRegistry)
- {
- $this->workflowRegistry = $workflowRegistry;
- }
- 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'))
- );
- }
- 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);
- }
- }
|