-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathFormFlowExtension.php
More file actions
134 lines (113 loc) · 4.74 KB
/
Copy pathFormFlowExtension.php
File metadata and controls
134 lines (113 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace Craue\FormFlowBundle\Twig\Extension;
use Craue\FormFlowBundle\Form\FormFlow;
use Craue\FormFlowBundle\Util\FormFlowUtil;
/**
* Twig extension for form flows.
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2015 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class FormFlowExtension extends \Twig_Extension {
/**
* @var FormFlowUtil
*/
protected $formFlowUtil;
public function setFormFlowUtil(FormFlowUtil $formFlowUtil) {
$this->formFlowUtil = $formFlowUtil;
}
/**
* {@inheritDoc}
*/
public function getName() {
return 'craue_formflow';
}
/**
* {@inheritDoc}
*/
public function getFilters() {
if (version_compare(\Twig_Environment::VERSION, '1.12', '<')) {
return array(
'craue_addDynamicStepNavigationParameters' => new \Twig_Filter_Method($this, 'addDynamicStepNavigationParameters'),
'craue_removeDynamicStepNavigationParameters' => new \Twig_Filter_Method($this, 'removeDynamicStepNavigationParameters'),
// methods for BC with third-party templates (e.g. MopaBootstrapBundle)
'craue_addDynamicStepNavigationParameter' => new \Twig_Filter_Method($this, 'addDynamicStepNavigationParameter'),
'craue_removeDynamicStepNavigationParameter' => new \Twig_Filter_Method($this, 'removeDynamicStepNavigationParameter'),
);
}
return array(
new \Twig_SimpleFilter('craue_addDynamicStepNavigationParameters', array($this, 'addDynamicStepNavigationParameters')),
new \Twig_SimpleFilter('craue_removeDynamicStepNavigationParameters', array($this, 'removeDynamicStepNavigationParameters')),
// methods for BC with third-party templates (e.g. MopaBootstrapBundle)
new \Twig_SimpleFilter('craue_addDynamicStepNavigationParameter', array($this, 'addDynamicStepNavigationParameter')),
new \Twig_SimpleFilter('craue_removeDynamicStepNavigationParameter', array($this, 'removeDynamicStepNavigationParameter')),
);
}
/**
* {@inheritDoc}
*/
public function getFunctions() {
if (version_compare(\Twig_Environment::VERSION, '1.12', '<')) {
return array(
'craue_isStepLinkable' => new \Twig_Function_Method($this, 'isStepLinkable'),
);
}
return array(
new \Twig_SimpleFunction('craue_isStepLinkable', array($this, 'isStepLinkable')),
);
}
/**
* Adds route parameters for dynamic step navigation.
* @param array $parameters Current route parameters.
* @param FormFlow $flow The flow involved.
* @param integer $stepNumber Number of the step the link will be generated for.
* @return array Route parameters plus instance and step parameter.
*/
public function addDynamicStepNavigationParameters(array $parameters, FormFlow $flow, $stepNumber) {
return $this->formFlowUtil->addRouteParameters($parameters, $flow, $stepNumber);
}
/**
* Removes route parameters for dynamic step navigation.
* @param array $parameters Current route parameters.
* @param FormFlow $flow The flow involved.
* @return array Route parameters without instance and step parameter.
*/
public function removeDynamicStepNavigationParameters(array $parameters, FormFlow $flow) {
return $this->formFlowUtil->removeRouteParameters($parameters, $flow);
}
/**
* @param FormFlow $flow The flow involved.
* @param integer $stepNumber Number of the step the link will be generated for.
* @return boolean If the step can be linked to.
*/
public function isStepLinkable(FormFlow $flow, $stepNumber) {
if (!$flow->isAllowDynamicStepNavigation()
|| $flow->getCurrentStepNumber() === $stepNumber
|| $flow->isStepSkipped($stepNumber)) {
return false;
}
$lastStepConsecutivelyDone = 0;
for ($i = $flow->getFirstStepNumber(), $lastStepNumber = $flow->getLastStepNumber(); $i < $lastStepNumber; ++$i) {
if ($flow->isStepDone($i)) {
$lastStepConsecutivelyDone = $i;
} else {
break;
}
}
$lastStepLinkable = $lastStepConsecutivelyDone + 1;
if ($stepNumber <= $lastStepLinkable) {
return true;
}
return false;
}
// methods for BC with third-party templates (e.g. MopaBootstrapBundle)
public function addDynamicStepNavigationParameter(array $parameters, FormFlow $flow, $stepNumber) {
@trigger_error('Twig filter craue_addDynamicStepNavigationParameter is deprecated since version 3.0. Use filter craue_addDynamicStepNavigationParameters instead.', E_USER_DEPRECATED);
return $this->addDynamicStepNavigationParameters($parameters, $flow, $stepNumber);
}
public function removeDynamicStepNavigationParameter(array $parameters, FormFlow $flow) {
@trigger_error('Twig filter craue_removeDynamicStepNavigationParameter is deprecated since version 3.0. Use filter craue_removeDynamicStepNavigationParameters instead.', E_USER_DEPRECATED);
return $this->removeDynamicStepNavigationParameters($parameters, $flow);
}
}