-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathFormFlowExtension.php
More file actions
118 lines (99 loc) · 3.99 KB
/
Copy pathFormFlowExtension.php
File metadata and controls
118 lines (99 loc) · 3.99 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
<?php
namespace Craue\FormFlowBundle\Twig\Extension;
use Craue\FormFlowBundle\Form\FormFlowInterface;
use Craue\FormFlowBundle\Util\FormFlowUtil;
/**
* Twig extension for form flows.
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2017 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() {
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() {
return array(
new \Twig_SimpleFunction('craue_isStepLinkable', array($this, 'isStepLinkable')),
);
}
/**
* Adds route parameters for dynamic step navigation.
* @param array $parameters Current route parameters.
* @param FormFlowInterface $flow The flow involved.
* @param int $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, FormFlowInterface $flow, $stepNumber) {
return $this->formFlowUtil->addRouteParameters($parameters, $flow, $stepNumber);
}
/**
* Removes route parameters for dynamic step navigation.
* @param array $parameters Current route parameters.
* @param FormFlowInterface $flow The flow involved.
* @return array Route parameters without instance and step parameter.
*/
public function removeDynamicStepNavigationParameters(array $parameters, FormFlowInterface $flow) {
return $this->formFlowUtil->removeRouteParameters($parameters, $flow);
}
/**
* @param FormFlowInterface $flow The flow involved.
* @param int $stepNumber Number of the step the link will be generated for.
* @return bool If the step can be linked to.
*/
public function isStepLinkable(FormFlowInterface $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, FormFlowInterface $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, FormFlowInterface $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);
}
}